Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
orderprops.php
1<?php
9
10use Bitrix\Main\Entity\DataManager,
11 Bitrix\Main\Entity\Validator,
15use \Bitrix\Main\ORM\EventResult;
17
18Loc::loadMessages(__FILE__);
19
37{
38 public static function getFilePath()
39 {
40 return __FILE__;
41 }
42
43 public static function getTableName()
44 {
45 return 'b_sale_order_props';
46 }
47
48 public static function getMap()
49 {
50 return array(
51 'ID' => array(
52 'primary' => true,
53 'autocomplete' => true,
54 'data_type' => 'integer',
55 'format' => '/^[0-9]{1,11}$/',
56 ),
57 'PERSON_TYPE_ID' => array(
58 'required' => true,
59 'data_type' => 'integer',
60 'format' => '/^[0-9]{1,11}$/',
61 ),
62 'NAME' => array(
63 'required' => true,
64 'data_type' => 'string',
65 'validation' => array(__CLASS__, 'getNameValidators'),
66 'title' => Loc::getMessage('ORDER_PROPS_ENTITY_NAME_FIELD'),
67 ),
68 'TYPE' => array(
69 'required' => true,
70 'data_type' => 'string',
71 'validation' => array(__CLASS__, 'getTypeValidators'),
72 ),
73 'REQUIRED' => array(
74 'data_type' => 'boolean',
75 'values' => array('N', 'Y'),
76 'save_data_modification' => array(__CLASS__, 'getRequiredSaveModifiers'),
77 ),
78 'DEFAULT_VALUE' => array(
79 'data_type' => 'string',
80 'validation' => array(__CLASS__, 'getValueValidators'),
81 'save_data_modification' => array(__CLASS__, 'getValueSaveModifiers'),
82 'fetch_data_modification' => array(__CLASS__, 'getValueFetchModifiers'),
83 'title' => Loc::getMessage('ORDER_PROPS_ENTITY_DEFAULT_VALUE_FIELD'),
84 ),
85 'SORT' => array(
86 'data_type' => 'integer',
87 'format' => '/^[0-9]{1,11}$/',
88 'title' => Loc::getMessage('ORDER_PROPS_ENTITY_SORT_FIELD'),
89 ),
90 'USER_PROPS' => array(
91 'data_type' => 'boolean',
92 'values' => array('N', 'Y'),
93 ),
94 'IS_LOCATION' => array(
95 'data_type' => 'boolean',
96 'values' => array('N', 'Y'),
97 ),
98 'PROPS_GROUP_ID' => array(
99 'required' => true,
100 'data_type' => 'integer',
101 'format' => '/^[0-9]{1,11}$/',
102 ),
103 'DESCRIPTION' => array(
104 'data_type' => 'string',
105 'validation' => array(__CLASS__, 'getDescriptionValidators'),
106 'title' => Loc::getMessage('ORDER_PROPS_ENTITY_DESCRIPTION_FIELD'),
107 ),
108 'IS_EMAIL' => array(
109 'data_type' => 'boolean',
110 'values' => array('N', 'Y'),
111 ),
112 'IS_PROFILE_NAME' => array(
113 'data_type' => 'boolean',
114 'values' => array('N', 'Y'),
115 ),
116 'IS_PAYER' => array(
117 'data_type' => 'boolean',
118 'values' => array('N', 'Y'),
119 ),
120 'IS_LOCATION4TAX' => array(
121 'data_type' => 'boolean',
122 'values' => array('N', 'Y'),
123 ),
124 'IS_FILTERED' => array(
125 'data_type' => 'boolean',
126 'values' => array('N', 'Y'),
127 ),
128 'CODE' => array(
129 'data_type' => 'string',
130 'validation' => array(__CLASS__, 'getCodeValidators'),
131 'title' => Loc::getMessage('ORDER_PROPS_ENTITY_CODE_FIELD'),
132 ),
133 'IS_ZIP' => array(
134 'data_type' => 'boolean',
135 'values' => array('N', 'Y'),
136 ),
137 'IS_PHONE' => array(
138 'data_type' => 'boolean',
139 'values' => array('N', 'Y'),
140 ),
141 'IS_ADDRESS' => array(
142 'data_type' => 'boolean',
143 'values' => array('N', 'Y'),
144 ),
145 'IS_ADDRESS_FROM' => array(
146 'data_type' => 'boolean',
147 'values' => array('N', 'Y'),
148 ),
149 'IS_ADDRESS_TO' => array(
150 'data_type' => 'boolean',
151 'values' => array('N', 'Y'),
152 ),
153 'ACTIVE' => array(
154 'data_type' => 'boolean',
155 'values' => array('N', 'Y'),
156 ),
157 'UTIL' => array(
158 'data_type' => 'boolean',
159 'values' => array('N', 'Y'),
160 ),
161 'INPUT_FIELD_LOCATION' => array(
162 'data_type' => 'integer',
163 'format' => '/^[0-9]{1,11}$/',
164 ),
165 'MULTIPLE' => array(
166 'data_type' => 'boolean',
167 'values' => array('N', 'Y'),
168 ),
169 'SETTINGS' => array(
170 'data_type' => 'string',
171 'validation' => array(__CLASS__, 'getSettingsValidators'),
172 'save_data_modification' => array(__CLASS__, 'getSettingsSaveModifiers'),
173 'fetch_data_modification' => array(__CLASS__, 'getSettingsFetchModifiers'),
174 ),
175
176 'GROUP' => array(
177 'data_type' => 'Bitrix\Sale\Internals\OrderPropsGroupTable',
178 'reference' => array('=this.PROPS_GROUP_ID' => 'ref.ID'),
179 'join_type' => 'LEFT',
180 ),
181 'PERSON_TYPE' => array(
182 'data_type' => 'Bitrix\Sale\Internals\PersonTypeTable',
183 'reference' => array('=this.PERSON_TYPE_ID' => 'ref.ID'),
184 'join_type' => 'LEFT',
185 ),
186 'ENTITY_REGISTRY_TYPE' => array(
187 'data_type' => 'string',
188 ),
189 'XML_ID' => array(
190 'data_type' => 'string',
191 ),
192 'ENTITY_TYPE' => array(
193 'data_type' => 'enum',
194 'default_value' => Registry::ENTITY_ORDER,
195 'required' => true,
196 'validation' => array(__CLASS__, 'validateEntityType'),
197 'values' => static::getEntityTypes()
198 ),
199 );
200 }
201
202 public static function onBeforeAdd(Event $event): EventResult
203 {
204 $result = new EventResult;
205 $fields = $event->getParameter('fields');
206
207 $modifyFieldList = [];
208 if (isset($fields['IS_FILTERED']))
209 {
210 $multiple = $fields['MULTIPLE'] ?? 'N';
211 if (
212 $multiple === 'Y'
213 && $fields['IS_FILTERED'] !== 'N'
214 )
215 {
216 $modifyFieldList['IS_FILTERED'] = 'N';
217 }
218 }
219
220 if (!empty($modifyFieldList))
221 {
222 $result->modifyFields($modifyFieldList);
223 }
224
225 return $result;
226 }
227
228 public static function onBeforeUpdate(Event $event): EventResult
229 {
230 $result = new EventResult;
231 $fields = $event->getParameter('fields');
232
233 $modifyFieldList = [];
234 if (isset($fields['IS_FILTERED']) || isset($fields['MULTIPLE']))
235 {
236 $multiple = null;
237 $filtered = null;
238 if (isset($fields['MULTIPLE']))
239 {
240 $multiple = $fields['MULTIPLE'];
241 }
242 if (isset($fields['IS_FILTERED']))
243 {
244 $filtered = $fields['IS_FILTERED'];
245 }
246
247 if ($multiple === null || $filtered === null)
248 {
249 $primary = $event->getParameter('primary');
250 $row = static::getRow([
251 'select' => [
252 'ID',
253 'MULTIPLE',
254 'IS_FILTERED'
255 ],
256 'filter' => $primary,
257 ]);
258 if ($row)
259 {
260 $multiple ??= $row['MULTIPLE'];
261 $filtered ??= $row['IS_FILTERED'];
262 }
263 }
264 if (
265 $multiple === 'Y'
266 && $filtered !== 'N'
267 )
268 {
269 $modifyFieldList['IS_FILTERED'] = 'N';
270 }
271 }
272
273 if (!empty($modifyFieldList))
274 {
275 $result->modifyFields($modifyFieldList);
276 }
277
278 return $result;
279 }
280
281 public static function getEntityTypes()
282 {
283 return [
286 ];
287 }
288
289 public static function validateEntityType()
290 {
291 return [
292 new EnumValidator(),
293 ];
294 }
295
296 // value
297
298 public static function getValueValidators()
299 {
300 return array(array(__CLASS__, 'validateValue'));
301 }
302 public static function validateValue($value, $primary, array $row, $field)
303 {
304 $maxlength = 500;
305
306 $valueForSave = self::modifyValueForSave($value, $row);
307 $length = isset($valueForSave) ? mb_strlen($valueForSave) : 0;
308
309 return $length > $maxlength
310 ? Loc::getMessage('SALE_ORDER_PROPS_DEFAULT_ERROR', array('#PROPERTY_NAME#'=> $row['NAME'],'#FIELD_LENGTH#' => $length, '#MAX_LENGTH#' => $maxlength))
311 : true;
312 }
313
314 public static function getValueSaveModifiers()
315 {
316 return array(array(__CLASS__, 'modifyValueForSave'));
317 }
318 public static function modifyValueForSave($value)
319 {
320 return is_array($value) ? serialize($value) : $value;
321 }
322
323 public static function getValueFetchModifiers()
324 {
325 return array(array(__CLASS__, 'modifyValueForFetch'));
326 }
327 public static function modifyValueForFetch($value, $query, $property, $alias)
328 {
329 if (!is_string($value) || $value === '')
330 {
331 return $value;
332 }
333
334 if (self::isSerialized($value))
335 {
336 if (
337 CheckSerializedData($value)
338 )
339 {
340 $value = unserialize($value, ['allowed_classes' => false]);
341 }
342 }
343 elseif (isset($property['MULTIPLE']) && $property['MULTIPLE'] == 'Y') // compatibility
344 {
345 switch($property['TYPE'])
346 {
347 case 'ENUM':
348 $value = explode(',', $value);
349 break;
350 case 'FILE':
351 $value = explode(', ', $value);
352 break;
353 }
354 }
355
356 return $value;
357 }
358
359 // filtered
360
366 public static function getFilteredSaveModifiers()
367 {
368 return array(array(__CLASS__, 'modifyFilteredForSave'));
369 }
370
378 public static function modifyFilteredForSave($value, array $data)
379 {
380 return $data['MULTIPLE'] == 'Y' ? 'N' : $value;
381 }
382
383 // settings
384
385 public static function getSettingsValidators()
386 {
387 return array(array(__CLASS__, 'validateSettings'));
388 }
389 public static function validateSettings($value)
390 {
391 $maxlength = 500;
392 $length = mb_strlen(self::modifySettingsForSave($value));
393 return $length > $maxlength
394 ? Loc::getMessage('SALE_ORDER_PROPS_SETTINGS_ERROR', array('#LENGTH#' => $length, '#MAXLENGTH#' => $maxlength))
395 : true;
396 }
397
398 public static function getSettingsSaveModifiers()
399 {
400 return array(array(__CLASS__, 'modifySettingsForSave'));
401 }
402 public static function modifySettingsForSave($value)
403 {
404 return serialize($value);
405 }
406
407 public static function getSettingsFetchModifiers()
408 {
409 return array(array(__CLASS__, 'modifySettingsForFetch'));
410 }
411 public static function modifySettingsForFetch($value)
412 {
413 if (empty($value))
414 {
415 return [];
416 }
417
418 $v = @unserialize($value, ['allowed_classes' => false]);
419 return is_array($v) ? $v : array();
420 }
421
422 // required
423
424 public static function getRequiredSaveModifiers()
425 {
426 return array(array(__CLASS__, 'modifyRequiredForSave'));
427 }
428 public static function modifyRequiredForSave ($value, array $property)
429 {
430 $isProfileName = isset($property['IS_PROFILE_NAME']) && $property['IS_PROFILE_NAME'] === 'Y';
431 $isLocation = isset($property['IS_LOCATION']) && $property['IS_LOCATION'] === 'Y';
432 $isLocation4Tax = isset($property['IS_LOCATION4TAX']) && $property['IS_LOCATION4TAX'] === 'Y';
433 $isPayer = isset($property['IS_PAYER']) && $property['IS_PAYER'] === 'Y';
434 $isZip = isset($property['IS_ZIP']) && $property['IS_ZIP'] === 'Y';
435
436 if ($value == 'Y' || $isProfileName || $isLocation || $isLocation4Tax || $isPayer || $isZip)
437 {
438 return 'Y';
439 }
440
441 return 'N';
442 }
443
444 // validators
445
446 public static function getNameValidators()
447 {
448 return array(new Validator\Length(1, 255));
449 }
450
451 public static function getTypeValidators()
452 {
453 return array(new Validator\Length(1, 20));
454 }
455
456 public static function getDescriptionValidators()
457 {
458 return array(new Validator\Length(null, 255));
459 }
460
461 public static function getCodeValidators()
462 {
463 return array(new Validator\Length(null, 50));
464 }
465
466 public static function generateXmlId()
467 {
468 return uniqid('bx_');
469 }
470
471 private static function isSerialized(string $data): bool
472 {
473 $data = trim( $data );
474 if ($data === 'N;')
475 {
476 return true;
477 }
478 if (strlen($data) < 4)
479 {
480 return false;
481 }
482 if (substr($data, 1, 1) !== ':')
483 {
484 return false;
485 }
486
487 $last = substr( $data, -1 );
488 if ($last !== ';' && $last !== '}')
489 {
490 return false;
491 }
492
493 $token = substr($data, 0,1);
494 if (
495 $token === 's'
496 && substr( $data, -2, 1) !== '"'
497 )
498 {
499 return false;
500 }
501 switch ($token)
502 {
503 case 's':
504 case 'a':
505 case 'O':
506 case 'E':
507 return (bool) preg_match("/^{$token}:[0-9]+:/s", $data);
508 case 'b':
509 case 'i':
510 case 'd':
511 return (bool) preg_match("/^{$token}:[0-9.E+-]+;$/", $data);
512 }
513
514 return false;
515 }
516}
getParameter($key)
Definition event.php:80
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static validateValue($value, $primary, array $row, $field)
static modifyValueForFetch($value, $query, $property, $alias)
static modifyRequiredForSave($value, array $property)
static modifyFilteredForSave($value, array $data)