Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
buttons.php
1<?php
2
4
6
7class Buttons
8{
9 protected const BTN_CLASS_MATCHER = '/class=(["\'][^"\']*\s|["\'])btn(\s[^"\']*["\']|["\'])/i';
10 protected const OLD_BUTTON_MATCHER = '/class=["\'][^"\']*u-btn-\w+[^"\']*["\']/i';
11 protected const BUTTON_COLOR_MATCHER = '/(?<=[\s\'"])u-btn-(\w+)([\s\'"])/i';
12 protected const BUTTON_COLOR_OUTLINE_MATCHER = '/(?<=[\s\'"])u-btn-outline-(\w+)([\s\'"])/i';
13 protected const BUTTON_COLOR_THEME_MATCHER = '/(?<=[\s\'"])u-(theme-bitrix-btn-v)(\d{1,})([\s\'"])/i';
14 protected const BUTTON_COLOR_THEME_OUTLINE_MATCHER = '/(?<=[\s\'"])u-(theme-bitrix-btn-outline-v)(\d{1,})([\s\'"])/i';
15 protected const BUTTON_BORDER_MATCHER = '/(?<=[\s\'"])g-brd-[\d]{1,2}([\s\'"])/i';
16 protected const PADDING_MATCHER = '/(?<=[\s\'"])(g-p[abtlrxy]-\d{1,3}(--\w\w)?)([\s\'"])/i';
17
21 protected $blockId;
25 protected $content;
26
27 public function __construct($blockId, $content)
28 {
29 $this->blockId = $blockId;
30 $this->content = $content;
31 }
32
33 public function update(): void
34 {
35 preg_match_all(self::BTN_CLASS_MATCHER, $this->content, $contentMatches);
36 $newContentMatches = [];
37 foreach ($contentMatches[0] as $contentMatchesItem)
38 {
39 $contentMatchesItem = $this->changeColor($contentMatchesItem);
40 $contentMatchesItem = $this->changeBorder($contentMatchesItem);
41 $contentMatchesItem = $this->changeRound($contentMatchesItem);
42 $contentMatchesItem = $this->changePadding($contentMatchesItem);
43 array_push($newContentMatches, $contentMatchesItem);
44 }
45
46 $this->content = str_replace($contentMatches[0], $newContentMatches, $this->content);
47 $this->save();
48 }
49
50 protected function save(): void
51 {
52 BlockTable::update(
53 $this->blockId,
54 [
55 'CONTENT' => $this->content
56 ]
57 );
58 }
59
60 protected function changeColor($content)
61 {
62 $content = preg_replace(self::BUTTON_COLOR_MATCHER, 'g-btn-$1 g-btn-type-solid$2', $content);
63 $content = preg_replace(self::BUTTON_COLOR_OUTLINE_MATCHER, 'g-btn-$1 g-btn-type-outline$2', $content);
64 $content = preg_replace(self::BUTTON_COLOR_THEME_MATCHER, 'g-$1$2 g-btn-type-solid$3', $content);
65 $content = preg_replace(self::BUTTON_COLOR_THEME_OUTLINE_MATCHER, 'g-$1$2 g-btn-type-outline$3', $content);
66 return $content;
67 }
68
69 protected function changeBorder($content)
70 {
71 $content = preg_replace(self::BUTTON_BORDER_MATCHER, '$1', $content);
72 return $content;
73 }
74
75 protected function changeRound($content)
76 {
77 $content = str_replace('g-rounded-50x', 'g-rounded-50', $content);
78 return $content;
79 }
80
81 protected function changePadding($content)
82 {
83 $content = preg_replace(self::PADDING_MATCHER, '$3', $content);
84 return $content;
85 }
86
87 public static function isOldButton($content): bool
88 {
89 return (bool) preg_match(self::OLD_BUTTON_MATCHER, $content);
90 }
91
92 public static function updateLanding(int $lid): void
93 {
94 $res = BlockTable::getList(
95 [
96 'select' => [
97 'ID', 'CONTENT'
98 ],
99 'filter' => [
100 'LID' => $lid,
101 ],
102 ]
103 );
104 while ($row = $res->fetch())
105 {
106 if(self::isOldButton($row['CONTENT']))
107 {
108 $buttonBlock = new self($row['ID'], $row['CONTENT']);
109 $buttonBlock->update();
110 }
111 }
112 }
113
114}
__construct($blockId, $content)
Definition buttons.php:27