Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
notification.php
1<?php
2
4
10{
11 public const TYPE_NOTICE = 'notice';
12 public const TYPE_ERROR = 'error';
13 public const TYPE_EXCEPTION = 'exception';
14
15 private $setting;
16 private $notificationList;
17 private $holdSaveToBase = false;
18
24 public function __construct(Setting $setting)
25 {
26 $this->setting = $setting;
27 $this->notificationList = $this->setting->get(Setting::SETTING_NOTICE_COLLECTION) ?? [];
28 }
29
36 public function save($result): bool
37 {
38 $this->holdSaveToBase = true;
39
40 if (isset($result['ERROR_ACTION']) && $result['ERROR_ACTION'])
41 {
42 $this->add($result['ERROR_ACTION'], '', self::TYPE_NOTICE);
43 }
44
45 if (isset($result['ERROR_MESSAGES']) && $result['ERROR_MESSAGES'])
46 {
47 $this->add($result['ERROR_MESSAGES'], '', self::TYPE_ERROR);
48 }
49
50 if (isset($result['ERROR_EXCEPTION']) && $result['ERROR_EXCEPTION'])
51 {
52 $this->add($result['ERROR_EXCEPTION'], '', self::TYPE_EXCEPTION);
53 }
54
55 $this->holdSaveToBase = false;
56
57 return $this->setting->set(Setting::SETTING_NOTICE_COLLECTION, $this->notificationList);
58 }
59
68 public function add($message, $code, $type): bool
69 {
70 if (is_array($message))
71 {
72 foreach ($message as $mess)
73 {
74 $this->notificationList[] = [
75 'code' => $code,
76 'message' => $mess,
77 'type' => $type,
78 ];
79 }
80 }
81 else
82 {
83 $this->notificationList[] = [
84 'code' => $code,
85 'message' => $message,
86 'type' => $type,
87 ];
88 }
89
90 return
91 !$this->holdSaveToBase
92 && $this->setting->set(
94 $this->notificationList
95 );
96 }
97
104 public function list(array $filter = []): ?array
105 {
106 $result = $this->notificationList;
107
108 if ($filter['type'] !== null)
109 {
110 foreach ($result as $key => $item)
111 {
112 if ($item['type'] !== $filter['type'])
113 {
114 unset($result[$key]);
115 }
116 }
117 }
118
119 return $result;
120 }
121
126 public function clean(): bool
127 {
128 return $this->setting->delete(Setting::SETTING_NOTICE_COLLECTION);
129 }
130}