|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge; |
| 4 | + |
| 5 | +use Bitrix\Main\ArgumentException; |
| 6 | +use Bitrix\Main\ORM\Objectify\Collection; |
| 7 | +use Bitrix\Main\ORM\Query\Query; |
| 8 | +use Bitrix\Main\Sms\Message; |
| 9 | +use Bitrix\Main\ObjectPropertyException; |
| 10 | +use Bitrix\Main\Sms\TemplateTable; |
| 11 | +use Bitrix\Main\SystemException; |
| 12 | +use RuntimeException; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class EventBridgeSms |
| 16 | + * @package Prokl\BitrixOrdinaryToolsBundle\Services\Email\EventBridge |
| 17 | + * |
| 18 | + * @since 28.07.2021 |
| 19 | + */ |
| 20 | +class EventBridgeSms |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Получить скомпилированный текст письма для события. |
| 24 | + * |
| 25 | + * @param Collection|null $templates Шаблоны. |
| 26 | + * @param array $context Значения полей ($arFields). |
| 27 | + * |
| 28 | + * @return Message |
| 29 | + * @throws RuntimeException |
| 30 | + */ |
| 31 | + public function compileMessage($templates, array $context) : Message |
| 32 | + { |
| 33 | + if (!$templates) { |
| 34 | + throw new RuntimeException('Template SMS object cannot be NULL'); |
| 35 | + } |
| 36 | + |
| 37 | + $result = []; |
| 38 | + foreach ($templates as $smsTemplate) { |
| 39 | + $result[] = $smsTemplate; |
| 40 | + } |
| 41 | + |
| 42 | + return Message::createFromTemplate(current($result), $context); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param string $eventName Код события. |
| 47 | + * @param string $siteId ID сайта. |
| 48 | + * @param string|null $langId ID языка. |
| 49 | + * |
| 50 | + * @return Collection|null |
| 51 | + * |
| 52 | + * @throws ArgumentException | ObjectPropertyException | SystemException Битриксовые ошибки. |
| 53 | + */ |
| 54 | + public function fetchTemplates(string $eventName, string $siteId, ?string $langId = null) |
| 55 | + { |
| 56 | + $filter = Query::filter() |
| 57 | + ->where('ACTIVE', 'Y') |
| 58 | + ->where('SITES.LID', $siteId); |
| 59 | + |
| 60 | + $filter->where('EVENT_NAME', $eventName); |
| 61 | + |
| 62 | + if ($langId !== null) { |
| 63 | + $filter->where(Query::filter() |
| 64 | + ->logic('or') |
| 65 | + ->where('LANGUAGE_ID', $langId) |
| 66 | + ->where('LANGUAGE_ID', '') |
| 67 | + ->whereNull('LANGUAGE_ID')); |
| 68 | + } |
| 69 | + |
| 70 | + $result = TemplateTable::getList([ |
| 71 | + 'select' => ['*', 'SITES.SITE_NAME', 'SITES.SERVER_NAME', 'SITES.LID'], |
| 72 | + 'filter' => $filter, |
| 73 | + ]); |
| 74 | + |
| 75 | + return $result->fetchCollection(); |
| 76 | + } |
| 77 | +} |
0 commit comments