Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
factory.php
1<?php
3
10
12{
13 protected static ?Factory $instance = null;
14 protected const SELECT = ['*', 'MEMBERS', 'MEMBERS.USER', 'MEMBERS.IMAGE'];
15
19 public static function getInstance(): Factory
20 {
21 if (self::$instance === null)
22 {
23 self::$instance = new self();
24 }
25
26 return self::$instance;
27 }
28
29
39 public function getLinkArrayByHash(string $hash): ?array
40 {
41 $sharingLink = $this->getLinkByHash($hash);
42
43 if ($sharingLink instanceof CrmDealLink)
44 {
45 return (new CrmDealLinkMapper())->convertToArray($sharingLink);
46 }
47
48 if ($sharingLink instanceof UserLink)
49 {
50 return (new UserLinkMapper())->convertToArray($sharingLink);
51 }
52
53 if ($sharingLink instanceof EventLink)
54 {
55 return (new EventLinkMapper())->convertToArray($sharingLink);
56 }
57
58 return null;
59 }
60
70 public function getLinkByHash(string $hash): ?\Bitrix\Calendar\Core\Base\EntityInterface
71 {
72 $sharingLinkEO = SharingLinkTable::query()
73 ->setSelect(self::SELECT)
74 ->where('HASH', $hash)
75 ->exec()->fetchObject()
76 ;
77
78 if ($sharingLinkEO === null)
79 {
80 return null;
81 }
82
83 if ($sharingLinkEO->getObjectType() === Helper::USER_SHARING_TYPE)
84 {
85 return (new UserLinkMapper())->getByEntityObject($sharingLinkEO);
86 }
87
88 if ($sharingLinkEO->getObjectType() === Helper::EVENT_SHARING_TYPE)
89 {
90 return (new EventLinkMapper())->getByEntityObject($sharingLinkEO);
91 }
92
93 if ($sharingLinkEO->getObjectType() === Helper::CRM_DEAL_SHARING_TYPE)
94 {
95 return (new CrmDealLinkMapper())->getByEntityObject($sharingLinkEO);
96 }
97
98 return null;
99 }
100
107 public function getAllUserLinks(int $userId): array
108 {
109 return (new UserLinkMapper())->getMap([
110 '=OBJECT_ID' => $userId,
111 '=OBJECT_TYPE' => Helper::USER_SHARING_TYPE,
112 '=ACTIVE' => 'Y',
113 ])->getCollection();
114 }
115
124 public function getUserLinks($userId): array
125 {
126 return (new UserLinkMapper())->getMap([
127 '=OBJECT_ID' => $userId,
128 '=OBJECT_TYPE' => Helper::USER_SHARING_TYPE,
129 '=ACTIVE' => 'Y',
130 '=MEMBERS_HASH' => null,
131 ])->getCollection();
132 }
133
142 public function getUserJointLinks($userId): array
143 {
144 return (new UserLinkMapper())->getMap([
145 '=OBJECT_ID' => $userId,
146 '=OBJECT_TYPE' => Helper::USER_SHARING_TYPE,
147 '=ACTIVE' => 'Y',
148 '!=MEMBERS_HASH' => null,
149 ])->getCollection();
150 }
151
159 public function createUserLink(int $userId): self
160 {
161 $userLink = (new UserLink())
162 ->setUserId($userId)
163 ->setActive(true)
164 ->setFrequentUse(1)
165 ;
166
167 $rule = (new Rule\Factory())->getRuleBySharingLink($userLink);
168 $userLink->setSharingRule($rule);
169
170 (new UserLinkMapper())->create($userLink);
171
172 return $this;
173 }
174
186 public function createEventLink(array $params): EventLink
187 {
188 $eventLink = (new EventLink())
189 ->setEventId($params['eventId'])
190 ->setOwnerId($params['ownerId'])
191 ->setHostId($params['hostId'])
192 ->setParentLinkHash($params['parentLinkHash'])
193 ->setActive(true)
194 ->setDateExpire($params['expiryDate'] ?? null)
195 ->setExternalUserName($params['externalUserName'] ?? null)
196 ;
197
198 (new EventLinkMapper())->create($eventLink);
199
200 return $eventLink;
201 }
202
215 public function createCrmDealLink(
216 int $ownerId,
217 int $entityId,
218 ?int $contactId = null,
219 ?int $contactType = null,
220 ?string $channelId = null,
221 ?string $senderId = null
222 ): CrmDealLink
223 {
224 $crmDealLink = (new CrmDealLink())
225 ->setOwnerId($ownerId)
226 ->setEntityId($entityId)
227 ->setContactType($contactType)
228 ->setContactId($contactId)
229 ->setChannelId($channelId)
230 ->setSenderId($senderId)
231 ->setActive(true)
232 ->setDateExpire(
233 Sharing\Helper::createSharingLinkExpireDate(
234 new DateTime(),
235 Sharing\Link\Helper::CRM_DEAL_SHARING_TYPE
236 )
237 )
238 ->setFrequentUse(1)
239 ;
240
241 $rule = (new Rule\Factory())->getRuleBySharingLink($crmDealLink);
242 $crmDealLink->setSharingRule($rule);
243
244 (new CrmDealLinkMapper())->create($crmDealLink);
245
246 return $crmDealLink;
247 }
248
249 public function createUserJointLink(int $userId, array $memberIds): \Bitrix\Calendar\Core\Base\EntityInterface
250 {
251 $memberHash = $this->generateMembersHash($userId, $memberIds);
252
253 if ($existJointLink = $this->getJointLinkByMembersHash($memberHash))
254 {
255 SharingLinkTable::update($existJointLink->getId(), [
256 'FREQUENT_USE' => $existJointLink->getFrequentUse() + 1,
257 ]);
258
259 return $existJointLink;
260 }
261
262 $userJointLink = (new UserLink())
263 ->setUserId($userId)
264 ->setActive(true)
265 ->setMembers($this->getMembersFromIds($memberIds))
266 ->setMembersHash($memberHash)
267 ->setFrequentUse(1)
268 ;
269
270 (new UserLinkMapper())->create($userJointLink);
271
272 return $userJointLink;
273 }
274
275 public function createCrmDealJointLink(CrmDealLink $crmDealLink, array $memberIds): CrmDealLink
276 {
277 $crmDealLink
278 ->setActive(true)
279 ->setMembers($this->getMembersFromIds($memberIds))
280 ->setFrequentUse(1)
281 ;
282
283 (new CrmDealLinkMapper())->create($crmDealLink);
284
285 return $crmDealLink;
286 }
287
288 private function getMembersFromIds(array $memberIds): array
289 {
290 $memberIds = array_map(static function ($memberId) {
291 return (int)$memberId;
292 }, $memberIds);
293
294 $result = [];
295 $users = UserTable::query()
296 ->whereIn('ID', $memberIds)
297 ->where('IS_REAL_USER', 'Y')
298 ->setSelect(['NAME', 'LAST_NAME', 'ID'])
299 ->exec()
300 ->fetchCollection()
301 ;
302
303 foreach ($users as $user)
304 {
305 $member = new Member();
306 $member
307 ->setId($user->getId())
308 ->setName($user->getName())
309 ->setLastName($user->getLastName())
310 ;
311 $result[] = $member;
312 }
313
314 return $result;
315 }
316
317 public function generateMembersHash(int $userId, array $memberIds): string
318 {
319 $memberIds = array_map(static function ($memberId) {
320 return (int)$memberId;
321 }, $memberIds);
322
323 sort($memberIds);
324 $implodedUsers = implode('|', $memberIds) . '|' . $userId;
325
326 return md5($implodedUsers);
327 }
328
338 public function getEventLinkByEventId(int $eventId): ?\Bitrix\Calendar\Core\Base\EntityInterface
339 {
340 $sharingLinkEO = SharingLinkTable::query()
341 ->setSelect(['*'])
342 ->where('OBJECT_ID', $eventId)
343 ->where('OBJECT_TYPE', Helper::EVENT_SHARING_TYPE)
344 ->where('ACTIVE', 'Y')
345 ->exec()->fetchObject()
346 ;
347
348 if ($sharingLinkEO === null)
349 {
350 return null;
351 }
352
353 return (new EventLinkMapper())->getByEntityObject($sharingLinkEO);
354 }
355
365 public function getDeletedEventLinkByEventId(int $eventId): ?\Bitrix\Calendar\Core\Base\EntityInterface
366 {
367 $sharingLinkEO = SharingLinkTable::query()
368 ->setSelect(self::SELECT)
369 ->where('OBJECT_ID', $eventId)
370 ->where('OBJECT_TYPE', Helper::EVENT_SHARING_TYPE)
371 ->exec()->fetchObject();
372
373 if ($sharingLinkEO === null)
374 {
375 return null;
376 }
377
378 return (new EventLinkMapper())->getByEntityObject($sharingLinkEO);
379 }
380
393 public function getCrmDealLink(
394 int $entityId,
395 int $ownerId,
396 ?int $contactId = null,
397 ?int $contactType = null
398 ): ?\Bitrix\Calendar\Core\Base\EntityInterface
399 {
400 $sharingLinkEO = SharingLinkTable::query()
401 ->setSelect(self::SELECT)
402 ->where('OBJECT_ID', $entityId)
403 ->where('OBJECT_TYPE', Helper::CRM_DEAL_SHARING_TYPE)
404 ->where('ACTIVE', 'Y')
405 ->where('OWNER_ID', $ownerId)
406 ->where('CONTACT_ID', $contactId)
407 ->where('CONTACT_TYPE', $contactType)
408 ->where('DATE_CREATE', '>=', (new DateTime())->setTime(0, 0))
409 ->whereNull('MEMBERS.MEMBER_ID')
410 ->exec()->fetchObject();
411
412 if ($sharingLinkEO === null)
413 {
414 return null;
415 }
416
417 return (new CrmDealLinkMapper())->getByEntityObject($sharingLinkEO);
418 }
419
420 public function getJointLinkByMembersHash(string $membersHash): ?\Bitrix\Calendar\Core\Base\EntityInterface
421 {
422 $sharingLinkEO = SharingLinkTable::query()
423 ->setSelect(self::SELECT)
424 ->where('MEMBERS_HASH', $membersHash)
425 ->where('ACTIVE', 'Y')
426 ->exec()->fetchObject()
427 ;
428
429 if ($sharingLinkEO === null)
430 {
431 return null;
432 }
433
434 return (new UserLinkMapper())->getByEntityObject($sharingLinkEO);
435 }
436
437 public function getParentLinkByConferenceId(string $conferenceId): ?Joint\JointLink
438 {
439 $entityObject = SharingLinkTable::query()
440 ->setSelect(['PARENT_LINK_HASH'])
441 ->where('CONFERENCE_ID', $conferenceId)
442 ->exec()->fetchObject()
443 ;
444
445 if (is_null($entityObject))
446 {
447 return null;
448 }
449
450 $parentLink = $this->getLinkByHash($entityObject->getParentLinkHash());
451
452 return $parentLink instanceof Joint\JointLink ? $parentLink : null;
453 }
454
455 public function getCrmDealJointLink(
456 int $entityId,
457 int $ownerId,
458 ?int $contactId = null,
459 ?int $contactType = null
460 ): ?\Bitrix\Calendar\Core\Base\EntityInterface
461 {
462 $sharingLinkEO = SharingLinkTable::query()
463 ->setSelect(self::SELECT)
464 ->where('OBJECT_ID', $entityId)
465 ->where('OBJECT_TYPE', Helper::CRM_DEAL_SHARING_TYPE)
466 ->where('ACTIVE', 'Y')
467 ->where('OWNER_ID', $ownerId)
468 ->where('CONTACT_ID', $contactId)
469 ->where('CONTACT_TYPE', $contactType)
470 ->whereNotNull('MEMBERS.MEMBER_ID')
471 ->exec()->fetchObject();
472
473 if ($sharingLinkEO === null)
474 {
475 return null;
476 }
477
478 return (new CrmDealLinkMapper())->getByEntityObject($sharingLinkEO);
479 }
480}