Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userconditioncontrol.php
1<?php
2
4
9
10Loc::loadMessages(__FILE__);
11
12if (!Loader::includeModule('sale'))
13{
14 return;
15}
16
17class UserConditionControl extends \CSaleCondCtrlComplex
18{
19 const ENTITY_USER_ID = 'CondMainUserId';
20 const ENTITY_USER_GROUP_ID = 'BX:CondMainUserGroupId';
21
23 {
24 return new EventResult(
25 EventResult::SUCCESS,
26 static::getControlDescr(),
27 'main'
28 );
29 }
30
31 public static function getControlDescr()
32 {
33 $description = parent::getControlDescr();
34 $description['SORT'] = 700;
35 return $description;
36 }
37
38 public static function getClassName()
39 {
40 return get_called_class();
41 }
42
43 public static function getControlShow($params)
44 {
45 $result = array(
46 'controlgroup' => true,
47 'group' => false,
48 'label' => Loc::getMessage('SALE_USER_CONDITION_CONTROL_GROUP_NAME'),
49 'showIn' => static::getShowIn($params['SHOW_IN_GROUPS']),
50 'children' => array()
51 );
52
53 foreach (static::getControls() as $control)
54 {
55 $result['children'][] = array(
56 'controlId' => $control['ID'],
57 'group' => false,
58 'label' => $control['LABEL'],
59 'showIn' => static::getShowIn($params['SHOW_IN_GROUPS']),
60 'control' => array(
61 $control['PREFIX'],
62 static::getLogicAtom($control['LOGIC']),
63 static::getValueAtom($control['JS_VALUE'])
64 )
65 );
66 }
67
68 return $result;
69 }
70
71 public static function checkBasket(array $order, array $userIds, $type)
72 {
73 if(empty($order['USER_ID']))
74 {
75 return false;
76 }
77
78 $orderUserId = (int)$order['USER_ID'];
79 if ($type === 'Equal')
80 {
81 return in_array($orderUserId, $userIds);
82 }
83 elseif($type === 'Not')
84 {
85 return !in_array($orderUserId, $userIds);
86 }
87
88 return false;
89 }
90
91 public static function generate($oneCondition, $params, $control, $subs = false)
92 {
93 $result = '';
94 if (is_string($control))
95 {
96 $control = static::getControls($control);
97 }
98 $error = !is_array($control);
99
100 $values = array();
101 if (!$error)
102 {
103 $values = static::check($oneCondition, $oneCondition, $control, false);
104 $error = ($values === false);
105 }
106
107 if (!$error)
108 {
109 switch ($control['ID'])
110 {
112 $stringArray = 'array('.implode(',', $values['value']).')';
113 $type = $oneCondition['logic'];
114 $result = static::getClassName() . "::checkBasket({$params['ORDER']}, $stringArray, '{$type}')";
115 break;
117 $result = self::generateOrderConditions($oneCondition, $params, $control, $values);
118 break;
119 }
120 }
121
122 return $result;
123 }
124
130 public static function getControls($controlId = false)
131 {
132 $controlList = array(
133 self::ENTITY_USER_ID => array(
134 'ID' => self::ENTITY_USER_ID,
135 'EXECUTE_MODULE' => 'sale',
136 'MODULE_ID' => 'main',
137 'MODULE_ENTITY' => 'main',
138 'ENTITY' => 'USER',
139 'FIELD' => 'USER_ID',
140 'FIELD_TYPE' => 'int',
141 'MULTIPLE' => 'N',
142 'GROUP' => 'N',
143 'LABEL' => Loc::getMessage('SALE_USER_CONDITION_CONTROL_FIELD_USER_ID'),
144 'PREFIX' => Loc::getMessage('SALE_USER_CONDITION_CONTROL_FIELD_USER_PREFIX'),
145 'LOGIC' => static::getLogic(array(BT_COND_LOGIC_EQ, BT_COND_LOGIC_NOT_EQ)),
146 'JS_VALUE' => array(
147 'type' => 'userPopup',
148 'popup_url' => self::getAdminSection().'user_search.php',
149 'popup_params' => array(
150 'lang' => LANGUAGE_ID,
151 ),
152 'param_id' => 'n',
153 'show_value' => 'Y',
154 'user_load_url' => '/bitrix/admin/sale_discount_edit.php?lang=' . LANGUAGE_ID,
155 'coreUserInfo' => 'Y',
156 ),
157 'PHP_VALUE' => array(
158 'VALIDATE' => 'user'
159 )
160 ),
161 );
162
163 if (!Main\ModuleManager::isModuleInstalled('bitrix24'))
164 {
165 $labels = array(
166 BT_COND_LOGIC_EQ => Loc::getMessage('SALE_USER_CONDITION_CONTROL_FIELD_USER_GROUP_EQ'),
167 BT_COND_LOGIC_NOT_EQ => Loc::getMessage('SALE_USER_CONDITION_CONTROL_FIELD_USER_GROUP_NOT_EQ'),
168 );
169
170 $userGroups = array();
171 $iterator = Main\GroupTable::getList(array(
172 'select' => array('ID', 'NAME', 'C_SORT'),
173 'order' => array('C_SORT' => 'ASC', 'NAME' => 'ASC')
174 ));
175 while ($row = $iterator->fetch())
176 $userGroups[$row['ID']] = $row['NAME'];
177 unset($row, $iterator);
178 $controlList[self::ENTITY_USER_GROUP_ID] = array(
179 'ID' => self::ENTITY_USER_GROUP_ID,
180 'EXECUTE_MODULE' => 'all',
181 'MODULE_ID' => 'main',
182 'MODULE_ENTITY' => 'main',
183 'ENTITY' => 'USER_GROUPS',
184 'FIELD' => 'USER_GROUPS',
185 'FIELD_TYPE' => 'int',
186 'MULTIPLE' => 'Y',
187 'GROUP' => 'N',
188 'LABEL' => Loc::getMessage('SALE_USER_CONDITION_CONTROL_FIELD_USER_GROUP_ID'),
189 'PREFIX' => Loc::getMessage('SALE_USER_CONDITION_CONTROL_FIELD_USER_GROUP_PREFIX'),
190 'LOGIC' => static::getLogicEx(array_keys($labels), $labels),
191 'JS_VALUE' => array(
192 'type' => 'select',
193 'multiple' => 'Y',
194 'values' => $userGroups,
195 'show_value' => 'Y'
196 ),
197 'PHP_VALUE' => array(
198 'VALIDATE' => 'list'
199 )
200 );
201 unset($userGroups, $labels);
202 }
203
204 return static::searchControl($controlList, $controlId);
205 }
206
207 public static function getShowIn($arControls)
208 {
209 return array(\CSaleCondCtrlGroup::getControlID());
210 }
211
212 private static function generateOrderConditions(array $oneCondition, array $params, array $control, $values)
213 {
214 $result = '';
215
216 $logic = static::SearchLogic($values['logic'], $control['LOGIC']);
217 if (!empty($logic['OP'][$control['MULTIPLE']]))
218 {
219 $joinOperator = '';
220 $multi = false;
221 if (isset($control['JS_VALUE']['multiple']) && $control['JS_VALUE']['multiple'] == 'Y')
222 {
223 $multi = true;
224 $joinOperator = ($logic['MULTI_SEP'] ?? ' && ');
225 }
226 $field = $params['ORDER'].'[\''.$control['FIELD'].'\']';
227 switch ($control['FIELD_TYPE'])
228 {
229 case 'int':
230 case 'double':
231 if (!$multi)
232 {
233 $result = str_replace(
234 array('#FIELD#', '#VALUE#'),
235 array($field, $values['value']),
236 $logic['OP'][$control['MULTIPLE']]
237 );
238 }
239 else
240 {
241 $list = array();
242 foreach ($values['value'] as $item)
243 {
244 $list[] = str_replace(
245 array('#FIELD#', '#VALUE#'),
246 array($field, $item),
247 $logic['OP'][$control['MULTIPLE']]
248 );
249 }
250 $result = '(('.implode(')'.$joinOperator.'(', $list).'))';
251 unset($list, $item);
252 }
253 break;
254 case 'char':
255 case 'string':
256 case 'text':
257 if (!$multi)
258 {
259 $result = str_replace(
260 array('#FIELD#', '#VALUE#'),
261 array($field, '"'.EscapePHPString($values['value']).'"'),
262 $logic['OP'][$control['MULTIPLE']]
263 );
264 }
265 else
266 {
267 $list = array();
268 foreach ($values['value'] as $item)
269 {
270 $list[] = str_replace(
271 array('#FIELD#', '#VALUE#'),
272 array($field, '"'.EscapePHPString($item).'"'),
273 $logic['OP'][$control['MULTIPLE']]
274 );
275 }
276 $result = '(('.implode(')'.$joinOperator.'(', $list).'))';
277 unset($list, $item);
278 }
279 break;
280 case 'date':
281 case 'datetime':
282 if (!$multi)
283 {
284 $result = str_replace(
285 array('#FIELD#', '#VALUE#'),
286 array($field, $values['value']),
287 $logic['OP'][$control['MULTIPLE']]
288 );
289 }
290 else
291 {
292 $list = array();
293 foreach ($values['value'] as $item)
294 {
295 $list[] = str_replace(
296 array('#FIELD#', '#VALUE#'),
297 array($field, $item),
298 $logic['OP'][$control['MULTIPLE']]
299 );
300 }
301 $result = '(('.implode(')'.$joinOperator.'(', $list).'))';
302 unset($list, $item);
303 }
304 break;
305 }
306 if ($result !== '')
307 $result = 'isset('.$field.') && '.$result;
308 }
309
310 return $result;
311 }
312
316 private static function getAdminSection()
317 {
318 //TODO: need use \CAdminPage::getSelfFolderUrl, but in general it is impossible now
319 return (defined('SELF_FOLDER_URL') ? SELF_FOLDER_URL : '/bitrix/admin/');
320 }
321}
static checkBasket(array $order, array $userIds, $type)
static generate($oneCondition, $params, $control, $subs=false)
static includeModule($moduleName)
Definition loader.php:69
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static isModuleInstalled($moduleName)