1: <?php
2: /**
3: * CBreadcrumbs class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CBreadcrumbs displays a list of links indicating the position of the current page in the whole website.
13: *
14: * For example, breadcrumbs like "Home > Sample Post > Edit" means the user is viewing an edit page
15: * for the "Sample Post". He can click on "Sample Post" to view that page, or he can click on "Home"
16: * to return to the homepage.
17: *
18: * To use CBreadcrumbs, one usually needs to configure its {@link links} property, which specifies
19: * the links to be displayed. For example,
20: *
21: * <pre>
22: * $this->widget('zii.widgets.CBreadcrumbs', array(
23: * 'links'=>array(
24: * 'Sample post'=>array('post/view', 'id'=>12),
25: * 'Edit',
26: * ),
27: * ));
28: * </pre>
29: *
30: * Because breadcrumbs usually appears in nearly every page of a website, the widget is better to be placed
31: * in a layout view. One can define a property "breadcrumbs" in the base controller class and assign it to the widget
32: * in the layout, like the following:
33: *
34: * <pre>
35: * $this->widget('zii.widgets.CBreadcrumbs', array(
36: * 'links'=>$this->breadcrumbs,
37: * ));
38: * </pre>
39: *
40: * Then, in each view script, one only needs to assign the "breadcrumbs" property as needed.
41: *
42: * @author Qiang Xue <qiang.xue@gmail.com>
43: * @package zii.widgets
44: * @since 1.1
45: */
46: class CBreadcrumbs extends CWidget
47: {
48: /**
49: * @var string the tag name for the breadcrumbs container tag. Defaults to 'div'.
50: */
51: public $tagName='div';
52: /**
53: * @var array the HTML attributes for the breadcrumbs container tag.
54: */
55: public $htmlOptions=array('class'=>'breadcrumbs');
56: /**
57: * @var boolean whether to HTML encode the link labels. Defaults to true.
58: */
59: public $encodeLabel=true;
60: /**
61: * @var string the first hyperlink in the breadcrumbs (called home link).
62: * If this property is not set, it defaults to a link pointing to {@link CWebApplication::homeUrl} with label 'Home'.
63: * If this property is false, the home link will not be rendered.
64: */
65: public $homeLink;
66: /**
67: * @var array list of hyperlinks to appear in the breadcrumbs. If this property is empty,
68: * the widget will not render anything. Each key-value pair in the array
69: * will be used to generate a hyperlink by calling CHtml::link(key, value). For this reason, the key
70: * refers to the label of the link while the value can be a string or an array (used to
71: * create a URL). For more details, please refer to {@link CHtml::link}.
72: * If an element's key is an integer, it means the element will be rendered as a label only (meaning the current page).
73: *
74: * The following example will generate breadcrumbs as "Home > Sample post > Edit", where "Home" points to the homepage,
75: * "Sample post" points to the "index.php?r=post/view&id=12" page, and "Edit" is a label. Note that the "Home" link
76: * is specified via {@link homeLink} separately.
77: *
78: * <pre>
79: * array(
80: * 'Sample post'=>array('post/view', 'id'=>12),
81: * 'Edit',
82: * )
83: * </pre>
84: */
85: public $links=array();
86: /**
87: * @var string String, specifies how each active item is rendered. Defaults to
88: * "<a href="{url}">{label}</a>", where "{label}" will be replaced by the corresponding item
89: * label while "{url}" will be replaced by the URL of the item.
90: * @since 1.1.11
91: */
92: public $activeLinkTemplate='<a href="{url}">{label}</a>';
93: /**
94: * @var string String, specifies how each inactive item is rendered. Defaults to
95: * "<span>{label}</span>", where "{label}" will be replaced by the corresponding item label.
96: * Note that inactive template does not have "{url}" parameter.
97: * @since 1.1.11
98: */
99: public $inactiveLinkTemplate='<span>{label}</span>';
100: /**
101: * @var string the separator between links in the breadcrumbs. Defaults to ' » '.
102: */
103: public $separator=' » ';
104:
105: /**
106: * Renders the content of the portlet.
107: */
108: public function run()
109: {
110: if(empty($this->links))
111: return;
112:
113: $definedLinks = $this->links;
114:
115: echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
116: $links=array();
117: if($this->homeLink===null)
118: $definedLinks=array_merge(array(Yii::t('zii','Home') => Yii::app()->homeUrl),$definedLinks);
119: elseif($this->homeLink!==false)
120: $links[]=$this->homeLink;
121: foreach($definedLinks as $label=>$url)
122: {
123: if(is_string($label) || is_array($url))
124: $links[]=strtr($this->activeLinkTemplate,array(
125: '{url}'=>CHtml::normalizeUrl($url),
126: '{label}'=>$this->encodeLabel ? CHtml::encode($label) : $label,
127: ));
128: else
129: $links[]=str_replace('{label}',$this->encodeLabel ? CHtml::encode($url) : $url,$this->inactiveLinkTemplate);
130: }
131: echo implode($this->separator,$links);
132: echo CHtml::closeTag($this->tagName);
133: }
134: }