Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
EntityLink.php
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
12use Bitrix\Im\V2\Common\ContextCustomer;
16
18{
19 use ContextCustomer;
20
21 public const TYPE_TASKS = 'TASKS';
22 public const TYPE_SONET = 'SONET_GROUP';
23 public const TYPE_CRM = 'CRM';
24 public const TYPE_MAIL = 'MAIL';
25 public const TYPE_CALL = 'CALL';
26
27 protected const HAS_URL = false;
28
29 private const CACHE_TTL = 18144000;
30
31 protected int $chatId;
32 protected string $entityId = '';
33 protected string $type = '';
34 protected string $url = '';
35
36 protected function __construct()
37 {
38 }
39
40 public static function getInstance(Chat $chat): self
41 {
42 $type = $chat->getEntityType() ?? '';
43 if ($type === self::TYPE_SONET && Loader::includeModule('socialnetwork'))
44 {
45 $instance = new SonetType();
46 }
47 elseif ($type === self::TYPE_TASKS && Loader::includeModule('tasks'))
48 {
49 $instance = new TasksType();
50 }
51 elseif (Loader::includeModule('calendar') && $type === \CCalendar::CALENDAR_CHAT_ENTITY_TYPE)
52 {
53 $instance = new CalendarType();
54 }
55 elseif ($type === self::TYPE_CRM && Loader::includeModule('crm'))
56 {
57 $instance = new CrmType($chat->getEntityId() ?? '');
58 }
59 elseif ($type === self::TYPE_CALL && Loader::includeModule('crm'))
60 {
61 $instance = new CallType($chat->getEntityData1() ?? '');
62 }
63 elseif ($type === self::TYPE_MAIL && Loader::includeModule('mail'))
64 {
65 $instance = new MailType();
66 }
67 else
68 {
69 $instance = new self();
70 }
71
72 $instance->type = $instance->type ?: $type;
73 $instance->chatId = $chat->getId() ?? 0;
74 $instance->entityId = $chat->getEntityId() ?? '';
75 $instance->fillUrl();
76
77 return $instance;
78 }
79
80 private function fillUrl(): void
81 {
82 if (!static::HAS_URL)
83 {
84 return;
85 }
86
87 $cache = Application::getInstance()->getCache();
88 if ($cache->initCache(self::CACHE_TTL, $this->getCacheId(), $this->getCacheDir()))
89 {
90 $cachedEntityUrl = $cache->getVars();
91
92 if (!is_array($cachedEntityUrl))
93 {
94 $cachedEntityUrl = [];
95 }
96
97 $this->url = $cachedEntityUrl['url'] ?? '';
98 return;
99 }
100
101 $this->url = $this->getUrl();
102 $cache->startDataCache();
103 $cache->endDataCache(['url' => $this->url]);
104 }
105
106 public static function cleanCache(int $chatId): void
107 {
108 Application::getInstance()->getCache()->cleanDir(static::getCacheDirByChatId($chatId));
109 }
110
111 private function getCacheDir(): string
112 {
113 return static::getCacheDirByChatId($this->chatId);
114 }
115
116 private static function getCacheDirByChatId(int $chatId): string
117 {
118 $cacheSubDir = $chatId % 100;
119
120 return "/bx/imc/chatentitylink/1/{$cacheSubDir}/{$chatId}";
121 }
122
123 private function getCacheId(): string
124 {
125 return "chat_entity_link_{$this->chatId}";
126 }
127
128 protected function getUrl(): string
129 {
130 return '';
131 }
132
133 protected function getRestType(): string
134 {
135 return $this->type;
136 }
137
138 public static function getRestEntityName(): string
139 {
140 return 'entityLink';
141 }
142
143 public function toRestFormat(array $option = []): array
144 {
145 return [
146 'type' => $this->getRestType(),
147 'url' => $this->url,
148 ];
149 }
150
155 public function toArray(array $options = []): array
156 {
157 return [
158 'TYPE' => $this->getRestType(),
159 'URL' => $this->url,
160 ];
161 }
162}