Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
keyboard.php
1<?php
9namespace Bitrix\Im\Bot;
10
12{
13 private $botId = 0;
14 private $colors = Array();
15 private $buttons = Array();
16 private $voteMode = false;
17
18 function __construct($botId = 0, array $colors = Array(), $voteMode = false)
19 {
20 $this->botId = intval($botId);
21 $this->voteMode = $voteMode? true: false;
22
23 $this->setDefaultColor($colors);
24 }
25
26 private function setDefaultColor(array $colors)
27 {
28 if (isset($colors['BG_COLOR']) && preg_match('/^#([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?\b$/D', $colors['BG_COLOR']))
29 {
30 $this->colors['BG_COLOR'] = $colors['BG_COLOR'];
31 }
32
33 if(isset($colors['TEXT_COLOR']) && preg_match('/^#([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?\b$/D', $colors['TEXT_COLOR']))
34 {
35 $this->colors['TEXT_COLOR'] = $colors['TEXT_COLOR'];
36 }
37
38 if(isset($colors['OFF_BG_COLOR']) && preg_match('/^#([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?\b$/D', $colors['OFF_BG_COLOR']))
39 {
40 $this->colors['OFF_BG_COLOR'] = $colors['OFF_BG_COLOR'];
41 }
42
43 if(isset($colors['OFF_TEXT_COLOR']) && preg_match('/^#([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?\b$/D', $colors['OFF_TEXT_COLOR']))
44 {
45 $this->colors['OFF_TEXT_COLOR'] = $colors['OFF_TEXT_COLOR'];
46 }
47 }
48
49 public function addButton($params)
50 {
51 $button = [];
52 $button['BOT_ID'] = $this->botId;
53 $button['TYPE'] = 'BUTTON';
54
55 if (
56 empty($params['TEXT'])
57 || !is_string($params['TEXT'])
58 || trim($params['TEXT']) == ''
59 )
60 {
61 return false;
62 }
63
64 if (
65 !empty($params['LINK'])
66 && is_string($params['LINK'])
67 && preg_match('#^(?:/|https?://)#', $params['LINK'])
68 )
69 {
70 $button['LINK'] = htmlspecialcharsbx($params['LINK']);
71 }
72 elseif (
73 !empty($params['FUNCTION'])
74 && is_string($params['FUNCTION'])
75 )
76 {
77 $button['FUNCTION'] = htmlspecialcharsbx($params['FUNCTION']);
78 }
79 elseif (!empty($params['APP_ID']))
80 {
81 $button['APP_ID'] = (int)$params['APP_ID'];
82 if (
83 isset($params['APP_PARAMS'])
84 && is_string($params['APP_PARAMS'])
85 && trim($params['APP_PARAMS']) <> ''
86 )
87 {
88 $button['APP_PARAMS'] = $params['APP_PARAMS'];
89 }
90 }
91 elseif (
92 !empty($params['ACTION'])
93 && is_string($params['ACTION'])
94 && in_array($params['ACTION'], ['PUT', 'SEND', 'COPY', 'CALL', 'DIALOG', 'LIVECHAT', 'HELP'], true)
95 && trim($params['ACTION_VALUE']) <> ''
96 )
97 {
98 $button['ACTION'] = $params['ACTION'];
99 $button['ACTION_VALUE'] = $params['ACTION_VALUE'];
100 }
101 elseif (
102 $this->botId > 0
103 && !empty($params['COMMAND'])
104 && is_string($params['COMMAND'])
105 && trim($params['COMMAND']) <> ''
106 )
107 {
108 $button['COMMAND'] = mb_substr($params['COMMAND'], 0, 1) == '/' ? mb_substr($params['COMMAND'], 1) : $params['COMMAND'];
109 $button['COMMAND_PARAMS'] = '';
110 if (
111 !empty($params['COMMAND_PARAMS'])
112 && is_string($params['COMMAND_PARAMS'])
113 && trim($params['COMMAND_PARAMS']) <> ''
114 )
115 {
116 $button['COMMAND_PARAMS'] = $params['COMMAND_PARAMS'];
117 }
118 }
119 else
120 {
121 return false;
122 }
123
124 $button['TEXT'] = trim($params['TEXT'] ?? '');
125
126 $button['VOTE'] = $this->voteMode ? 'Y': 'N';
127
128 $blockParam = $params['BLOCK'] ?? null;
129 $button['BLOCK'] = $blockParam === 'Y'? 'Y': 'N';
130
131 $button['WAIT'] = 'N';
132
133 $button['CONTEXT'] = in_array(($params['CONTEXT'] ?? null), ['MOBILE', 'DESKTOP']) ? $params['CONTEXT']: 'ALL';
134
135 $disabledParam = $params['DISABLED'] ?? null;
136 $button['DISABLED'] = $disabledParam === 'Y'? 'Y': 'N';
137
138 $button['DISPLAY'] = in_array(($params['DISPLAY'] ?? null), ['BLOCK', 'LINE'])? $params['DISPLAY']: 'BLOCK';
139
140 if (isset($params['WIDTH']) && (int)$params['WIDTH'] > 0)
141 {
142 $button['WIDTH'] = (int)$params['WIDTH'];
143 }
144
145 if (isset($params['BG_COLOR']) && preg_match('/^#([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?\b$/D', $params['BG_COLOR']))
146 {
147 $button['BG_COLOR'] = $params['BG_COLOR'];
148 }
149 else if (isset($this->colors['BG_COLOR']))
150 {
151 $button['BG_COLOR'] = $this->colors['BG_COLOR'];
152 }
153
154 if(isset($params['TEXT_COLOR']) && preg_match('/^#([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?\b$/D', $params['TEXT_COLOR']))
155 {
156 $button['TEXT_COLOR'] = $params['TEXT_COLOR'];
157 }
158 else if (isset($this->colors['TEXT_COLOR']))
159 {
160 $button['TEXT_COLOR'] = $this->colors['TEXT_COLOR'];
161 }
162
163 if(isset($params['OFF_BG_COLOR']) && preg_match('/^#([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?\b$/D', $params['OFF_BG_COLOR']))
164 {
165 $button['OFF_BG_COLOR'] = $params['OFF_BG_COLOR'];
166 }
167 else if (isset($this->colors['OFF_BG_COLOR']))
168 {
169 $button['OFF_BG_COLOR'] = $this->colors['OFF_BG_COLOR'];
170 }
171
172 if(isset($params['OFF_TEXT_COLOR']) && preg_match('/^#([a-fA-F0-9]){3}(([a-fA-F0-9]){3})?\b$/D', $params['OFF_TEXT_COLOR']))
173 {
174 $button['OFF_TEXT_COLOR'] = $params['OFF_TEXT_COLOR'];
175 }
176 else if (isset($this->colors['OFF_TEXT_COLOR']))
177 {
178 $button['OFF_TEXT_COLOR'] = $this->colors['OFF_TEXT_COLOR'];
179 }
180
181 $this->buttons[] = $button;
182
183 return false;
184 }
185
186 public function addNewLine()
187 {
188 $button['TYPE'] = 'NEWLINE';
189 $this->buttons[] = $button;
190 }
191
192 public static function getKeyboardByJson($params, $textReplace = array(), $options = Array())
193 {
194 if (is_string($params))
195 {
196 $params = \CUtil::JsObjectToPhp($params);
197 }
198 if (!is_array($params))
199 {
200 return null;
201 }
202
203 $colors = is_array($params['COLORS'])? $params['COLORS']: Array();
204 $voteMode = isset($params['VOTE']) && $params['VOTE'] == 'Y';
205
206 $keyboard = new self($params['BOT_ID'], $colors, $voteMode);
207 foreach ($params['BUTTONS'] as $button)
208 {
209 if (isset($button['TYPE']) && $button['TYPE'] == 'NEWLINE')
210 {
211 $keyboard->addNewLine();
212 }
213 elseif (isset($button['FUNCTION']) && $options['ENABLE_FUNCTIONS'] != 'Y')
214 {
215 }
216 else
217 {
218 if (isset($button['TEXT']))
219 {
220 foreach ($textReplace as $key => $value)
221 {
222 $button['TEXT'] = str_replace($key, $value, $button['TEXT']);
223 }
224 }
225 $keyboard->addButton($button);
226 }
227 }
228
229 return $keyboard->isEmpty()? null: $keyboard;
230 }
231
232 public function isEmpty()
233 {
234 return empty($this->buttons);
235 }
236
237 public function isAllowSize()
238 {
239 return $this->getJson()? true: false;
240 }
241
242 public function getArray()
243 {
244 return $this->buttons;
245 }
246
247 public function getJson()
248 {
249 $result = \Bitrix\Im\Common::jsonEncode($this->buttons);
250 return mb_strlen($result) < 60000? $result: "";
251 }
252}
__construct($botId=0, array $colors=Array(), $voteMode=false)
Definition keyboard.php:18
static getKeyboardByJson($params, $textReplace=array(), $options=Array())
Definition keyboard.php:192