Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
2
4
10
11abstract class Base
12{
20 public const FIELD_ID = '';
21
29 public const TYPE_ID = '';
30
36 protected const FIELD_PREFIX = 'UF_';
37
41 protected static ?bool $bitrix24Include = null;
42
46 protected static ?array $languages = null;
47
48 private static ?array $fields = null;
49
53 protected static ?array $allowedProductTypeList = null;
54
55 protected static ?array $allowedOperations = null;
56
69 public static function getDescription(): ?array
70 {
71 if (!static::isAllowed())
72 {
73 return null;
74 }
75
76 return [
77 'ID' => static::getFieldId(),
78 'TYPE' => static::getTypeId(),
79 'TITLE' => static::getTitle(),
80 ];
81 }
82
93 abstract public static function getConfig(): ?array;
94
95 abstract public static function isAllowed(): bool;
96
97 public static function getTitle(): string
98 {
99 $result = static::getTitleInternal();
100
101 return ($result ?? static::getUserFieldName(static::getFieldId()));
102 }
103
104 abstract protected static function getTitleInternal(): ?string;
105
106 abstract public static function getUserFieldBaseParam(): array;
107
108 public static function isExists(): bool
109 {
110 $row = static::load();
111 return !empty($row);
112 }
113
114 public static function create(): Main\Result
115 {
116 if (!static::isAllowed())
117 {
118 return static::getCommonError('CATALOG_SYSTEMFIELD_ERR_DISALLOWED_FIELD');
119 }
120
121 $description = static::getDescription();
122 if (
123 $description === null
124 || empty($description['TYPE'])
125 || empty($description['ID'])
126 )
127 {
128 return static::getCommonError('CATALOG_SYSTEMFIELD_ERR_BAD_FIELD_DESCRIPTION');
129 }
130 if (!is_a($description['TYPE'], Type\Base::class, true))
131 {
132 return static::getCommonError('CATALOG_SYSTEMFIELD_ERR_BAD_FIELD_TYPE');
133 }
134
135 $className = $description['TYPE'];
136
137 $config = static::getConfig();
138 if ($config === null)
139 {
140 return static::getCommonError('CATALOG_SYSTEMFIELD_ERR_BAD_CONFIG_DESCRIPTION');
141 }
142
143 $result = $className::create($config);
144 if ($result->isSuccess())
145 {
146 static::updateProductFormConfiguration();
147 }
148
149 return $result;
150 }
151
152 public static function updateProductFormConfiguration(): void {}
153
154 public static function getTypeId(): string
155 {
156 return static::TYPE_ID;
157 }
158
159 public static function getFieldId(): string
160 {
161 return static::FIELD_ID;
162 }
163
164 protected static function isBitrix24(): bool
165 {
166 if (self::$bitrix24Include === null)
167 {
168 self::$bitrix24Include = Loader::includeModule('bitrix24');
169 }
170
171 return self::$bitrix24Include;
172 }
173
174 protected static function getUserFieldName(string $id): string
175 {
176 return self::FIELD_PREFIX.$id;
177 }
178
184 protected static function getLanguages(): array
185 {
186 if (self::$languages === null)
187 {
188 self::$languages = [];
189 $iterator = LanguageTable::getList([
190 'select' => ['ID'],
191 'filter' => ['=ACTIVE' => 'Y']
192 ]);
193 while ($row = $iterator->fetch())
194 {
195 self::$languages[] = $row['ID'];
196 }
197 unset($row, $iterator);
198 }
199
200 return self::$languages;
201 }
202
203 protected static function getMessages(string $file, array $messageIds): array
204 {
205 $messageList = array_fill_keys(array_keys($messageIds), []);
206 $languages = self::getLanguages();
207 foreach ($languages as $languageId)
208 {
209 $mess = Loc::loadLanguageFile($file, $languageId);
210 foreach ($messageIds as $index => $phrase)
211 {
212 $message = (string)($mess[$phrase] ?? null);
213 if ($message !== '')
214 {
215 $messageList[$index][$languageId] = $message;
216 }
217 }
218 }
219 unset($message, $languageId, $languages);
220
221 return $messageList;
222 }
223
224 protected static function getCommonError(string $errorCode): Main\Result
225 {
226 $result = new Main\Result();
227 $result->addError(new Main\Error(
229 $errorCode,
230 ['#TITLE#' => static::getTitle()]
231 )
232 ));
233 return $result;
234 }
235
236 public static function getGridAction(ProductGroupAction $panel): ?array
237 {
238 if (!static::isAllowed())
239 {
240 return null;
241 }
242
243 $description = static::getDescription();
244 if (
245 $description === null
246 || empty($description['TYPE'])
247 || empty($description['ID'])
248 )
249 {
250 return null;
251 }
252 if (
253 !class_exists($description['TYPE'])
254 || !is_a($description['TYPE'], Type\Base::class, true))
255 {
256 return null;
257 }
258
259 $actionConfig = static::getGridActionConfig($panel);
260 if ($actionConfig === null)
261 {
262 return null;
263 }
264
265 $className = $description['TYPE'];
266
267 return $className::getGridAction($actionConfig);
268 }
269
270 protected static function getGridActionConfig(ProductGroupAction $panel): ?array
271 {
272 return null;
273 }
274
275 public static function load(): ?array
276 {
277 if (self::$fields === null)
278 {
279 self::$fields = [];
280 }
281 $className = get_called_class();
282 if (!array_key_exists($className, self::$fields))
283 {
284 self::$fields[$className] = self::loadInternal($className);
285 }
286
287 return self::$fields[$className];
288 }
289
290 public static function clearCache(): void
291 {
292 self::$languages = null;
293 self::$fields = null;
294 self::$allowedProductTypeList = null;
295 self::$allowedOperations = null;
296 }
297
298 private static function loadInternal(string $className): ?array
299 {
301 $config = $className::getUserFieldBaseParam();
302 if (empty($config))
303 {
304 return null;
305 }
306 if ($config['USER_TYPE_ID'] === null)
307 {
308 return null;
309 }
310
311 $iterator = Main\UserFieldTable::getList([
312 'select' => array_merge(
313 ['*'],
315 ),
316 'filter' => [
317 '=ENTITY_ID' => $config['ENTITY_ID'],
318 '=FIELD_NAME' => $config['FIELD_NAME'],
319 '=USER_TYPE_ID' => $config['USER_TYPE_ID'],
320 ],
321 'runtime' => [
323 ],
324 ]);
325 $row = $iterator->fetch();
326 unset($iterator, $config);
327
328 if (!empty($row))
329 {
330 return static::afterLoadInternalModify($row);
331 }
332
333 return null;
334 }
335
336 protected static function afterLoadInternalModify(array $row): array
337 {
338 $row['ID'] = (int)$row['ID'];
339 $row['SORT'] = (int)$row['SORT'];
340
341 foreach (Main\UserFieldTable::getLabelFields() as $fieldName)
342 {
343 if ($fieldName === 'LANGUAGE_ID')
344 {
345 unset($row[$fieldName]);
346 }
347 else
348 {
349 if (isset($row[$fieldName]) && $row[$fieldName] === '')
350 {
351 $row[$fieldName] = null;
352 }
353 }
354 }
355
356 return $row;
357 }
358
359 public static function checkAllowedProductType(int $type): bool
360 {
361 if (self::$allowedProductTypeList === null)
362 {
363 self::$allowedProductTypeList = [];
364 }
365 $className = get_called_class();
366 if (!isset(self::$allowedProductTypeList[$className]))
367 {
368 self::$allowedProductTypeList[$className] = static::getAllowedProductTypeList();
369 }
370
371 return in_array($type, self::$allowedProductTypeList[$className], true);
372 }
373
374 public static function getAllowedProductTypeList(): array
375 {
376 return [];
377 }
378
379 public static function checkRestictions(array $restrictions): bool
380 {
381 if (isset($restrictions['TYPE']))
382 {
383 if (!static::checkAllowedProductType($restrictions['TYPE']))
384 {
385 return false;
386 }
387 }
388
389 return true;
390 }
391
392 public static function getGroupActionRequest(ProductGroupAction $panel): ?array
393 {
394 $field = static::getUserFieldBaseParam();
395 $requestName = $panel->getFormRowFieldName($field['FIELD_NAME']);
396 $value = Main\Context::getCurrent()->getRequest()->get($requestName);
397
398 if ($value === null)
399 {
400 return null;
401 }
402 if ($field['MULTIPLE'] === 'Y' && !is_array($value))
403 {
404 $value = [$value];
405 }
406
407 return [$field['FIELD_NAME'] => $value];
408 }
409
410 public static function checkAllowedOperation(string $operation): bool
411 {
412 if (self::$allowedOperations === null)
413 {
414 self::$allowedOperations = [];
415 }
416 $className = get_called_class();
417 if (!isset(self::$allowedOperations[$className]))
418 {
419 self::$allowedOperations[$className] = array_fill_keys(static::getAllowedOperations(), true);
420 }
421
422 return isset(self::$allowedOperations[$className][$operation]);
423 }
424
425 public static function getAllowedOperations(): array
426 {
427 return [];
428 }
429
430 public static function getOperationSelectFieldList(string $operation): array
431 {
432 return [];
433 }
434
435 public static function prepareValue(string $operation, array $productRow): array
436 {
437 return $productRow;
438 }
439
440 public static function renderAdminFormControl(array $field, array $product, array $config): ?string
441 {
442 if (!static::isAllowed())
443 {
444 return null;
445 }
446 if (!static::checkRestictions($product))
447 {
448 return null;
449 }
450 if (!static::isExists())
451 {
452 return null;
453 }
454
455 $userFieldManager = Main\UserField\Internal\UserFieldHelper::getInstance()->getManager();
456 $request = Main\Context::getCurrent()->getRequest();
457
458 return $userFieldManager->GetEditFormHTML(
459 $config['FROM_FORM'],
460 $request->getPost($field['FIELD_NAME']) ?? '',
461 $field
462 );
463 }
464
465 public static function getUiDescription(array $restrictions): ?array
466 {
467 if (
468 static::isAllowed()
469 && static::checkRestictions($restrictions)
470 && static::isExists()
471 )
472 {
473 $userField = static::load();
474 if ($userField === null)
475 {
476 return null;
477 }
478
479 $description = [
480 'entity' => 'product',
481 'name' => $userField['FIELD_NAME'],
482 'originalName' => $userField['FIELD_NAME'],
483 'title' => $userField['EDIT_FORM_LABEL'] ?? $userField['FIELD_NAME'],
484 'hint' => $userField['HELP_MESSAGE'],
485 'editable' => true,
486 'required' => $userField['MANDATORY'] === 'Y',
487 'multiple' => $userField['MULTIPLE'] === 'Y',
488 'placeholders' => null,
489 'defaultValue' => $userField['SETTINGS']['DEFAULT_VALUE'] ?? '',
490 'optionFlags' => 1, // showAlways */
491 'options' => [
492 'showCode' => 'true',
493 ],
494 'data' => [],
495 ];
496
497 return static::getUiDescriptionInternal($description, $userField, $restrictions);
498 }
499
500 return null;
501 }
502
503 protected static function getUiDescriptionInternal(array $description, array $userField, array $restrictions): ?array
504 {
505 return $description;
506 }
507}
static getUiDescriptionInternal(array $description, array $userField, array $restrictions)
Definition base.php:503
static prepareValue(string $operation, array $productRow)
Definition base.php:435
static checkAllowedProductType(int $type)
Definition base.php:359
static getGroupActionRequest(ProductGroupAction $panel)
Definition base.php:392
static getCommonError(string $errorCode)
Definition base.php:224
static getOperationSelectFieldList(string $operation)
Definition base.php:430
static getUserFieldName(string $id)
Definition base.php:174
static checkAllowedOperation(string $operation)
Definition base.php:410
static getGridActionConfig(ProductGroupAction $panel)
Definition base.php:270
static renderAdminFormControl(array $field, array $product, array $config)
Definition base.php:440
static afterLoadInternalModify(array $row)
Definition base.php:336
static checkRestictions(array $restrictions)
Definition base.php:379
static getUiDescription(array $restrictions)
Definition base.php:465
static getGridAction(ProductGroupAction $panel)
Definition base.php:236
static getMessages(string $file, array $messageIds)
Definition base.php:203
static loadLanguageFile($file, $language=null, $normalize=true)
Definition loc.php:224
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getLabelsSelect(string $referenceName=null)
static getLabelsReference(string $referenceName=null, string $languageId=null)