Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
event.php
1<?php
9
10use Bitrix\Main;
12
13class Event
14{
15 const ERR_SITE = 'site';
16 const ERR_TEMPLATES = 'templates';
17 const ERR_MODULE = 'module';
18
19 protected $eventName;
20 protected $fields;
21 protected $siteId;
22 protected $languageId;
23 protected $templateId;
24
30 public function __construct($eventName, array $fields = [])
31 {
32 $this->eventName = $eventName;
33 $this->fields = $fields;
34 }
35
40 public function setSite($siteId)
41 {
42 $this->siteId = $siteId;
43 return $this;
44 }
45
50 public function setLanguage($languageId)
51 {
52 $this->languageId = $languageId;
53 return $this;
54 }
55
60 public function setTemplate($templateId)
61 {
62 $this->templateId = $templateId;
63 return $this;
64 }
65
70 public function send($directly = false)
71 {
72 $result = new Main\Result();
73
74 if(!Main\Loader::includeModule("messageservice"))
75 {
76 $result->addError(new Main\Error("Module messageservice is not installed.", self::ERR_MODULE));
77 return $result;
78 }
79
80 $senderId = Main\Config\Option::get("main", "sms_default_service");
81 if($senderId == '')
82 {
83 //messageservice will try to use any available sender
84 $senderId = null;
85 }
86
87 $messageListResult = $this->createMessageList();
88 if (!$messageListResult->isSuccess())
89 {
90 return $result->addErrors($messageListResult->getErrors());
91 }
92 $messageList = $messageListResult->getData();
93
94 foreach($messageList as $message)
95 {
96 $smsMessage = \Bitrix\MessageService\Sender\SmsManager::createMessage([
97 'SENDER_ID' => $senderId,
98 'MESSAGE_FROM' => $message->getSender(),
99 'MESSAGE_TO' => $message->getReceiver(),
100 'MESSAGE_BODY' => $message->getText(),
101 ]);
102
103 $event = new Main\Event('main', 'onBeforeSendSms', [
104 'message' => $smsMessage,
105 'template' => $message->getTemplate()
106 ]);
107 $event->send();
108 foreach($event->getResults() as $evenResult)
109 {
110 if($evenResult->getType() === Main\EventResult::ERROR)
111 {
112 continue 2;
113 }
114 }
115
116 if($directly)
117 {
118 $smsResult = $smsMessage->sendDirectly();
119 }
120 else
121 {
122 $smsResult = $smsMessage->send();
123 }
124
125 if(!$smsResult->isSuccess())
126 {
127 $result->addErrors($smsResult->getErrors());
128 }
129 }
130
131 return $result;
132 }
133
137 public function createMessageList(): Main\Result
138 {
139 $result = new Main\Result();
140 $context = Main\Context::getCurrent();
141
142 if($this->siteId === null)
143 {
144 $this->siteId = $context->getSite();
145 if($this->siteId === null)
146 {
147 $result->addError(new Main\Error("Can't filter templates, the siteId is not set.", self::ERR_SITE));
148 return $result;
149 }
150 }
151
152 if($this->languageId === null)
153 {
154 $this->languageId = $context->getLanguage();
155 }
156
157 $templates = $this->fetchTemplates();
158 if($templates->isEmpty())
159 {
160 $result->addError(new Main\Error("Templates not found.", self::ERR_TEMPLATES));
161 return $result;
162 }
163
164 $messageList = [];
165 foreach($templates as $template)
166 {
167 $messageList[] = Message::createFromTemplate($template, $this->fields);
168 }
169 $result->setData($messageList);
170
171 return $result;
172 }
173
177 protected function fetchTemplates()
178 {
179 $filter = Query::filter()
180 ->where("ACTIVE", "Y")
181 ->where("SITES.LID", $this->siteId);
182
183 if($this->templateId !== null)
184 {
185 //specific template was supplied
186 $filter->where("ID", $this->templateId);
187 }
188 else
189 {
190 //select templates by conditions
191 $filter->where("EVENT_NAME", $this->eventName);
192
193 if($this->languageId !== null)
194 {
195 $filter->where(Query::filter()
196 ->logic('or')
197 ->where('LANGUAGE_ID', $this->languageId)
198 ->where('LANGUAGE_ID', '')
199 ->whereNull('LANGUAGE_ID')
200 );
201 }
202 }
203
204 $res = TemplateTable::getList([
205 'select' => ['*', 'SITES.SITE_NAME', 'SITES.SERVER_NAME', 'SITES.LID'],
206 'filter' => $filter,
207 ]);
208
209 return $res->fetchCollection();
210
211 }
212}
static includeModule($moduleName)
Definition loader.php:69
setLanguage($languageId)
Definition event.php:50
send($directly=false)
Definition event.php:70
__construct($eventName, array $fields=[])
Definition event.php:30
setTemplate($templateId)
Definition event.php:60