Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mailboxdirectory.php
1<?php
2
3namespace Bitrix\Mail;
4
7use Bitrix\Main\Entity\ExpressionField;
8use Bitrix\Main\Entity\Query;
9
11{
12 public static function fetchAllDirsTypes($mailboxId)
13 {
15 'filter' => [
16 'LOGIC' => 'AND',
17 '=MAILBOX_ID' => $mailboxId,
18 [
19 'LOGIC' => 'OR',
20 '=IS_OUTCOME' => MailboxDirectoryTable::ACTIVE,
21 '=IS_TRASH' => MailboxDirectoryTable::ACTIVE,
23 ]
24 ],
25 'select' => ['*'],
26 'order' => ['LEVEL' => 'ASC']
27 ])->fetchCollection();
28 }
29
30 public static function fetchOneLevelByParentId($mailboxId, $id, $level)
31 {
33 'filter' => [
34 'LOGIC' => 'AND',
35 '=MAILBOX_ID' => $mailboxId,
36 '=PARENT_ID' => $id,
37 '=LEVEL' => $level,
38 ],
39 'select' => ['*']
40 ]);
41
42 $result = [];
43
44 while ($row = $query->fetchObject())
45 {
46 $result[$row->getPath()] = $row;
47 }
48
49 return $result;
50 }
51
52 public static function fetchAllLevelByParentId($mailboxId, $path, $level)
53 {
55 'filter' => [
56 'LOGIC' => 'AND',
57 '=MAILBOX_ID' => $mailboxId,
58 '%=PATH' => $path,
59 '>=LEVEL' => $level,
60 ],
61 'select' => ['*'],
62 'order' => ['LEVEL' => 'ASC']
63 ]);
64
65 $result = [];
66
67 while ($row = $query->fetchObject())
68 {
69 $result[$row->getPath()] = $row;
70 }
71
72 return $result;
73 }
74
75 public static function fetchOneByMailboxIdAndHash($mailboxId, $hash)
76 {
78 'filter' => [
79 '=MAILBOX_ID' => $mailboxId,
80 '=DIR_MD5' => $hash
81 ]
82 ])->fetchObject();
83 }
84
85 public static function fetchOneOutcome($mailboxId)
86 {
88 'filter' => [
89 '=MAILBOX_ID' => $mailboxId,
90 '=IS_OUTCOME' => MailboxDirectoryTable::ACTIVE
91 ]
92 ])->fetchObject();
93 }
94
95 public static function fetchTrashAndSpamHash($mailboxId)
96 {
98 'filter' => [
99 'LOGIC' => 'AND',
100 '=MAILBOX_ID' => $mailboxId,
101 [
102 'LOGIC' => 'OR',
103 '=IS_TRASH' => MailboxDirectoryTable::ACTIVE,
104 '=IS_SPAM' => MailboxDirectoryTable::ACTIVE,
105 ]
106 ]
107 ]);
108
109 $result = [];
110
111 while ($row = $query->fetch())
112 {
113 $result[] = $row['DIR_MD5'];
114 }
115
116 return $result;
117 }
118
119 public static function fetchOneById($id)
120 {
122 'filter' => [
123 '=ID' => $id
124 ]
125 ])->fetchObject();
126 }
127
128 public static function fetchOneByHash($mailboxId, $hash)
129 {
131 'filter' => [
132 '=MAILBOX_ID' => $mailboxId,
133 '=DIR_MD5' => $hash
134 ]
135 ])->fetchObject();
136 }
137
138 public static function updateSync($id, $val)
139 {
141 $id,
142 [
143 'IS_SYNC' => $val
144 ]
145 );
146 }
147
148 public static function resetDirsTypes($mailboxId, $type)
149 {
151 $connection = $entity->getConnection();
152
153 $query = sprintf(
154 'UPDATE %s SET %s WHERE %s',
155 $connection->getSqlHelper()->quote($entity->getDbTableName()),
156 $connection->getSqlHelper()->prepareUpdate($entity->getDbTableName(), [
158 ])[0],
159 Query::buildFilterSql(
160 $entity,
161 [
162 'MAILBOX_ID' => $mailboxId,
164 ]
165 )
166 );
167
168 return $connection->query($query);
169 }
170
171 public static function update($id, $data)
172 {
173 return MailboxDirectoryTable::update($id, $data);
174 }
175
176 public static function add(array $data)
177 {
178 return MailboxDirectoryTable::add($data);
179 }
180
181 public static function addMulti($rows, $ignoreEvents = false)
182 {
183 return MailboxDirectoryTable::addMulti($rows, $ignoreEvents);
184 }
185
186 public static function deleteList(array $filter)
187 {
189 $connection = $entity->getConnection();
190
191 return $connection->query(sprintf(
192 'DELETE FROM %s WHERE %s',
193 $connection->getSqlHelper()->quote($entity->getDbTableName()),
194 Query::buildFilterSql($entity, $filter)
195 ));
196 }
197
198 public static function updateSyncDirs(array $values, $val, $mailboxId)
199 {
201 $connection = $entity->getConnection();
202
203 return $connection->query(sprintf(
204 "UPDATE %s SET %s WHERE %s",
205 $connection->getSqlHelper()->quote($entity->getDbTableName()),
206 $connection->getSqlHelper()->prepareUpdate($entity->getDbTableName(), [
207 'IS_SYNC' => $val,
208 ])[0],
209 Query::buildFilterSql(
210 $entity,
211 [
212 '=MAILBOX_ID' => $mailboxId,
213 '@DIR_MD5' => $values,
214 'IS_DISABLED' => MailboxDirectoryTable::INACTIVE,
215 ]
216 )
217 ));
218 }
219
220 public static function fetchAll($mailboxId)
221 {
223 'filter' => [
224 '=MAILBOX_ID' => $mailboxId
225 ],
226 'select' => ['*'],
227 /*
228 When assembling directories, we look for their parents.
229 Sorting ensures that for a directories that parents are processed first,
230 and for children, matching parents are always found from those processed.
231 */
232 'order' => ['LEVEL' => 'ASC']
233 ]);
234
235 $result = [];
236
237 while ($row = $query->fetchObject())
238 {
239 $result[$row->getPath()] = $row;
240 }
241
242 return $result;
243 }
244
245 public static function fetchAllSyncDirs($mailboxId)
246 {
248 'filter' => [
249 '=MAILBOX_ID' => $mailboxId,
250 '=IS_SYNC' => MailboxDirectoryTable::ACTIVE,
251 '=IS_DISABLED' => MailboxDirectoryTable::INACTIVE
252 ],
253 'select' => ['*'],
254 'order' => ['LEVEL' => 'ASC']
255 ])->fetchCollection();
256 }
257
258 public static function fetchAllDisabledDirs($mailboxId)
259 {
261 'filter' => [
262 '=MAILBOX_ID' => $mailboxId,
263 '=IS_DISABLED' => MailboxDirectoryTable::ACTIVE
264 ],
265 'select' => ['*'],
266 'order' => ['ID' => 'ASC']
267 ])->fetchCollection();
268 }
269
270 public static function countMessagesSyncDirs($mailboxId)
271 {
273 'filter' => [
274 '=MAILBOX_ID' => $mailboxId,
275 '=IS_SYNC' => MailboxDirectoryTable::ACTIVE,
276 '=IS_DISABLED' => MailboxDirectoryTable::INACTIVE
277 ],
278 'select' => ['CNT'],
279 'runtime' => [
280 new ExpressionField('CNT', 'SUM(%s)', 'MESSAGE_COUNT'),
281 ]
282 ])->fetch();
283
284 return (int)$counter['CNT'];
285 }
286
287 public static function getMinSyncTime($mailboxId)
288 {
290 'filter' => [
291 '=MAILBOX_ID' => $mailboxId,
292 '=IS_SYNC' => MailboxDirectoryTable::ACTIVE,
293 ],
294 'select' => ['MIN_SYNC_TIME'],
295 'runtime' => [
296 new ExpressionField('MIN_SYNC_TIME', 'MIN(COALESCE(%s, 0))', 'SYNC_TIME'),
297 ]
298 ])->fetch();
299
300 return (int)$res['MIN_SYNC_TIME'];
301 }
302
303 public static function countSyncDirs($mailboxId)
304 {
306 'filter' => [
307 '=MAILBOX_ID' => $mailboxId,
308 '=IS_SYNC' => MailboxDirectoryTable::ACTIVE,
309 '=IS_DISABLED' => MailboxDirectoryTable::INACTIVE
310 ],
311 'select' => ['CNT'],
312 'runtime' => [
313 new ExpressionField('CNT', 'COUNT(*)'),
314 ]
315 ])->fetch();
316
317 return (int)$counter['CNT'];
318 }
319
320 public static function updateMessageCount($id, $val)
321 {
323 $id,
324 [
325 'MESSAGE_COUNT' => $val
326 ]
327 );
328 }
329
330 public static function updateFlags($id, $flags)
331 {
333 $id,
334 [
335 'FLAGS' => $flags
336 ]
337 );
338 }
339
340 public static function updateSyncTime($id, $val)
341 {
343 $id,
344 [
345 'SYNC_TIME' => $val
346 ]
347 );
348 }
349
350 public static function setSyncLock(int $id, int $time)
351 {
353 $connection = $entity->getConnection();
354
355 $query = sprintf(
356 "UPDATE %s SET %s WHERE %s",
357 $connection->getSqlHelper()->quote($entity->getDbTableName()),
358 $connection->getSqlHelper()->prepareUpdate($entity->getDbTableName(), [
359 'SYNC_LOCK' => $time,
360 ])[0],
361 Query::buildFilterSql(
362 $entity,
363 [
364 '=ID' => $id,
365 [
366 'LOGIC' => 'OR',
367 '=SYNC_LOCK' => 'IS NULL',
368 '<SYNC_LOCK' => time() - Mailbox::getTimeout(),
369 ]
370 ]
371 )
372 );
373
374 $connection->query($query);
375 $count = $connection->getAffectedRowsCount();
376
377 return $count;
378 }
379}
static fetchOneOutcome($mailboxId)
static fetchAllLevelByParentId($mailboxId, $path, $level)
static deleteList(array $filter)
static fetchTrashAndSpamHash($mailboxId)
static fetchOneByHash($mailboxId, $hash)
static resetDirsTypes($mailboxId, $type)
static addMulti($rows, $ignoreEvents=false)
static fetchAllDisabledDirs($mailboxId)
static countMessagesSyncDirs($mailboxId)
static fetchAllSyncDirs($mailboxId)
static fetchOneLevelByParentId($mailboxId, $id, $level)
static fetchAllDirsTypes($mailboxId)
static updateSyncDirs(array $values, $val, $mailboxId)
static setSyncLock(int $id, int $time)
static fetchOneByMailboxIdAndHash($mailboxId, $hash)
static getList(array $parameters=array())
static addMulti($rows, $ignoreEvents=false)
static update($primary, array $data)