Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
PropertyProvider.php
1<?php
2
4
16use CIBlockProperty;
17use CIBlockSectionPropertyLink;
18
19Loader::requireModule('ui');
20
22{
28 private array $entity;
29 private string $propertyType;
30 private ?string $userType;
31 private PropertyTypeSettings $typeSettings;
32 private PropertyFeatureEditorFields $futureEditor;
33
39 public function __construct(string $propertyType, ?string $userType, array $entityFields)
40 {
41 $this->propertyType = $propertyType;
42 $this->userType = $userType;
43 $this->entity = $entityFields;
44
45 $this->initEntityDefaultValues();
46 }
47
48 private function initEntityDefaultValues(): void
49 {
50 $fields = $this->getEntityFields();
51 foreach ($fields as $field)
52 {
53 $name = $field['name'];
54 if (array_key_exists($name, $this->entity))
55 {
56 continue;
57 }
58
59 if (isset($field['default_value']))
60 {
61 $this->entity[$name] = $field['default_value'];
62 }
63 elseif ($field['type'] === 'boolean')
64 {
65 $this->entity[$name] = 'N';
66 }
67 }
68 }
69
70 public function getEntityId(): ?int
71 {
72 return $this->entity['ID'] ?? null;
73 }
74
75 public function getEntityIblockId(): ?int
76 {
77 return $this->entity['IBLOCK_ID'] ?? null;
78 }
79
80 public function getEntityData(): array
81 {
82 $values = $this->entity;
83
84 $setValues = $this->getPropertyTypeSettings()->getSetValues();
85 if (!empty($setValues))
86 {
87 $values = array_merge($values, $setValues);
88 }
89
90 return $values;
91 }
92
93 public function getEntityFields(): array
94 {
95 $fields = [
96 [
97 'name' => 'PROPERTY_TYPE',
98 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_PROPERTY_TYPE'),
99 'type' => 'list',
100 'data' => [
101 'items' => $this->getPropertyTypeItems(),
102 ],
103 'required' => true,
104 'disabled' => !is_null($this->getEntityId()) && $this->getEntityId() > 0,
105 ],
106 [
107 'name' => 'CODE',
108 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_CODE'),
109 'type' => 'text',
110 ],
111 [
112 'name' => 'NAME',
113 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_NAME'),
114 'type' => 'text',
115 'required' => true,
116 ],
117 [
118 'name' => 'SORT',
119 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_SORT'),
120 'type' => 'number',
121 'default_value' => 100,
122 'hint' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_SORT_HINT'),
123 ],
124 [
125 'name' => 'ACTIVE',
126 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_ACTIVE'),
127 'type' => 'boolean',
128 'default_value' => 'Y',
129 ],
130 [
131 'name' => 'MULTIPLE',
132 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_MULTIPLE'),
133 'type' => 'boolean',
134 ],
135 [
136 'name' => 'IS_REQUIRED',
137 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_IS_REQUIRED'),
138 'type' => 'boolean',
139 ],
140 [
141 'name' => 'SEARCHABLE',
142 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_SEARCHABLE'),
143 'type' => 'boolean',
144 ],
145 ];
146
147 return $this->clearHiddenFields($fields);
148 }
149
150 public function getAdditionalFields(): array
151 {
152 $fields = [
153 [
154 'name' => 'FILTRABLE',
155 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_FILTERABLE_MSGVER_1'),
156 'type' => 'boolean',
157 ],
158 [
159 'name' => 'WITH_DESCRIPTION',
160 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_WITH_DESCRIPTION'),
161 'type' => 'boolean',
162 ],
163 [
164 'name' => 'MULTIPLE_CNT',
165 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_MULTIPLE_CNT'),
166 'type' => 'number',
167 ],
168 [
169 'name' => 'HINT',
170 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_HINT'),
171 'type' => 'text',
172 ],
173 [
174 'name' => 'SECTION_PROPERTY',
175 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_SECTION_PROPERTY'),
176 'type' => 'boolean',
177 ],
178 [
179 'name' => 'SMART_FILTER',
180 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_SMART_FILTER'),
181 'type' => 'boolean',
182 ],
183 [
184 'name' => 'DISPLAY_TYPE',
185 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_DISPLAY_TYPE'),
186 'type' => 'list',
187 'data' => [
188 'items' => $this->getDisplayTypeItems(),
189 ],
190 ],
191 [
192 'name' => 'DISPLAY_EXPANDED',
193 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_DISPLAY_EXPANDED'),
194 'type' => 'boolean',
195 ],
196 [
197 'name' => 'FILTER_HINT',
198 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_FILTER_HINT'),
199 'type' => 'textarea',
200 ],
201 [
202 'name' => 'ROW_COUNT',
203 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_ROW_COUNT'),
204 'type' => 'number',
205 ],
206 [
207 'name' => 'COL_COUNT',
208 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_COL_COUNT'),
209 'type' => 'number',
210 ],
211 [
212 'name' => 'FILE_TYPE',
213 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_FILE_TYPE'),
214 'type' => 'text',
215 ],
216 [
217 'name' => 'LINK_IBLOCK_ID',
218 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_LINK_IBLOCK_ID'),
219 'type' => 'list',
220 'data' => [
221 'items' => $this->getLinkIblockIdItems(),
222 ],
223 ],
224 [
225 'name' => 'LIST_TYPE',
226 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_LIST_TYPE'),
227 'type' => 'list',
228 'data' => [
229 'items' => $this->getListTypeItems(),
230 ],
231 ],
232 ];
233
234 if (Option::get('iblock', 'show_xml_id') === 'Y')
235 {
236 $fields[] = [
237 'name' => 'XML_ID',
238 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_XML_ID'),
239 'type' => 'text',
240 ];
241 }
242
243 // add default value
244 $html = $this->getPropertyTypeSettings()->getDefaultValueHtml();
245 if (isset($html))
246 {
247 $fields[] = [
248 'name' => 'DEFAULT_VALUE',
249 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_DEFAULT_VALUE'),
250 'type' => 'custom',
251 'data' => [
252 'html' => $html,
253 ],
254 ];
255 }
256 else
257 {
258 $fields[] = [
259 'name' => 'DEFAULT_VALUE',
260 'title' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_DEFAULT_VALUE'),
261 'type' => 'text',
262 ];
263 }
264
265 // add feature fields
266 $futureFields = $this->getPropertyFeatureEditorFields();
267 if ($futureFields->isHasFields())
268 {
269 array_push($fields, ... $futureFields->getEntityFields());
270 }
271
272 return $this->clearHiddenFields($fields);
273 }
274
275 private function clearHiddenFields(array $fields): array
276 {
277 return array_filter(
278 $fields,
279 function(array $item)
280 {
281 return
282 isset($item['name'])
283 && $this->getPropertyTypeSettings()->isShownField($item['name'])
284 ;
285 }
286 );
287 }
288
289 public function getSettingsHtml(): ?string
290 {
291 return $this->getPropertyTypeSettings()->getSettingsHtml();
292 }
293
299 private function getPropertyFeatureEditorFields(): PropertyFeatureEditorFields
300 {
301 $this->futureEditor ??= new PropertyFeatureEditorFields(
302 $this->entity
303 );
304
305 return $this->futureEditor;
306 }
307
313 private function getPropertyTypeSettings(): PropertyTypeSettings
314 {
315 if (isset($this->typeSettings))
316 {
317 return $this->typeSettings;
318 }
319
320 if (isset($this->userType))
321 {
322 $this->typeSettings = PropertyTypeSettings::createByUserType($this->propertyType, $this->userType, $this->entity);
323 }
324 else
325 {
326 $this->typeSettings = new PropertyTypeSettings($this->propertyType, null);
327 }
328
329 return $this->typeSettings;
330 }
331
337 private function getDisplayTypeItems(): array
338 {
339 $result = [];
340
341 $types = CIBlockSectionPropertyLink::getDisplayTypes($this->propertyType, $this->userType);
342 foreach ($types as $type => $name)
343 {
344 $result[] = [
345 'NAME' => $name,
346 'VALUE' => $type,
347 ];
348 }
349
350 return $result;
351 }
352
353 private function hideSkuProperty(): bool
354 {
355 if ($this->getEntityId())
356 {
357 return false;
358 }
359
360 if (!Loader::includeModule('catalog'))
361 {
362 return false;
363 }
364
365 $iblockId = $this->getEntityIblockId();
366 if ($iblockId === null)
367 {
368 return false;
369 }
370
371 $catalog = \CCatalogSku::GetInfoByProductIBlock($iblockId);
372 if (!empty($catalog))
373 {
374 return true;
375 }
376
377 $catalog = \CCatalogSku::GetInfoByOfferIBlock($iblockId);
378 if (!empty($catalog))
379 {
380 return (int)$catalog['SKU_PROPERTY_ID'] > 0;
381 }
382
383 return false;
384 }
385
391 private function getPropertyTypeItems(): array
392 {
393 $result = [];
394
395 $baseTypes = Property::getBaseTypeList(true);
396 foreach ($baseTypes as $type => $name)
397 {
398 $result[] = [
399 'NAME' => $name,
400 'VALUE' => $type,
401 ];
402 }
403
404 $userTypes = CIBlockProperty::GetUserType();
405 if ($this->hideSkuProperty())
406 {
407 unset($userTypes[PropertyTable::USER_TYPE_SKU]);
408 }
409 Collection::sortByColumn($userTypes, [
410 'DESCRIPTION' => SORT_STRING,
411 ]);
412
413 foreach ($userTypes as $type => $item)
414 {
415 $result[] = [
416 'NAME' => $item['DESCRIPTION'],
417 'VALUE' => "{$item['PROPERTY_TYPE']}:{$type}",
418 ];
419 }
420
421 return $result;
422 }
423
429 private function getListTypeItems(): array
430 {
431 $result = [];
432
433 $result[] = [
434 'NAME' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_LIST_TYPE_CHECKBOX'),
435 'VALUE' => PropertyTable::CHECKBOX,
436 ];
437 $result[] = [
438 'NAME' => Loc::getMessage('IBLOCK_ENTITY_EDITOR_PROPERTY_LIST_TYPE_LISTBOX'),
439 'VALUE' => PropertyTable::LISTBOX,
440 ];
441
442 return $result;
443 }
444
450 private function getLinkIblockIdItems(): array
451 {
452 $result = [];
453
454 $filter = [
455 '=ACTIVE' => 'Y',
456 ];
457
458 $iblockId = ($this->entity['IBLOCK_ID'] ?? 0);
459 if ($iblockId > 0)
460 {
461 $filter['!=ID'] = $iblockId;
462 }
463
464 $rows = IblockTable::getList([
465 'select' => [
466 'ID',
467 'NAME',
468 'TYPE_NAME' => 'TYPE.LANG_MESSAGE.NAME',
469 ],
470 'filter' => $filter,
471 ]);
472 foreach ($rows as $row)
473 {
474 $result[] = [
475 'NAME' => "{$row['TYPE_NAME']}. {$row['NAME']}",
476 'VALUE' => $row['ID'],
477 ];
478 }
479
480 return $result;
481 }
482
483 #region not used parent methods
484
485 public function getFields(): array
486 {
487 throw new NotImplementedException('Not used');
488 }
489
490 public function getGUID(): string
491 {
492 throw new NotImplementedException('Not used');
493 }
494
495 public function getEntityTypeName(): string
496 {
497 throw new NotImplementedException('Not used');
498 }
499
500 public function getEntityConfig(): array
501 {
502 throw new NotImplementedException('Not used');
503 }
504
505 #endregion not used parent methods
506}
__construct(string $propertyType, ?string $userType, array $entityFields)
static createByUserType(string $propertyType, string $userType, array $propertyFields, ?string $htmlName='USER_TYPE_SETTINGS')
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())