Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
contextmenu.php
1<?php
9namespace Bitrix\Im\Bot;
10
12{
13 private $botId = 0;
14 private $items = Array();
15
16 function __construct($botId = 0)
17 {
18 $this->botId = intval($botId);
19 }
20
21 public function addItem($params)
22 {
23 $button = Array();
24 if (isset($button['BOT_ID']) && intval($button['BOT_ID']) > 0)
25 {
26 $button['BOT_ID'] = intval($button['BOT_ID']);
27 }
28 else
29 {
30 $button['BOT_ID'] = $this->botId;
31 }
32 if (!isset($params['TEXT']) || trim($params['TEXT']) == '')
33 {
34 return false;
35 }
36
37 if (isset($params['LINK']) && preg_match('#^(?:/|https?://)#', $params['LINK']))
38 {
39 $button['LINK'] = htmlspecialcharsbx($params['LINK']);
40 }
41 else if (isset($params['FUNCTION']))
42 {
43 $button['FUNCTION'] = htmlspecialcharsbx($params['FUNCTION']);
44 }
45 else if (isset($params['APP_ID']))
46 {
47 $button['APP_ID'] = intval($params['APP_ID']);
48 if (isset($params['APP_PARAMS']) && trim($params['APP_PARAMS']) <> '')
49 {
50 $button['APP_PARAMS'] = $params['APP_PARAMS'];
51 }
52 }
53 else if (
54 isset($params['ACTION'])
55 && in_array($params['ACTION'], ['PUT', 'SEND', 'COPY', 'CALL', 'DIALOG'])
56 && trim($params['ACTION_VALUE']) <> ''
57 )
58 {
59 $button['ACTION'] = $params['ACTION'];
60 $button['ACTION_VALUE'] = $params['ACTION_VALUE'];
61 }
62 else if ($this->botId > 0 && isset($params['COMMAND']) && trim($params['COMMAND']) <> '')
63 {
64 $button['COMMAND'] = mb_substr($params['COMMAND'], 0, 1) == '/'? mb_substr($params['COMMAND'], 1) : $params['COMMAND'];
65 $button['COMMAND_PARAMS'] = isset($params['COMMAND_PARAMS']) && trim($params['COMMAND_PARAMS']) <> ''? $params['COMMAND_PARAMS']: '';
66 }
67 else
68 {
69 return false;
70 }
71
72 $button['TEXT'] = htmlspecialcharsbx(trim($params['TEXT']));
73
74 $button['CONTEXT'] = isset($params['CONTEXT']) && in_array($params['CONTEXT'], ['MOBILE', 'DESKTOP']) ? $params['CONTEXT'] : 'ALL';
75
76 $button['DISABLED'] = (isset($params['DISABLED']) && $params['DISABLED'] == 'Y') ? 'Y' : 'N';
77
78 $this->items[] = $button;
79
80 return false;
81 }
82
83 public static function getByJson($params, $textReplace = array(), $options = Array())
84 {
85 if (is_string($params))
86 {
87 $params = \CUtil::JsObjectToPhp($params);
88 }
89 if (!is_array($params))
90 {
91 return null;
92 }
93
94 $menu = new self($params['BOT_ID']);
95 foreach ($params['ITEMS'] as $button)
96 {
97 if (isset($button['FUNCTION']) && $options['ENABLE_FUNCTIONS'] != 'Y')
98 {
99 }
100 else
101 {
102 if (isset($button['TEXT']))
103 {
104 foreach ($textReplace as $key => $value)
105 {
106 $button['TEXT'] = str_replace($key, $value, $button['TEXT']);
107 }
108 }
109 $menu->addItem($button);
110 }
111 }
112
113 return $menu->isEmpty()? null: $menu;
114 }
115
116 public function isEmpty()
117 {
118 return empty($this->items);
119 }
120
121 public function isAllowSize()
122 {
123 return $this->getJson()? true: false;
124 }
125
126 public function getArray()
127 {
128 return $this->items;
129 }
130
131 public function getJson()
132 {
133 $result = \Bitrix\Im\Common::jsonEncode($this->items);
134 return mb_strlen($result) < 60000? $result: "";
135 }
136}
static getByJson($params, $textReplace=array(), $options=Array())