Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mailboxsyncmanager.php
1<?php
3
4use COption;
10
12{
13 private $userId;
14 private $mailCheckInterval;
15
16 public function __construct($userId)
17 {
18 $this->userId = $userId;
19 $this->mailCheckInterval = COption::getOptionString('intranet', 'mail_check_period', 10) * 60;
20 }
21
22 public static function checkSyncWithCrm(int $mailboxId): bool
23 {
24 if (Loader::includeModule('crm'))
25 {
26 return (bool)MailFilterTable::getCount([
27 '=MAILBOX_ID' => $mailboxId,
28 '=ACTION_TYPE' => 'crm_imap',
29 ]);
30 }
31
32 return false;
33 }
34
35 public function getFailedToSyncMailboxes()
36 {
37 $mailboxes = [];
38 $mailboxesSyncInfo = $this->getMailboxesSyncInfo();
39
40 foreach ($mailboxesSyncInfo as $mailboxId => $lastMailCheckData)
41 {
42 if (!$lastMailCheckData['isSuccess'])
43 {
44 $mailboxes[$mailboxId] = $lastMailCheckData;
45 }
46 }
47 return $mailboxes;
48 }
49
50 public function getSuccessSyncedMailboxes()
51 {
52 $mailboxesToSync = [];
53 $mailboxesSyncInfo = $this->getMailboxesSyncInfo();
54
55 foreach ($mailboxesSyncInfo as $mailboxId => $lastMailCheckData)
56 {
57 if ($lastMailCheckData['isSuccess'])
58 {
59 $mailboxesToSync[$mailboxId] = $lastMailCheckData;
60 }
61 }
62 return $mailboxesToSync;
63 }
64
65 /*
66 * It's time for synchronization for at least one mailbox.
67 */
68 public function isMailNeedsToBeSynced()
69 {
70 return count($this->getNeedToBeSyncedMailboxes()) > 0;
71 }
72
73 /*
74 * Returns mailboxes that are recommended to be synchronized.
75 */
77 {
78 $mailboxesSyncData = $this->getSuccessSyncedMailboxes();
79 $mailboxesToSync = [];
80 foreach ($mailboxesSyncData as $mailboxId => $lastMailCheckData)
81 {
82 if ($lastMailCheckData['timeStarted'] >= 0 && (time() - intval($lastMailCheckData['timeStarted']) >= $this->mailCheckInterval))
83 {
84 $mailboxesToSync[$mailboxId] = $lastMailCheckData;
85 }
86 }
87 return $mailboxesToSync;
88 }
89
90 public function getMailCheckInterval()
91 {
92 return $this->mailCheckInterval;
93 }
94
95 public function deleteSyncData($mailboxId)
96 {
97 $filter = [
98 '=MAILBOX_ID' => $mailboxId,
99 '=ENTITY_TYPE' => 'MAILBOX',
100 '=ENTITY_ID' => $mailboxId,
101 '=PROPERTY_NAME' => 'SYNC_STATUS',
102 ];
103
104 return MailEntityOptionsTable::deleteList($filter);
105 }
106
107 public function setDefaultSyncData($mailboxId)
108 {
109 $this->saveSyncStatus($mailboxId, true, 0);
110 }
111
112 private function buildTimeForSyncStatus($time): int
113 {
114 if($time !== null && (int)$time >= 0)
115 {
116 return (int)$time;
117 }
118
119 return time();
120 }
121
122 public function setSyncStartedData($mailboxId, $time = null)
123 {
124 $this->saveSyncStatus($mailboxId, true, $this->buildTimeForSyncStatus($time));
125 }
126
127 public function setSyncStatus($mailboxId, $isSuccess, $time = null)
128 {
129 $this->saveSyncStatus($mailboxId, $isSuccess, $this->buildTimeForSyncStatus($time));
130 }
131
132 private function saveSyncStatus($mailboxID, $status, $date)
133 {
134 $filter = [
135 '=MAILBOX_ID' => $mailboxID,
136 '=ENTITY_TYPE' => 'MAILBOX',
137 '=ENTITY_ID' => $mailboxID,
138 '=PROPERTY_NAME' => 'SYNC_STATUS',
139 ];
140
141 $keyRow = [
142 'MAILBOX_ID' => $mailboxID,
143 'ENTITY_TYPE' => 'MAILBOX',
144 'ENTITY_ID' => $mailboxID,
145 'PROPERTY_NAME' => 'SYNC_STATUS',
146 ];
147
148 $fields = $keyRow;
149
150 $fields['VALUE'] = $status;
151 $fields['DATE_INSERT'] = DateTime::createFromTimestamp($date);
152
153 if(MailEntityOptionsTable::getCount($filter))
154 {
155 MailEntityOptionsTable::update(
156 $keyRow,
157 [
158 'DATE_INSERT' => $fields['DATE_INSERT'],
159 'VALUE' => $fields['VALUE'],
160 ],
161 );
162 }
163 else
164 {
165 MailEntityOptionsTable::add(
166 $fields
167 );
168 }
169 }
170
171 public function getMailboxSyncInfo($mailboxID)
172 {
173 $dateLastOpening = \Bitrix\Mail\Internals\MailEntityOptionsTable::getList(
174 [
175 'select' => [
176 'VALUE',
177 'DATE_INSERT',
178 ],
179 'filter' => [
180 '=MAILBOX_ID' => $mailboxID,
181 '=ENTITY_TYPE' => 'MAILBOX',
182 '=ENTITY_ID' => $mailboxID,
183 '=PROPERTY_NAME' => 'SYNC_STATUS',
184 ],
185 'limit' => 1,
186 ]
187 )->fetch();
188
189 if(isset($dateLastOpening['VALUE']))
190 {
191 return [
192 'isSuccess' => (bool)$dateLastOpening['VALUE'],
193 'timeStarted' => $dateLastOpening['DATE_INSERT']->getTimestamp(),
194 ];
195 }
196
197 return false;
198 }
199
203 public function getMailboxesSyncInfo()
204 {
205 $mailboxesSyncInfo = [];
206
207 $userMailboxIds = array_keys(MailboxTable::getUserMailboxes());
208 foreach ($userMailboxIds as $id)
209 {
210 $id = (int)$id;
211 $mailboxSyncInfo = $this->getMailboxSyncInfo($id);
212 if($mailboxSyncInfo !== false)
213 {
214 $mailboxesSyncInfo[$id] = $mailboxSyncInfo;
215 }
216 }
217 return $mailboxesSyncInfo;
218 }
219
223 public function getNextTimeToSync($lastMailCheckData)
224 {
225 return intval($lastMailCheckData['timeStarted']) + $this->mailCheckInterval - time();
226 }
227
228 /*
229 * Returns the time remaining until the required recommended mail synchronization.
230 * If it's time to synchronize, it will return 0.
231 */
232 public function getTimeBeforeNextSync()
233 {
234 $mailboxesSuccessSynced = $this->getSuccessSyncedMailboxes();
235 $timeBeforeNextSyncMailboxes = [];
236
237 foreach ($mailboxesSuccessSynced as $mailboxId => $lastMailCheckData)
238 {
239 $timeBeforeNextSyncMailboxes[] = intval($lastMailCheckData['timeStarted']) + $this->mailCheckInterval - time();
240 }
241
242 return !empty($timeBeforeNextSyncMailboxes) && min($timeBeforeNextSyncMailboxes) > 0 ? min($timeBeforeNextSyncMailboxes) : 0;
243 }
244
249 {
250 $mailboxesIdsFailedToSync = array_keys($this->getFailedToSyncMailboxes());
251 return !empty($mailboxesIdsFailedToSync) && count($mailboxesIdsFailedToSync) > 0
252 ? (int)$mailboxesIdsFailedToSync[0]
253 : null;
254 }
255
256 public function getLastMailboxSyncIsSuccessStatus($mailboxId)
257 {
258 $mailboxesOptions = $this->getMailboxesSyncInfo();
259 if (!(isset($mailboxesOptions[$mailboxId]) && array_key_exists('isSuccess', $mailboxesOptions[$mailboxId])))
260 {
261 return null;
262 }
263 return $mailboxesOptions[$mailboxId]['isSuccess'];
264 }
265
266 public function getLastMailboxSyncTime($mailboxId)
267 {
268 $mailboxesOptions = $this->getMailboxesSyncInfo();
269 if (!(isset($mailboxesOptions[$mailboxId]) && array_key_exists('timeStarted', $mailboxesOptions[$mailboxId])))
270 {
271 return null;
272 }
273 return $mailboxesOptions[$mailboxId]['timeStarted'];
274 }
275}
setSyncStatus($mailboxId, $isSuccess, $time=null)
static getUserMailboxes($userId=null)
Definition mailbox.php:257
static createFromTimestamp($timestamp)
Definition datetime.php:246