Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
helper.php
1<?php
2
4
5use Bitrix\Calendar\Core\Base\SingletonTrait;
10
11class Helper
12{
14
23 public function saveLinkRule(string $linkHash, array $ruleArray)
24 {
26 $link = (new \Bitrix\Calendar\Sharing\Link\Factory())->getLinkByHash($linkHash);
27 if (is_null($link))
28 {
29 return false;
30 }
31
32 if ($link instanceof UserLink && \CCalendar::GetCurUserId() !== $link->getUserId())
33 {
34 return false;
35 }
36
37 if ($this->isUserResponsibleForLink($link))
38 {
39 return false;
40 }
41
42 $linkObjectRule = (new Factory())->getLinkObjectRuleByLink($link);
43 if (is_null($linkObjectRule))
44 {
45 return false;
46 }
47
48 $sharingRuleMapper = new Mapper();
49 $rule = $sharingRuleMapper->buildRuleFromArray($ruleArray);
50 $sharingRuleMapper->saveForLinkObject($rule, $linkObjectRule);
51
52 return true;
53 }
54
60 private function isUserResponsibleForLink(Link $link): bool
61 {
62 if (!Loader::includeModule('crm'))
63 {
64 return false;
65 }
66
67 if (!($link instanceof CrmDealLink))
68 {
69 return false;
70 }
71
72 $currentUserId = (new \Bitrix\Crm\Service\Context())->getUserId();
73
74 return $this->getAssignedByIdCrmDeal($link) !== $currentUserId;
75 }
76
81 private function getAssignedByIdCrmDeal(CrmDealLink $link): ?int
82 {
83 $entityBroker = \Bitrix\Crm\Service\Container::getInstance()->getEntityBroker(\CCrmOwnerType::Deal);
84 if (!$entityBroker)
85 {
86 return null;
87 }
88
89 $entity = $entityBroker->getById($link->getEntityId());
90 if (!$entity)
91 {
92 return null;
93 }
94
95 return $entity->getAssignedById();
96 }
97}