Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
TemplateManager.php
1<?php
2
4
9
11{
12
13 protected EdnaRu $utils;
15
17 {
18 parent::__construct($providerId);
19
20 $this->utils = $utils;
21 $this->emoji = $emoji;
22 }
23
28 public function getTemplatesList(array $context = null): array
29 {
30 $templatesResult = $this->utils->getMessageTemplates();
31 if (!$templatesResult->isSuccess())
32 {
33 return [];
34 }
35
36 $templates = $templatesResult->getData();
37 if (!is_array($templates))
38 {
39 return [];
40 }
41
42 //todo: Compatibility only with previous version without placeholders. Remove it!
43 $placeholderEnabled = \Bitrix\Main\Config\Option::get('messageservice', 'template_placeholders_enabled', 'N') === 'Y';
44
45 $result = [];
46 foreach ($templates as $template)
47 {
48
49 //todo: Compatibility only with previous version without placeholders. Remove it!
50 if (!empty($template['placeholders']) && !$placeholderEnabled)
51 {
52 continue;
53 }
54
55 $tmp = [
56 'ID' => Json::encode($template['content']), //todo: Refactor it!
57 'TITLE' => $template['name'],
58 'PREVIEW' => $template['content']['text'] ?? '',
59 ];
60
61 if (!empty($template['content']['header']['text']))
62 {
63 $tmp['HEADER'] = $template['content']['header']['text'];
64 }
65 if (!empty($template['content']['footer']['text']))
66 {
67 $tmp['FOOTER'] = $template['content']['footer']['text'];
68 }
69 if (!empty($template['content']['keyboard']['rows']))
70 {
71 $tmp['KEYBOARD'] = $template['content']['keyboard'];
72 }
73 if (!empty($template['placeholders']))
74 {
75 $tmp['PLACEHOLDERS'] = [];
76 }
77 if (!empty($template['placeholders']['text']))
78 {
79 $tmp['PLACEHOLDERS']['PREVIEW'] = $template['placeholders']['text'];
80 }
81 if (!empty($template['placeholders']['header']))
82 {
83 $tmp['PLACEHOLDERS']['HEADER'] = $template['placeholders']['header'];
84 }
85 if (!empty($template['placeholders']['footer']))
86 {
87 $tmp['PLACEHOLDERS']['FOOTER'] = $template['placeholders']['footer'];
88 }
89
90 $result[] = $tmp;
91 }
92
93 return $result;
94 }
95
96 public function prepareTemplate($templateData): array
97 {
98 try
99 {
100 $messageTemplateDecoded = Json::decode($templateData);
101 $messageTemplateDecoded =
102 $this->emoji->convertEmojiInTemplate($messageTemplateDecoded, InternalOption::EMOJI_ENCODE);
103 }
104 catch (\Bitrix\Main\ArgumentException $e)
105 {
106 throw new ArgumentException('Incorrect message template');
107 }
108
109 return $messageTemplateDecoded;
110 }
111
112 public function isTemplatesBased(): bool
113 {
114 return true;
115 }
116
117}
__construct(string $providerId, EdnaRu $utils, EmojiConverter $emoji)