Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
notification.php
1<?php
10
11use Bitrix\Main;
16
43{
44 protected $data;
45 protected $eventCount = 0;
46
51 public function __construct($id = 0)
52 {
53 if($id > 0)
54 {
55 $this->data = Internal\LogNotificationTable::wakeUpObject($id);
56 }
57 else
58 {
59 $this->data = Internal\LogNotificationTable::createObject();
60 }
61 }
62
66 public function fill()
67 {
68 if($this->data->state <> Objectify\State::RAW)
69 {
70 $this->data->fill(Fields\FieldTypeMask::SCALAR);
71 }
72 }
73
77 public function fillActions()
78 {
79 if($this->data->state <> Objectify\State::RAW)
80 {
81 $this->data->fillActions();
82 }
83 }
84
89 public function save()
90 {
91 //save to DB, including the actions collection
92 $result = $this->data->save();
93
94 if($result->isSuccess())
95 {
96 $agent = static::getAgentName($this->getId());
97
98 if($result instanceof Main\ORM\Data\UpdateResult)
99 {
100 \CAgent::RemoveAgent($agent, "main");
101 }
102 \CAgent::AddAgent($agent, "main", "N", $this->getCheckInterval()*60);
103 }
104
105 return $result;
106 }
107
112 public function delete()
113 {
114 $id = $this->getId();
115
116 $result = $this->data->delete();
117
118 if($result->isSuccess())
119 {
120 \CAgent::RemoveAgent(static::getAgentName($id), "main");
121 }
122
123 return $result;
124 }
125
130 public function setFromArray(array $values)
131 {
132 foreach($this->data->entity->getFields() as $fieldName => $field)
133 {
134 if(!isset($values[$fieldName]))
135 {
136 continue;
137 }
138 if($fieldName == "ID")
139 {
140 continue;
141 }
142 if(!($field instanceof Fields\ScalarField))
143 {
144 continue;
145 }
146
147 $value = $values[$fieldName];
148 if($field instanceof Fields\BooleanField)
149 {
150 $value = ($value == "Y");
151 }
152 $this->data->set($fieldName, $value);
153 }
154 }
155
160 public function setActionsFromArray(array $values)
161 {
162 //set the actions collection from the array
163 if($this->data->state <> Objectify\State::RAW)
164 {
165 $this->data->removeAllActions();
166 }
167 foreach($values as $postAction)
168 {
169 if($postAction["ID"] > 0)
170 {
171 $action = Internal\LogNotificationActionTable::wakeUpObject($postAction["ID"]);
172 }
173 else
174 {
175 $action = Internal\LogNotificationActionTable::createObject();
176 }
177 $action->setNotificationType($postAction["NOTIFICATION_TYPE"]);
178 $action->setRecipient($postAction["RECIPIENT"]);
179 $action->setAdditionalText($postAction["ADDITIONAL_TEXT"]);
180
181 $this->data->addToActions($action);
182 }
183 }
184
189 public function getActions()
190 {
191 $actions = [];
192 if(($collection = $this->data->getActions()))
193 {
194 foreach($collection as $record)
195 {
196 $actions[$record->getId()] = Action::createByType(
197 $record->getNotificationType(),
198 $record->getRecipient(),
199 $record->getAdditionalText()
200 );
201 }
202 }
203 return $actions;
204 }
205
213 public function __call($name, $arguments)
214 {
215 if(($first = substr($name, 0, 3)) == "get" || $first == "set")
216 {
218 return $this->data->__call($name, $arguments);
219 }
220
221 throw new Main\SystemException(sprintf(
222 'Unknown method `%s` for object `%s`', $name, get_called_class()
223 ));
224 }
225
230 public function setEventCount($n)
231 {
232 $this->eventCount = $n;
233 return $this;
234 }
235
239 public function getEventCount()
240 {
241 return $this->eventCount;
242 }
243
249 public static function checkConditions($id)
250 {
251 $notification = new static($id);
252 $notification->fill();
253
254 if($notification->getAuditTypeId() == '')
255 {
256 //nonexistent
257 return '';
258 }
259
260 $interval = intval($notification->getCheckInterval());
261 $dateInterval = new Main\Type\DateTime();
262 $dateInterval->add("-T{$interval}M");
263
264 $notification->setDateChecked(new Main\Type\DateTime());
265 $result = $notification->data->save();
266 if(!$result->isSuccess())
267 {
268 return '';
269 }
270
271 $filter = Query\Query::filter()
272 ->where("AUDIT_TYPE_ID", $notification->getAuditTypeId())
273 ->where("TIMESTAMP_X", ">", $dateInterval);
274
275 if($notification->getItemId() <> '')
276 {
277 $filter->whereLike("ITEM_ID", '%'.$notification->getItemId().'%');
278 }
279 if($notification->getUserId() > 0)
280 {
281 $filter->where("USER_ID", $notification->getUserId());
282 }
283 if($notification->getRemoteAddr() <> '')
284 {
285 $filter->whereLike("REMOTE_ADDR", '%'.$notification->getRemoteAddr().'%');
286 }
287 if($notification->getUserAgent() <> '')
288 {
289 $filter->whereLike("USER_AGENT", '%'.$notification->getUserAgent().'%');
290 }
291 if($notification->getRequestUri() <> '')
292 {
293 $filter->whereLike("REQUEST_URI", '%'.$notification->getRequestUri().'%');
294 }
295
297 ->addSelect(new Fields\ExpressionField('CNT', 'COUNT(1)'))
298 ->where($filter)
299 ->fetch();
300
301 if($eventCount["CNT"] >= $notification->getAlertCount())
302 {
303 //notification triggered
304 $notification->setEventCount($eventCount["CNT"]);
305 $notification->send();
306 }
307
308 return static::getAgentName($id);
309 }
310
314 public function send()
315 {
316 $this->fillActions();
317 foreach($this->getActions() as $action)
318 {
319 $action->send($this);
320 }
321 }
322
327 protected static function getAgentName($id)
328 {
329 return '\Bitrix\Main\EventLog\Notification::checkConditions('.$id.');';
330 }
331}
static createByType($type, $recipient, $text)
Definition action.php:41