Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
menu.php
1<?php
3
4use \Bitrix\Landing\Landing;
5use \Bitrix\Landing\Hook\Page\Settings;
6use \Bitrix\Main\Localization\Loc;
7
8Loc::loadMessages(__FILE__);
9
10class Menu
11{
19 public static function prepareManifest(array $manifest, \Bitrix\Landing\Block $block = null, array $params = []): array
20 {
21 // add attrs to work TWO MENUS together
22 if (!isset($manifest['attrs']) || !is_array($manifest['attrs']))
23 {
24 $manifest['attrs'] = array();
25 }
26 if (!isset($manifest['attrs']['.navbar-collapse']))
27 {
28 $manifest['attrs']['.navbar-collapse'] = array(
29 array(
30 'hidden' => true,
31 'attribute' => 'id',
32 ),
33 );
34 }
35 if (!isset($manifest['attrs']['button.navbar-toggler']))
36 {
37 $manifest['attrs']['button.navbar-toggler'] = array(
38 array(
39 'hidden' => true,
40 'attribute' => 'aria-controls',
41 ),
42 array(
43 'hidden' => true,
44 'attribute' => 'data-target',
45 ),
46 );
47 }
48
49 // add callbacks
50 $manifest['callbacks'] = array(
51 'afterAdd' => function (\Bitrix\Landing\Block &$block) use($params)
52 {
53 $manifest = $block->getManifest();
54 $needSave = false;
55
56 // autogenerate MENU
57 // predefine params
58 if (isset($params['selector']))
59 {
60 $selector = $params['selector'];
61 }
62 else
63 {
64 $selector = '.landing-block-node-menu-list-item-link';
65 }
66 $count = isset($params['count']) ? $params['count'] : 5;
67 $source = isset($params['source']) ? $params['source'] : null;
68
69 // new menu items
70 $menuItems = array();
71
72 if (isset($manifest['nodes'][$selector]))
73 {
74 // fill menu
75 if ($source == 'catalog')
76 {
77 $site = $block->getSite();
78 if ($site['TYPE'] == 'STORE')
79 {
80 $menuItems = self::getCatalogMenu(
81 $block->getSiteId(),
82 $count
83 );
84 }
85 }
86 else if ($source == 'personal')
87 {
88 $menuItems = self::getPersonalMenu();
89 }
90
91 // save new items
92 if (!empty($menuItems))
93 {
94 $block->updateNodes(array(
95 $selector => $menuItems
96 ));
97 $needSave = true;
98 }
99 }
100 else if ($source == 'structure')
101 {
102 // after add immediately
103 \Bitrix\Landing\Subtype\Menu::redrawStructureMenu($block);
104 // after site creating if it is processing now
105 $eventManager = \Bitrix\Main\EventManager::getInstance();
106 $eventManager->addEventHandler('landing', 'onAfterDemoCreate',
107 function(\Bitrix\Main\Event $event) use($block)
108 {
109 \Bitrix\Landing\Subtype\Menu::redrawStructureMenu($block);
110 }
111 );
112 }
113
114 // to work TWO MENUS together
115 // todo: check in manifest
116 $navbarCollapseSection = $params['navbarCollapseSection'] ?? '.navbar-collapse';
117 $navbarTogglerButton = $params['navbarTogglerButton'] ?? 'button.navbar-toggler';
118
119 if (
120 isset($manifest['attrs'][$navbarCollapseSection])
121 && isset($manifest['attrs'][$navbarTogglerButton])
122 )
123 {
124 $newId = 'navBar' . $block->getId();
125 $block->setAttributes(array(
126 $navbarCollapseSection => array('id' => $newId),
127 $navbarTogglerButton => array(
128 'aria-controls' => $newId,
129 'data-target' => '#' . $newId,
130 ),
131 ));
132 $needSave = true;
133 }
134
135 // SAVE
136 if($needSave)
137 {
138 $block->save();
139 }
140 },
141 );
142
143 return $manifest;
144 }
145
151 public static function redrawStructureMenu(\Bitrix\Landing\Block $block): void
152 {
153 $manifest = $block->getManifest();
154
155 return;
156
157 if (empty($manifest['menu']))
158 {
159 return;
160 }
161
162 // collect all pages top level
163 $menu = [];
164 $inFolders = [];
165 $res = Landing::getList([
166 'select' => [
167 'ID', 'TITLE', 'FOLDER_ID'
168 ],
169 'filter' => [
170 'SITE_ID' => $block->getSiteId(),
171 '==AREAS.ID' => null
172 ]
173 ]);
174 while ($row = $res->fetch())
175 {
176 if ($row['FOLDER_ID'])
177 {
178 $inFolders[] = $row;
179 continue;
180 }
181 $menu[$row['ID']] = [
182 'text' => $row['TITLE'],
183 'href' => '#landing' . $row['ID'],
184 'target' => '_self',
185 'children' => []
186 ];
187 }
188
189 // collect pages on folder
190 if ($inFolders)
191 {
192 foreach ($inFolders as $row)
193 {
194 if (isset($menu[$row['FOLDER_ID']]))
195 {
196 $menu[$row['FOLDER_ID']]['children'][] = [
197 'text' => $row['TITLE'],
198 'href' => '#landing' . $row['ID'],
199 'target' => '_self',
200 'children' => []
201 ];
202 }
203 }
204 }
205
206 // save new menu
207 foreach ($manifest['menu'] as $selector => $foo)
208 {
209 $block->updateNodes([
210 $selector => [
211 array_values($menu)
212 ]
213 ]);
214 }
215 $block->save();
216 }
217
224 protected static function getCatalogMenu($siteId, $count)
225 {
226 $menuItems = array();
227
228 if (!\Bitrix\Main\Loader::includeModule('iblock'))
229 {
230 return $menuItems;
231 }
232
233 \Bitrix\Landing\Hook::setEditMode(true);
234 $settings = Settings::getDataForSite($siteId);
235 if ($settings['IBLOCK_ID'])
236 {
237 $res = \CIBlockSection::getList(
238 array(),
239 array(
240 'IBLOCK_ID' => $settings['IBLOCK_ID'],
241 'SECTION_ID' => $settings['SECTION_ID']
242 ? $settings['SECTION_ID']
243 : false
244 ),
245 false,
246 array(
247 'ID', 'NAME'
248 ),
249 array(
250 'nTopCount' => $count
251 )
252 );
253 while ($row = $res->fetch())
254 {
255 $menuItems[] = array(
256 'text' => $row['NAME'],
257 'href' => '#catalogSection' . $row['ID'],
258 'attrs' => array(
259 'data-url' => '#catalogSection' . $row['ID']
260 )
261 );
262 }
263 }
264
265 return $menuItems;
266 }
267
272 protected static function getPersonalMenu()
273 {
274 return array(
275 array(
276 'text' => Loc::getMessage('LANDING_BLOCK_ST_PERSONAL_PERSONAL'),
277 'href' => '#system_personal'
278 ),
279 array(
280 'text' => Loc::getMessage('LANDING_BLOCK_ST_PERSONAL_ORDERS'),
281 'href' => '#system_personal?SECTION=orders'
282 ),
283 array(
284 'text' => Loc::getMessage('LANDING_BLOCK_ST_PERSONAL_ACCOUNT'),
285 'href' => '#system_personal?SECTION=account'
286 ),
287 array(
288 'text' => Loc::getMessage('LANDING_BLOCK_ST_PERSONAL_PRIVATE'),
289 'href' => '#system_personal?SECTION=private'
290 ),
291 array(
292 'text' => Loc::getMessage('LANDING_BLOCK_ST_PERSONAL_ORDERS_HISTORY'),
293 'href' => '#system_personal?SECTION=orders&filter_history=Y'
294 ),
295 array(
296 'text' => Loc::getMessage('LANDING_BLOCK_ST_PERSONAL_PROFILE'),
297 'href' => '#system_personal?SECTION=profile'
298 ),
299 array(
300 'text' => Loc::getMessage('LANDING_BLOCK_ST_PERSONAL_CART'),
301 'href' => '#system_cart'
302 ),
303 array(
304 'text' => Loc::getMessage('LANDING_BLOCK_ST_PERSONAL_SUBSCRIBE'),
305 'href' => '#system_personal?SECTION=subscribe'
306 )
307 );
308 }
309}
static getCatalogMenu($siteId, $count)
Definition menu.php:224
static redrawStructureMenu(\Bitrix\Landing\Block $block)
Definition menu.php:151
static prepareManifest(array $manifest, \Bitrix\Landing\Block $block=null, array $params=[])
Definition menu.php:19
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())