Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
block.php
1<?php
3
4use \Bitrix\Landing\Block as BlockCore;
5use \Bitrix\Landing\UpdateBlock;
6use \Bitrix\Landing\Rights;
7use \Bitrix\Landing\Internals\BlockTable;
8
10{
14 const STEPPER_COUNT = 1;
15
20 protected static $moduleId = 'landing';
21
28 public static function register($codes, array $params = [])
29 {
30 if (!is_array($codes))
31 {
32 $codes = [$codes];
33 }
34
35 $res = UpdateBlock::getList([
36 'select' => [
37 'ID', 'CODE'
38 ],
39 'filter' => [
40 '=CODE' => $codes
41 ]
42 ]);
43 while ($row = $res->fetch())
44 {
45 UpdateBlock::update($row['ID'], [
46 'LAST_BLOCK_ID' => 0,
47 'PARAMS' => $params
48 ]);
49 $codes = array_diff($codes, [$row['CODE']]);
50 }
51
52 if (!empty($codes))
53 {
54 foreach ($codes as $code)
55 {
56 UpdateBlock::add([
57 'CODE' => $code,
58 'PARAMS' => $params
59 ]);
60 }
61 }
62
63 // reg stepper
64 \Bitrix\Main\Update\Stepper::bindClass(
65 'Bitrix\Landing\Update\Block', 'landing', 600
66 );
67 }
68
74 public static function unregister($codes)
75 {
76 if (!is_array($codes))
77 {
78 $codes = [$codes];
79 }
80
81 $res = UpdateBlock::getList([
82 'select' => [
83 'ID'
84 ],
85 'filter' => [
86 '=CODE' => $codes
87 ]
88 ]);
89 while ($row = $res->fetch())
90 {
91 UpdateBlock::delete($row['ID']);
92 }
93 }
94
102 protected static function prepareClassesToSet($classes, array $addClasses = [], array $removeClasses = [])
103 {
104 if (is_array($classes) && isset($classes['classList']))
105 {
106 $classes = $classes['classList'];
107 }
108 if (!is_array($classes))
109 {
110 $classes = [$classes];
111 }
112
113 $classesUnique = array_unique($classes);
114 // all nodes have equal classes
115 if (count($classesUnique) === 1)
116 {
117 $result = [
118 [
119 'classList' => $classesUnique[0],
120 'suffix' => '',
121 ],
122 ];
123 }
124 // different classes
125 else
126 {
127 $classesSorted = [];
128 // find most frequent class
129 $counts = array_count_values($classes);
130 arsort($counts);
131 $mainClass = key($counts);
132 foreach ($classes as $pos => $class)
133 {
134 if ($class !== $mainClass)
135 {
136 $classesSorted[] = [
137 'classList' => $class,
138 'suffix' => '@' . $pos,
139 ];
140 }
141 }
142 $result = array_merge(
143 [
144 [
145 'classList' => $mainClass,
146 'suffix' => '',
147 ],
148 ],
149 $classesSorted
150 );
151 }
152
153 // add and remove classes
154 foreach ($result as $pos => $class)
155 {
156 if ($addClasses || $removeClasses)
157 {
158 $classList = explode(' ', $class['classList']);
159
160 if ($addClasses)
161 {
162 $classList = array_merge($classList, $addClasses);
163 }
164 if ($removeClasses)
165 {
166 $classList = array_diff($classList, $removeClasses);
167 }
168 $result[$pos]['classList'] = implode(' ', array_unique($classList));
169 }
170 }
171
172 return $result;
173 }
174
183 public static function executeStep(array $filter, &$count = 0, $limit = 0, array $params = [])
184 {
185 $lastId = 0;
186
188
189 $res = BlockTable::getList([
190 'select' => [
191 'ID', 'CONTENT'
192 ],
193 'filter' => $filter,
194 'order' => [
195 'ID' => 'asc'
196 ],
197 'limit' => $limit
198 ]);
199 while ($row = $res->fetch())
200 {
201 $lastId = $row['ID'];
202 $count++;
203
204 // gets content from exist block
205 $block = new BlockCore($row['ID']);
206 $block->setAccess(BlockCore::ACCESS_X);
207 $export = $block->export([
208 'clear_form' => false
209 ]);
210
211 // and apply to the new layout
212 $newContent = BlockCore::getContentFromRepository(
213 $block->getCode()
214 );
215 $block->saveContent($newContent);
216 // update cards
217 if ($export['cards'])
218 {
219 $block->updateCards(
220 $export['cards']
221 );
222 }
223 // update style
224 if ($export['style'])
225 {
226 foreach ($export['style'] as $selector => $classes)
227 {
228 $addClasses = [];
229 if (isset($params[$selector]['new_class']))
230 {
231 if (is_array($params[$selector]['new_class']))
232 {
233 if (count($params[$selector]['new_class']) > 1)
234 {
235 $addClasses = $params[$selector]['new_class'];
236 }
237 else
238 {
239 $addClasses = explode(' ', trim($params[$selector]['new_class'][0]));
240 }
241 }
242 else
243 {
244 $addClasses = explode(' ', trim($params[$selector]['new_class']));
245 }
246 }
247
248 $removeClasses = [];
249 if (isset($params[$selector]['remove_class']))
250 {
251 if (is_array($params[$selector]['remove_class']))
252 {
253 if (count($params[$selector]['remove_class']) > 1)
254 {
255 $removeClasses = $params[$selector]['remove_class'];
256 }
257 else
258 {
259 $removeClasses = explode(' ', trim($params[$selector]['remove_class'][0]));
260 }
261 }
262 else
263 {
264 $removeClasses = explode(' ', trim($params[$selector]['remove_class']));
265 }
266 }
267
268 // change wrapper to valid selector
269 if ($selector == '#wrapper')
270 {
271 $selector = '#' . $block->getAnchor($block->getId());
272 }
273
274 foreach (self::prepareClassesToSet($classes, $addClasses, $removeClasses) as $class)
275 {
276 $block->setClasses(array(
277 $selector . $class['suffix'] => array(
278 'classList' => [$class['classList']]
279 )
280 ));
281 }
282 }
283 }
284 // update nodes
285 if ($export['nodes'])
286 {
287 foreach ($export['nodes'] as $selector => $node)
288 {
289 if (isset($params[$selector]['update_video_to_lazyload']))
290 {
291 $export['nodes'][$selector] = self::updateVideoToLazyload($node);
292 }
293 }
294
295 $block->updateNodes(
296 $export['nodes']
297 );
298 }
299 // update menu
300 if ($export['menu'])
301 {
302 $block->updateNodes(
303 $export['menu']
304 );
305 }
306 // update attrs
307 if ($export['attrs'])
308 {
309 if (isset($export['attrs']['#wrapper']))
310 {
311 $wrapperCode = '#' . $block->getAnchor($block->getId());
312 $export['attrs'][$wrapperCode] = $export['attrs']['#wrapper'];
313 unset($export['attrs']['#wrapper']);
314 }
315 $block->setAttributes(
316 $export['attrs']
317 );
318 }
319
320 // and save block with new layout and old content
321 $block->save();
322 }
323
325
326 return $lastId;
327 }
328
334 public function execute(array &$result)
335 {
336 $finished = true;
337
338 // check queue
339 $res = UpdateBlock::getList([
340 'select' => [
341 'ID', 'CODE', 'LAST_BLOCK_ID', 'PARAMS'
342 ],
343 'order' => [
344 'ID' => 'asc'
345 ],
346 'limit' => 1
347 ]);
348 if (!($rowUpdate = $res->fetch()))
349 {
350 return false;
351 }
352
353 // gets common quantity
354 $res = BlockTable::getList([
355 'select' => [
356 new \Bitrix\Main\Entity\ExpressionField(
357 'CNT', 'COUNT(*)'
358 )
359 ],
360 'filter' => [
361 '=CODE' => $rowUpdate['CODE'],
362 '!=DESIGNED' => 'Y',
363 ]
364 ]);
365
366 // skip blocks that not exists
367 $row = $res->fetch();
368 if(!$row || (int) $row['CNT'] === 0)
369 {
370 UpdateBlock::delete(
371 $rowUpdate['ID']
372 );
373 return $this->execute($result);
374 }
375 $result['count'] = $row['CNT'];
376
377 // gets finished count
378 $res = BlockTable::getList([
379 'select' => [
380 new \Bitrix\Main\Entity\ExpressionField(
381 'CNT', 'COUNT(*)'
382 )
383 ],
384 'filter' => [
385 '<=ID' => $rowUpdate['LAST_BLOCK_ID'],
386 '=CODE' => $rowUpdate['CODE'],
387 '!=DESIGNED' => 'Y',
388 ]
389 ]
390 );
391 if ($row = $res->fetch())
392 {
393 $result['steps'] = $row['CNT'];
394 }
395
396 // gets block group for update
397 $lastId = $this::executeStep([
398 '>ID' => $rowUpdate['LAST_BLOCK_ID'],
399 '=CODE' => $rowUpdate['CODE'],
400 '!=DESIGNED' => 'Y',
401 ],
402 $count,
403 $this::STEPPER_COUNT,
404 $rowUpdate['PARAMS']
405 );
406 if ($lastId > 0)
407 {
408 $finished = false;
409 }
410
411 // finish or continue
412 if (!$finished)
413 {
414 UpdateBlock::update($rowUpdate['ID'], [
415 'LAST_BLOCK_ID' => $lastId
416 ]);
417 return true;
418 }
419 else
420 {
421 UpdateBlock::delete($rowUpdate['ID']);
422 return true;//for check next item in UpdateBlock
423 }
424 }
425
426 protected static function updateVideoToLazyload($nodes)
427 {
428 foreach($nodes as $key => $node)
429 {
430 if($node['src'])
431 {
432 // youtube
433 if (strpos($node['src'], 'www.youtube.com') !== false)
434 {
435 if ($node['source'])
436 {
437 $pattern = "#(youtube\\.com|youtu\\.be|youtube\\-nocookie\\.com)\\/(watch\\?(.*&)?v=|v\\/|u\\/|embed\\/?)?(videoseries\\?list=(.*)|[\\w-]{11}|\\?listType=(.*)&list=(.*))(.*)#";
438 if(preg_match($pattern, $node['source'], $matches))
439 {
440 $nodes[$key]['preview'] = "//img.youtube.com/vi/{$matches[4]}/sddefault.jpg";
441 }
442 }
443 }
444 }
445 }
446
447 return $nodes;
448 }
449}
execute(array &$result)
Definition block.php:334
static executeStep(array $filter, &$count=0, $limit=0, array $params=[])
Definition block.php:183
static unregister($codes)
Definition block.php:74
static updateVideoToLazyload($nodes)
Definition block.php:426
static prepareClassesToSet($classes, array $addClasses=[], array $removeClasses=[])
Definition block.php:102