Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
element.php
1<?php
3
11
12Loc::loadMessages(__FILE__);
13
15{
16 const SELECTOR_ENTITY = 'element';
17
18 const FIELD_ALLOWED_SELECT = 0x0001;
19 const FIELD_ALLOWED_ORDER = 0x0002;
20 const FIELD_ALLOWED_ALL = self::FIELD_ALLOWED_SELECT|self::FIELD_ALLOWED_ORDER;
21
22 protected static $catalogIncluded = null;
23
28 public static function onBuildSourceListHandler(Main\Event $event)
29 {
31 $selector = $event->getParameter('SELECTOR');
32
33 $iblockList = [];
34 $catalogs = [];
35 $restrictions = $selector->getModuleRestrictions('iblock');
36 if (!empty($restrictions))
37 {
38 $getCatalogs = $selector->checkSiteMode([Landing\Source\Selector::SITE_MODE_STORE]);
39 if (!empty($restrictions['IBLOCK_ID']) && is_array($restrictions['IBLOCK_ID']))
40 {
41 $catalogs = static::getCatalogs($restrictions['IBLOCK_ID']);
42 if (!empty($catalogs))
43 $catalogs = array_fill_keys($catalogs, true);
44
45 $filter = ['ID' => $restrictions['IBLOCK_ID']];
46 if (!empty($restrictions['SITE_ID']))
47 {
48 $filter['SITE_ID'] = $restrictions['SITE_ID'];
49 }
50 $iterator = \CIBlock::GetList(['SORT' => 'ASC'], $filter, false);
51 while ($row = $iterator->Fetch())
52 {
53 $row['ID'] = (int)$row['ID'];
54 if (!$getCatalogs && isset($catalogs[$row['ID']]))
55 {
56 continue;
57 }
58 $iblockList[] = [
59 'ID' => $row['ID'],
60 'TITLE' => $row['NAME']
61 ];
62 }
63 unset($row, $iterator);
64 unset($filter);
65 }
66 }
67 unset($restrictions);
68 if (empty($iblockList))
69 {
70 return new Main\EventResult(Main\EventResult::ERROR, null, 'iblock');
71 }
72
73 $elementFields = static::getElementFields();
74 $productFields = static::getProductFields();
75
76 $result = [];
77 foreach ($iblockList as $iblock)
78 {
79 $sourceId = self::SELECTOR_ENTITY.$iblock['ID'];
80 $path = $selector->getSourceFilterBaseUri('iblock', $sourceId);
81 $action = $selector->getResultAction();
82
83 switch ($action['TYPE'])
84 {
85 case Landing\Source\Selector::ACTION_TYPE_EVENT:
86 $resultActionType = Iblock\Component\Selector\Entity::RESULT_ACTION_TYPE_EVENT;
87 break;
88 case Landing\Source\Selector::ACTION_TYPE_SLIDER:
89 $resultActionType = Iblock\Component\Selector\Entity::RESULT_ACTION_TYPE_SLIDER;
90 break;
91 default:
92 $resultActionType = '';
93 break;
94 }
95 if ($resultActionType === '')
96 continue;
97
98 $actionList = $selector->getDefaultLinkActions();
99 $fields = $elementFields;
100 if (isset($catalogs[$iblock['ID']]))
101 {
102 $fields = array_merge($fields, $productFields);
103 $actionList = array_merge($actionList, static::getProductActionList());
104 }
105
106 $properties = static::getIblockProperties($iblock['ID']);
107 if (!empty($properties))
108 $fields = array_merge($fields, $properties);
109 unset($properties);
110
111 $fields = array_merge($fields, static::getLinkDefinition($actionList));
112 unset($actionList);
113
114 $dataSettings = [
115 'ORDER' => static::getOrderFields($fields),
116 'FIELDS' => static::getShowedFields($fields)
117 ];
118
119 $parameters = [
120 'GRID_ID' => 'iblock'.$sourceId.'_selector',
121 'BASE_LINK' => $path->getUri(),
122 'IBLOCK_ID' => $iblock['ID'],
123 'MULTIPLE_SELECT' => 'N',
124 'USE_MODE' => Iblock\Component\Selector\Entity::MODE_SLIDER,
125 'RESULT_ACTION_TYPE' => $resultActionType,
126 'RESULT_ACTION_NAME' => $action['NAME'],
127 'RESULT_DATA_TYPE' => Iblock\Component\Selector\Entity::RESULT_DATA_TYPE_FILTER,
128 'RESULT_DATA_SET_LIST' => [],
129 'PAGETITLE_FILTER' => 'Y',
130 'SIMPLE_PRODUCTS' => 'Y'
131 ];
132
133 $row = [
134 'SOURCE_ID' => $sourceId,
135 'TITLE' => $iblock['TITLE'],
136 'TYPE' => Landing\Source\Selector::SOURCE_TYPE_COMPONENT,
137 'SETTINGS' => [
138 'COMPONENT_NAME' => 'bitrix:iblock.selector.landing',
139 'COMPONENT_TEMPLATE_NAME' => '.default',
140 'COMPONENT_PARAMS' => $parameters,
141 'WRAPPER' => [
142 'USE_PADDING' => false,
143 'PLAIN_VIEW' => false,
144 'USE_UI_TOOLBAR' => 'N'
145 ]
146 ],
147 'SOURCE_FILTER' => [
148 'IBLOCK_ID' => $iblock['ID'],
149 'ACTIVE_DATE' => 'Y',
150 'CHECK_PERMISSIONS' => 'Y',
151 'MIN_PERMISSION' => 'R'
152 ],
153 'DATA_SETTINGS' => $dataSettings,
154 'DATA_LOADER' => '\Bitrix\Iblock\LandingSource\DataLoader'
155 ];
156 $result[] = $row;
157 }
158 unset($sourceId, $path, $action, $dataSettings, $parameters, $row, $iblock);
159 unset($iblockList);
160
161 unset($selector);
162
163 return new Main\EventResult(Main\EventResult::SUCCESS, $result, 'iblock');
164 }
165
170 protected static function getCatalogs(array $iblocks = [])
171 {
172 $result = [];
173 if (self::$catalogIncluded === null)
174 self::$catalogIncluded = Loader::includeModule('catalog');
175 if (self::$catalogIncluded)
176 {
177 $filter = [];
178 if (!empty($iblocks))
179 $filter['@IBLOCK_ID'] = $iblocks;
180
181 $iterator = Catalog\CatalogIblockTable::getList([
182 'select' => ['IBLOCK_ID'],
183 'filter' => $filter
184 ]);
185 while ($row = $iterator->fetch())
186 $result[] = (int)$row['IBLOCK_ID'];
187 unset($row, $iterator, $filter);
188 }
189 return $result;
190 }
191
195 protected static function getElementFields()
196 {
197 $result = [];
198
199 $result['ID'] = [
200 'ID' => 'ID',
201 'NAME' => 'ID',
202 'TYPE' => Node\Type::TEXT,
203 'ALLOWED' => self::FIELD_ALLOWED_ORDER
204 ];
205 $result['NAME'] = [
206 'ID' => 'NAME',
207 'NAME' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_FIELD_NAME'),
208 'TYPE' => Node\Type::TEXT,
209 'ALLOWED' => self::FIELD_ALLOWED_ALL
210 ];
211 $result['PREVIEW_TEXT'] = [
212 'ID' => 'PREVIEW_TEXT',
213 'NAME' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_FIELD_PREVIEW_TEXT'),
214 'TYPE' => Node\Type::TEXT,
215 'ALLOWED' => self::FIELD_ALLOWED_SELECT
216 ];
217 $result['PREVIEW_PICTURE'] = [
218 'ID' => 'PREVIEW_PICTURE',
219 'NAME' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_FIELD_PREVIEW_PICTURE'),
220 'TYPE' => Node\Type::IMAGE,
221 'ALLOWED' => self::FIELD_ALLOWED_SELECT
222 ];
223 $result['DETAIL_TEXT'] = [
224 'ID' => 'DETAIL_TEXT',
225 'NAME' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_FIELD_DETAIL_TEXT'),
226 'TYPE' => Node\Type::TEXT,
227 'ALLOWED' => self::FIELD_ALLOWED_SELECT
228 ];
229 $result['DETAIL_PICTURE'] = [
230 'ID' => 'DETAIL_PICTURE',
231 'NAME' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_FIELD_DETAIL_PICTURE'),
232 'TYPE' => Node\Type::IMAGE,
233 'ALLOWED' => self::FIELD_ALLOWED_SELECT
234 ];
235 $result['SORT'] = [
236 'ID' => 'SORT',
237 'NAME' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_FIELD_SORT'),
238 'TYPE' => Node\Type::TEXT,
239 'ALLOWED' => self::FIELD_ALLOWED_ORDER
240 ];
241
242 return $result;
243 }
244
248 protected static function getProductFields()
249 {
250 $result = [];
251
252 $result['AVAILABLE'] = [
253 'ID' => 'AVAILABLE',
254 'NAME' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_PRODUCT_FIELD_AVAILABLE'),
255 'TYPE' => Node\Type::TEXT,
256 'ALLOWED' => self::FIELD_ALLOWED_ALL
257 ];
258/* $result['PRICE'] = [
259 'ID' => 'PRICE',
260 'NAME' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_PRODUCT_FIELD_PRICE'),
261 'TYPE' => Node\Type::TEXT,
262 'ALLOWED' => self::FIELD_ALLOWED_ALL
263 ]; */
264/* $result['WEIGHT'] = [
265 'ID' => 'WEIGHT',
266 'NAME' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_PRODUCT_FIELD_WEIGHT'),
267 'TYPE' => Node\Type::TEXT,
268 'ALLOWED' => self::FIELD_ALLOWED_SELECT
269 ];
270 $result['SIZES'] = [
271 'ID' => 'SIZES',
272 'NAME' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_PRODUCT_FIELD_SIZES'),
273 'TYPE' => Node\Type::TEXT,
274 'ALLOWED' => self::FIELD_ALLOWED_SELECT,
275 'COMPILE' => [
276 'FIELDS' => ['LENGTH', 'WIDTH', 'HEIGHT'],
277 'SEPARATOR' => ' * '
278 ]
279 ]; */
280
281 return $result;
282 }
283
288 protected static function getIblockProperties($iblockId)
289 {
290 $result = [];
291
292 $listCodes = Iblock\Model\PropertyFeature::getListPageShowPropertyCodes($iblockId);
293 $detailCodes = Iblock\Model\PropertyFeature::getDetailPageShowPropertyCodes($iblockId);
294 if (empty($listCodes) && empty($detailCodes))
295 return $result;
296
297 $allCodes = array_unique(array_merge($listCodes, $detailCodes));
298 $listCodes = array_fill_keys($listCodes, true);
299 $detailCodes = array_fill_keys($detailCodes, true);
300
301 $iterator = Iblock\PropertyTable::getList([
302 'select' => [
303 'ID', 'IBLOCK_ID', 'NAME', 'SORT', 'PROPERTY_TYPE',
304 'MULTIPLE', 'FILE_TYPE',
305 'USER_TYPE', 'USER_TYPE_SETTINGS_LIST'
306 ],
307 'filter' => [
308 '=IBLOCK_ID' => $iblockId,
309 '@ID' => $allCodes,
310 '=ACTIVE' => 'Y'
311 ],
312 'order' => ['SORT' => 'ASC', 'NAME' => 'ASC']
313 ]);
314 while ($row = $iterator->fetch())
315 {
316 $id = (int)$row['ID'];
317 $index = 'PROPERTY_'.$row['ID'];
318 $titleCode = 'IBLOCK_LANDING_SOURCE_ELEMENT_PROPERTY_TITLE';
319 if (!isset($listCodes[$id]) || !isset($detailCodes[$id]))
320 {
321 $titleCode = (isset($listCodes[$id])
322 ? 'IBLOCK_LANDING_SOURCE_ELEMENT_PROPERTY_LIST_TITLE'
323 : 'IBLOCK_LANDING_SOURCE_ELEMENT_PROPERTY_DETAIL_TITLE'
324 );
325 }
326 $title = Loc::getMessage(
327 $titleCode,
328 ['#NAME#' => $row['NAME']]
329 );
330
331 switch ($row['PROPERTY_TYPE'])
332 {
333 case Iblock\PropertyTable::TYPE_FILE:
334 if (self::checkImageProperty($row))
335 {
336 $result[$index] = [
337 'ID' => $index,
338 'NAME' => $title,
339 'TYPE' => Node\Type::IMAGE,
340 'ALLOWED' => self::FIELD_ALLOWED_SELECT
341 ];
342 }
343 break;
344 default:
345 $result[$index] = [
346 'ID' => $index,
347 'NAME' => $title,
348 'TYPE' => Node\Type::TEXT,
349 'ALLOWED' => ($row['MULTIPLE'] == 'Y' ? self::FIELD_ALLOWED_SELECT : self::FIELD_ALLOWED_ALL)
350 ];
351 break;
352 }
353 }
354
355 return $result;
356 }
357
361 protected static function getProductActionList()
362 {
363 return [
364 [
365 'type' => 'buy',
366 'name' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_ACTION_TITLE_BUY')
367 ],
368 [
369 'type' => 'addToCart',
370 'name' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_ACTION_TITLE_ADD_TO_CART')
371 ]
372 ];
373 }
374
379 protected static function getLinkDefinition(array $actions)
380 {
381 return [
382 'LINK' => [
383 'ID' => 'LINK',
384 'NAME' => Loc::getMessage('IBLOCK_LANDING_SOURCE_ELEMENT_ACTIONS'),
385 'TYPE' => Node\Type::LINK,
386 'ACTIONS' => $actions,
387 'ALLOWED' => self::FIELD_ALLOWED_SELECT
388 ]
389 ];
390 }
391
396 protected static function getOrderFields(array $fields)
397 {
398 $result = [];
399
400 foreach ($fields as $row)
401 {
402 if (($row['ALLOWED'] & self::FIELD_ALLOWED_ORDER) == 0)
403 continue;
404 $result[] = [
405 'ID' => $row['ID'],
406 'NAME' => $row['NAME']
407 ];
408 }
409 unset($row);
410
411 return $result;
412 }
413
418 protected static function getShowedFields(array $fields)
419 {
420 $result = [];
421
422 foreach ($fields as $row)
423 {
424 if (($row['ALLOWED'] & self::FIELD_ALLOWED_SELECT) == 0)
425 continue;
426 $item = $row;
427 unset($item['ALLOWED']);
428 $result[] = $item;
429 }
430 unset($item, $row);
431
432 return $result;
433 }
434
439 protected static function checkImageProperty(array $property)
440 {
441 if (empty($property['FILE_TYPE']))
442 return false;
443 $property['FILE_TYPE'] = mb_strtolower(str_replace(' ', '', trim($property['FILE_TYPE'])));
444 if (empty($property['FILE_TYPE']))
445 return false;
446 $rawFileTypes = explode(',', $property['FILE_TYPE']);
447 if (empty($rawFileTypes))
448 return false;
449 $rawFileTypes = array_fill_keys($rawFileTypes, true);
450 if (
451 !isset($rawFileTypes['jpg'])
452 && !isset($rawFileTypes['gif'])
453 && !isset($rawFileTypes['png'])
454 && !isset($rawFileTypes['jpeg'])
455 )
456 return false;
457 return true;
458 }
459}
static getLinkDefinition(array $actions)
Definition element.php:379
static checkImageProperty(array $property)
Definition element.php:439
static getCatalogs(array $iblocks=[])
Definition element.php:170
static getOrderFields(array $fields)
Definition element.php:396
static getIblockProperties($iblockId)
Definition element.php:288
static getShowedFields(array $fields)
Definition element.php:418
static includeModule($moduleName)
Definition loader.php:69
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29