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