Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
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 $this->setAdditionalParameters($this->arParams['additionalParameters'] ?? []);
109 $this->setParentComponent($this->getAdditionalParameter('parentComponent'));
110
111 $this->arResult['additionalParameters'] = $this->getAdditionalParameters();
112 $this->arResult['userField'] = $this->getUserField();
113 $this->arResult['fieldName'] = $this->getFieldName();
114 $this->arResult['value'] = $this->getFieldValue();
115 }
116
120 public function getUserField()
121 {
122 return $this->userField;
123 }
124
128 public function setUserField($userField): void
129 {
130 if (!is_array($userField))
131 {
132 $userField = [];
133 }
134
135 $this->userField = $userField;
136 }
137
142 public function getAdditionalParameter(string $key)
143 {
144 return ($this->additionalParameters[$key] ?? null);
145 }
146
150 public function getAdditionalParameters(): array
151 {
153 }
154
158 public function setAdditionalParameters(?array $additionalParameters): void
159 {
160 $this->additionalParameters = $additionalParameters;
161 }
162
166 public function getParentComponent(): ?CBitrixComponent
167 {
168 return $this->__parent;
169 }
170
174 public function setParentComponent(?CBitrixComponent $_parent): void
175 {
176 $this->__parent = $_parent;
177 }
178
183 protected function prepareResult(): void
184 {
185
186 }
187
188 protected function initAvailableModes(): void
189 {
190 if(!empty($this->additionalParameters['mode']))
191 {
192 $modes = (is_array($this->additionalParameters['mode']) ?
193 $this->additionalParameters['mode'] : [$this->additionalParameters['mode']]
194 );
195 }
196 else
197 {
198 $modes = [static::MODE_DEFAULT];
199 }
200
201 $this->setAvailableModes($modes);
202 }
203
207 public function getAvailableModes(): array
208 {
209 return $this->availableModes;
210 }
211
215 public function setAvailableModes(array $availableModes): void
216 {
217 $this->availableModes = $availableModes;
218 }
219
220 protected function initMode(): void
221 {
222 $availableModes = $this->getAvailableModes();
223 $mode = array_shift($availableModes);
224 $this->setMode($mode);
225 }
226
230 protected function setMode(string $mode): void
231 {
232 $this->mode = $mode;
233 }
234
235 protected function initMediaType(): void
236 {
237 $mediaType = static::MEDIA_TYPE_DEFAULT;
238 if (isset($this->additionalParameters['mediaType']) && $this->additionalParameters['mediaType'])
239 {
240 $mediaType = $this->additionalParameters['mediaType'];
241 }
242
243 $this->setMediaType($mediaType);
244 }
245
249 protected function setMediaType(string $mediaType): void
250 {
251 $this->mediaType = $mediaType;
252 }
253
259 protected function resolveTemplateName(): string
260 {
261 return ($this->getAvailableTemplateFolder() ?? static::TEMPLATE_NAME_DEFAULT);
262 }
263
267 final public function getMode(): string
268 {
269 return $this->mode;
270 }
271
275 protected function getTemplateNameFromMode(): string
276 {
277 return ($this->getMode() ?: static::TEMPLATE_NAME_DEFAULT);
278 }
279
285 final protected function resolveTemplatePage(): ?string
286 {
287 return ($this->isPossibleMediaType() ?
288 $this->getTemplatePageFromMediaType() : null
289 );
290 }
291
296 final protected function isExistTemplatePage(?string $templatePage = ''): bool
297 {
298 if(empty($templatePage))
299 {
300 $templatePage = $this->getTemplatePage();
301 }
302 return $this->hasTemplatePage($templatePage);
303 }
304
309 final protected function isPossibleMediaType(): bool
310 {
311 static $mediaTypes = null;
312 if($mediaTypes === null)
313 {
314 $mediaTypes = $this->getMediaTypes();
315 }
316 return in_array($this->getMediaType(), $mediaTypes, true);
317 }
318
322 protected function getTemplatePageFromMediaType(): string
323 {
324 return $this->getMediaType() ?: static::MEDIA_TYPE_DEFAULT;
325 }
326
331 final protected function getMediaTypes(): array
332 {
333 $reflection = new ReflectionClass(__CLASS__);
334 $constants = $reflection->getConstants();
335 $result = [];
336 foreach($constants as $name => $value)
337 {
338 if(mb_strpos($name, 'MEDIA_TYPE_') === 0)
339 {
340 $result[$name] = $value;
341 }
342 }
343 return $result;
344 }
345
349 protected function getMediaType(): string
350 {
351 return $this->mediaType;
352 }
353
357 final protected function hasTemplateFolder(): bool
358 {
359 static $checkedTemplateFolders = [];
360
361 if(
362 !array_key_exists($this->getMode(), $checkedTemplateFolders)
363 ||
364 $checkedTemplateFolders[$this->getMode()] === null
365 )
366 {
367 $this->setTemplateName($this->getTemplateNameFromMode());
368 $this->componentTemplate->Init($this);
369 $checkedTemplateFolders[$this->getMode()] = $this->componentTemplate->hasTemplate();
370 }
371
372 return $checkedTemplateFolders[$this->getMode()];
373 }
374
379 final protected function getAvailableTemplateFolder(): ?string
380 {
381 $availableMethodsKey = $this->generateAvailableModesHash();
382 static $availableMode = [];
383
384 if(
385 !array_key_exists($availableMethodsKey, $availableMode)
386 ||
387 $availableMode[$availableMethodsKey] === null
388 )
389 {
390 foreach($this->getAvailableModes() as $mode)
391 {
392 $this->setMode($mode);
393 if($this->hasTemplateFolder())
394 {
395 $availableMode[$availableMethodsKey] = $this->getMode();
396 break;
397 }
398 }
399 }
400 return $availableMode[$availableMethodsKey];
401 }
402
406 final protected function generateAvailableModesHash(): string
407 {
408 return md5(static::getUserTypeId() . json_encode($this->getAvailableModes()));
409 }
410
415 final protected function hasTemplatePage(string $templatePage): bool
416 {
417 static $isCheckedTemplatePage = null;
418
419 if($isCheckedTemplatePage === null)
420 {
421 $this->componentTemplate->Init($this, $this->getTemplateNameFromMode());
422 $isCheckedTemplatePage = $this->componentTemplate->hasTemplatePage($templatePage);
423 }
424
425 return $isCheckedTemplatePage;
426 }
427
431 protected function getFieldName(): string
432 {
433 $nameFromAdditionalParameters = ($this->additionalParameters['NAME'] ?? null);
434 $nameFromUserField = ($this->userField['FIELD_NAME'] ?? null);
435
436 $fieldName = $nameFromAdditionalParameters ?? $nameFromUserField;
437 if (!$fieldName)
438 {
439 return '';
440 }
441
442 if($this->isMultiple() && !mb_substr_count($fieldName, '[]'))
443 {
444 $fieldName .= '[]';
445 }
446
447 return $fieldName;
448 }
449
453 public function isMultiple(): bool
454 {
455 return (isset($this->userField['MULTIPLE']) && $this->userField['MULTIPLE'] === 'Y');
456 }
457
461 protected function getFieldValue(): array
462 {
463 $value = [];
464
465 if(empty($this->additionalParameters['bVarsFromForm']) && !isset($this->additionalParameters['VALUE']))
466 {
467 $value = (
468 isset($this->userField['ENTITY_VALUE_ID']) && $this->userField['ENTITY_VALUE_ID'] <= 0
469 ? ($this->userField['SETTINGS']['DEFAULT_VALUE'] ?? [])
470 : ($this->userField['VALUE'] ?? [])
471 );
472 }
473 elseif(isset($this->additionalParameters['VALUE']))
474 {
475 $value = $this->additionalParameters['VALUE'];
476 }
477 elseif(isset($this->userField['FIELD_NAME']))
478 {
479 $value = Context::getCurrent()->getRequest()->get($this->userField['FIELD_NAME']);
480 }
481
482 return self::normalizeFieldValue($value);
483 }
484
489 final protected static function normalizeFieldValue($value): array
490 {
491 if(!is_array($value))
492 {
493 $value = array($value);
494 }
495 if(empty($value))
496 {
497 $value = array(null);
498 }
499
500 return $value;
501 }
502
506 final public function getHtmlBuilder()
507 {
508 if(!array_key_exists(static::getUserTypeId(), self::$htmlBuilder))
509 {
510 $this->setHtmlBuilder(new HtmlBuilder(static::getUserTypeId()));
511 }
512
513 return self::$htmlBuilder[static::getUserTypeId()];
514 }
515
519 final public function setHtmlBuilder(HtmlBuilder $htmlBuilder): void
520 {
521 self::$htmlBuilder[static::getUserTypeId()] = $htmlBuilder;
522 }
523
527 final public function isDefaultMode(): bool
528 {
529 return ($this->getMediaType() === static::MEDIA_TYPE_DEFAULT);
530 }
531
535 final public function isMobileMode(): bool
536 {
537 return ($this->getMediaType() === static::MEDIA_TYPE_MOBILE);
538 }
539
543 final public function isAjaxRequest(): bool
544 {
545 return Context::getCurrent()->getRequest()->isAjaxRequest();
546 }
547
551 abstract protected static function getUserTypeId(): string;
552}
isExistTemplatePage(?string $templatePage='')
setParentComponent(?CBitrixComponent $_parent)
setAdditionalParameters(?array $additionalParameters)
setHtmlBuilder(HtmlBuilder $htmlBuilder)
static getCurrent()
Definition context.php:241