Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mailingtrigger.php
1<?php
8namespace Bitrix\Sender;
9
14
15Loc::loadMessages(__FILE__);
16
34{
40 public static function getTableName()
41 {
42 return 'b_sender_mailing_trigger';
43 }
44
50 public static function getMap()
51 {
52 return array(
53
54 'MAILING_CHAIN_ID' => array(
55 'data_type' => 'integer',
56 'primary' => true,
57 'required' => true,
58 ),
59 'NAME' => array(
60 'data_type' => 'string',
61 ),
62 'EVENT' => array(
63 'data_type' => 'string',
64 'primary' => true,
65 'required' => true,
66 ),
67 'IS_TYPE_START' => array(
68 'data_type' => 'boolean',
69 'values' => array(false, true),
70 'primary' => true,
71 'required' => true,
72 ),
73 'ENDPOINT' => array(
74 'data_type' => 'text',
75 'serialized' => true,
76 'required' => true,
77 ),
78 'MAILING_CHAIN' => array(
79 'data_type' => 'Bitrix\Sender\MailingChainTable',
80 'reference' => array('=this.MAILING_CHAIN_ID' => 'ref.ID'),
81 ),
82 );
83 }
84
85
92 public static function onAfterAdd(Entity\Event $event)
93 {
94 $result = new Entity\EventResult;
95 $data = $event->getParameters();
96
97 if(is_string($data['fields']['ENDPOINT']))
98 {
99 $data['fields']['ENDPOINT'] = unserialize($data['fields']['ENDPOINT'], ['allowed_classes' => false]);
100 }
101 if(!is_array($data['fields']['ENDPOINT']))
102 {
103 $data['fields']['ENDPOINT'] = null;
104 }
105 static::actualizeHandlers($data['primary']['MAILING_CHAIN_ID'], $data['fields']['ENDPOINT'], null);
106
107 return $result;
108 }
109
116 public static function onUpdate(Entity\Event $event)
117 {
118 $result = new Entity\EventResult;
119 $data = $event->getParameters();
120
121 $itemDb = static::getByPrimary($data['primary']);
122 $item = $itemDb->fetch();
123
124 if(!is_array($data['fields']['ENDPOINT']))
125 {
126 $data['fields']['ENDPOINT'] = null;
127 }
128 if(!$item || !is_array($item['ENDPOINT']))
129 {
130 $item['ENDPOINT'] = null;
131 }
132 static::actualizeHandlers($data['primary']['MAILING_CHAIN_ID'], $data['fields']['ENDPOINT'], $item['ENDPOINT']);
133
134 return $result;
135 }
136
143 public static function onDelete(Entity\Event $event)
144 {
145 $result = new Entity\EventResult;
146 $data = $event->getParameters();
147
148 $itemDb = static::getByPrimary($data['primary']);
149 while($item = $itemDb->fetch())
150 {
151 if(!is_array($item['ENDPOINT']))
152 {
153 $item['ENDPOINT'] = null;
154 }
155
156 static::actualizeHandlers($item['MAILING_CHAIN_ID'], null, $item['ENDPOINT']);
157 }
158
159 return $result;
160 }
161
169 public static function actualizeHandlers($chainId, array $fieldsNew = null, array $fieldsOld = null)
170 {
171 $settingsNew = null;
172 $settingsOld = null;
173
174 if($fieldsNew)
175 $settingsNew = new Trigger\Settings($fieldsNew);
176 if($fieldsOld)
177 $settingsOld = new Trigger\Settings($fieldsOld);
178
179
180 // if old item was closed trigger
181 if($settingsOld && $settingsOld->isClosedTrigger())
182 {
183 // delete agent
184 $agentName = Trigger\Manager::getClosedEventAgentName(
185 $settingsOld->getEventModuleId(),
186 $settingsOld->getEventType(),
187 $chainId
188 );
189
190 $agent = new \CAgent;
191 $agentListDb = $agent->GetList(array(), array('MODULE_ID' => 'sender', 'NAME' => $agentName));
192 while($agentItem = $agentListDb->Fetch())
193 $agent->Delete($agentItem['ID']);
194 }
195
196
197 // if new item is closed trigger
198 if($settingsNew && $settingsNew->isClosedTrigger())
199 {
200 // check active state of mailing
201 $chainDb = MailingChainTable::getList(array(
202 'select' => array('ID'),
203 'filter' => array('=ID' => $chainId, '=MAILING.ACTIVE' => 'Y')
204 ));
205 if(!$chainDb->fetch())
206 return;
207
208 // add new agent
209 $agentName = Trigger\Manager::getClosedEventAgentName(
210 $settingsNew->getEventModuleId(),
211 $settingsNew->getEventType(),
212 $chainId
213 );
214
215 // set date of next exec
216 $agentTime = $settingsNew->getClosedTriggerTime();
217 $agentInterval = $settingsNew->getClosedTriggerInterval();
218 if($agentInterval <= 0) $agentInterval = 1440;
219
220 $agentTimeArray = explode(":", $agentTime);
221 $agentDate = new DateTime;
222 $agentDate->setTime((int)$agentTimeArray[0], (int)$agentTimeArray[1]);
223
224 // set next exec on next day if exec was today
225 if($agentDate->getTimestamp() < time())
226 $agentDate->add("1 days");
227
228 // add agent
229 $agent = new \CAgent;
230 $agent->AddAgent($agentName, 'sender', 'N', $agentInterval*60, '', 'Y', $agentDate->toString());
231
232 return;
233 }
234
235 // actualize deleted/changed event
236 if($settingsOld && !$settingsOld->isClosedTrigger() && $settingsOld->getFullEventType())
237 {
238 // if delete operation(no the NEW)
239 // or change operation(the NEW is not equal to the OLD)
240 if(!$settingsNew || $settingsOld->getFullEventType() != $settingsNew->getFullEventType())
241 {
242 Trigger\Manager::actualizeHandler(array(
243 'MODULE_ID' => $settingsOld->getEventModuleId(),
244 'EVENT_TYPE' => $settingsOld->getEventType(),
245 'CALLED_BEFORE_CHANGE' => true
246 ));
247 }
248 }
249
250 // actualize new event
251 if($settingsNew && $settingsNew->getFullEventType())
252 {
253 $calledBeforeChange = ($fieldsOld ? false : true);
254 Trigger\Manager::actualizeHandler(array(
255 'MODULE_ID' => $settingsNew->getEventModuleId(),
256 'EVENT_TYPE' => $settingsNew->getEventType(),
257 'CALLED_BEFORE_CHANGE' => $calledBeforeChange
258 ));
259 }
260 }
261
262
270 public static function deleteList(array $filter)
271 {
272 $entity = static::getEntity();
273 $connection = $entity->getConnection();
274
275 \CTimeZone::disable();
276 $sql = sprintf(
277 'DELETE FROM %s WHERE %s',
278 $connection->getSqlHelper()->quote($entity->getDbTableName()),
279 Query::buildFilterSql($entity, $filter)
280 );
281 $res = $connection->query($sql);
282 \CTimeZone::enable();
283
284 return $res;
285 }
286}
static loadMessages($file)
Definition loc.php:64
setTime($hour, $minute, $second=0, $microseconds=0)
Definition datetime.php:144
static onDelete(Entity\Event $event)
static onUpdate(Entity\Event $event)
static onAfterAdd(Entity\Event $event)
static actualizeHandlers($chainId, array $fieldsNew=null, array $fieldsOld=null)