Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
blacklist.php
1<?php
2
3namespace Bitrix\Mail;
4
10
11Localization\Loc::loadMessages(__FILE__);
12
29class BlacklistTable extends Entity\DataManager
30{
31
32 public static function getFilePath()
33 {
34 return __FILE__;
35 }
36
37 public static function getTableName()
38 {
39 return 'b_mail_blacklist';
40 }
41
45 public static function getObjectClass()
46 {
47 return BlacklistEmail::class;
48 }
49
50 public static function getMap()
51 {
52 return [
53 'ID' => [
54 'data_type' => 'integer',
55 'primary' => true,
56 'autocomplete' => true,
57 ],
58 'SITE_ID' => [
59 'data_type' => 'string',
60 'required' => true,
61 ],
62 'MAILBOX_ID' => [
63 'data_type' => 'integer',
64 ],
65 'USER_ID' => [
66 'data_type' => 'integer',
67 ],
68 'ITEM_TYPE' => [
69 'data_type' => 'integer',
70 'required' => true,
71 ],
72 'ITEM_VALUE' => [
73 'data_type' => 'string',
74 'required' => true,
75 'fetch_data_modification' => function ()
76 {
77 return [
78 function ($value, $query, $data)
79 {
80 if (Blacklist\ItemType::DOMAIN == $data['ITEM_TYPE'])
81 {
82 $value = sprintf('@%s', $value);
83 }
84
85 return $value;
86 },
87 ];
88 },
89 ],
90 ];
91 }
92
93 public static function replace($siteId, $mailboxId, array $list)
94 {
95 global $DB;
96
97 if ($mailboxId > 0)
98 {
99 $DB->query(sprintf("DELETE FROM b_mail_blacklist WHERE MAILBOX_ID = %u", $mailboxId));
100 }
101 else
102 {
103 $DB->query(sprintf("DELETE FROM b_mail_blacklist WHERE SITE_ID = '%s' AND MAILBOX_ID = 0", $DB->forSql($siteId)));
104 }
105
106 if (!empty($list))
107 {
108 foreach ($list as $item)
109 {
110 static::add([
111 'SITE_ID' => $siteId,
112 'MAILBOX_ID' => $mailboxId,
113 'ITEM_TYPE' => Blacklist\ItemType::resolveByValue($item),
114 'ITEM_VALUE' => $item,
115 ]);
116 }
117 }
118 }
119
126 public static function addMailsBatch(array $list, $userId = null)
127 {
128 if (empty($list))
129 {
130 return 0;
131 }
132 if (is_null($userId))
133 {
134 $userId = 0;
135 }
136 $sqlHelper = Application::getConnection()->getSqlHelper();
137 $addList = [];
138 foreach ($list as $index => $item)
139 {
140 $itemToAdd = [
141 'SITE_ID' => SITE_ID,
142 'MAILBOX_ID' => 0,
143 'USER_ID' => $userId,
144 'ITEM_TYPE' => Blacklist\ItemType::resolveByValue($item),
145 'ITEM_VALUE' => $item,
146 ];
147 $addList[] = $itemToAdd;
148 }
149
150 if (count($addList) === 0)
151 {
152 return 0;
153 }
154 $keys = implode(', ', array_keys(current($addList)));
155 $values = [];
156 foreach ($addList as $item)
157 {
158 $values[] = implode(
159 ", ",
160 [
161 "'" . $sqlHelper->forSql($item['SITE_ID']) . "'",
162 (int)$item['MAILBOX_ID'],
163 (int)$item['USER_ID'],
164 $item['ITEM_TYPE'],
165 "'" . $sqlHelper->forSql($item['ITEM_VALUE']) . "'",
166 ]
167 );
168 }
169 $values = implode('), (', $values);
170
171 $tableName = static::getTableName();
172 $sql = $sqlHelper->getInsertIgnore($tableName, "($keys)", " VALUES($values)");
173 Application::getConnection()->query($sql);
174 return Application::getConnection()->getAffectedRowsCount();
175 }
176
184 public static function getUserAddressesListQuery($userId, $includeAddressesForAllUsers = true)
185 {
186 $filter = ['LOGIC' => 'OR'];
187 $userMailboxes = \Bitrix\Mail\MailboxTable::getUserMailboxes();
188 if (!empty($userMailboxes))
189 {
190 $mailboxesIds = array_column($userMailboxes, 'ID');
191 if ($includeAddressesForAllUsers)
192 {
193 $mailboxesIds[] = 0;
194 }
195 $filter[] = [
196 '@MAILBOX_ID' => $mailboxesIds,
197 '=USER_ID' => 0,
198 ];
199 }
200 $newStyleAddressesFilter = [];
201 $userIds = [$userId];
202 $newStyleAddressesFilter['=MAILBOX_ID'] = 0;
203
204 if ($includeAddressesForAllUsers)
205 {
206 $userIds[] = 0;
207 }
208 $newStyleAddressesFilter['@USER_ID'] = $userIds;
209 $mailsQuery = \Bitrix\Mail\BlacklistTable::query()
210 ->addSelect('*');
211 $filter[] = $newStyleAddressesFilter;
212 return $mailsQuery->setFilter([$filter]);
213 }
214
215 public static function deleteList($filter)
216 {
217 $entity = static::getEntity();
218 $connection = $entity->getConnection();
219 return $connection->query(sprintf(
220 'DELETE FROM %s WHERE %s',
221 $connection->getSqlHelper()->quote($entity->getDbTableName()),
222 Query::buildFilterSql(
223 $entity,
224 $filter
225 )
226 ));
227 }
228}
static deleteList($filter)
static getUserAddressesListQuery($userId, $includeAddressesForAllUsers=true)
static addMailsBatch(array $list, $userId=null)
static replace($siteId, $mailboxId, array $list)
Definition blacklist.php:93
static getConnection($name="")