1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\widgets\navigation; |
4
|
|
|
|
5
|
|
|
use app\widgets\navigation\models\Navigation; |
6
|
|
|
use Yii; |
7
|
|
|
use yii\base\Widget; |
8
|
|
|
use yii\caching\TagDependency; |
9
|
|
|
use yii\helpers\ArrayHelper; |
10
|
|
|
use yii\helpers\Html; |
11
|
|
|
use yii\helpers\Json; |
12
|
|
|
use app\components\Menu; |
13
|
|
|
|
14
|
|
|
class NavigationWidget extends Widget |
15
|
|
|
{ |
16
|
|
|
public $prependItems; |
17
|
|
|
public $appendItems; |
18
|
|
|
public $options; |
19
|
|
|
public $rootId = 1; |
20
|
|
|
public $depth = 99; |
21
|
|
|
public $useCache = true; |
22
|
|
|
public $viewFile = 'navigation'; |
23
|
|
|
public $widget = ''; |
24
|
|
|
public $linkTemplate = '<a href="{url}" title="{label}" itemprop="url"><span itemprop="name">{label}</span></a>'; |
25
|
|
|
public $submenuTemplate = "\n<ul>\n{items}\n</ul>\n"; |
26
|
|
|
|
27
|
|
|
public function init() |
28
|
|
|
{ |
29
|
|
|
$schema = [ |
30
|
|
|
'role'=> "navigation", |
31
|
|
|
'itemscope' => '', |
32
|
|
|
'itemtype' => "http://schema.org/SiteNavigationElement", |
33
|
|
|
|
34
|
|
|
]; |
35
|
|
|
if (!trim($this->widget)) { |
36
|
|
|
$this->widget = Menu::className(); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
if (!is_array($this->options)) { |
39
|
|
|
$this->options = $schema; |
40
|
|
|
} else { |
41
|
|
|
$this->options = ArrayHelper::merge($schema, $this->options); |
42
|
|
|
} |
43
|
|
|
Html::addCssClass($this->options, 'navigation-widget'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function run() |
47
|
|
|
{ |
48
|
|
|
Yii::beginProfile("NavigationWidget for ".$this->rootId); |
49
|
|
|
$items = null; |
50
|
|
|
$cacheKey = implode( |
51
|
|
|
':', |
52
|
|
|
[ |
53
|
|
|
'Navigation', |
54
|
|
|
$this->rootId, |
55
|
|
|
$this->depth, |
56
|
|
|
$this->viewFile |
57
|
|
|
] |
58
|
|
|
); |
59
|
|
|
if ($this->useCache) { |
60
|
|
|
if (false === $items = \Yii::$app->cache->get($cacheKey)) { |
61
|
|
|
$items = null; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
if (null === $items) { |
65
|
|
|
$root = Navigation::find() |
66
|
|
|
->where(['id' => $this->rootId]) |
67
|
|
|
->with('children') |
68
|
|
|
->orderBy(['sort_order' => SORT_ASC]) |
69
|
|
|
->one(); |
70
|
|
|
$items = []; |
71
|
|
|
if ($this->depth > 0) { |
72
|
|
|
foreach ($root->children as $child) { |
73
|
|
|
$items[] = self::getTree($child, $this->depth - 1); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
View Code Duplication |
if (count($items) > 0) { |
77
|
|
|
\Yii::$app->cache->set( |
78
|
|
|
$cacheKey, |
79
|
|
|
$items, |
80
|
|
|
86400, |
81
|
|
|
new TagDependency([ |
82
|
|
|
'tags' => [ |
83
|
|
|
\devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag(Navigation::className()) |
|
|
|
|
84
|
|
|
] |
85
|
|
|
]) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
$items = ArrayHelper::merge((array) $this->prependItems, $items, (array) $this->appendItems); |
90
|
|
|
$currentUri = Yii::$app->request->url; |
91
|
|
|
array_walk($items, function(&$item) use ($currentUri) { |
92
|
|
|
if ($item['url'] === $currentUri) { |
93
|
|
|
$item['active'] = true; |
94
|
|
|
} |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
$result = $this->render( |
98
|
|
|
$this->viewFile, |
99
|
|
|
[ |
100
|
|
|
'widget' => $this->widget, |
101
|
|
|
'items' => $items, |
102
|
|
|
'options' => $this->options, |
103
|
|
|
'linkTemplate' => $this->linkTemplate, |
104
|
|
|
'submenuTemplate' => $this->submenuTemplate, |
105
|
|
|
] |
106
|
|
|
); |
107
|
|
|
Yii::endProfile("NavigationWidget for ".$this->rootId); |
108
|
|
|
return $result; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Navigation $model |
113
|
|
|
* @param int $depth |
114
|
|
|
* @return array |
|
|
|
|
115
|
|
|
*/ |
116
|
|
|
private static function getTree($model, $depth) |
117
|
|
|
{ |
118
|
|
|
if (trim($model->url)) { |
119
|
|
|
$url = trim($model->url); |
120
|
|
|
} else { |
121
|
|
|
$params = (trim($model->route_params)) ? Json::decode($model->route_params) : []; |
122
|
|
|
$url = ArrayHelper::merge([$model->route], $params); |
123
|
|
|
} |
124
|
|
|
$tree = [ |
125
|
|
|
'model' => $model, |
126
|
|
|
'label' => $model->name, |
127
|
|
|
'url' => $url, |
128
|
|
|
'items' => [], |
129
|
|
|
'options' => [], |
130
|
|
|
]; |
131
|
|
|
if (!empty($model->advanced_css_class)) { |
132
|
|
|
$tree['options'] = ['class' => $model->advanced_css_class]; |
133
|
|
|
} |
134
|
|
|
if ($depth > 0) { |
135
|
|
|
foreach ($model->children as $child) { |
136
|
|
|
$tree['items'][] = self::getTree($child, $depth - 1); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
return $tree; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.