Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
propertyfeature.php
1<?php
3
7
8Loc::loadMessages(__FILE__);
9
11{
12 const EVENT_ID_FEATURE_LIST = 'OnPropertyFeatureBuildList'; // event name for build feature list
13
14 const FEATURE_ID_LIST_PAGE_SHOW = 'LIST_PAGE_SHOW'; // show property in element list
15 const FEATURE_ID_DETAIL_PAGE_SHOW = 'DETAIL_PAGE_SHOW'; // detail page show property
16
24 public static function addFeatures($propertyId, array $features): Main\Entity\Result
25 {
26 $result = new Main\Entity\Result();
27
28 $propertyId = (int)$propertyId;
29 if ($propertyId <= 0)
30 {
31 $result->addError(new Main\Error(
32 Loc::getMessage('PROPERTY_FEATURE_ERR_BAD_PROPERTY_ID')
33 ));
34 return $result;
35 }
36
37 if (empty($features))
38 {
39 $result->addError(new Main\Error(
40 Loc::getMessage('PROPERTY_FEATURE_ERR_EMPTY_FEATURE_LIST')
41 ));
42 return $result;
43 }
44 $features = self::checkFeatureList($features);
45 if ($features === null)
46 {
47 $result->addError(new Main\Error(
48 Loc::getMessage('PROPERTY_FEATURE_ERR_BAD_FEATURE_LIST')
49 ));
50 return $result;
51 }
52
53 $resultIds = [];
54 foreach ($features as $row)
55 {
56 $row['PROPERTY_ID'] = $propertyId;
57 $internalResult = Iblock\PropertyFeatureTable::add($row);
58 if (!$internalResult->isSuccess())
59 {
60 $result->addErrors($internalResult->getErrors());
61 return $result;
62 }
63
64 $resultIds[] = $internalResult->getId();
65 }
66 unset($internalResult, $row);
67
68 $result->setData($resultIds);
69 return $result;
70 }
71
79 public static function updateFeatures($propertyId, array $features): Main\Entity\Result
80 {
81 $result = new Main\Entity\Result();
82
83 $propertyId = (int)$propertyId;
84 if ($propertyId <= 0)
85 {
86 $result->addError(new Main\Error(
87 Loc::getMessage('PROPERTY_FEATURE_ERR_BAD_PROPERTY_ID')
88 ));
89 return $result;
90 }
91
92 if (empty($features))
93 {
94 $result->addError(new Main\Error(
95 Loc::getMessage('PROPERTY_FEATURE_ERR_EMPTY_FEATURE_LIST')
96 ));
97 return $result;
98 }
99
100 $features = self::checkFeatureList($features);
101 if ($features === null)
102 {
103 $result->addError(new Main\Error(
104 Loc::getMessage('PROPERTY_FEATURE_ERR_BAD_FEATURE_LIST')
105 ));
106 return $result;
107 }
108
109 $currentList = [];
110 $iterator = Iblock\PropertyFeatureTable::getList([
111 'select' => ['*'],
112 'filter' => ['=PROPERTY_ID' => $propertyId]
113 ]);
114 while ($row = $iterator->fetch())
115 {
116 $currentList[self::getIndex($row)] = $row;
117 }
118 unset($row, $iterator);
119
120 foreach ($features as $index => $row)
121 {
122 if (isset($currentList[$index]))
123 {
124 $internalResult = Iblock\PropertyFeatureTable::update(
125 $currentList[$index]['ID'],
126 ['IS_ENABLED' => $row['IS_ENABLED']]
127 );
128 }
129 else
130 {
131 $row['PROPERTY_ID'] = $propertyId;
132 $internalResult = Iblock\PropertyFeatureTable::add($row);
133 }
134 if (!$internalResult->isSuccess())
135 {
136 $result->addErrors($internalResult->getErrors());
137 return $result;
138 }
139 }
140 unset($internalResult, $index, $row);
141 unset($currentList);
142
143 return $result;
144 }
145
153 public static function setFeatures($propertyId, array $features): Main\Entity\Result
154 {
155 $result = new Main\Entity\Result();
156
157 $propertyId = (int)$propertyId;
158 if ($propertyId <= 0)
159 {
160 $result->addError(new Main\Error(
161 Loc::getMessage('PROPERTY_FEATURE_ERR_BAD_PROPERTY_ID')
162 ));
163 return $result;
164 }
165
166 if (empty($features))
167 {
168 $result->addError(new Main\Error(
169 Loc::getMessage('PROPERTY_FEATURE_ERR_EMPTY_FEATURE_LIST')
170 ));
171 return $result;
172 }
173 $features = self::checkFeatureList($features);
174 if ($features === null)
175 {
176 $result->addError(new Main\Error(
177 Loc::getMessage('PROPERTY_FEATURE_ERR_BAD_FEATURE_LIST')
178 ));
179 return $result;
180 }
181
182 $currentList = [];
183 $idList = [];
184 $iterator = Iblock\PropertyFeatureTable::getList([
185 'select' => ['*'],
186 'filter' => ['=PROPERTY_ID' => $propertyId]
187 ]);
188 while ($row = $iterator->fetch())
189 {
190 $row['ID'] = (int)$row['ID'];
191 $currentList[self::getIndex($row)] = $row;
192 $idList[$row['ID']] = $row['ID'];
193 }
194 unset($row, $iterator);
195
196 foreach ($features as $index => $row)
197 {
198 if (isset($currentList[$index]))
199 {
200 $internalResult = Iblock\PropertyFeatureTable::update(
201 $currentList[$index]['ID'],
202 ['IS_ENABLED' => $row['IS_ENABLED']]
203 );
204 if ($internalResult->isSuccess())
205 unset($idList[$currentList[$index]['ID']]);
206 }
207 else
208 {
209 $row['PROPERTY_ID'] = $propertyId;
210 $internalResult = Iblock\PropertyFeatureTable::add($row);
211 }
212 if (!$internalResult->isSuccess())
213 {
214 $result->addErrors($internalResult->getErrors());
215 return $result;
216 }
217 }
218 unset($internalResult, $index, $row);
219 unset($currentList);
220
221 if (!empty($idList))
222 {
223 $conn = Main\Application::getConnection();
224 $helper = $conn->getSqlHelper();
225 $query = 'delete from '.$helper->quote(Iblock\PropertyFeatureTable::getTableName()).
226 ' where '.$helper->quote('PROPERTY_ID').' = '.$propertyId.
227 ' and '.$helper->quote('ID').' in ('.implode(',', $idList).')';
228 $conn->queryExecute($query);
229 unset($query, $helper, $conn);
230 }
231 unset($idList);
232
233 return $result;
234 }
235
242 protected static function checkFeatureList(array $list): ?array
243 {
244 if (empty($list))
245 return null;
246
247 $result = [];
248 foreach ($list as $rawRow)
249 {
250 $row = self::checkFeature($rawRow);
251 if ($row === null)
252 return null;
253 $result[self::getIndex($row)] = $row;
254 }
255 unset($rawRow);
256
257 return $result;
258 }
259
266 protected static function checkFeature(array $feature): ?array
267 {
268 if (empty($feature))
269 return null;
270 if (!isset($feature['MODULE_ID']))
271 return null;
272 $feature['MODULE_ID'] = trim((string)$feature['MODULE_ID']);
273 if ($feature['MODULE_ID'] === '')
274 return null;
275 if (!isset($feature['FEATURE_ID']))
276 return null;
277 $feature['FEATURE_ID'] = trim((string)$feature['FEATURE_ID']);
278 if ($feature['FEATURE_ID'] === '')
279 return null;
280 if (!isset($feature['IS_ENABLED']))
281 return null;
282 $feature['IS_ENABLED'] = trim((string)$feature['IS_ENABLED']);
283 if ($feature['IS_ENABLED'] !== 'Y' && $feature['IS_ENABLED'] !== 'N')
284 return null;
285
286 return [
287 'MODULE_ID' => $feature['MODULE_ID'],
288 'FEATURE_ID' => $feature['FEATURE_ID'],
289 'IS_ENABLED' => $feature['IS_ENABLED']
290 ];
291 }
292
299 public static function getIndex(array $feature): string
300 {
301 return $feature['MODULE_ID'].':'.$feature['FEATURE_ID'];
302 }
303
311 public static function parseIndex(string $index): ?array
312 {
313 $parts = explode(':', $index);
314 if (count($parts) === 2)
315 {
316 return [
317 'MODULE_ID' => $parts[0],
318 'FEATURE_ID' => $parts[1],
319 ];
320 }
321
322 return null;
323 }
324
332 public static function getPropertyFeatureList(array $property, array $description = []): array
333 {
334 $result = self::getIblockFeatureList();
335
336 $event = new Main\Event(
337 'iblock',
338 __CLASS__.'::'.self::EVENT_ID_FEATURE_LIST,
339 ['property' => $property, 'description' => $description]
340 );
341 $event->send();
342 foreach($event->getResults() as $eventResult)
343 {
344 if ($eventResult->getType() !== Main\EventResult::SUCCESS)
345 continue;
346 $list = $eventResult->getParameters();
347 if (empty($list) || !is_array($list))
348 continue;
349 foreach ($list as $item)
350 {
351 if (empty($item) || !is_array($item))
352 continue;
353 $item = self::prepareFeatureDescription($item);
354 if (empty($item))
355 continue;
356 $result[] = $item;
357 }
358 unset($item, $list);
359 }
360 unset($eventResult);
361
362 return $result;
363 }
364
376 public static function getListPageShowPropertyCodes($iblockId, array $parameters = []): ?array
377 {
378 $iblockId = (int)$iblockId;
379 if ($iblockId <= 0)
380 return null;
381
383 $iblockId,
384 [
385 '=MODULE_ID' => 'iblock',
386 '=FEATURE_ID' => self::FEATURE_ID_LIST_PAGE_SHOW
387 ],
388 $parameters
389 );
390 }
391
403 public static function getDetailPageShowPropertyCodes($iblockId, array $parameters = []): ?array
404 {
405 $iblockId = (int)$iblockId;
406 if ($iblockId <= 0)
407 return null;
408
410 $iblockId,
411 [
412 '=MODULE_ID' => 'iblock',
413 '=FEATURE_ID' => self::FEATURE_ID_DETAIL_PAGE_SHOW
414 ],
415 $parameters
416 );
417 }
418
431 public static function getDetailPageShowProperties($iblockId, array $parameters = []): ?array
432 {
433 return self::getDetailPageShowPropertyCodes($iblockId, $parameters);
434 }
435
448 protected static function getFilteredPropertyCodes(int $iblockId, array $filter, array $parameters = []): ?array
449 {
450 if (Main\Config\Option::get('iblock', 'property_features_enabled') !== 'Y')
451 {
452 return null;
453 }
454
455 if ($iblockId <= 0)
456 {
457 return null;
458 }
459 if (!isset($filter['=MODULE_ID']) || !isset($filter['=FEATURE_ID']))
460 {
461 return null;
462 }
463
464 $getCode = (isset($parameters['CODE']) && $parameters['CODE'] == 'Y');
465
466 $result = [];
467 $filter['=PROPERTY.IBLOCK_ID'] = $iblockId;
468 $filter['=PROPERTY.ACTIVE'] = 'Y';
469 $filter['=IS_ENABLED'] = 'Y';
470 $iterator = Iblock\PropertyFeatureTable::getList([
471 'select' => [
472 'IBLOCK_PROPERTY_ID' => 'PROPERTY.ID',
473 'IBLOCK_PROPERTY_CODE' => 'PROPERTY.CODE',
474 'IBLOCK_PROPERTY_SORT' => 'PROPERTY.SORT',
475 ],
476 'filter' => $filter,
477 'order' => [
478 'IBLOCK_PROPERTY_SORT' => 'ASC',
479 'IBLOCK_PROPERTY_ID' => 'ASC',
480 ],
481 ]);
482 while ($row = $iterator->fetch())
483 {
484 $result[(int)$row['IBLOCK_PROPERTY_ID']] = self::getPropertyCode(
485 [
486 'ID' => $row['IBLOCK_PROPERTY_ID'],
487 'CODE' => $row['IBLOCK_PROPERTY_CODE']
488 ],
489 $getCode
490 );
491 }
492 unset($row, $iterator);
493 unset($filter, $getCode);
494
495 return (!empty($result) ? array_values($result) : null);
496 }
497
505 protected static function getPropertyCode(array $property, bool $getCode = false): string
506 {
507 $property['ID'] = (string)$property['ID'];
508 if ($getCode)
509 {
510 $property['CODE'] = trim((string)$property['CODE']);
511 if ($property['CODE'] === '')
512 {
513 $property['CODE'] = null;
514 }
515
516 return ($property['CODE'] ?? $property['ID']);
517 }
518 else
519 {
520 return $property['ID'];
521 }
522 }
523
530 private static function prepareFeatureDescription(array $row): array
531 {
532 if (empty($row))
533 return [];
534 if (
535 !isset($row['MODULE_ID'])
536 || !isset($row['FEATURE_ID'])
537 || !isset($row['FEATURE_NAME'])
538 )
539 return [];
540
541 return [
542 'MODULE_ID' => $row['MODULE_ID'],
543 'FEATURE_ID' => $row['FEATURE_ID'],
544 'FEATURE_NAME' => $row['FEATURE_NAME']
545 ];
546 }
547
554 private static function getIblockFeatureList(): array
555 {
556 return [
557 [
558 'MODULE_ID' => 'iblock',
559 'FEATURE_ID' => self::FEATURE_ID_LIST_PAGE_SHOW,
560 'FEATURE_NAME' => Loc::getMessage('PROPERTY_FEATURE_NAME_LIST_PAGE_SHOW')
561 ],
562 [
563 'MODULE_ID' => 'iblock',
564 'FEATURE_ID' => self::FEATURE_ID_DETAIL_PAGE_SHOW,
565 'FEATURE_NAME' => Loc::getMessage('PROPERTY_FEATURE_NAME_DETAIL_PAGE_SHOW')
566 ]
567 ];
568 }
569
575 public static function isEnabledFeatures(): bool
576 {
577 return (Main\Config\Option::get('iblock', 'property_features_enabled') == 'Y');
578 }
579
585 public static function isPropertyFeaturesExist(): bool
586 {
587 return Iblock\PropertyFeatureTable::getCount([], ['ttl' => 86400]) > 0;
588 }
589}
static getDetailPageShowProperties($iblockId, array $parameters=[])
static getPropertyCode(array $property, bool $getCode=false)
static setFeatures($propertyId, array $features)
static updateFeatures($propertyId, array $features)
static getPropertyFeatureList(array $property, array $description=[])
static getListPageShowPropertyCodes($iblockId, array $parameters=[])
static addFeatures($propertyId, array $features)
static getFilteredPropertyCodes(int $iblockId, array $filter, array $parameters=[])
static getDetailPageShowPropertyCodes($iblockId, array $parameters=[])
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29