1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace common\widgets;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* Class Breadcrumbs
* @package common\widgets
*/
class Breadcrumbs extends \yii\widgets\Breadcrumbs
{
public $active = ['id' => 10, 'pid' => 0, 'title' => '首页'];
public $tag = 'li';
public $itemTemplate = "<li class='menu-dropdown classic-menu-dropdown '>{link}</li>";
public $activeItemTemplate = "<li class='menu-dropdown classic-menu-dropdown active'>{link}</li>";
/**
* 下拉菜单模板 level 2
* @var string
*/
public $itemTemplate2 = "<li class='dropdown-submenu '>{link}</li>";
public $activeItemTemplate2 = "<li class='dropdown-submenu active'>{link}</li>";
/**
* Renders the widget.
*/
public function run()
{
if (empty($this->links)) {
return;
}
$links = [];
if ($this->homeLink === null) {
$links[] = $this->renderItem([
'label' => Yii::t('yii', 'Home'),
'url' => Yii::$app->homeUrl,
], $this->itemTemplate);
} elseif ($this->homeLink !== false) {
$links[] = $this->renderItem($this->homeLink, $this->itemTemplate);
}
foreach ($this->links as $link) {
$add1 = '';
if (isset($link['notes'])) {
$add1 .= "<ul class='dropdown-menu pull-left'>";
foreach ($link['notes'] as $level1) {
$add2 = '';
if (isset($level1['notes'])) {
$itemTemplate = $this->itemTemplate2;
$activeTemplate = $this->activeItemTemplate2;
$add2 .= "<ul class='dropdown-menu pull-left'>";
foreach ($level1['notes'] as $level2) {
$add2 .= $this->renderItem(
$level2,
isset($level2['id']) && $level2['id'] == $this->active['id'] ?
$this->itemTemplate : $this->activeItemTemplate
);
}
$add2 .= "</ul>";
} else {
$itemTemplate = $this->itemTemplate;
$activeTemplate = $this->activeItemTemplate;
}
$add1 .= $this->renderItem(
$level1,
isset($level1['id']) && $level1['id'] == $this->active['id'] ?
$activeTemplate : $itemTemplate,
$add2
);
}
$add1 .= "</ul>";
}
if (!is_array($link)) {
$link = ['label' => $link];
}
if ( $link['id'] == $this->active['pid'] || ($link['id'] == $this->active['id'] && 0 == $this->active['pid']) ) {
$link['label'] = $this->active['title'];
$that = $this->renderItem(
$link,
$this->activeItemTemplate,
$add1
);
} else {
$that = $this->renderItem(
$link,
$this->itemTemplate,
$add1
);
}
$links[] = $that;
}
echo Html::tag($this->tag, implode('', $links), $this->options);
}
/**
* Renders a single breadcrumb item.
* @param array $link the link to be rendered. It must contain the "label" element. The "url" element is optional.
* @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the link.
* @return string the rendering result
* @throws InvalidConfigException if `$link` does not have "label" element.
*/
protected function renderItem($link, $template, $add = '')
{
$encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels);
if (array_key_exists('label', $link)) {
$label = $encodeLabel ? Html::encode($link['label']) : $link['label'];
if (isset($link['icon']) && $link['icon']) {
$label = "<i class='" . $link['icon'] . "'> </i> " . $label;
}
} else {
throw new InvalidConfigException('The "label" element is required for each link.');
}
if (isset($link['template'])) {
$template = $link['template'];
}
if (isset($link['url'])) {
$options = $link;
unset($options['template'], $options['label'], $options['url'], $options['notes']);
$link = Html::a($label, $link['url'], $options);
} else {
$link = $label;
}
if ($add != '')
$link .= $add;
return strtr($template, ['{link}' => $link]);
}
}