Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventmanager.php
1<?php
8namespace Bitrix\Main\Mail;
9
10use Bitrix\Main;
11
13{
17 public static function checkEvents()
18 {
19 if(
20 (defined("DisableEventsCheck") && DisableEventsCheck === true)
21 ||
22 (
23 defined("BX_CRONTAB_SUPPORT") && BX_CRONTAB_SUPPORT === true
24 &&
25 (!defined("BX_CRONTAB") || BX_CRONTAB !== true)
26 )
27 )
28 {
29 return null;
30 }
31
32 $manage_cache = Main\Application::getInstance()->getManagedCache();
33 if(CACHED_b_event !== false && $manage_cache->read(CACHED_b_event, "events"))
34 return "";
35
36 return static::executeEvents();
37 }
38
44 public static function executeEvents()
45 {
46 $manage_cache = Main\Application::getInstance()->getManagedCache();
47
48 $bulk = intval(Main\Config\Option::get("main", "mail_event_bulk", 5));
49 if($bulk <= 0)
50 $bulk = 5;
51
52 $rsMails = null;
53
54 $connection = Main\Application::getConnection();
55 $helper = $connection->getSqlHelper();
56
57 $strSql= "SELECT 'x' FROM b_event WHERE SUCCESS_EXEC='N' LIMIT 1";
58 $resultEventDb = $connection->query($strSql);
59 if($resultEventDb->fetch())
60 {
61 if(!$connection->lock('event'))
62 return "";
63 }
64 else
65 {
66 if(CACHED_b_event!==false)
67 $manage_cache->set("events", true);
68
69 return "";
70 }
71
72 $strSql = "
73 SELECT ID, C_FIELDS, EVENT_NAME, MESSAGE_ID, LID,
74 " . $helper->formatDate('DD.MM.YYYY HH24:MI:SS', 'DATE_INSERT') . " as DATE_INSERT,
75 DUPLICATE, LANGUAGE_ID
76 FROM b_event
77 WHERE SUCCESS_EXEC='N'
78 ORDER BY ID
79 LIMIT ".$bulk;
80
81 $rsMails = $connection->query($strSql);
82
83 if ($rsMails)
84 {
85 $arCallableModificator = array();
86 $cnt = 0;
87 foreach(Internal\EventTable::getFetchModificatorsForFieldsField() as $callableModificator)
88 {
89 if(is_callable($callableModificator))
90 {
91 $arCallableModificator[] = $callableModificator;
92 }
93 }
94 while($arMail = $rsMails->fetch())
95 {
96 foreach($arCallableModificator as $callableModificator)
97 $arMail['C_FIELDS'] = call_user_func_array($callableModificator, array($arMail['C_FIELDS']));
98
99 $arFiles = array();
100 $fileListDb = Internal\EventAttachmentTable::getList(array(
101 'select' => array('FILE_ID'),
102 'filter' => array('=EVENT_ID' => $arMail["ID"])
103 ));
104 while($file = $fileListDb->fetch())
105 {
106 $arFiles[] = $file['FILE_ID'];
107 }
108 $arMail['FILE'] = $arFiles;
109
110 if(!is_array($arMail['C_FIELDS']))
111 {
112 $arMail['C_FIELDS'] = array();
113 }
114 try
115 {
116 $flag = Event::handleEvent($arMail);
117 Internal\EventTable::update($arMail["ID"], array('SUCCESS_EXEC' => $flag, 'DATE_EXEC' => new Main\Type\DateTime));
118 }
119 catch (\Exception $e)
120 {
121 Internal\EventTable::update($arMail["ID"], array('SUCCESS_EXEC' => "E", 'DATE_EXEC' => new Main\Type\DateTime));
122
123 $application = Main\Application::getInstance();
124 $exceptionHandler = $application->getExceptionHandler();
125 $exceptionHandler->writeToLog($e);
126
127 break;
128 }
129
130 $cnt++;
131 if($cnt >= $bulk)
132 break;
133 }
134 }
135
136 $connection->unlock('event');
137
138 if($cnt === 0 && CACHED_b_event !== false)
139 $manage_cache->set("events", true);
140
141 return null;
142 }
143
148 public static function cleanUpAgent()
149 {
150 $period = abs(intval(Main\Config\Option::get("main", "mail_event_period", 14)));
151 $periodInSeconds = $period * 24 * 3600;
152
153 $connection = Main\Application::getConnection();
154 $datetime = $connection->getSqlHelper()->addSecondsToDateTime('-' . $periodInSeconds);
155
156 $strSql = "DELETE FROM b_event WHERE DATE_EXEC <= " . $datetime;
157 $connection->query($strSql);
158
159 $strSql = "DELETE FROM b_event_attachment "
160 . " WHERE IS_FILE_COPIED='N'"
161 . " AND NOT EXISTS(SELECT e.ID FROM b_event e WHERE e.ID=EVENT_ID)";
162 $connection->query($strSql);
163
164 \CAgent::addAgent(
165 self::class . '::cleanUpAttachmentAgent();',
166 'main',
167 "N",
168 60,
169 "",
170 "Y"
171 );
172
173 return "CEvent::CleanUpAgent();";
174 }
175
181 public static function cleanUpAttachmentAgent()
182 {
183 $connection = Main\Application::getConnection();
184 $rows = Internal\EventAttachmentTable::getList([
185 'select' => ['EVENT_ID', 'FILE_ID'],
186 'filter' => [
187 '=IS_FILE_COPIED' => 'Y',
188 '=EVENT.ID' => null,
189 ],
190 'limit' => 5
191 ])->fetchAll();
192 foreach ($rows as $row)
193 {
194 \CFile::Delete($row['FILE_ID']);
195 $strSql = "DELETE FROM b_event_attachment "
196 . " WHERE EVENT_ID=" . intval($row['EVENT_ID'])
197 . " AND FILE_ID=" . intval($row['FILE_ID']);
198 $connection->query($strSql);
199 }
200
201 return !empty($rows) ? self::class . '::cleanUpAttachmentAgent();' : '';
202 }
203
210 public static function onMailEventSubscriptionList(array $data)
211 {
212 $row = Internal\BlacklistTable::getRow([
213 'select' => ['ID'],
214 'filter' => ['=CODE' => $data['FIELDS']['CODE']]
215 ]);
216 if ($row)
217 {
218 return [];
219 }
220
221 return [
222 [
223 'ID' => 'main/mail/event',
224 'NAME' => 'Mail events',
225 'DESC' => '',
226 'SELECTED' => true,
227 ]
228 ];
229 }
230
237 public static function onMailEventSubscriptionDisable(array $data)
238 {
239 if (empty($data['FIELDS']) || empty($data['FIELDS']['CODE']))
240 {
241 return false;
242 }
243
244 $code = mb_strtolower(trim($data['FIELDS']['CODE']));
245 if (!check_email($code))
246 {
247 return false;
248 }
249
250 return Internal\BlacklistTable::add([
251 'CODE' => $code,
252 'CATEGORY_ID' => Internal\BlacklistTable::CategoryManual,
253 'DATE_INSERT' => new Main\Type\DateTime()
254 ])->isSuccess();
255 }
256}
static onMailEventSubscriptionList(array $data)
static onMailEventSubscriptionDisable(array $data)