TreeMenuWidget.php
1.81 KB
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
<?php
namespace common\components\artboxtree\treemenu;
use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
class TreeMenuWidget extends \common\components\artboxtree\ArtboxTreeWidget {
public $displayField = 'name';
/**
* Init the widget object.
*/
public function init() {
parent::init();
}
/**
* Runs the widget.
*/
public function run() {
$run = parent::run();
if (!is_null($run))
return $run;
$models = $this->_hierarchyTreeData(array_values($this->dataProvider->getModels()), $this->rootParentId);
return $this->renderTreelist($models);
}
protected function renderTreelist($models) {
foreach ($models as $index => $model) {
$row = $this->renderTreelistItem($model['item']);
$children = empty($model['children']) ? '' : $this->renderTreelist($model['children']);
$output[] = '<li>'. $row . $children .'</li>';
}
if (!empty($output))
return '<ul>'. implode("\n", $output) .'</ul>';
}
protected function renderTreelistItem($model)
{
$options = [];
$id = ArrayHelper::getValue($model, $this->keyNameId);
Html::addCssClass($options, "treelistitem-$id");
$parent_id = ArrayHelper::getValue($model, $this->keyNameParentId);
if ($parent_id) {
Html::addCssClass($options, "treelistitem-parent-$parent_id");
}
// if (is_string($this->value)) {
// return ArrayHelper::getValue($model, $this->value);
// } else {
// return call_user_func($this->value, $model, $key, $index, $this);
// }
return Html::tag('span', ArrayHelper::getValue($model, $this->displayField), $options);
}
}