1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
sharing.php
См. документацию.
1<?php
2namespace Bitrix\Calendar\Sharing;
3
4use Bitrix\Calendar\Internals\SharingLinkTable;
5use Bitrix\Calendar\Sharing\Link\UserLink;
6use Bitrix\Calendar\Sharing\Link\Factory;
7use Bitrix\Calendar\Sharing\Link\UserLinkMapper;
8use Bitrix\Calendar\Util;
9use Bitrix\Main\Error;
10use Bitrix\Main\Result;
11
13{
14 public const FEATURE_NAME = 'calendar_sharing';
15 public const ERROR_CODE_100010 = 100010;
16 public const ERROR_CODE_100020 = 100020;
17
18 protected int $userId;
19 protected UserLink|null|bool $userLink = false;
20
21 protected const OPTION_SORT_JOINT_LINKS_BY_FREQUENT_USE = 'sortJointLinksByFrequentUse';
22 protected const OPTION_SHARING_SETTINGS_COLLAPSED = 'sharingSettingsCollapsed';
23
27 public function __construct(int $userId)
28 {
29 $this->userId = $userId;
30 }
31
39 public function enable(): Result
40 {
41 $result = new Result();
42
43 if(!$this->isEnabled())
44 {
45 Factory::getInstance()->createUserLink($this->userId);
46 }
47 else
48 {
49 $result->addError(new Error('Sharing is already enabled', 100010));
50 }
51
52 $this->userLink = false;
53
54 return $result;
55 }
56
64 public function disable(): Result
65 {
66 $result = new Result();
67
68 if ($this->isEnabled())
69 {
70 $userLinks = $this->getAllUserLinks();
71 if (!empty($userLinks))
72 {
73 $userLinkMapper = new UserLinkMapper();
74 foreach ($userLinks as $userLink)
75 {
76 $userLinkMapper->delete($userLink);
77 }
78 }
79 }
80 else
81 {
82 $result->addError(new Error('Sharing is already disabled', 100020));
83 }
84
85 $this->userLink = false;
86
87 return $result;
88 }
89
96 public function deactivateUserLink(?string $hash)
97 {
98 $result = new Result();
99
100 $link = $this->getUserLinkByHash($hash);
101 if (empty($link))
102 {
103 $result->addError(new Error('Link not found'));
104
105 return $result;
106 }
107
108 $updateResult = SharingLinkTable::update((int)$link['ID'], [
109 'ACTIVE' => 'N',
110 ]);
111 if (!$updateResult->isSuccess())
112 {
113 $result->addError(new Error('Delete link error'));
114
115 return $result;
116 }
117
118 return $result;
119 }
120
126 public function increaseFrequentUse(?string $hash): Result
127 {
128 $result = new Result();
129
130 $link = $this->getUserLinkByHash($hash);
131 if (empty($link))
132 {
133 $result->addError(new Error('Link not found'));
134
135 return $result;
136 }
137
138 $updateResult = SharingLinkTable::update((int)$link['ID'], [
139 'FREQUENT_USE' => $link['FREQUENT_USE'] + 1,
140 ]);
141 if (!$updateResult->isSuccess())
142 {
143 $result->addError(new Error('Update error'));
144
145 return $result;
146 }
147
148 return $result;
149 }
150
157 public function generateUserJointLink(array $memberIds): Result
158 {
159 $result = new Result();
160
161 if (!$this->isEnabled())
162 {
163 $result->addError(new Error('Sharing is disabled', 100050));
164 }
165
166 if ($result->isSuccess())
167 {
169 $userJointLink = Factory::getInstance()->createUserJointLink($this->userId, $memberIds);
170
171 $linkArray = (new UserLinkMapper())->convertToArray($userJointLink);
172
173 $result->setData([
174 'url' => $linkArray['shortUrl'],
175 'link' => $linkArray,
176 ]);
177 }
178
179 return $result;
180 }
181
189 public function isEnabled(): bool
190 {
191 return (bool)$this->getActiveLinkUrl();
192 }
193
194 public function getLinkInfo(): array
195 {
196 $linkRuleMapper = new Link\Rule\Mapper();
197 $userLink = $this->getUserLink();
198 if (is_null($userLink))
199 {
200 $linkObjectRule = new Link\Rule\UserRule($this->userId);
201 $sharingRule = $linkRuleMapper->getFromLinkObjectRule($linkObjectRule);
202 $sharingHash = null;
203 $url = null;
204 }
205 else
206 {
207 $sharingRule = $userLink->getSharingRule();
208 $sharingHash = $userLink->getHash();
209 $url = Helper::getShortUrl($userLink->getUrl());
210 }
211
212 return [
213 'url' => $url,
214 'hash' => $sharingHash,
215 'rule' => $linkRuleMapper->convertToArray($sharingRule),
216 ];
217 }
218
219 public function getUserInfo(): array
220 {
221 return [
222 'id' => $this->userId,
223 'name' => \CCalendar::GetUserName($this->userId),
224 'avatar' => \CCalendar::GetUserAvatarSrc($this->userId),
225 'isCollabUser' => Util::isCollabUser($this->userId),
226 ];
227 }
228
234 public function getAllUserLinkInfo(): array
235 {
236 $userLinks = $this->getUserJointLinks();
237 $userLinkMapper = new UserLinkMapper();
238
240 return array_map(static function($userLink) use ($userLinkMapper) {
241 return $userLinkMapper->convertToArray($userLink);
242 }, $userLinks);
243 }
244
252 public function getActiveLinkShortUrl(): ?string
253 {
254 $url = $this->getActiveLinkUrl();
255 if (!empty($url))
256 {
257 $url = Helper::getShortUrl($url);
258 }
259
260 return $url;
261 }
262
270 public function getActiveLinkUrl(): ?string
271 {
272 $userLink = $this->getUserLink();
273 return $userLink && $userLink->isActive() ? $userLink->getUrl() : null;
274 }
275
283 public function getUserLink(): ?UserLink
284 {
285 if (!$this->userLink)
286 {
287 $this->userLink = $this->getUserLinkByUserId($this->userId);
288 }
289
290 return $this->userLink;
291 }
292
296 public function getLinkSettings(): array
297 {
298 $settings = [];
299 $linkInfo = $this->getLinkInfo();
300
301 if (!empty($linkInfo))
302 {
303 $calendarSettings = \CCalendar::GetSettings();
304 $settings = [
305 'weekStart' => \CCalendar::GetWeekStart(),
306 'workTimeStart' => $calendarSettings['work_time_start'],
307 'workTimeEnd' => $calendarSettings['work_time_end'],
308 'weekHolidays' => $calendarSettings['week_holidays'],
309 'rule' => [
310 'hash' => $linkInfo['hash'],
311 'slotSize' => $linkInfo['rule']['slotSize'],
312 'ranges' => $linkInfo['rule']['ranges'],
313 ],
314 ];
315 }
316
317 return $settings;
318 }
319
320 public function getOptions(): array
321 {
322 return [
323 self::OPTION_SORT_JOINT_LINKS_BY_FREQUENT_USE => \CUserOptions::GetOption(
324 'calendar',
325 self::OPTION_SORT_JOINT_LINKS_BY_FREQUENT_USE,
326 'Y',
327 $this->userId,
328 ) === 'Y',
329 self::OPTION_SHARING_SETTINGS_COLLAPSED => \CUserOptions::getOption(
330 'calendar',
331 self::OPTION_SHARING_SETTINGS_COLLAPSED,
332 'N',
333 $this->userId,
334 ) === 'Y',
335 ];
336 }
337
338 public function setSortJointLinksByFrequentUse(bool $sortByFrequentUse): void
339 {
340 \CUserOptions::SetOption(
341 'calendar',
342 self::OPTION_SORT_JOINT_LINKS_BY_FREQUENT_USE,
343 $sortByFrequentUse ? 'Y' : 'N',
344 false,
345 $this->userId,
346 );
347 }
348
349 public function setSharingSettingsCollapsed(bool $isCollapsed): void
350 {
351 \CUserOptions::SetOption(
352 'calendar',
353 self::OPTION_SHARING_SETTINGS_COLLAPSED,
354 $isCollapsed ? 'Y' : 'N',
355 false,
356 $this->userId,
357 );
358 }
359
366 protected function getUserLinkByUserId(int $userId): ?UserLink
367 {
368 $userLinks = Factory::getInstance()->getUserLinks($userId);
369
370 return !empty($userLinks) ? array_shift($userLinks) : null;
371 }
372
378 protected function getAllUserLinks(): array
379 {
380 return Factory::getInstance()->getAllUserLinks($this->userId);
381 }
382
388 protected function getUserJointLinks(): array
389 {
390 return Factory::getInstance()->getUserJointLinks($this->userId);
391 }
392
393 protected function getUserLinkByHash(?string $hash)
394 {
395 if (empty($hash))
396 {
397 return null;
398 }
399
400 return SharingLinkTable::query()
401 ->setSelect(['ID', 'HASH', 'OBJECT_ID', 'OBJECT_TYPE', 'ACTIVE', 'FREQUENT_USE'])
402 ->where('OBJECT_ID', $this->userId)
403 ->where('OBJECT_TYPE', Link\Helper::USER_SHARING_TYPE)
404 ->where('HASH', $hash)
405 ->where('ACTIVE', 'Y')
406 ->setLimit(1)
407 ->exec()->fetch()
408 ;
409 }
410}
$hash
Определения ajax_redirector.php:8
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
const ERROR_CODE_100020
Определения sharing.php:16
const FEATURE_NAME
Определения sharing.php:14
getActiveLinkShortUrl()
Определения sharing.php:252
setSharingSettingsCollapsed(bool $isCollapsed)
Определения sharing.php:349
const OPTION_SHARING_SETTINGS_COLLAPSED
Определения sharing.php:22
getUserLinkByUserId(int $userId)
Определения sharing.php:366
getUserLinkByHash(?string $hash)
Определения sharing.php:393
const OPTION_SORT_JOINT_LINKS_BY_FREQUENT_USE
Определения sharing.php:21
UserLink null bool $userLink
Определения sharing.php:19
setSortJointLinksByFrequentUse(bool $sortByFrequentUse)
Определения sharing.php:338
increaseFrequentUse(?string $hash)
Определения sharing.php:126
const ERROR_CODE_100010
Определения sharing.php:15
deactivateUserLink(?string $hash)
Определения sharing.php:96
__construct(int $userId)
Определения sharing.php:27
static isCollabUser(int $userId)
Определения util.php:337
Определения error.php:15
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$settings
Определения product_settings.php:43
$url
Определения iframe.php:7