1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
repo.php
См. документацию.
1<?php
2namespace Bitrix\Landing\PublicAction;
3
4use Bitrix\Landing;
5use Bitrix\Landing\Error;
6use Bitrix\Landing\Manager;
7use Bitrix\Landing\Site;
8use Bitrix\Main\Localization\Loc;
9use Bitrix\Rest\Marketplace\Client;
10use Bitrix\Rest\PlacementTable;
11use Bitrix\Landing\Placement;
12use Bitrix\Landing\PublicActionResult;
13use Bitrix\Landing\Node\StyleImg;
14
15Loc::loadMessages(__FILE__);
16
17class Repo
18{
25 public static function checkContent($content, $splitter = '#SANITIZE#')
26 {
30 $bad,
31 $splitter
32 );
33 $result->setResult(array(
34 'is_bad' => $bad,
35 'content' => $content,
36 ));
37
38 return $result;
39 }
40
48 public static function register(string $code, array $fields, array $manifest = []): PublicActionResult
49 {
51 $error = new \Bitrix\Landing\Error;
52
53 static::onRegisterCheckFields($fields, $error);
54 static::onRegisterBefore($fields, $manifest, $error);
55 if (!empty($error->getErrors()))
56 {
57 $result->setError($error);
58
59 return $result;
60 }
61
62 $fields['XML_ID'] = trim($code);
63
64 // check intersect item of nodes and styles for background type
65 if (is_array($manifest['nodes'] ?? null))
66 {
67 foreach ($manifest['nodes'] as $selector => $manifestItem)
68 {
69 $styleItem = null;
70
71 if (isset($manifest['style'][$selector]))
72 {
73 $styleItem = $manifest['style'][$selector];
74 }
75 if (isset($manifest['style']['nodes'][$selector]))
76 {
77 $styleItem = $manifest['style']['nodes'][$selector];
78 }
79
80 if ($styleItem['type'] ?? null)
81 {
82 if (!empty(array_intersect((array)$styleItem['type'], StyleImg::STYLES_WITH_IMAGE)))
83 {
84 $error->addError(
85 'MANIFEST_INTERSECT_IMG',
86 Loc::getMessage('LANDING_APP_MANIFEST_INTERSECT_IMG', ['#selector#' => $selector])
87 );
88 $result->setError($error);
89
90 return $result;
91 }
92 }
93 }
94 }
95
96 if (isset($fields['CONTENT']))
97 {
98 // sanitize content
99 $fields['CONTENT'] = Manager::sanitize(
100 $fields['CONTENT'],
101 $bad
102 );
103 if ($bad)
104 {
105 $error->addError(
106 'CONTENT_IS_BAD',
107 Loc::getMessage('LANDING_APP_CONTENT_IS_BAD')
108 );
109 $result->setError($error);
110
111 return $result;
112 }
113 // sanitize card's content
114 if (
115 isset($manifest['cards']) &&
116 is_array($manifest['cards'])
117 )
118 {
119 foreach ($manifest['cards'] as $cardCode => &$card)
120 {
121 if (
122 isset($card['presets']) &&
123 is_array($card['presets'])
124 )
125 {
126 foreach ($card['presets'] as $presetCode => &$preset)
127 {
128 foreach (['html', 'name', 'values'] as $code)
129 {
130 if (isset($preset[$code]))
131 {
132 $preset[$code] = Manager::sanitize(
133 $preset[$code],
134 $bad
135 );
136 if ($bad)
137 {
138 $error->addError(
139 'PRESET_CONTENT_IS_BAD',
140 Loc::getMessage(
141 'LANDING_APP_PRESET_CONTENT_IS_BAD',
142 array(
143 '#preset#' => $presetCode,
144 '#card#' => $cardCode,
145 ))
146 );
147 $result->setError($error);
148
149 return $result;
150 }
151 }
152 }
153 }
154 unset($preset);
155 }
156 }
157 unset($card);
158 }
159 }
160
161 $fields['MANIFEST'] = serialize($manifest);
162
163 // set app code
164 if (($app = \Bitrix\Landing\PublicAction::restApplication()))
165 {
166 $fields['APP_CODE'] = $app['CODE'];
167 }
168
169 // check unique
170 $exists = false;
171 if ($fields['XML_ID'])
172 {
173 $exists = Landing\Repo::getList([
174 'select' => ['ID'],
175 'filter' =>
176 isset($fields['APP_CODE'])
177 ? [
178 '=XML_ID' => $fields['XML_ID'],
179 '=APP_CODE' => $fields['APP_CODE'],
180 ]
181 : [
182 '=XML_ID' => $fields['XML_ID'],
183 ],
184 ])->fetch();
185 }
186
187 // register (add / update)
188 if ($exists)
189 {
190 $res = Landing\Repo::update($exists['ID'], $fields);
191 }
192 else
193 {
195 }
196 if ($res->isSuccess())
197 {
198 if (
199 isset($fields['RESET']) &&
200 $fields['RESET'] == 'Y'
201 )
202 {
204 'repo_' . $res->getId()
205 );
206 }
207 $result->setResult($res->getId());
208 }
209 else
210 {
211 $error->addFromResult($res);
212 $result->setError($error);
213 }
214
215 return $result;
216 }
217
224 protected static function onRegisterCheckFields($fields, Error $error): void
225 {
226 $requiredFields = [
227 'NAME',
228 'CONTENT',
229 'SECTIONS',
230 'PREVIEW',
231 ];
232
233 foreach ($requiredFields as $field)
234 {
235 if (!isset($fields[$field]))
236 {
237 $error->addError(
238 'REQUIRED_FIELD_NO_EXISTS',
239 Loc::getMessage('LANDING_FIELD_NO_EXISTS', ['#field#' => $field])
240 );
241 }
242 }
243 }
244
252 protected static function onRegisterBefore(array &$fields, array &$manifest, Error $error): void
253 {
254 // todo: test err
255
256 // unset not allowed keys
257 $notAllowedManifestKey = ['callbacks'];
258 foreach ($notAllowedManifestKey as $key)
259 {
260 if (isset($manifest[$key]))
261 {
262 unset($manifest[$key]);
263 }
264 }
265
266 // unset not allowed site types
267 if (isset($manifest['block']['type']))
268 {
269 $manifest['block']['type'] = array_filter(
270 (array)$manifest['block']['type'],
271 function ($type) use ($error) {
272 $notAllowedBlockTypes = [
274 ];
275 if (in_array(mb_strtolower($type), $notAllowedBlockTypes))
276 {
277 $error->addError(
278 'UNSUPPORTED_BLOCK_TYPE',
279 Loc::getMessage('LANDING_UNSUPPORTED_BLOCK_TYPE', ['#type#' => $type])
280 );
281
282 return false;
283 }
284 return true;
285 }
286 );
287 }
288
289 // unset not allowed subtypes
290 if (isset($manifest['block']['subtype']))
291 {
292 $manifest['block']['subtype'] = array_filter(
293 (array)$manifest['block']['subtype'],
294 function ($type) use ($error) {
295 $notAllowedSubtypes = [
296 'widget',
297 ];
298 if (in_array(mb_strtolower($type), $notAllowedSubtypes))
299 {
300 $error->addError(
301 'UNSUPPORTED_BLOCK_SUBTYPE',
302 Loc::getMessage('LANDING_UNSUPPORTED_BLOCK_SUBTYPE', ['#type#' => $type])
303 );
304
305 return false;
306 }
307 return true;
308 }
309 );
310 }
311 }
312
318 public static function unregister($code)
319 {
321 $error = new \Bitrix\Landing\Error;
322
323 $result->setResult(false);
324
325 if (!is_string($code))
326 {
327 return $result;
328 }
329
330 // search and delete
331 if ($code)
332 {
333 // set app code
334 $app = \Bitrix\Landing\PublicAction::restApplication();
335
336 $row = Landing\Repo::getList(array(
337 'select' => array(
338 'ID',
339 ),
340 'filter' =>
341 isset($app['CODE'])
342 ? array(
343 '=XML_ID' => $code,
344 '=APP_CODE' => $app['CODE'],
345 )
346 : array(
347 '=XML_ID' => $code,
348 ),
349 ))->fetch();
350 if ($row)
351 {
352 // delete all sush blocks from landings
353 $codeToDelete = array();
354 $res = Landing\Repo::getList(array(
355 'select' => array(
356 'ID',
357 ),
358 'filter' =>
359 isset($app['CODE'])
360 ? array(
361 '=XML_ID' => $code,
362 '=APP_CODE' => $app['CODE'],
363 )
364 : array(
365 '=XML_ID' => $code,
366 ),
367 ));
368 while ($rowRepo = $res->fetch())
369 {
370 $codeToDelete[] = 'repo_' . $rowRepo['ID'];
371 }
372 if (!empty($codeToDelete))
373 {
374 Landing\Block::deleteByCode($codeToDelete);
375 }
376 // delete block from repo
377 $res = Landing\Repo::delete($row['ID']);
378 if ($res->isSuccess())
379 {
380 $result->setResult(true);
381 }
382 else
383 {
384 $error->addFromResult($res);
385 }
386 }
387 }
388
389 $result->setError($error);
390
391 return $result;
392 }
393
399 public static function getAppInfo($code)
400 {
402 $error = new \Bitrix\Landing\Error;
403 $app = array();
404
405 if (!is_string($code))
406 {
407 return $result;
408 }
409
410 if ($appLocal = Landing\Repo::getAppByCode($code))
411 {
412 $app = array(
413 'CODE' => $appLocal['CODE'],
414 'NAME' => $appLocal['APP_NAME'],
415 'DATE_FINISH' => (string)$appLocal['DATE_FINISH'],
416 'PAYMENT_ALLOW' => $appLocal['PAYMENT_ALLOW'],
417 'ICON' => '',
418 'PRICE' => array(),
419 'UPDATES' => 0,
420 );
421 if (\Bitrix\Main\Loader::includeModule('rest'))
422 {
423 $appRemote = Client::getApp($code);
424 if (isset($appRemote['ITEMS']))
425 {
426 $data = $appRemote['ITEMS'];
427 if (isset($data['ICON']))
428 {
429 $app['ICON'] = $data['ICON'];
430 }
431 if (isset($data['PRICE']) && !empty($data['PRICE']))
432 {
433 $app['PRICE'] = $data['PRICE'];
434 }
435 }
436 $updates = Client::getUpdates(array(
437 $code => $appLocal['VERSION'],
438 ));
439 if (
440 isset($updates['ITEMS'][0]['VERSIONS']) &&
441 is_array($updates['ITEMS'][0]['VERSIONS'])
442 )
443 {
444 $app['UPDATES'] = count($updates['ITEMS'][0]['VERSIONS']);
445 }
446 }
447 $result->setResult($app);
448 }
449
450 if (empty($app))
451 {
452 $error->addError(
453 'NOT_FOUND',
454 Loc::getMessage('LANDING_APP_NOT_FOUND')
455 );
456 }
457
458 $result->setError($error);
459
460 return $result;
461 }
462
468 public static function bind(array $fields)
469 {
471 $error = new \Bitrix\Landing\Error;
472 \trimArr($fields);
473
474 if (($app = \Bitrix\Landing\PublicAction::restApplication()))
475 {
476 $fields['APP_ID'] = $app['ID'];
477 }
478
479 $res = Placement::getList(array(
480 'select' => array(
481 'ID',
482 ),
483 'filter' => array(
484 'APP_ID' => isset($fields['APP_ID'])
485 ? $fields['APP_ID']
486 : false,
487 'PLACEMENT' => isset($fields['PLACEMENT'])
488 ? $fields['PLACEMENT']
489 : false,
490 'PLACEMENT_HANDLER' => isset($fields['PLACEMENT_HANDLER'])
491 ? $fields['PLACEMENT_HANDLER']
492 : false,
493 ),
494 ));
495 // add, if not exist
496 if (!$res->fetch())
497 {
498 if (\Bitrix\Main\Loader::includeModule('rest'))
499 {
500 // first try add in the local table
501 $resLocal = Placement::add($fields);
502 if ($resLocal->isSuccess())
503 {
504 // then add in the rest table
505 $resRest = PlacementTable::add(
506 $fields
507 );
508 if ($resRest->isSuccess())
509 {
510 $result->setResult(true);
511 }
512 else
513 {
514 $error->addFromResult($resRest);
515 Placement::delete($resLocal->getId());
516 }
517 }
518 else
519 {
520 $error->addFromResult($resLocal);
521 }
522 }
523 }
524 else
525 {
526 $error->addError(
527 'PLACEMENT_EXIST',
528 Loc::getMessage('LANDING_APP_PLACEMENT_EXIST')
529 );
530 }
531
532 $result->setError($error);
533
534 return $result;
535 }
536
543 public static function unbind($code, $handler = null)
544 {
546 $error = new \Bitrix\Landing\Error;
547
548 if (!is_string($code))
549 {
550 return $result;
551 }
552
553 $code = trim($code);
554 $wasDeleted = false;
555
556 if (($app = \Bitrix\Landing\PublicAction::restApplication()))
557 {
558 $fields['APP_ID'] = $app['ID'];
559 }
560 if (
561 !isset($fields['APP_ID']) ||
562 !$fields['APP_ID']
563 )
564 {
565 return $result;
566 }
567
568 // common ORM params
569 $params = [
570 'select' => [
571 'ID',
572 ],
573 'filter' => [
574 'APP_ID' => $fields['APP_ID'],
575 '=PLACEMENT' => $code,
576 ],
577 ];
578 if ($handler)
579 {
580 $params['filter']['=PLACEMENT_HANDLER'] = trim($handler);
581 }
582
583 // at first, delete local binds
584 $res = Placement::getList($params);
585 while ($row = $res->fetch())
586 {
587 $wasDeleted = true;
588 Placement::delete($row['ID']);
589 }
590 unset($res, $row);
591
592 // then delete from rest placements
593 if (\Bitrix\Main\Loader::includeModule('rest'))
594 {
595 $res = PlacementTable::getList($params);
596 while ($row = $res->fetch())
597 {
598 PlacementTable::delete($row['ID']);
599 }
600 unset($res, $row);
601 }
602
603 // make answer
604 if ($wasDeleted)
605 {
606 $result->setResult(true);
607 }
608 else
609 {
610 $error->addError(
611 'PLACEMENT_NO_EXIST',
612 Loc::getMessage('LANDING_APP_PLACEMENT_NO_EXIST')
613 );
614 $result->setError($error);
615 }
616
617 return $result;
618 }
619
625 public static function getList(array $params = array()): PublicActionResult
626 {
628 $params = $result->sanitizeKeys($params);
629
630 if (!is_array($params))
631 {
632 $params = [];
633 }
634 if (
635 !isset($params['filter']) ||
636 !is_array($params['filter'])
637 )
638 {
639 $params['filter'] = array();
640 }
641 // set app code
642 if (($app = \Bitrix\Landing\PublicAction::restApplication()))
643 {
644 $params['filter']['APP_CODE'] = $app['CODE'];
645 }
646 else
647 {
648 $params['filter']['APP_CODE'] = false;
649 }
650
651 // manifest always needed
652 if (isset($params['select']))
653 {
654 $params['select'][] = 'MANIFEST';
655 }
656
657 $data = [];
658 $res = Landing\Repo::getList($params);
659
660 while ($row = $res->fetch())
661 {
662 if (isset($row['DATE_CREATE']))
663 {
664 $row['DATE_CREATE'] = (string) $row['DATE_CREATE'];
665 }
666 if (isset($row['DATE_MODIFY']))
667 {
668 $row['DATE_MODIFY'] = (string) $row['DATE_MODIFY'];
669 }
670 $row['MANIFEST'] = unserialize($row['MANIFEST'], ['allowed_classes' => false]);
671 $data[] = $row;
672 }
673 $result->setResult($data);
674
675 return $result;
676 }
677}
$type
Определения options.php:106
static sanitize($value, &$bad=false, $splitter=' ')
Определения manager.php:1310
const STYLES_WITH_IMAGE
Определения styleimg.php:15
static getList(array $params=array())
Определения repo.php:625
static onRegisterBefore(array &$fields, array &$manifest, Error $error)
Определения repo.php:252
static getAppInfo($code)
Определения repo.php:399
static unregister($code)
Определения repo.php:318
static bind(array $fields)
Определения repo.php:468
static onRegisterCheckFields($fields, Error $error)
Определения repo.php:224
static unbind($code, $handler=null)
Определения repo.php:543
static checkContent($content, $splitter='#SANITIZE#')
Определения repo.php:25
static update($id, $fields=array())
Определения repo.php:39
static getAppByCode($code)
Определения repo.php:302
static add($fields)
Определения repo.php:21
static delete($id)
Определения repo.php:56
const SCOPE_CODE_MAINPAGE
Определения type.php:24
Определения error.php:15
static register($extName)
Определения extension.php:36
$content
Определения commerceml.php:144
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
$app
Определения proxy.php:8
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
Определения agent.php:3
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$error
Определения subscription_card_product.php:20
$fields
Определения yandex_run.php:501