Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basebuilder.php
1<?php
3
6
7abstract class BaseBuilder
8{
9 public const TYPE_AUTODETECT = 'AUTO';
10
11 public const TYPE_ID = 'BASE';
12 protected const TYPE_WEIGHT = null;
13 protected const PATH_PREFIX = '';
14
15 public const PAGE_ELEMENT_LIST = 'elementList';
16 public const PAGE_ELEMENT_DETAIL = 'elementDetail';
17 public const PAGE_ELEMENT_COPY = 'elementCopy';
18 public const PAGE_ELEMENT_SAVE = 'elementSave';
19 public const PAGE_ELEMENT_SEARCH = 'elementSearch';
20 public const PAGE_ELEMENT_SEO = 'elementSeo';
21 public const PAGE_SECTION_LIST = 'sectionList';
22 public const PAGE_SECTION_DETAIL = 'sectionDetail';
23 public const PAGE_SECTION_COPY = 'sectionCopy';
24 public const PAGE_SECTION_SAVE = 'sectionSave';
25 public const PAGE_SECTION_SEARCH = 'sectionSearch';
26 public const PAGE_SECTION_SEO = 'sectionSeo';
27 public const PAGE_CATALOG_SEO = 'catalogSeo';
28
29 public const ENTITY_SECTION = 'section';
30 public const ENTITY_ELEMENT = 'element';
31
32 protected const SLIDER_PATH_VARIABLE = 'slider_path';
33
35 protected $request;
37 protected $id;
39 protected $weight;
41 protected $languageId;
42
44 protected $iblockId;
46 protected $iblock;
48 protected $iblockListMode;
51
53 protected $prefix;
54
55 protected $urlParams = [];
56
57 protected $compiledUrlParams = '';
58
59 protected $config = [];
60
61 protected $urlTemplates = [];
62
63 protected $templateVariables = [];
65 protected $sliderMode;
66
67 public function __construct()
68 {
69 $this->request = Main\Context::getCurrent()->getRequest();
70
71 $this->initSettings();
72 $this->initConfig();
73 $this->resetIblock();
74 $this->initIblockListMode();
75 $this->initUrlTemplates();
76 }
77
78 public function __destruct()
79 {
80 $this->request = null;
81 }
82
83 public function getId(): string
84 {
85 return $this->id;
86 }
87
88 public function getWeight(): ?int
89 {
90 return $this->weight;
91 }
92
93 public function setLanguageId(string $languageId): void
94 {
95 $this->languageId = $languageId;
96 $this->setTemplateVariable('#LANGUAGE_ID#', $this->languageId);
97 $this->setTemplateVariable('#LANGUAGE#', $this->getLanguageParam());
98 $this->setTemplateVariable('#BASE_PARAMS#', $this->getBaseParams());
99 }
100
101 public function getLanguageId(): string
102 {
103 return $this->languageId;
104 }
105
106 public function setIblockId(int $iblockId): void
107 {
108 if ($this->iblockId !== $iblockId)
109 {
110 $this->resetIblock();
111 if ($iblockId > 0)
112 {
113 $iblock = \CIBlock::GetArrayByID($iblockId);
114 if (!empty($iblock) && is_array($iblock))
115 {
116 $this->iblockId = $iblockId;
117 $this->iblock = $iblock;
118 }
119 unset($iblock);
120 }
121 $this->initIblockListMode();
122 $this->initUrlTemplates();
123 $this->setTemplateVariable('#IBLOCK_ID#', (string)$this->iblockId);
124 $this->setTemplateVariable('#BASE_PARAMS#', $this->getBaseParams());
125 }
126 }
127
128 public function setPrefix(string $prefix): void
129 {
130 $this->prefix = $prefix;
131 $this->setTemplateVariable('#PATH_PREFIX#', $this->prefix);
132 }
133
134 public function getPrefix(): string
135 {
136 return $this->prefix;
137 }
138
139 public function setUrlParams(array $list): void
140 {
141 if ($this->isSliderMode())
142 {
143 $list += static::getSliderOptions();
144 }
145 $this->urlParams = array_filter($list, [__CLASS__, 'clearNull']);
146 $this->compiledUrlParams = $this->compileUrlParams($this->urlParams);
147 }
148
149 public function getCompiledParams(array $params): string
150 {
151 return $this->compileUrlParams($params);
152 }
153
154 public function isIblockListMixed(): bool
155 {
156 $this->initIblockListMode();
158 }
159
160 public function setMixedIblockList(): void
161 {
163 }
164
165 public function setSeparateIblockList(): void
166 {
168 }
169
170 public function preloadUrlData(string $entityType, array $entityIds): void
171 {
172 switch ($entityType)
173 {
175 $this->preloadSectionUrlData($entityIds);
176 break;
178 $this->preloadElementUrlData($entityIds);
179 break;
180 }
181 }
182
183 public function clearPreloadedUrlData(): void {}
184
185 abstract public function use(): bool;
186
187 public function getSectionListUrl(?int $parentId, array $options = [], string $additional = ''): string
188 {
189 return $this->fillUrlTemplate(
190 $this->getUrlTemplate(self::PAGE_SECTION_LIST),
191 $this->getListVariables(self::PAGE_SECTION_LIST, $parentId, $options, $additional)
192 );
193 }
194
195 public function getSectionDetailUrl(?int $entityId, array $options = [], string $additional = ''): string
196 {
197 return $this->fillUrlTemplate(
198 $this->getUrlTemplate(self::PAGE_SECTION_DETAIL),
199 $this->getDetailVariables(self::PAGE_SECTION_DETAIL, $entityId, $options, $additional)
200 );
201 }
202
203 public function getSectionSaveUrl(?int $entityId, array $options = [], string $additional = ''): string
204 {
205 return $this->fillUrlTemplate(
206 $this->getUrlTemplate(self::PAGE_SECTION_SAVE),
207 $this->getDetailVariables(self::PAGE_SECTION_SAVE, $entityId, $options, $additional)
208 );
209 }
210
211 public function getSectionSearchUrl(array $options = [], string $additional = ''): string
212 {
213 return $this->fillUrlTemplate(
214 $this->getUrlTemplate(self::PAGE_SECTION_SEARCH),
215 $this->getExtendedVariables($options, $additional)
216 );
217 }
218
219 public function getElementListUrl(?int $parentId, array $options = [], string $additional = ''): string
220 {
221 return $this->fillUrlTemplate(
222 $this->getUrlTemplate(self::PAGE_ELEMENT_LIST),
223 $this->getListVariables(self::PAGE_ELEMENT_LIST, $parentId, $options, $additional)
224 );
225 }
226
227 public function getElementDetailUrl(?int $entityId, array $options = [], string $additional = ''): string
228 {
229 return $this->fillUrlTemplate(
230 $this->getUrlTemplate(self::PAGE_ELEMENT_DETAIL),
231 $this->getDetailVariables(self::PAGE_ELEMENT_DETAIL, $entityId, $options, $additional)
232 );
233 }
234
235 public function getElementCopyUrl(?int $entityId, array $options = [], string $additional = ''): string
236 {
237 return $this->fillUrlTemplate(
238 $this->getUrlTemplate(self::PAGE_ELEMENT_COPY),
239 $this->getDetailVariables(self::PAGE_ELEMENT_COPY, $entityId, $options, $additional)
240 );
241 }
242
243 public function getElementSaveUrl(?int $entityId, array $options = [], string $additional = ''): string
244 {
245 return $this->fillUrlTemplate(
246 $this->getUrlTemplate(self::PAGE_ELEMENT_SAVE),
247 $this->getDetailVariables(self::PAGE_ELEMENT_SAVE, $entityId, $options, $additional)
248 );
249 }
250
251 public function getElementSearchUrl(array $options = [], string $additional = ''): string
252 {
253 return $this->fillUrlTemplate(
254 $this->getUrlTemplate(self::PAGE_ELEMENT_SEARCH),
255 $this->getExtendedVariables($options, $additional)
256 );
257 }
258
259 public function getCatalogSeoUrl(array $options = [], string $additional = ''): string
260 {
261 return $this->fillUrlTemplate(
262 $this->getUrlTemplate(self::PAGE_CATALOG_SEO),
263 $this->getExtendedVariables($options, $additional)
264 );
265 }
266
267 public function getElementSeoUrl(int $productId, array $options = [], string $additional = ''): string
268 {
269 return $this->fillUrlTemplate(
270 $this->getUrlTemplate(self::PAGE_ELEMENT_SEO),
271 $this->getDetailSeoVariables($productId, $options, $additional)
272 );
273 }
274
275 public function getSectionSeoUrl(int $sectionId, array $options = [], string $additional = ''): string
276 {
277 return $this->fillUrlTemplate(
278 $this->getUrlTemplate(self::PAGE_SECTION_SEO),
279 $this->getSectionSeoVariables($sectionId, $options, $additional)
280 );
281 }
282
283 public function getContextMenuItems(string $pageType, array $items = [], array $options = []): ?array
284 {
285 return null;
286 }
287
288 public function getBaseParams(): string
289 {
290 if ($this->iblockId === null)
291 {
292 return '';
293 }
294
295 return 'IBLOCK_ID='.$this->iblockId
296 .'&type='.urlencode($this->iblock['IBLOCK_TYPE_ID'])
297 .'&lang='.urlencode($this->languageId);
298 }
299
300 public function getUrlParams(array $options = [], string $additional = ''): string
301 {
302 return $this->getBaseParams().$this->extendUrl($options, $additional);
303 }
304
305 public function getLanguageParam(): string
306 {
307 return 'lang='.urlencode($this->languageId);
308 }
309
310 public function getUrlBuilderIdParam(): string
311 {
312 return 'urlBuilderId='.urlencode($this->id);
313 }
314
315 public function setSliderMode(bool $mode): void
316 {
317 $this->sliderMode = $mode;
318 }
319
320 public function isSliderMode(): bool
321 {
322 return $this->sliderMode;
323 }
324
325 public function getDetailPageSlider(): string
326 {
327 $path = $this->getSliderPath();
328 if (!$this->checkSliderPath($path))
329 {
330 return '';
331 }
332 $path = \CUtil::JSEscape($path);
333
334 return '<script>'
335 . 'window.history.replaceState({}, \'\', \'' . $path . '\');' . "\n"
336 . 'BX.ready(function () {' . "\n"
337 . ' BX.SidePanel.Instance.open(' . "\n"
338 . ' \'' . $path . '\'' . "\n"
339 . ' );' . "\n"
340 . '});' . "\n"
341 . '</script>'
342 ;
343 }
344
345 public function showDetailPageSlider(): void
346 {
347 echo $this->getDetailPageSlider();
348 }
349
350 protected function checkCurrentPage(array $urlList): bool
351 {
352 $currentPage = $this->request->getRequestedPage();
353 foreach ($urlList as $url)
354 {
355 if (strncmp($currentPage, $url, strlen($url)) === 0)
356 {
357 return true;
358 }
359 }
360
361 return false;
362 }
363
364 protected function initSettings(): void
365 {
366 $this->id = static::TYPE_ID;
367 $this->weight = static::TYPE_WEIGHT;
368 $this->setLanguageId(LANGUAGE_ID);
369 $this->setPrefix(static::PATH_PREFIX);
370 $this->setSliderMode($this->request->get('IFRAME') === 'Y');
371 }
372
373 protected function initConfig(): void
374 {
375
376 }
377
378 protected static function clearNull($value): bool
379 {
380 return $value !== null;
381 }
382
383 protected function resetIblock(): void
384 {
385 $this->iblockId = null;
386 $this->iblock = null;
387 $this->iblockListMode = null;
388 }
389
390 protected function initIblockListMode(): void
391 {
392 if ($this->iblockListMode !== null)
393 {
394 return;
395 }
396 $listMode = '';
397 if ($this->iblockId !== null)
398 {
399 $listMode = (string)$this->iblock['LIST_MODE'];
400 }
401 if (
404 )
405 {
406 $listMode = (Main\Config\Option::get('iblock', 'combined_list_mode') === 'Y'
407 ? Iblock\IblockTable::LIST_MODE_COMBINED
408 : Iblock\IblockTable::LIST_MODE_SEPARATE
409 );
410 }
411 $this->iblockListMode = $listMode;
412 $this->iblockListMixed = ($this->iblockListMode === Iblock\IblockTable::LIST_MODE_COMBINED);
413 }
414
415 protected function setIblockListMode(string $listMode): void
416 {
417 if (
420 )
421 {
422 $this->iblockListMode = $listMode;
423 $this->iblockListMixed = ($this->iblockListMode === Iblock\IblockTable::LIST_MODE_COMBINED);
424 $this->initUrlTemplates();
425 }
426 }
427
428 protected function compileUrlParams(array $params): string
429 {
430 $result = '';
431 $this->compileParamsLevel($result, '', $params);
432 return $result;
433 }
434
435 protected function compileParamsLevel(string &$result, string $prefix, array $params): void
436 {
437 $params = array_filter($params, [__CLASS__, 'clearNull']);
438 if (empty($params))
439 {
440 return;
441 }
442 foreach ($params as $key => $value)
443 {
444 if ($prefix === '' && is_numeric($key))
445 {
446 continue;
447 }
448 $index = ($prefix !== '' ? $prefix.'['.$key.']' : $key);
449 if (is_array($value))
450 {
451 $this->compileParamsLevel($result, $index, $value);
452 }
453 else
454 {
455 $result .= '&'.urlencode($index).'='.urlencode((string)$value);
456 }
457 }
458 unset($index, $key, $value);
459 }
460
461 protected function getParentFilter(?int $parentId): string
462 {
463 $result = '';
464 if ($parentId !== null)
465 {
466 if ($parentId === -1)
467 {
468 $result = $this->compileUrlParams([
469 'find_section_section' => $parentId
470 ]);
471 }
472 elseif ($parentId >= 0)
473 {
474 $result = $this->compileUrlParams([
475 'find_section_section' => $parentId,
476 'SECTION_ID' => $parentId,
477 'apply_filter' => 'Y'
478 ]);
479 }
480 }
481
482 return $result;
483 }
484
485 protected function getEntityFilter(?int $entityId): string
486 {
487 $result = '';
488 if ($entityId !== null && $entityId >= 0)
489 {
490 $result = $this->compileUrlParams([
491 'ID' => $entityId,
492 ]);
493 }
494
495 return $result;
496 }
497
498 protected function extendUrl(array $options = [], string $additional = ''): string
499 {
500 $result = $this->compiledUrlParams;
501 $compiledOptions = $this->compileUrlParams($options);
502 if ($compiledOptions !== '')
503 {
504 $result .= $compiledOptions;
505 }
506 unset($compiledOptions);
507
508 if ($additional !== '')
509 {
510 $result .= $additional;
511 }
512 return $result;
513 }
514
515 abstract protected function initUrlTemplates(): void;
516
517 protected function getUrlTemplate(string $templateId): ?string
518 {
519 return ($this->urlTemplates[$templateId] ?? null);
520 }
521
522 protected function fillUrlTemplate(?string $template, array $replaces): string
523 {
524 if ($template === null)
525 {
526 return '';
527 }
528 if (empty($replaces))
529 {
530 return $template;
531 }
532 return str_replace(array_keys($replaces), array_values($replaces), $template);
533 }
534
535 protected function setTemplateVariable(string $name, string $value): void
536 {
537 $this->templateVariables[$name] = $value;
538 }
539
540 protected function getTemplateVariables(): array
541 {
543 }
544
545 protected function getExtendedVariables(array $options = [], string $additional = ''): array
546 {
547 $replaces = $this->getTemplateVariables();
548 $replaces['#ADDITIONAL_PARAMETERS#'] = $this->extendUrl($options, $additional);
549 return $replaces;
550 }
551
552 protected function getListVariables(string $page, ?int $parentId, array $options = [], string $additional = ''): array
553 {
554 $replaces = $this->getExtendedVariables($options, $additional);
555 $replaces['#PARENT_ID#'] = (string)$parentId;
556 $replaces['#PARENT_FILTER#'] = $this->getParentFilter($parentId);
557 return $replaces;
558 }
559
560 protected function getDetailVariables(string $page, ?int $entityId, array $options = [], string $additional = ''): array
561 {
562 $replaces = $this->getExtendedVariables($options, $additional);
563 $replaces['#ENTITY_ID#'] = (string)$entityId;
564 $replaces['#ENTITY_FILTER#'] = $this->getEntityFilter($entityId);
565 return $replaces;
566 }
567
568 protected function getDetailSeoVariables(?int $entityId, array $options = [], string $additional = ''): array
569 {
570 $replaces = $this->getExtendedVariables($options, $additional);
571 $replaces['#PRODUCT_ID#'] = (string)$entityId;
572
573 return $replaces;
574 }
575
576 protected function getSectionSeoVariables(?int $sectionId, array $options = [], string $additional = ''): array
577 {
578 $replaces = $this->getExtendedVariables($options, $additional);
579 $replaces['#SECTION_ID#'] = (string)$sectionId;
580
581 return $replaces;
582 }
583
584 protected function getCopyAction(): string
585 {
586 return '&action=copy';
587 }
588
589 protected function preloadSectionUrlData(array $sectionIds): void {}
590
591 protected function preloadElementUrlData(array $elementIds): void {}
592
593 protected static function getSliderOptions(): array
594 {
595 return [
596 'IFRAME' => 'Y',
597 'IFRAME_TYPE' => 'SIDE_SLIDER',
598 ];
599 }
600
601 protected function getSliderPath(): ?string
602 {
603 return $this->request->get(self::SLIDER_PATH_VARIABLE);
604 }
605
606 public function getSliderPathOption(string $path): ?array
607 {
608 if ($path === '')
609 {
610 return null;
611 }
612
613 return [
614 self::SLIDER_PATH_VARIABLE => $path,
615 ];
616 }
617
618 public function getSliderPathString(string $path): string
619 {
620 if ($path === '')
621 {
622 return '';
623 }
624
625 return self::SLIDER_PATH_VARIABLE . '=' . $path;
626 }
627
628 protected function checkSliderPath(?string $path): bool
629 {
630 if ($path === null)
631 {
632 $path = $this->getSliderPath();
633 }
634 if ($path === null || $path === '')
635 {
636 return false;
637 }
638
639 $prepared = [];
640 foreach ($this->getSliderPathTemplates() as $mask)
641 {
642 if (preg_match($mask, $path, $prepared))
643 {
644 return true;
645 }
646 }
647
648 return false;
649 }
650
651 protected function getSliderPathTemplates(): array
652 {
653 return [];
654 }
655
662 public function openSettingsPage(): void
663 {
664 }
665
671 public function subscribeOnAfterSettingsSave(): void
672 {
673 }
674}
getUrlParams(array $options=[], string $additional='')
getSectionListUrl(?int $parentId, array $options=[], string $additional='')
fillUrlTemplate(?string $template, array $replaces)
getSectionSeoVariables(?int $sectionId, array $options=[], string $additional='')
getSectionSeoUrl(int $sectionId, array $options=[], string $additional='')
compileParamsLevel(string &$result, string $prefix, array $params)
getContextMenuItems(string $pageType, array $items=[], array $options=[])
getElementSearchUrl(array $options=[], string $additional='')
getDetailVariables(string $page, ?int $entityId, array $options=[], string $additional='')
getCatalogSeoUrl(array $options=[], string $additional='')
getElementListUrl(?int $parentId, array $options=[], string $additional='')
getDetailSeoVariables(?int $entityId, array $options=[], string $additional='')
getElementDetailUrl(?int $entityId, array $options=[], string $additional='')
getSectionDetailUrl(?int $entityId, array $options=[], string $additional='')
getElementSaveUrl(?int $entityId, array $options=[], string $additional='')
getElementCopyUrl(?int $entityId, array $options=[], string $additional='')
preloadUrlData(string $entityType, array $entityIds)
getSectionSearchUrl(array $options=[], string $additional='')
getListVariables(string $page, ?int $parentId, array $options=[], string $additional='')
getElementSeoUrl(int $productId, array $options=[], string $additional='')
getExtendedVariables(array $options=[], string $additional='')
setTemplateVariable(string $name, string $value)
getSectionSaveUrl(?int $entityId, array $options=[], string $additional='')
extendUrl(array $options=[], string $additional='')