31 private $typeId =
null;
34 private $includeTriggers =
true;
40 private $templateCounter = 0;
59 $this->messageCode = Message\iBase::CODE_MAIL;
71 $this->messageCode = $messageCode;
83 $this->version = $version;
95 $this->typeId = $typeId;
119 $this->includeTriggers = $include;
128 public function get()
130 $list = $this->getTemplates(1);
131 return count($list) > 0 ? $list[0] :
null;
141 return $this->getTemplates();
151 return count($this->
getList()) > 0;
162 foreach($this->
getList() as $template)
164 $categoryCode = $template[
'CATEGORY'];
165 if (isset($result[$categoryCode]))
170 $result[$categoryCode] = array(
171 'id' => $categoryCode,
176 $result = array_values($result);
196 foreach($this->
getList() as $template)
198 $list[$template[
'CATEGORY']][] = $template;
209 private function getTemplates($limit = 0)
219 'OnPresetTemplateList',
224 foreach ($event->getResults() as $eventResult)
226 if ($eventResult->getType() == EventResult::ERROR)
231 $eventResultParameters = $eventResult->getParameters();
232 if (!is_array($eventResultParameters))
237 foreach ($eventResultParameters as $template)
239 $template = $this->prepareTemplate($template);
240 if (!$this->checkTemplate($template))
245 $result[] = $template;
247 if ($limit && count($result) >= $limit)
255 array(
'\Bitrix\Sender\Templates\Recent',
'onPresetTemplateList'),
256 array(
'\Bitrix\Sender\Preset\Templates\Mail',
'onPresetTemplateList'),
257 array(
'\Bitrix\Sender\Preset\Templates\Sms',
'onPresetTemplateList'),
258 array(
'\Bitrix\Sender\Preset\Templates\Rc',
'onPresetTemplateList'),
259 array(
'\Bitrix\Sender\Preset\Templates\Toloka',
'onPresetTemplateList'),
260 array(
'\Bitrix\Sender\Preset\Templates\AudioCall',
'onPresetTemplateList')
262 foreach ($providers as $provider)
264 if (!is_callable($provider))
269 $eventResult = call_user_func_array($provider, $parameters);
270 if (!is_array($eventResult))
274 foreach ($eventResult as $template)
276 $template = $this->prepareTemplate($template);
277 if (!$this->checkTemplate($template))
282 $result[] = $template;
283 if ($limit && count($result) >= $limit)
299 private function checkTemplate($template)
301 if (count($template[
'FIELDS']) === 0)
306 if ($this->messageCode)
308 $messageCodes = $template[
'MESSAGE_CODE'];
309 if (!is_array($messageCodes))
311 $messageCodes = array($messageCodes);
313 if (!in_array($this->messageCode, $messageCodes))
319 if ($this->version && $template[
'VERSION'] < $this->version)
324 if (!$this->includeTriggers && $template[
'IS_TRIGGER'])
329 if (!in_array($template[
'TYPE'], Type::getCodes()))
334 if ($this->
id && $this->
id != $template[
'ID'])
347 private function prepareTemplate($template)
349 if (!is_array($template))
355 if (!isset($template[
'TYPE']))
360 if (!isset($template[
'IS_TRIGGER']))
362 $template[
'IS_TRIGGER'] =
false;
366 if (!isset($template[
'ID']))
368 $template[
'ID'] = ++$this->templateCounter;
372 if (!isset($template[
'FIELDS']) || !is_array($template[
'FIELDS']))
374 $template[
'FIELDS'] = [];
378 if (!isset($template[
'SEGMENTS']) || !is_array($template[
'SEGMENTS']))
380 $template[
'SEGMENTS'] = [];
384 if (!isset($template[
'DISPATCH']) || !is_array($template[
'DISPATCH']))
386 $template[
'DISPATCH'] = [];
390 if (isset($template[
'HTML']) && $template[
'HTML'] && count($template[
'FIELDS']) === 0)
392 $template[
'FIELDS'][
'MESSAGE'] = array(
394 'VALUE' => $template[
'HTML'],
399 if (!isset($template[
'CATEGORY']) || !$template[
'CATEGORY'])
401 $template[
'CATEGORY'] = $template[
'TYPE'];
404 if (!isset($template[
'VERSION']) || !$template[
'VERSION'])
406 $template[
'VERSION'] = 2;
409 if (!isset($template[
'HINT']) || !$template[
'HINT'])
411 $template[
'HINT'] =
'';
414 if (!isset($template[
'HOT']) || !$template[
'HOT'])
416 $template[
'HOT'] =
false;
418 $template[
'HOT'] = (bool) $template[
'HOT'];
421 if (!isset($template[
'MESSAGE_CODE']) || !$template[
'MESSAGE_CODE'])
423 $template[
'MESSAGE_CODE'] = Message\iBase::CODE_MAIL;
429 $template[
'IS_SUPPORT_BLOCK_EDITOR'] = $template[
'FIELDS'][
'MESSAGE'][
'ON_DEMAND'] ??
'';
static loadMessages($file)