Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
selector.php
1<?php
9
15
16Loc::loadMessages(__FILE__);
17
23{
25 private $messageCode;
26
28 private $version;
29
31 private $typeId = null;
32
34 private $includeTriggers = true;
35
37 private $id = null;
38
40 private $templateCounter = 0;
41
47 public static function create()
48 {
49 return new static();
50 }
51
57 public function withDefaultMessageCode()
58 {
59 $this->messageCode = Message\iBase::CODE_MAIL;
60 return $this;
61 }
62
69 public function withMessageCode($messageCode)
70 {
71 $this->messageCode = $messageCode;
72 return $this;
73 }
74
81 public function withVersion($version)
82 {
83 $this->version = $version;
84 return $this;
85 }
86
93 public function withTypeId($typeId)
94 {
95 $this->typeId = $typeId;
96 return $this;
97 }
98
105 public function withId($id)
106 {
107 $this->id = $id;
108 return $this;
109 }
110
117 public function withTriggers($include)
118 {
119 $this->includeTriggers = $include;
120 return $this;
121 }
122
128 public function get()
129 {
130 $list = $this->getTemplates(1);
131 return count($list) > 0 ? $list[0] : null;
132 }
133
139 public function getList()
140 {
141 return $this->getTemplates();
142 }
143
149 public function hasAny()
150 {
151 return count($this->getList()) > 0;
152 }
153
159 public function getCategories()
160 {
161 $result = array();
162 foreach($this->getList() as $template)
163 {
164 $categoryCode = $template['CATEGORY'];
165 if (isset($result[$categoryCode]))
166 {
167 continue;
168 }
169
170 $result[$categoryCode] = array(
171 'id' => $categoryCode,
172 'name' => Category::getName(Category::getId($categoryCode))
173 );
174 }
175
176 $result = array_values($result);
177 usort(
178 $result,
179 function ($a, $b)
180 {
181 return Category::sortByCode($a['id'], $b['id']);
182 }
183 );
184
185 return $result;
186 }
187
193 public function getCategorized()
194 {
195 $list = array();
196 foreach($this->getList() as $template)
197 {
198 $list[$template['CATEGORY']][] = $template;
199 }
200
201 return $list;
202 }
203
209 private function getTemplates($limit = 0)
210 {
211 $result = array();
212 $parameters = array(
213 Type::getCode($this->typeId),
214 $this->id,
215 $this->messageCode
216 );
217 $event = new Event(
218 'sender',
219 'OnPresetTemplateList',
220 $parameters
221 );
222 $event->send();
223
224 foreach ($event->getResults() as $eventResult)
225 {
226 if ($eventResult->getType() == EventResult::ERROR)
227 {
228 continue;
229 }
230
231 $eventResultParameters = $eventResult->getParameters();
232 if (!is_array($eventResultParameters))
233 {
234 continue;
235 }
236
237 foreach ($eventResultParameters as $template)
238 {
239 $template = $this->prepareTemplate($template);
240 if (!$this->checkTemplate($template))
241 {
242 continue;
243 }
244
245 $result[] = $template;
246
247 if ($limit && count($result) >= $limit)
248 {
249 return $result;
250 }
251 }
252 }
253
254 $providers = array(
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')
261 );
262 foreach ($providers as $provider)
263 {
264 if (!is_callable($provider))
265 {
266 continue;
267 }
268
269 $eventResult = call_user_func_array($provider, $parameters);
270 if (!is_array($eventResult))
271 {
272 continue;
273 }
274 foreach ($eventResult as $template)
275 {
276 $template = $this->prepareTemplate($template);
277 if (!$this->checkTemplate($template))
278 {
279 continue;
280 }
281
282 $result[] = $template;
283 if ($limit && count($result) >= $limit)
284 {
285 return $result;
286 }
287 }
288 }
289
290 return $result;
291
292 }
293
299 private function checkTemplate($template)
300 {
301 if (count($template['FIELDS']) === 0)
302 {
303 return false;
304 }
305
306 if ($this->messageCode)
307 {
308 $messageCodes = $template['MESSAGE_CODE'];
309 if (!is_array($messageCodes))
310 {
311 $messageCodes = array($messageCodes);
312 }
313 if (!in_array($this->messageCode, $messageCodes))
314 {
315 return false;
316 }
317 }
318
319 if ($this->version && $template['VERSION'] < $this->version)
320 {
321 return false;
322 }
323
324 if (!$this->includeTriggers && $template['IS_TRIGGER'])
325 {
326 return false;
327 }
328
329 if (!in_array($template['TYPE'], Type::getCodes()))
330 {
331 return false;
332 }
333
334 if ($this->id && $this->id != $template['ID'])
335 {
336 return false;
337 }
338
339 return true;
340 }
341
347 private function prepareTemplate($template)
348 {
349 if (!is_array($template))
350 {
351 $template = array();
352 }
353
354 // type
355 if (!isset($template['TYPE']))
356 {
357 $template['TYPE'] = Type::getCode(Type::ADDITIONAL);
358 }
359 // type
360 if (!isset($template['IS_TRIGGER']))
361 {
362 $template['IS_TRIGGER'] = false;
363 }
364
365 // type
366 if (!isset($template['ID']))
367 {
368 $template['ID'] = ++$this->templateCounter;
369 }
370
371 // fields of template
372 if (!isset($template['FIELDS']) || !is_array($template['FIELDS']))
373 {
374 $template['FIELDS'] = [];
375 }
376
377 // segments of template
378 if (!isset($template['SEGMENTS']) || !is_array($template['SEGMENTS']))
379 {
380 $template['SEGMENTS'] = [];
381 }
382
383 // dispatch of template
384 if (!isset($template['DISPATCH']) || !is_array($template['DISPATCH']))
385 {
386 $template['DISPATCH'] = [];
387 }
388
389 // compatibility for mail templates
390 if (isset($template['HTML']) && $template['HTML'] && count($template['FIELDS']) === 0)
391 {
392 $template['FIELDS']['MESSAGE'] = array(
393 'CODE' => 'MESSAGE',
394 'VALUE' => $template['HTML'],
395 'ON_DEMAND' => TemplateTable::isContentForBlockEditor($template['HTML'])
396 );
397 }
398
399 if (!isset($template['CATEGORY']) || !$template['CATEGORY'])
400 {
401 $template['CATEGORY'] = $template['TYPE'];
402 }
403
404 if (!isset($template['VERSION']) || !$template['VERSION'])
405 {
406 $template['VERSION'] = 2;
407 }
408
409 if (!isset($template['HINT']) || !$template['HINT'])
410 {
411 $template['HINT'] = '';
412 }
413
414 if (!isset($template['HOT']) || !$template['HOT'])
415 {
416 $template['HOT'] = false;
417 }
418 $template['HOT'] = (bool) $template['HOT'];
419
420 // default message code is MAIL
421 if (!isset($template['MESSAGE_CODE']) || !$template['MESSAGE_CODE'])
422 {
423 $template['MESSAGE_CODE'] = Message\iBase::CODE_MAIL;
424 }
425
426 // compatibility
427 if ($template['MESSAGE_CODE'] === Message\iBase::CODE_MAIL)
428 {
429 $template['IS_SUPPORT_BLOCK_EDITOR'] = $template['FIELDS']['MESSAGE']['ON_DEMAND'] ?? '';
430 }
431
432 return $template;
433 }
434
435}
static loadMessages($file)
Definition loc.php:64
static isContentForBlockEditor($content)
Definition template.php:258
static sortByCode($codeA, $codeB)
Definition category.php:46