Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
site.php
1<?php
3
4use \Bitrix\Landing\Manager;
5use \Bitrix\Landing\File;
6use \Bitrix\Landing\Rights;
7use \Bitrix\Landing\Landing;
8use \Bitrix\Landing\Site as SiteCore;
9use \Bitrix\Landing\PublicActionResult;
10use \Bitrix\Main\Localization\Loc;
11
12Loc::loadMessages(__FILE__);
13
14class Site
15{
21 protected static function clearDisallowFields(array $fields)
22 {
23 $disallow = ['ACTIVE', 'SPECIAL', 'TPL_CODE'];
24
25 if (is_array($fields))
26 {
27 foreach ($fields as $k => $v)
28 {
29 if (in_array($k, $disallow))
30 {
31 unset($fields[$k]);
32 }
33 }
34 }
35
36 return $fields;
37 }
38
44 public static function getAdditionalFields($id)
45 {
46 $result = new PublicActionResult();
47 $id = (int)$id;
48
49 if (($fields = SiteCore::getAdditionalFields($id)))
50 {
51 foreach ($fields as $key => $field)
52 {
53 $fields[$key] = $field->getValue();
54 if (!$fields[$key])
55 {
56 unset($fields[$key]);
57 }
58 }
59 $result->setResult(
60 $fields
61 );
62 }
63
64 return $result;
65 }
66
72 public static function getPublicUrl($id)
73 {
74 static $mixedParams = ['id'];
75
76 $result = new PublicActionResult();
77 $result->setResult(SiteCore::getPublicUrl($id));
78 return $result;
79 }
80
86 public static function getPreview(int $id): PublicActionResult
87 {
88 $result = new PublicActionResult();
89 $result->setResult(SiteCore::getPreview($id));
90 return $result;
91 }
92
99 public static function getList(array $params = [], $initiator = null)
100 {
101 $result = new PublicActionResult();
102 $params = $result->sanitizeKeys($params);
103 $getPublicUrl = false;
104 $getPreviewPicture = false;
105 $getPhone = false;
106 $mobileHit = $initiator === 'mobile';
107
108 if ($mobileHit)
109 {
110 \Bitrix\Landing\Connector\Mobile::forceMobile();
111 }
112
113 // necessary params for us
114 if (
115 !isset($params['select']) ||
116 !is_array($params['select'])
117 )
118 {
119 $params['select'] = ['*'];
120 }
121 if (
122 !isset($params['filter']) ||
123 !is_array($params['filter'])
124 )
125 {
126 $params['filter'] = [];
127 }
128
129 // fix for smn sites
130 if (
131 isset($params['filter']['=TYPE']) &&
132 $params['filter']['=TYPE'] == 'STORE'
133 )
134 {
135 $params['filter']['=TYPE'] = [
136 $params['filter']['=TYPE'],
137 'SMN'
138 ];
139 }
140 if (
141 isset($params['filter']['TYPE']) &&
142 $params['filter']['TYPE'] == 'STORE'
143 )
144 {
145 $params['filter']['TYPE'] = [
146 $params['filter']['TYPE'],
147 'SMN'
148 ];
149 }
150
151 if (isset($params['filter']['CHECK_PERMISSIONS']))
152 {
153 unset($params['filter']['CHECK_PERMISSIONS']);
154 }
155
156 // extend select's param
157 if (is_array($params['select']))
158 {
159 if (in_array('DOMAIN_NAME', $params['select']))
160 {
161 $params['select']['DOMAIN_NAME'] = 'DOMAIN.DOMAIN';
162 }
163 if (in_array('PUBLIC_URL', $params['select']))
164 {
165 $getPublicUrl = true;
166 }
167 if (in_array('PREVIEW_PICTURE', $params['select']))
168 {
169 $getPreviewPicture = true;
170 }
171 if (in_array('PHONE', $params['select']))
172 {
173 $getPhone = true;
174 $params['select'][] = 'ID';
175 }
176 // delete this keys for ORM
177 $deleted = ['DOMAIN_NAME', 'PUBLIC_URL', 'PREVIEW_PICTURE', 'PHONE'];
178 foreach ($params['select'] as $k => $code)
179 {
180 if (in_array($code, $deleted))
181 {
182 unset($params['select'][$k]);
183 }
184 }
185 }
186
187 // set additional select fields
188 if (
189 $getPreviewPicture &&
190 !in_array('LANDING_ID_INDEX', $params['select'])
191 )
192 {
193 $params['select'][] = 'LANDING_ID_INDEX';
194 }
195 if (!in_array('ID', $params['select']))
196 {
197 $params['select'][] = 'ID';
198 }
199 if (!in_array('TYPE', $params['select']))
200 {
201 $params['select'][] = 'TYPE';
202 }
203
204 // get ORM data
205 $data = [];
206 $landingIndexes = [];
207 $res = SiteCore::getList($params);
208 while ($row = $res->fetch())
209 {
210 if (isset($row['DATE_CREATE']))
211 {
212 $row['DATE_CREATE'] = (string) $row['DATE_CREATE'];
213 }
214 if (isset($row['DATE_MODIFY']))
215 {
216 $row['DATE_MODIFY'] = (string) $row['DATE_MODIFY'];
217 }
218 if ($row['LANDING_ID_INDEX'] && $getPreviewPicture)
219 {
220 $landingIndexes[$row['ID']] = $row['LANDING_ID_INDEX'];
221 }
222 if ($getPublicUrl)
223 {
224 $row['PUBLIC_URL'] = '';
225 }
226 if ($getPreviewPicture)
227 {
228 $row['PREVIEW_PICTURE'] = '';
229 }
230 if ($getPhone)
231 {
232 $row['PHONE'] = \Bitrix\Landing\Connector\Crm::getContacts(
233 $row['ID']
234 )['PHONE'] ?? null;
235 }
236 $data[$row['ID']] = $row;
237 }
238
239 // gets public url for sites
240 if ($getPublicUrl || $getPreviewPicture)
241 {
242 $urls = SiteCore::getPublicUrl(array_keys($data), true, !$mobileHit);
243 foreach ($urls as $siteId => $url)
244 {
245 $data[$siteId]['PUBLIC_URL'] = $url;
246 }
247 }
248
249 // get preview pictures
250 if ($landingIndexes)
251 {
252 $landing = Landing::createInstance(0);
253 foreach ($landingIndexes as $siteId => $landingId)
254 {
255 $data[$siteId]['PREVIEW_PICTURE'] = $landing->getPreview(
256 $landingId,
257 false,
258 $data[$siteId]['PUBLIC_URL']
259 );
260 }
261 }
262
263 // set and return result
264 $result->setResult(
265 array_values($data)
266 );
267
268 return $result;
269 }
270
276 public static function add(array $fields)
277 {
278 $result = new PublicActionResult();
279 $error = new \Bitrix\Landing\Error;
280
281 $fields = self::clearDisallowFields($fields);
282 $fields['ACTIVE'] = 'N';
283
284 $res = SiteCore::add($fields);
285
286 if ($res->isSuccess())
287 {
288 $result->setResult($res->getId());
289 }
290 else
291 {
292 $error->addFromResult($res);
293 $result->setError($error);
294 }
295
296 return $result;
297 }
298
305 public static function update($id, array $fields)
306 {
307 $result = new PublicActionResult();
308 $error = new \Bitrix\Landing\Error;
309
310 $fields = self::clearDisallowFields($fields);
311
312 $res = SiteCore::update($id, $fields);
313
314 if ($res->isSuccess())
315 {
316 $result->setResult(true);
317 }
318 else
319 {
320 $error->addFromResult($res);
321 $result->setError($error);
322 }
323
324 return $result;
325 }
326
332 public static function delete($id)
333 {
334 $result = new PublicActionResult();
335 $error = new \Bitrix\Landing\Error;
336
337 $res = SiteCore::delete($id);
338
339 if ($res->isSuccess())
340 {
341 $result->setResult(true);
342 }
343 else
344 {
345 $error->addFromResult($res);
346 $result->setError($error);
347 }
348
349 return $result;
350 }
351
358 public static function markDelete($id, $mark = true)
359 {
360 $result = new PublicActionResult();
361 $error = new \Bitrix\Landing\Error;
362 $id = (int)$id;
363
364 if ($mark)
365 {
366 $res = SiteCore::markDelete($id);
367 }
368 else
369 {
370 $res = SiteCore::markUnDelete($id);
371 }
372 if ($res->isSuccess())
373 {
374 $result->setResult($res->getId());
375 }
376 else
377 {
378 $error->addFromResult($res);
379 $result->setError($error);
380 }
381
382 return $result;
383 }
384
390 public static function markUnDelete($id)
391 {
392 return self::markDelete($id, false);
393 }
394
401 public static function addFolder(int $siteId, array $fields): PublicActionResult
402 {
403 $result = new PublicActionResult();
404 $error = new \Bitrix\Landing\Error;
405 if (!($fields['PARENT_ID'] ?? null))
406 {
407 $fields['PARENT_ID'] = null;
408 }
409 $addResult = SiteCore::addFolder($siteId, $fields);
410
411 if ($addResult->isSuccess())
412 {
413 $result->setResult($addResult->getId());
414 }
415 else
416 {
417 $error->addFromResult($addResult);
418 $result->setError($error);
419 }
420
421 return $result;
422 }
423
431 public static function updateFolder(int $siteId, int $folderId, array $fields): PublicActionResult
432 {
433 $result = new PublicActionResult();
434 $error = new \Bitrix\Landing\Error;
435
436 if (!($fields['PARENT_ID'] ?? null))
437 {
438 $fields['PARENT_ID'] = null;
439 }
440 $addResult = SiteCore::updateFolder($siteId, $folderId, $fields);
441
442 if ($addResult->isSuccess())
443 {
444 $result->setResult(true);
445 }
446 else
447 {
448 $error->addFromResult($addResult);
449 $result->setError($error);
450 }
451
452 return $result;
453 }
454
462 public static function moveFolder(int $folderId, ?int $toFolderId, ?int $toSiteId = null): PublicActionResult
463 {
464 $result = new PublicActionResult();
465 $error = new \Bitrix\Landing\Error;
466 $moveResult = SiteCore::moveFolder($folderId, $toFolderId ?: null, $toSiteId ?: null);
467
468 if ($moveResult->isSuccess())
469 {
470 $result->setResult($moveResult->getId());
471 }
472 else
473 {
474 $error->addFromResult($moveResult);
475 $result->setError($error);
476 }
477
478 return $result;
479 }
480
487 public static function publicationFolder(int $folderId, bool $mark = true): PublicActionResult
488 {
489 $result = new PublicActionResult();
490 $error = new \Bitrix\Landing\Error;
491
492 $publicationResult = SiteCore::publicationFolder($folderId, $mark);
493
494 if ($publicationResult->isSuccess())
495 {
496 $result->setResult($publicationResult->isSuccess());
497 }
498 else
499 {
500 $error->addFromResult($publicationResult);
501 $result->setError($error);
502 }
503
504 return $result;
505 }
506
512 public static function unPublicFolder(int $folderId): PublicActionResult
513 {
514 return self::publicationFolder($folderId, false);
515 }
516
523 public static function getFolders(int $siteId, array $filter = []): PublicActionResult
524 {
525 $result = new PublicActionResult();
526 if (array_key_exists('PARENT_ID', $filter) && !($filter['PARENT_ID'] ?? null))
527 {
528 $filter['PARENT_ID'] = null;
529 }
530
531 $rows = array_values(SiteCore::getFolders($siteId, $filter));
532 foreach ($rows as &$row)
533 {
534 if (isset($row['DATE_CREATE']))
535 {
536 $row['DATE_CREATE'] = (string) $row['DATE_CREATE'];
537 }
538 if (isset($row['DATE_MODIFY']))
539 {
540 $row['DATE_MODIFY'] = (string) $row['DATE_MODIFY'];
541 }
542 }
543 unset($row);
544
545 $result->setResult($rows);
546
547 return $result;
548 }
549
556 public static function markFolderDelete(int $id, bool $mark = true): PublicActionResult
557 {
558 $result = new PublicActionResult();
559 $error = new \Bitrix\Landing\Error;
560
561 if ($mark)
562 {
563 $res = SiteCore::markFolderDelete($id);
564 }
565 else
566 {
567 $res = SiteCore::markFolderUnDelete($id);
568 }
569 if ($res->isSuccess())
570 {
571 $result->setResult(true);
572 }
573 else
574 {
575 $error->addFromResult($res);
576 $result->setError($error);
577 }
578
579 return $result;
580 }
581
587 public static function markFolderUnDelete(int $id): PublicActionResult
588 {
589 return self::markFolderDelete($id, false);
590 }
591
598 public static function publication(int $id, bool $mark = true): PublicActionResult
599 {
600 $result = new PublicActionResult();
601 $error = new \Bitrix\Landing\Error;
602
603 if ($mark)
604 {
605 $res = SiteCore::publication($id);
606 }
607 else
608 {
609 $res = SiteCore::unpublic($id);
610 }
611
612 if ($res->isSuccess())
613 {
614 $result->setResult($res->getId());
615 }
616 else
617 {
618 $error->addFromResult($res);
619 $result->setError($error);
620 }
621
622 return $result;
623 }
624
630 public static function unpublic(int $id): PublicActionResult
631 {
632 return self::publication($id, false);
633 }
634
641 public static function fullExport($id, array $params = array())
642 {
643 $result = new PublicActionResult();
644
645 $result->setResult(
646 SiteCore::fullExport($id, $params)
647 );
648
649 return $result;
650 }
651
658 public static function setRights($id, $rights = [])
659 {
660 static $mixedParams = ['rights'];
661
662 $result = new PublicActionResult();
663 $error = new \Bitrix\Landing\Error;
664 $result->setResult(false);
665 $id = (int)$id;
666
667 if (!is_array($rights))
668 {
669 $rights = [];
670 }
671
672 // check access for set rights
673 if (!Rights::isAdmin())
674 {
675 $error->addError(
676 'IS_NOT_ADMIN',
677 Loc::getMessage('LANDING_IS_NOT_ADMIN_ERROR')
678 );
679 $result->setError($error);
680 }
682 {
683 $error->addError(
684 'FEATURE_NOT_AVAIL',
685 \Bitrix\Landing\Restriction\Manager::getSystemErrorMessage(
686 'limit_sites_access_permissions'
687 )
688 );
689 $result->setError($error);
690 }
691 // set rights
692 else
693 {
694 $result->setResult(
696 $id,
697 $rights
698 )
699 );
700 }
701
702 return $result;
703 }
704
710 public static function getRights($id)
711 {
712 $result = new PublicActionResult();
713 $error = new \Bitrix\Landing\Error;
714 $result->setResult([]);
715 $id = (int)$id;
716
717 // check access for get rights
719 {
720 $error->addError(
721 'FEATURE_NOT_AVAIL',
722 \Bitrix\Landing\Restriction\Manager::getSystemErrorMessage(
723 'limit_sites_access_permissions'
724 )
725 );
726 $result->setError($error);
727 }
728 // get rights
729 else
730 {
731 $result->setResult(
733 $id
734 )
735 );
736 }
737
738 return $result;
739 }
740
750 public static function uploadFile($id, $picture, $ext = false, array $params = [], $temp = false): PublicActionResult
751 {
752 static $internal = true;
753 static $mixedParams = ['picture'];
754
755 $result = new PublicActionResult();
756 $result->setResult(false);
757 $error = new \Bitrix\Landing\Error;
758 $id = (int)$id;
759
760 $res = SiteCore::getList(array(
761 'filter' => array(
762 'ID' => $id
763 )
764 ));
765
766 if ($res->fetch())
767 {
768 $file = Manager::savePicture($picture, $ext, $params);
769 if ($file)
770 {
771 File::addToSite($id, $file['ID'], Utils::isTrue($temp));
772 $result->setResult(array(
773 'id' => $file['ID'],
774 'src' => $file['SRC']
775 ));
776 }
777 else
778 {
779 $error->addError(
780 'FILE_ERROR',
781 Loc::getMessage('LANDING_FILE_ERROR')
782 );
783 $result->setError($error);
784 }
785 }
786
787
788 return $result;
789 }
790
796 public static function setScope($type)
797 {
798 \Bitrix\Landing\Site\Type::setScope($type);
799
800 return new PublicActionResult();
801 }
802
810 protected static function binding(int $id, \Bitrix\Landing\Binding\Entity $binding, bool $bind): PublicActionResult
811 {
812 $result = new PublicActionResult();
813
814 if (Rights::hasAccessForSite($id, Rights::ACCESS_TYPES['read']) && !$binding->isForbiddenBindingAction())
815 {
816 if ($bind)
817 {
818 $result->setResult($binding->bindSite($id));
819 }
820 else
821 {
822 $result->setResult($binding->unbindSite($id));
823 }
824 }
825 else
826 {
827 $result->setResult(false);
828 }
829
830 return $result;
831 }
832
839 public static function bindingToMenu(int $id, string $menuCode): PublicActionResult
840 {
841 \Bitrix\Landing\Site\Type::setScope('KNOWLEDGE');
842 $binding = new \Bitrix\Landing\Binding\Menu($menuCode);
843 return self::binding($id, $binding, true);
844 }
845
852 public static function unbindingFromMenu(int $id, string $menuCode): PublicActionResult
853 {
854 \Bitrix\Landing\Site\Type::setScope('KNOWLEDGE');
855 $binding = new \Bitrix\Landing\Binding\Menu($menuCode);
856 return self::binding($id, $binding, false);
857 }
858
865 public static function bindingToGroup(int $id, int $groupId): PublicActionResult
866 {
867 \Bitrix\Landing\Site\Type::setScope('KNOWLEDGE');
868
869 if (
870 \Bitrix\landing\Connector\SocialNetwork::canCreateNewBinding($groupId) &&
871 !\Bitrix\landing\Binding\Group::getList($groupId)
872 )
873 {
874 $binding = new \Bitrix\Landing\Binding\Group($groupId);
875 $result = self::binding($id, $binding, true);
876 if ($result->getResult())
877 {
879 \Bitrix\Landing\Site::update($id, [
880 'TYPE' => 'GROUP'
881 ]);
883 }
884 return $result;
885 }
886
887 $result = new PublicActionResult();
888 $result->setResult(false);
889 return $result;
890 }
891
898 public static function unbindingFromGroup(int $id, int $groupId): PublicActionResult
899 {
900 \Bitrix\Landing\Site\Type::setScope('GROUP');
901
902 if (\Bitrix\landing\Connector\SocialNetwork::canCreateNewBinding($groupId))
903 {
904 $binding = new \Bitrix\Landing\Binding\Group($groupId);
905 $result = self::binding($id, $binding, false);
906 if ($result->getResult())
907 {
909 \Bitrix\Landing\Site::update($id, [
910 'TYPE' => 'KNOWLEDGE'
911 ]);
913 }
914 return $result;
915 }
916
917 $result = new PublicActionResult();
918 $result->setResult(false);
919 return $result;
920 }
921
927 protected static function removeEmptyBindings(array $bindings): array
928 {
929 // if PUBLIC_URL is empty user don't have read access
930 foreach ($bindings as $i => $binding)
931 {
932 if (!$binding['PUBLIC_URL'])
933 {
934 unset($bindings[$i]);
935 }
936 }
937
938 return array_values($bindings);
939 }
940
946 public static function getMenuBindings(?string $menuCode = null): PublicActionResult
947 {
948 $result = new PublicActionResult();
949 \Bitrix\Landing\Site\Type::setScope('KNOWLEDGE');
950 $bindings = \Bitrix\Landing\Binding\Menu::getList($menuCode);
951 $result->setResult(self::removeEmptyBindings($bindings));
952 return $result;
953 }
954
960 public static function getGroupBindings(?int $groupId = null): PublicActionResult
961 {
962 $result = new PublicActionResult();
963 \Bitrix\Landing\Site\Type::setScope('GROUP');
964 $bindings = \Bitrix\Landing\Binding\Group::getList($groupId);
965 $result->setResult(self::removeEmptyBindings($bindings));
966 return $result;
967 }
968}
static addToSite(int $id, int $fileId, bool $temp=false)
Definition file.php:228
const FEATURE_PERMISSIONS_AVAILABLE
Definition manager.php:42
static savePicture($file, $ext=false, $params=array())
Definition manager.php:585
static checkFeature(string $feature, array $params=array())
Definition manager.php:831
static getGroupBindings(?int $groupId=null)
Definition site.php:960
static unPublicFolder(int $folderId)
Definition site.php:512
static addFolder(int $siteId, array $fields)
Definition site.php:401
static binding(int $id, \Bitrix\Landing\Binding\Entity $binding, bool $bind)
Definition site.php:810
static fullExport($id, array $params=array())
Definition site.php:641
static getFolders(int $siteId, array $filter=[])
Definition site.php:523
static updateFolder(int $siteId, int $folderId, array $fields)
Definition site.php:431
static getMenuBindings(?string $menuCode=null)
Definition site.php:946
static setRights($id, $rights=[])
Definition site.php:658
static getAdditionalFields($id)
Definition site.php:44
static getPreview(int $id)
Definition site.php:86
static bindingToMenu(int $id, string $menuCode)
Definition site.php:839
static moveFolder(int $folderId, ?int $toFolderId, ?int $toSiteId=null)
Definition site.php:462
static removeEmptyBindings(array $bindings)
Definition site.php:927
static unbindingFromMenu(int $id, string $menuCode)
Definition site.php:852
static markFolderDelete(int $id, bool $mark=true)
Definition site.php:556
static getList(array $params=[], $initiator=null)
Definition site.php:99
static clearDisallowFields(array $fields)
Definition site.php:21
static publication(int $id, bool $mark=true)
Definition site.php:598
static bindingToGroup(int $id, int $groupId)
Definition site.php:865
static unbindingFromGroup(int $id, int $groupId)
Definition site.php:898
static markDelete($id, $mark=true)
Definition site.php:358
static add(array $fields)
Definition site.php:276
static uploadFile($id, $picture, $ext=false, array $params=[], $temp=false)
Definition site.php:750
static update($id, array $fields)
Definition site.php:305
static markFolderUnDelete(int $id)
Definition site.php:587
static publicationFolder(int $folderId, bool $mark=true)
Definition site.php:487
static setOperationsForSite($siteId, array $rights=[])
Definition rights.php:675
static getOperationsForSite($siteId)
Definition rights.php:518
static hasAccessForSite($siteId, $accessType, $deleted=false)
Definition rights.php:544
static setGlobalOff()
Definition rights.php:105
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29