1 <?php
2 3 4 5 6 7
8 namespace foundation\widgets;
9
10 use foundation\helpers\ArrayHelper;
11 use foundation\helpers\Html;
12 use foundation\helpers\Icon;
13 use foundation\enum\Enum;
14
15 16 17 18 19 20 21 22
23 class Breadcrumbs extends base\Widget
24 {
25 26 27
28 public $encodeLabel = true;
29 30 31
32 public $homeLabel;
33 34 35
36 public $homeUrl;
37 38 39
40 public $tagName = 'ul';
41 42 43 44 45 46 47
48 public $items = array();
49
50
51 52 53
54 public function init()
55 {
56 parent::init();
57 $this->initItems();
58 }
59
60 61 62
63 public function run()
64 {
65 $this->renderItems();
66 }
67
68 69 70
71 public function initItems()
72 {
73 if (!empty($this->items)) {
74 $links = array();
75 if ($this->homeLabel !== false) {
76 $label = $this->homeLabel !== null ? $this->homeLabel : Icon::icon(Enum::ICON_HOME);
77 $links[$label] = array('href' => $this->homeUrl !== null ? $this->homeUrl : \Yii::app()->homeUrl);
78 }
79
80 foreach ($this->items as $label => $options) {
81 if (is_string($label)) {
82 if ($this->encodeLabel)
83 $label = \CHtml::encode($label);
84 if (is_string($options))
85 $links[$label] = array('href' => \CHtml::normalizeUrl($options));
86 else {
87 $url = ArrayHelper::removeValue($options, 'url', '');
88 $options['href'] = \CHtml::normalizeUrl($url);
89 $links[$label] = $options;
90 }
91 } else
92 $links[$options] = array('href' => '#');
93 }
94 $this->items = $links;
95 }
96 }
97
98 99 100
101 public function renderItems()
102 {
103 if (empty($this->items))
104 return;
105
106 Html::addCssClass($this->htmlOptions, Enum::BREADCRUMBS);
107 echo \CHtml::openTag($this->tagName, $this->htmlOptions);
108 foreach ($this->items as $label => $options) {
109 $this->renderItem($label, $options);
110 }
111 echo \CHtml::closeTag($this->tagName);
112
113 }
114
115 116 117 118 119
120 public function renderItem($label, $options = array())
121 {
122 if ($this->tagName === 'nav') {
123 $url = ArrayHelper::removeValue($options, 'href', '#');
124 $htmlOptions = ArrayHelper::removeValue($options, 'options', array());
125 echo \CHtml::openTag('li', $htmlOptions);
126 echo \CHtml::link($label, $url);
127 echo \CHtml::closeTag('li');
128 } else {
129 $htmlOptions = ArrayHelper::removeValue($options, 'options', array());
130 echo \CHtml::tag('a', array_merge($htmlOptions, $options), $label);
131 }
132 }
133 }