Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sharing.php
1<?php
3
11
13{
14 public const ERROR_CODE_100010 = 100010;
15 public const ERROR_CODE_100020 = 100020;
16
17 protected int $userId;
18
22 public function __construct(int $userId)
23 {
24 $this->userId = $userId;
25 }
26
34 public function enable(): Result
35 {
36 $result = new Result();
37
38 if(!$this->isEnabled())
39 {
40 Factory::getInstance()->createUserLink($this->userId);
41 }
42 else
43 {
44 $result->addError(new Error('Sharing is already enabled', 100010));
45 }
46
47 return $result;
48 }
49
57 public function disable(): Result
58 {
59 $result = new Result();
60
61 if ($this->isEnabled())
62 {
63 $userLinks = $this->getAllUserLinks();
64 if (!empty($userLinks))
65 {
66 $userLinkMapper = new UserLinkMapper();
67 foreach ($userLinks as $userLink)
68 {
69 $userLinkMapper->delete($userLink);
70 }
71 }
72 }
73 else
74 {
75 $result->addError(new Error('Sharing is already disabled', 100020));
76 }
77
78 return $result;
79 }
80
87 public function deactivateUserLink(?string $hash)
88 {
89 $result = new Result();
90
91 $link = $this->getUserLinkByHash($hash);
92 if (empty($link))
93 {
94 $result->addError(new Error('Link not found'));
95
96 return $result;
97 }
98
99 $updateResult = SharingLinkTable::update((int)$link['ID'], [
100 'ACTIVE' => 'N',
101 ]);
102 if (!$updateResult->isSuccess())
103 {
104 $result->addError(new Error('Delete link error'));
105
106 return $result;
107 }
108
109 return $result;
110 }
111
117 public function increaseFrequentUse(?string $hash): Result
118 {
119 $result = new Result();
120
121 $link = $this->getUserLinkByHash($hash);
122 if (empty($link))
123 {
124 $result->addError(new Error('Link not found'));
125
126 return $result;
127 }
128
129 $updateResult = SharingLinkTable::update((int)$link['ID'], [
130 'FREQUENT_USE' => $link['FREQUENT_USE'] + 1,
131 ]);
132 if (!$updateResult->isSuccess())
133 {
134 $result->addError(new Error('Update error'));
135
136 return $result;
137 }
138
139 return $result;
140 }
141
148 public function generateUserJointLink(array $memberIds): Result
149 {
150 $result = new Result();
151
152 if (!$this->isEnabled())
153 {
154 $result->addError(new Error('Sharing is disabled', 100050));
155 }
156
157 if ($result->isSuccess())
158 {
159 $userJointLink = Factory::getInstance()->createUserJointLink($this->userId, $memberIds);
160 $url = Helper::getShortUrl($userJointLink->getUrl());
161
162 $result->setData([
163 'url' => $url,
164 ]);
165
166// \CCalendarNotify::Send([
167// 'mode' => \CCalendarNotify::NOTIFY_USERS_ADDED_TO_MULTI_LINK,
168// 'userId' => $this->userId, //from
169// 'guestIds' => $memberIds, //to
170// 'params' => [
171// 'url' => $url,
172// 'linkId' => $userJointLink->getId(),
173// ],
174// ]);
175 }
176
177 return $result;
178 }
179
187 public function isEnabled(): bool
188 {
189 return (bool)$this->getActiveLinkUrl();
190 }
191
192 public function getLinkInfo(): array
193 {
194 $linkRuleMapper = new Link\Rule\Mapper();
195 $userLink = $this->getUserLink();
196 if (is_null($userLink))
197 {
198 $linkObjectRule = new Link\Rule\UserRule($this->userId);
199 $sharingRule = $linkRuleMapper->getFromLinkObjectRule($linkObjectRule);
200 $sharingHash = null;
201 $url = null;
202 }
203 else
204 {
205 $sharingRule = $userLink->getSharingRule();
206 $sharingHash = $userLink->getHash();
207 $url = Helper::getShortUrl($userLink->getUrl());
208 }
209
210 return [
211 'url' => $url,
212 'hash' => $sharingHash,
213 'rule' => $linkRuleMapper->convertToArray($sharingRule),
214 ];
215 }
216
222 public function getAllUserLinkInfo(): array
223 {
224 $userLinks = $this->getUserJointLinks();
225 $userLinkMapper = new UserLinkMapper();
226
228 return array_map(static function($userLink) use ($userLinkMapper) {
229 return $userLinkMapper->convertToArray($userLink);
230 }, $userLinks);
231 }
232
240 public function getActiveLinkShortUrl(): ?string
241 {
242 $url = $this->getActiveLinkUrl();
243 if (!empty($url))
244 {
245 $url = Helper::getShortUrl($url);
246 }
247
248 return $url;
249 }
250
258 public function getActiveLinkUrl(): ?string
259 {
260 $userLink = $this->getUserLink();
261 return $userLink && $userLink->isActive() ? $userLink->getUrl() : null;
262 }
263
271 public function getUserLink(): ?UserLink
272 {
273 return $this->getUserLinkByUserId($this->userId);
274 }
275
279 public function getLinkSettings(): array
280 {
281 $settings = [];
282 $linkInfo = $this->getLinkInfo();
283
284 if (!empty($linkInfo))
285 {
286 $calendarSettings = \CCalendar::GetSettings();
287 $settings = [
288 'weekStart' => \CCalendar::GetWeekStart(),
289 'workTimeStart' => $calendarSettings['work_time_start'],
290 'workTimeEnd' => $calendarSettings['work_time_end'],
291 'weekHolidays' => $calendarSettings['week_holidays'],
292 'rule' => [
293 'hash' => $linkInfo['hash'],
294 'slotSize' => $linkInfo['rule']['slotSize'],
295 'ranges' => $linkInfo['rule']['ranges'],
296 ],
297 ];
298 }
299
300 return $settings;
301 }
302
309 protected function getUserLinkByUserId(int $userId): ?UserLink
310 {
311 $userLinks = Factory::getInstance()->getUserLinks($userId);
312
313 return !empty($userLinks) ? array_shift($userLinks) : null;
314 }
315
321 protected function getAllUserLinks(): array
322 {
323 return Factory::getInstance()->getAllUserLinks($this->userId);
324 }
325
331 protected function getUserJointLinks(): array
332 {
333 return Factory::getInstance()->getUserJointLinks($this->userId);
334 }
335
336 protected function getUserLinkByHash(?string $hash)
337 {
338 if (empty($hash))
339 {
340 return null;
341 }
342
343 return SharingLinkTable::query()
344 ->setSelect(['ID', 'HASH', 'OBJECT_ID', 'OBJECT_TYPE', 'ACTIVE', 'FREQUENT_USE'])
345 ->where('OBJECT_ID', $this->userId)
346 ->where('OBJECT_TYPE', Link\Helper::USER_SHARING_TYPE)
347 ->where('HASH', $hash)
348 ->where('ACTIVE', 'Y')
349 ->setLimit(1)
350 ->exec()->fetch()
351 ;
352 }
353}
static getShortUrl(string $url)
Definition helper.php:134
generateUserJointLink(array $memberIds)
Definition sharing.php:148
getUserLinkByHash(?string $hash)
Definition sharing.php:336
increaseFrequentUse(?string $hash)
Definition sharing.php:117
deactivateUserLink(?string $hash)
Definition sharing.php:87