1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
baseufcomponent.php
См. документацию.
1<?php
2
4
7use CBitrixComponent;
8use CBitrixComponentTemplate;
9use ReflectionClass;
10
15abstract class BaseUfComponent extends CBitrixComponent
16{
17 public const
18 MODE_DEFAULT = '.default',
21
22 /*
23 * List of available media types
24 * MediaType === Template folder by default, may be overriding in child class
25 */
26 public const
27 MEDIA_TYPE_DEFAULT = '.default',
29
33 protected static
35
41 protected
45
46
51 private
52 $mediaType = '',
53 $mode = '',
54 $availableModes = [];
55
56 public function __construct($component = null)
57 {
58 parent::__construct($component);
59 $this->componentTemplate = new CBitrixComponentTemplate();
60 }
61
62 final public function executeComponent()
63 {
64 $this->initResult();
65 $this->prepareResult();
66
67 $this->initAvailableModes();
68 $this->initMode();
69 $this->initMediaType();
70
71 $templateName = $this->resolveTemplateName();
72 $templatePage = $this->resolveTemplatePage();
73
74 $this->setTemplateName($templateName);
75
76 if($templatePage && !$this->isExistTemplatePage($templatePage))
77 {
78 // changing to default templatePage if file with $templatePage name not exist ...
79 if($templatePage !== static::TEMPLATE_PAGE_DEFAULT)
80 {
81 $templatePage = static::TEMPLATE_PAGE_DEFAULT;
82 }
83 // ... or setting default templateName if $templatePage name
84 // is equal to TEMPLATE_PAGE_DEFAULT and not exist in current templateName folder
85 else
86 {
87 $this->setTemplateName(static::TEMPLATE_NAME_DEFAULT);
88 }
89 }
90
91 if($templatePage)
92 {
93 $this->includeComponentTemplate($templatePage);
94 }
95 else
96 {
97 $this->__showError(str_replace(
98 ['#NAME#', '#PAGE#'],
99 [$this->getMode(), $this->getMediaType()],
100 "Cannot find '#NAME#' template with page '#PAGE#'"
101 ));
102 }
103 }
104
105 final protected function initResult(): void
106 {
107 $this->setUserField($this->arParams['~userField'] ?? []);
108 // TODO: $this->arParams['additionalParameters'] contains already html-encoded values (mostly by chance). This is deeply incorrect.
109 $this->setAdditionalParameters($this->arParams['additionalParameters'] ?? []);
110 $this->setParentComponent($this->getAdditionalParameter('parentComponent'));
111
112 $this->arResult['additionalParameters'] = $this->getAdditionalParameters();
113 $this->arResult['userField'] = $this->getUserField();
114 $this->arResult['fieldName'] = $this->getFieldName();
115 $this->arResult['value'] = $this->getFieldValue();
116 }
117
121 public function getUserField()
122 {
123 return $this->userField;
124 }
125
129 public function setUserField($userField): void
130 {
131 if (!is_array($userField))
132 {
133 $userField = [];
134 }
135
136 $this->userField = $userField;
137 }
138
143 public function getAdditionalParameter(string $key)
144 {
145 return ($this->additionalParameters[$key] ?? null);
146 }
147
152 {
154 }
155
160 {
161 $this->additionalParameters = $additionalParameters;
162 }
163
167 public function getParentComponent(): ?CBitrixComponent
168 {
169 return $this->__parent;
170 }
171
175 public function setParentComponent(?CBitrixComponent $_parent): void
176 {
177 $this->__parent = $_parent;
178 }
179
184 protected function prepareResult(): void
185 {
186
187 }
188
189 protected function initAvailableModes(): void
190 {
191 if(!empty($this->additionalParameters['mode']))
192 {
193 $modes = (is_array($this->additionalParameters['mode']) ?
194 $this->additionalParameters['mode'] : [$this->additionalParameters['mode']]
195 );
196 }
197 else
198 {
199 $modes = [static::MODE_DEFAULT];
200 }
201
202 $this->setAvailableModes($modes);
203 }
204
208 public function getAvailableModes(): array
209 {
210 return $this->availableModes;
211 }
212
216 public function setAvailableModes(array $availableModes): void
217 {
218 $this->availableModes = $availableModes;
219 }
220
221 protected function initMode(): void
222 {
223 $availableModes = $this->getAvailableModes();
224 $mode = array_shift($availableModes);
225 $this->setMode($mode);
226 }
227
231 protected function setMode(string $mode): void
232 {
233 $this->mode = $mode;
234 }
235
236 protected function initMediaType(): void
237 {
238 $mediaType = static::MEDIA_TYPE_DEFAULT;
239 if (isset($this->additionalParameters['mediaType']) && $this->additionalParameters['mediaType'])
240 {
241 $mediaType = $this->additionalParameters['mediaType'];
242 }
243
244 $this->setMediaType($mediaType);
245 }
246
250 protected function setMediaType(string $mediaType): void
251 {
252 $this->mediaType = $mediaType;
253 }
254
260 protected function resolveTemplateName(): string
261 {
262 return ($this->getAvailableTemplateFolder() ?? static::TEMPLATE_NAME_DEFAULT);
263 }
264
268 final public function getMode(): string
269 {
270 return $this->mode;
271 }
272
276 protected function getTemplateNameFromMode(): string
277 {
278 return ($this->getMode() ?: static::TEMPLATE_NAME_DEFAULT);
279 }
280
286 final protected function resolveTemplatePage(): ?string
287 {
288 return ($this->isPossibleMediaType() ?
289 $this->getTemplatePageFromMediaType() : null
290 );
291 }
292
297 final protected function isExistTemplatePage(?string $templatePage = ''): bool
298 {
299 if(empty($templatePage))
300 {
301 $templatePage = $this->getTemplatePage();
302 }
303 return $this->hasTemplatePage($templatePage);
304 }
305
310 final protected function isPossibleMediaType(): bool
311 {
312 static $mediaTypes = null;
313 if($mediaTypes === null)
314 {
315 $mediaTypes = $this->getMediaTypes();
316 }
317 return in_array($this->getMediaType(), $mediaTypes, true);
318 }
319
323 protected function getTemplatePageFromMediaType(): string
324 {
325 return $this->getMediaType() ?: static::MEDIA_TYPE_DEFAULT;
326 }
327
332 final protected function getMediaTypes(): array
333 {
334 $reflection = new ReflectionClass(__CLASS__);
335 $constants = $reflection->getConstants();
336 $result = [];
337 foreach($constants as $name => $value)
338 {
339 if(str_starts_with($name, 'MEDIA_TYPE_'))
340 {
341 $result[$name] = $value;
342 }
343 }
344 return $result;
345 }
346
350 protected function getMediaType(): string
351 {
352 return $this->mediaType;
353 }
354
358 final protected function hasTemplateFolder(): bool
359 {
360 static $checkedTemplateFolders = [];
361
362 $name = $this->getName();
363 $mode = $this->getMode();
364
365 if (!isset($checkedTemplateFolders[$name]))
366 {
367 $checkedTemplateFolders[$name] = [];
368 }
369
370 if (
371 !array_key_exists($mode, $checkedTemplateFolders)
372 || $checkedTemplateFolders[$name][$mode] === null
373 )
374 {
376 $this->componentTemplate->Init($this);
377 $checkedTemplateFolders[$name][$mode] = $this->componentTemplate->hasTemplate();
378 }
379
380 return $checkedTemplateFolders[$name][$mode];
381 }
382
387 final protected function getAvailableTemplateFolder(): ?string
388 {
389 $availableMethodsKey = $this->generateAvailableModesHash();
390 static $availableMode = [];
391
392 if(
393 !array_key_exists($availableMethodsKey, $availableMode)
394 ||
395 $availableMode[$availableMethodsKey] === null
396 )
397 {
398 foreach($this->getAvailableModes() as $mode)
399 {
400 $this->setMode($mode);
401 if($this->hasTemplateFolder())
402 {
403 $availableMode[$availableMethodsKey] = $this->getMode();
404 break;
405 }
406 }
407 }
408 return $availableMode[$availableMethodsKey];
409 }
410
414 final protected function generateAvailableModesHash(): string
415 {
416 return md5(static::getUserTypeId() . json_encode($this->getAvailableModes()));
417 }
418
423 final protected function hasTemplatePage(string $templatePage): bool
424 {
425 static $isCheckedTemplatePage = null;
426
427 if($isCheckedTemplatePage === null)
428 {
429 $this->componentTemplate->Init($this, $this->getTemplateNameFromMode());
430 $isCheckedTemplatePage = $this->componentTemplate->hasTemplatePage($templatePage);
431 }
432
433 return $isCheckedTemplatePage;
434 }
435
439 protected function getFieldName(): string
440 {
441 $nameFromAdditionalParameters = ($this->additionalParameters['NAME'] ?? null);
442 $nameFromUserField = ($this->userField['FIELD_NAME'] ?? null);
443
444 $fieldName = $nameFromAdditionalParameters ?? $nameFromUserField;
445 if (!$fieldName)
446 {
447 return '';
448 }
449
450 if($this->isMultiple() && !mb_substr_count($fieldName, '[]'))
451 {
452 $fieldName .= '[]';
453 }
454
455 return $fieldName;
456 }
457
461 public function isMultiple(): bool
462 {
463 return (isset($this->userField['MULTIPLE']) && $this->userField['MULTIPLE'] === 'Y');
464 }
465
469 protected function getFieldValue(): array
470 {
471 $value = [];
472
473 if(empty($this->additionalParameters['bVarsFromForm']) && !isset($this->additionalParameters['VALUE']))
474 {
475 $value = (
476 isset($this->userField['ENTITY_VALUE_ID']) && $this->userField['ENTITY_VALUE_ID'] <= 0
477 ? ($this->userField['SETTINGS']['DEFAULT_VALUE'] ?? [])
478 : ($this->userField['VALUE'] ?? [])
479 );
480 }
481 elseif(isset($this->additionalParameters['VALUE']))
482 {
483 $value = $this->additionalParameters['VALUE'];
484 }
485 elseif(isset($this->userField['FIELD_NAME']))
486 {
487 $value = Context::getCurrent()->getRequest()->get($this->userField['FIELD_NAME']);
488 }
489
490 return self::normalizeFieldValue($value);
491 }
492
497 final protected static function normalizeFieldValue($value): array
498 {
499 if(!is_array($value))
500 {
501 $value = array($value);
502 }
503 if(empty($value))
504 {
505 $value = array(null);
506 }
507
508 return $value;
509 }
510
514 final public function getHtmlBuilder()
515 {
516 if(!array_key_exists(static::getUserTypeId(), self::$htmlBuilder))
517 {
518 $this->setHtmlBuilder(new HtmlBuilder(static::getUserTypeId()));
519 }
520
521 return self::$htmlBuilder[static::getUserTypeId()];
522 }
523
527 final public function setHtmlBuilder(HtmlBuilder $htmlBuilder): void
528 {
529 self::$htmlBuilder[static::getUserTypeId()] = $htmlBuilder;
530 }
531
535 final public function isDefaultMode(): bool
536 {
537 return ($this->getMediaType() === static::MEDIA_TYPE_DEFAULT);
538 }
539
543 final public function isMobileMode(): bool
544 {
545 return ($this->getMediaType() === static::MEDIA_TYPE_MOBILE);
546 }
547
551 final public function isAjaxRequest(): bool
552 {
553 return Context::getCurrent()->getRequest()->isAjaxRequest();
554 }
555
559 abstract protected static function getUserTypeId(): string;
560}
setMediaType(string $mediaType)
Определения baseufcomponent.php:250
__construct($component=null)
Определения baseufcomponent.php:56
isExistTemplatePage(?string $templatePage='')
Определения baseufcomponent.php:297
static normalizeFieldValue($value)
Определения baseufcomponent.php:497
hasTemplatePage(string $templatePage)
Определения baseufcomponent.php:423
setAvailableModes(array $availableModes)
Определения baseufcomponent.php:216
setParentComponent(?CBitrixComponent $_parent)
Определения baseufcomponent.php:175
setAdditionalParameters(?array $additionalParameters)
Определения baseufcomponent.php:159
getAdditionalParameter(string $key)
Определения baseufcomponent.php:143
setHtmlBuilder(HtmlBuilder $htmlBuilder)
Определения baseufcomponent.php:527
$__parent
Определения component.php:31
getTemplatePage()
Определения component.php:329
getName()
Определения component.php:170
__showError($errorMessage, $errorCode="")
Определения component.php:1519
includeComponentTemplate($templatePage="", $customTemplatePath="")
Определения component.php:724
setTemplateName($templateName)
Определения component.php:227
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$name
Определения menu_edit.php:35
Определения culture.php:9
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257