Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
designer.php
1<?php
3
4use \Bitrix\Landing\Block;
5use \Bitrix\Landing\Hook;
6use \Bitrix\Landing\Restriction;
7use \Bitrix\Main\EventManager;
8use \Bitrix\Main\Localization\Loc;
9
10Loc::loadMessages(__FILE__);
11
13{
18 protected $block = null;
19
24 public function __construct(int $blockId)
25 {
26 $block = new Block($blockId, [], [
27 'designer_mode' => true
28 ]);
29 if ($block->exist())
30 {
31 $this->block = $block;
32 }
33 }
34
39 public function isReady(): bool
40 {
41 return $this->block !== null;
42 }
43
48 public function getBlock(): ?Block
49 {
50 return $this->block;
51 }
52
58 public static function setLandingDesignBlockMode(bool $flag): void
59 {
60 $eventManager = EventManager::getInstance();
61 $eventManager->addEventHandler('landing', 'onLandingView',
62 function(\Bitrix\Main\Event $event) use($flag)
63 {
64 $result = new \Bitrix\Main\Entity\EventResult;
65 $options = $event->getParameter('options');
66 $options['design_block'] = $flag;
67 $options['design_block_allowed'] = Restriction\Manager::isAllowed('limit_crm_superblock');
68 $result->modifyFields([
69 'options' => $options
70 ]);
71 return $result;
72 }
73 );
74 }
75
81 public function execHooks(): void
82 {
83 $hooksExec = [];
84
85 foreach (Hook::getForSite($this->block->getSiteId()) as $hook)
86 {
87 if ($hook->enabled())
88 {
89 $hooksExec[$hook->getCode()] = $hook;
90 }
91 }
92
93 foreach (Hook::getForLanding($this->block->getLandingId()) as $hook)
94 {
95 if ($hook->enabled())
96 {
97 $hooksExec[$hook->getCode()] = $hook;
98 }
99 }
100
101 foreach ($hooksExec as $hook)
102 {
103 if ($hook->enabledInEditMode())
104 {
105 $hook->exec();
106 }
107 }
108 }
109
114 protected static function getRepo(): array
115 {
116 static $repo = [];
117
118 if ($repo)
119 {
120 return $repo;
121 }
122
123 $res = DesignerRepo::getList([
124 'order' => [
125 'SORT' => 'asc'
126 ]
127 ]);
128 while ($row = $res->fetch())
129 {
130 $repo[] = $row;
131 }
132
133 return $repo;
134 }
135
142 private static function adjustStylesType(string $selector, array &$item): void
143 {
144 if ($selector === 'landing-block-node-title')
145 {
146 $item['type'] = (array)$item['type'];
147 $item['type'][] = 'heading';
148 }
149 }
150
155 protected static function getTypeClassReferences(): array
156 {
157 $references = [];
158 $selectorName = [];
159
160 foreach (self::getRepo() as $repoItem)
161 {
162 if (
163 !isset($repoItem['MANIFEST']) ||
164 !is_array($repoItem['MANIFEST'])
165 )
166 {
167 continue;
168 }
169 ksort($repoItem['MANIFEST']);
170 foreach (['nodes', 'style'] as $category)
171 {
172 if (isset($repoItem['MANIFEST'][$category]))
173 {
174 foreach ($repoItem['MANIFEST'][$category] as $selector => $item)
175 {
176 if (!is_array($item))
177 {
178 continue;
179 }
180 $selector = trim($selector, '.');
181 if (!isset($references[$selector]))
182 {
183 $references[$selector] = [];
184 }
185 if (!isset($references[$selector][$category]))
186 {
187 $references[$selector][$category] = [];
188 }
189 if (!isset($selectorName[$selector]))
190 {
191 $selectorName[$selector] = Loc::getMessage('LANDING_DESIGN_NODE_' . mb_strtoupper($selector));
192 }
193 if ($category === 'style')
194 {
195 self::adjustStylesType($selector, $item);
196 }
197 $item['name'] = $selectorName[$selector];
198 $references[$selector][$category] = $item;
199 if (
200 isset($repoItem['MANIFEST']['assets']) &&
201 is_array($repoItem['MANIFEST']['assets'])
202 )
203 {
204 $references[$selector]['assets'] = $repoItem['MANIFEST']['assets'];
205 }
206 }
207 }
208 }
209 }
210
211 return $references;
212 }
213
219 public static function parseManifest(string $content): array
220 {
221 static $references = [];
222 $manifest = [
223 'nodes' => [],
224 'style' => [],
225 ];
226
227 if (!$references)
228 {
229 $references = self::getTypeClassReferences();
230 }
231
232 if (preg_match_all('/[\s"]+((' . implode('|', array_keys($references)) . ')[-\d]*)[\s"]+/is', $content, $matches))
233 {
234 foreach ($matches[2] as $i => $selector)
235 {
236 if (isset($references[$selector]))
237 {
238 foreach ($manifest as $code => &$items)
239 {
240 if (isset($references[$selector][$code]))
241 {
242 $items['.'.$matches[1][$i]] = $references[$selector][$code];
243 }
244 }
245 unset($items);
246 if (isset($references[$selector]['assets']))
247 {
248 $manifest['assets'] = $references[$selector]['assets'];
249 }
250 }
251 }
252 }
253
254 return $manifest;
255 }
256
262 public static function getRepository(bool $installRepo = true): array
263 {
264 $repo = [];
265
266 foreach (self::getRepo() as $row)
267 {
268 $repo[] = [
269 'name' => $row['TITLE'] ?: Loc::getMessage('LANDING_DESIGNER_REPO_ELEM_' . mb_strtoupper($row['XML_ID'])),
270 'code' => $row['XML_ID'],
271 'html' => $row['HTML'],
272 'manifest' => $row['MANIFEST']
273 ];
274 }
275
276 if (!$repo && $installRepo)
277 {
279 return self::getRepository(false);
280 }
281
282 return $repo;
283 }
284
290 public static function registerRepoElement(array $fields): void
291 {
292 if (isset($fields['XML_ID']) && is_string($fields['XML_ID']))
293 {
294 $res = DesignerRepo::getList([
295 'select' => [
296 'ID'
297 ],
298 'filter' => [
299 '=XML_ID' => $fields['XML_ID']
300 ]
301 ]);
302 if ($row = $res->fetch())
303 {
304 DesignerRepo::update($row['ID'], $fields);
305 }
306 else
307 {
308 DesignerRepo::add($fields)->isSuccess();
309 }
310 }
311 }
312
318 public static function unregisterRepoElement(string $code): void
319 {
320 $res = DesignerRepo::getList([
321 'select' => [
322 'ID'
323 ],
324 'filter' => [
325 '=XML_ID' => $code
326 ]
327 ]);
328 if ($row = $res->fetch())
329 {
330 DesignerRepo::delete($row['ID']);
331 }
332 }
333}
static getRepository(bool $installRepo=true)
Definition designer.php:262
static unregisterRepoElement(string $code)
Definition designer.php:318
static registerRepoElement(array $fields)
Definition designer.php:290
static setLandingDesignBlockMode(bool $flag)
Definition designer.php:58
static parseManifest(string $content)
Definition designer.php:219
static getForLanding($id)
Definition hook.php:275
static getForSite($id)
Definition hook.php:251
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29