1 | <?php |
||
28 | class MenuWidget extends Widget |
||
29 | { |
||
30 | /** |
||
31 | * Init level menu html tag id. |
||
32 | * @var string |
||
33 | */ |
||
34 | public $menuId; |
||
35 | |||
36 | /** |
||
37 | * Primary key name. |
||
38 | * @var string |
||
39 | */ |
||
40 | public $primaryKeyName = 'id'; |
||
41 | |||
42 | /** |
||
43 | * Relation key name. |
||
44 | * @var string |
||
45 | */ |
||
46 | public $parentKeyName = 'parentId'; |
||
47 | |||
48 | /** |
||
49 | * Main container html tag. |
||
50 | * @var string |
||
51 | */ |
||
52 | public $mainContainerTag = 'ul'; |
||
53 | |||
54 | /** |
||
55 | * Main container html options. |
||
56 | * @var array |
||
57 | */ |
||
58 | public $mainContainerOptions = []; |
||
59 | |||
60 | /** |
||
61 | * Item container html tag. |
||
62 | * @var string |
||
63 | */ |
||
64 | public $itemContainerTag = 'li'; |
||
65 | |||
66 | /** |
||
67 | * Item container html options. |
||
68 | * @var array |
||
69 | */ |
||
70 | public $itemContainerOptions = []; |
||
71 | |||
72 | /** |
||
73 | * Item template to display widget elements. |
||
74 | * @var string|array |
||
75 | */ |
||
76 | public $itemTemplate; |
||
77 | |||
78 | /** |
||
79 | * Addition item template params. |
||
80 | * @var array |
||
81 | */ |
||
82 | public $itemTemplateParams = []; |
||
83 | |||
84 | /** |
||
85 | * Data records. |
||
86 | * @var ActiveRecord[] |
||
87 | */ |
||
88 | public $data; |
||
89 | |||
90 | /** |
||
91 | * Starts the output widget of the multi level view records according with the menu type. |
||
92 | * @throws InvalidConfigException |
||
93 | */ |
||
94 | public function run() |
||
100 | |||
101 | /** |
||
102 | * Check whether a particular record can be used as a parent. |
||
103 | * @param ActiveRecord $mainModel |
||
104 | * @param int $newParentId |
||
105 | * @param string $primaryKeyName |
||
106 | * @param string $parentKeyName |
||
107 | * @return bool |
||
108 | */ |
||
109 | public static function checkNewParentId(ActiveRecord $mainModel, int $newParentId, string $primaryKeyName = 'id', string $parentKeyName = 'parentId'): bool |
||
125 | |||
126 | /** |
||
127 | * Reassigning child objects to their new parent after delete the main model record. |
||
128 | * @param ActiveRecord $mainModel |
||
129 | * @param string $primaryKeyName |
||
130 | * @param string $parentKeyName |
||
131 | */ |
||
132 | public static function afterDeleteMainModel(ActiveRecord $mainModel, string $primaryKeyName = 'id', string $parentKeyName = 'parentId'): void |
||
136 | |||
137 | /** |
||
138 | * Check for configure. |
||
139 | * @throws InvalidConfigException |
||
140 | */ |
||
141 | private function checkConfiguration() |
||
151 | |||
152 | /** |
||
153 | * Group records in to sub levels according with the relation to parent records. |
||
154 | * @param array $models |
||
155 | * @throws InvalidConfigException |
||
156 | * @return array |
||
157 | */ |
||
158 | private function groupLevels(array $models): array |
||
187 | |||
188 | /** |
||
189 | * Base render. |
||
190 | * @param array $items |
||
191 | * @param int $level |
||
192 | * @return string |
||
193 | */ |
||
194 | private function renderItems(array $items, int $level = 0): string |
||
224 | |||
225 | /** |
||
226 | * Get attribute values in current level. |
||
227 | * @param string $attributeName |
||
228 | * @param int $level |
||
229 | * @throws InvalidConfigException |
||
230 | * @return mixed |
||
231 | */ |
||
232 | private function levelAttributeValue(string $attributeName, int $level) |
||
257 | } |
||
258 |