1 | <?php |
||
27 | class MenuWidget extends Widget |
||
28 | { |
||
29 | /** |
||
30 | * Primary key name. |
||
31 | * @var string |
||
32 | */ |
||
33 | public $primaryKeyName = 'id'; |
||
34 | |||
35 | /** |
||
36 | * Relation key name. |
||
37 | * @var string |
||
38 | */ |
||
39 | public $parentKeyName = 'parentId'; |
||
40 | |||
41 | /** |
||
42 | * Main container html tag. |
||
43 | * @var string |
||
44 | */ |
||
45 | public $mainContainerTag = 'ul'; |
||
46 | |||
47 | /** |
||
48 | * Main container html options. |
||
49 | * @var array |
||
50 | */ |
||
51 | public $mainContainerOptions = []; |
||
52 | |||
53 | /** |
||
54 | * Item container html tag. |
||
55 | * @var string |
||
56 | */ |
||
57 | public $itemContainerTag = 'li'; |
||
58 | |||
59 | /** |
||
60 | * Item container html options. |
||
61 | * @var array |
||
62 | */ |
||
63 | public $itemContainerOptions = []; |
||
64 | |||
65 | /** |
||
66 | * Item template to display widget elements. |
||
67 | * @var string|array |
||
68 | */ |
||
69 | public $itemTemplate; |
||
70 | |||
71 | /** |
||
72 | * Addition item template params. |
||
73 | * @var array |
||
74 | */ |
||
75 | public $itemTemplateParams = []; |
||
76 | |||
77 | /** |
||
78 | * Data records. |
||
79 | * @var ActiveRecord[] |
||
80 | */ |
||
81 | public $data; |
||
82 | |||
83 | /** |
||
84 | * Starts the output widget of the multi level view records according with the menu type. |
||
85 | * @throws InvalidConfigException |
||
86 | */ |
||
87 | public function run() |
||
93 | |||
94 | /** |
||
95 | * Check whether a particular record can be used as a parent. |
||
96 | * @param ActiveRecord $mainModel |
||
97 | * @param int $newParentId |
||
98 | * @param string $primaryKeyName |
||
99 | * @param string $parentKeyName |
||
100 | * @return bool |
||
101 | */ |
||
102 | public static function checkNewParentId(ActiveRecord $mainModel, int $newParentId, string $primaryKeyName = 'id', string $parentKeyName = 'parentId'): bool |
||
118 | |||
119 | /** |
||
120 | * Reassigning child objects to their new parent after delete the main model record. |
||
121 | * @param ActiveRecord $mainModel |
||
122 | * @param string $primaryKeyName |
||
123 | * @param string $parentKeyName |
||
124 | */ |
||
125 | public static function afterDeleteMainModel(ActiveRecord $mainModel, string $primaryKeyName = 'id', string $parentKeyName = 'parentId'): void |
||
129 | |||
130 | /** |
||
131 | * Check for configure. |
||
132 | * @throws InvalidConfigException |
||
133 | */ |
||
134 | private function checkConfiguration() |
||
144 | |||
145 | /** |
||
146 | * Group records in to sub levels according with the relation to parent records. |
||
147 | * @param array $models |
||
148 | * @throws InvalidConfigException |
||
149 | * @return array |
||
150 | */ |
||
151 | private function groupLevels(array $models): array |
||
180 | |||
181 | /** |
||
182 | * Base render. |
||
183 | * @param array $items |
||
184 | * @param int $level |
||
185 | * @return string |
||
186 | */ |
||
187 | private function renderItems(array $items, int $level = 0): string |
||
209 | |||
210 | /** |
||
211 | * Get attribute values in current level. |
||
212 | * @param string $attributeName |
||
213 | * @param int $level |
||
214 | * @throws InvalidConfigException |
||
215 | * @return mixed |
||
216 | */ |
||
217 | private function levelAttributeValue(string $attributeName, int $level) |
||
242 | } |
||
243 |