1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
entityproperty.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale;
4
5use Bitrix\Location\Entity\Address;
6use Bitrix\Sale\Internals\Input;
7use Bitrix\Main;
8use Bitrix\Main\Localization\Loc;
9use Bitrix\Sale\Internals\OrderPropsGroupTable;
10use Bitrix\Sale\Internals\OrderPropsTable;
11
12Loc::loadMessages(__FILE__);
13
18abstract class EntityProperty
19{
20 protected $fields = [];
21
25 abstract protected static function getEntityType(): string;
26
30 public static function getRegistryType()
31 {
33 }
34
39 public function getField($name)
40 {
41 return $this->fields[$name] ?? null;
42 }
43
51 public static function getList(array $parameters = [])
52 {
53 $filter = [
54 '=ENTITY_REGISTRY_TYPE' => static::getRegistryType(),
55 '=ENTITY_TYPE' => static::getEntityType()
56 ];
57
58 if (isset($parameters['filter']))
59 {
60 $filter[] = $parameters['filter'];
61 }
62
63 $parameters['filter'] = $filter;
64 return OrderPropsTable::getList($parameters);
65 }
66
74 public static function getObjectById($propertyId)
75 {
76 $dbRes = static::getList([
77 'filter' => [
78 '=ID' => $propertyId
79 ]
80 ]);
81
82 $data = $dbRes->fetch();
83 if ($data)
84 {
85 return new static($data);
86 }
87
88 return null;
89 }
90
98 public function getGroupInfo(bool $refreshData = false)
99 {
100 static $groupList = [];
101
102 if ($refreshData || !isset($groupList[$this->getPersonTypeId()]))
103 {
104 $dbRes = OrderPropsGroupTable::getList([
105 'filter' => [
106 '=PERSON_TYPE_ID' => $this->getPersonTypeId()
107 ]
108 ]);
109 while ($group = $dbRes->fetch())
110 {
111 $groupList[$this->getPersonTypeId()][$group['ID']] = $group;
112 }
113 }
114
115 $groupId = $this->getGroupId();
116
117 if (!isset($groupList[$this->getPersonTypeId()][$groupId]))
118 {
119 return [
120 'ID' => 0,
121 'NAME' => Loc::getMessage('SOP_UNKNOWN_GROUP'),
122 ];
123 }
124
125 return $groupList[$this->getPersonTypeId()][$groupId];
126 }
127
137 public function __construct(array $property, array $relation = null)
138 {
139 if (isset($property['SETTINGS']) && is_array($property['SETTINGS']))
140 {
141 $property += $property['SETTINGS'];
142 unset ($property['SETTINGS']);
143 }
144
145 $this->fields = $property;
146
147 if ($relation === null)
148 {
149 $relation = $this->loadRelations();
150 }
151
152 $this->fields['RELATION'] = $relation;
153
154 if ($this->fields['TYPE'] === 'ENUM')
155 {
156 if (!isset($property['OPTIONS']))
157 {
158 $this->fields['OPTIONS'] = $this->loadOptions();
159 }
160 }
161
162 if (isset($this->fields['DEFAULT_VALUE']))
163 {
164 $this->fields['DEFAULT_VALUE'] = $this->normalizeValue($this->fields['DEFAULT_VALUE']);
165 }
166 }
167
176 public function normalizeValue($value)
177 {
178 if ($this->fields['TYPE'] === 'FILE')
179 {
180 return Input\File::loadInfo($value);
181 }
182 elseif ($this->fields['TYPE'] === 'ADDRESS' && Main\Loader::includeModule('location'))
183 {
184 if (is_array($value))
185 {
189 return $value;
190 }
191 elseif (is_numeric($value))
192 {
196 $address = Address::load((int)$value);
197
198 $value = ($address instanceof Address) ? $address->toArray() : null;
199 }
200 elseif (is_string($value) && !empty($value))
201 {
205
206 try
207 {
209 }
210 catch (\Exception $exception)
211 {
212 $result = (new Address(LANGUAGE_ID))
213 ->setFieldValue(Address\FieldType::ADDRESS_LINE_2, $value)
214 ->toArray();
215 }
216
217 return $result;
218 }
219 }
220 elseif ($this->fields['TYPE'] === "STRING")
221 {
222 if (isset($this->fields['IS_EMAIL']) && $this->fields['IS_EMAIL'] === "Y" && !empty($value))
223 {
224 $value = trim((string)$value);
225 }
226
227 if (Input\StringInput::isMultiple($value))
228 {
229 foreach ($value as $key => $data)
230 {
232 {
233 unset($value[$key]);
234 }
235 }
236 }
237
238 return $value;
239 }
240
241 return $value;
242 }
243
250 protected function loadOptions()
251 {
252 $options = array();
253
255 'select' => ['VALUE', 'NAME'],
256 'filter' => ['ORDER_PROPS_ID' => $this->getId()],
257 'order' => ['SORT' => 'ASC']
258 ]);
259
260 while ($data = $dbRes->fetch())
261 {
262 $options[$data['VALUE']] = $data['NAME'];
263 }
264
265 return $options;
266 }
267
271 protected function loadRelations()
272 {
274 }
275
284 public static function getMeaningfulValues($personTypeId, $request)
285 {
286 $result = [];
287
288 $personTypeId = intval($personTypeId);
289 if ($personTypeId <= 0 || !is_array($request))
290 {
291 return [];
292 }
293
294 $dbRes = static::getList([
295 'select' => [
296 'ID',
297 'IS_LOCATION',
298 'IS_EMAIL',
299 'IS_PROFILE_NAME',
300 'IS_PAYER',
301 'IS_LOCATION4TAX',
302 'IS_ZIP',
303 'IS_PHONE',
304 'IS_ADDRESS',
305 'IS_ADDRESS_FROM',
306 'IS_ADDRESS_TO',
307 ],
308 'filter' => [
309 '=ACTIVE' => 'Y',
310 '=UTIL' => 'N',
311 '=PERSON_TYPE_ID' => $personTypeId
312 ]
313 ]);
314
315 while ($row = $dbRes->fetch())
316 {
317 if (array_key_exists($row["ID"], $request))
318 {
319 foreach ($row as $key => $value)
320 {
321 if (($value === "Y") && (mb_substr($key, 0, 3) === "IS_"))
322 {
323 $result[mb_substr($key, 3)] = $request[$row["ID"]];
324 }
325 }
326 }
327 }
328
329 return $result;
330 }
331
337 public function checkValue($value)
338 {
339 $result = new Result();
340
341 static $errors = [];
342
343 if (
344 $this->getField('TYPE') === "STRING"
345 && (int)$this->getField('MAXLENGTH') <= 0
346 )
347 {
348 $this->fields['MAXLENGTH'] = 500;
349 }
350
351 $error = Input\Manager::getError($this->fields, $value);
352
353 if (!is_array($error))
354 {
356 }
357
358 foreach ($error as $item)
359 {
360 if (!is_array($item))
361 {
362 $item = [$item];
363 }
364
365 foreach ($item as $message)
366 {
367 if (isset($errorsList[$this->getId()]) && in_array($message, $errors[$this->getId()]))
368 {
369 continue;
370 }
371
372 $result->addError(
373 new Main\Error(
374 Loc::getMessage(
375 "SALE_PROPERTY_ERROR",
376 ["#PROPERTY_NAME#" => $this->getField('NAME'), "#ERROR_MESSAGE#" => $message]
377 )
378 )
379 );
380 }
381 }
382
383 if (!is_array($value) && isset($value))
384 {
385 $value = trim((string)$value);
386
387 if ($value !== '' && $this->getField('IS_EMAIL') === 'Y')
388 {
389 if (!check_email($value, true))
390 {
391 $result->addError(
392 new Main\Error(
393 Loc::getMessage("SALE_GOPE_WRONG_EMAIL")
394 )
395 );
396 }
397 }
398 }
399
400 return $result;
401 }
402
410 public function checkRequiredValue($value)
411 {
412 static $errors = [];
413
414 $result = new Result();
415
416 $errorList = Input\Manager::getRequiredError($this->fields, $value);
417
418 foreach ($errorList as $error)
419 {
420 if (is_array($error))
421 {
422 foreach ($error as $message)
423 {
424 $result->addError(new ResultError($message));
425 $errors[$this->getId()][] = $message;
426 }
427 }
428 else
429 {
430 $result->addError(new ResultError($error));
431 $errors[$this->getId()][] = $error;
432 }
433 }
434
435 return $result;
436 }
437
445 {
446 $value = $propertyValue->getField('VALUE');
447
448 if ($this->getType() == 'FILE')
449 {
450 $value = Input\File::asMultiple($value);
451
452 foreach ($value as $i => $file)
453 {
454 if (Input\File::isDeletedSingle($file))
455 {
456 unset($value[$i]);
457 }
458 else
459 {
460 if (Input\File::isUploadedSingle($file))
461 {
462 $fileId = \CFile::SaveFile(array('MODULE_ID' => 'sale') + $file, 'sale/order/properties');
463 if (is_numeric($fileId))
464 {
465 $file = $fileId;
466 }
467 }
468
469 $value[$i] = Input\File::loadInfoSingle($file);
470 }
471 }
472
473 $property = $this->getFields();
474 $propertyValue->setField('VALUE', $value);
475 $value = Input\File::getValue($property, $value);
476
477 $originalFields = $propertyValue->getFields()->getOriginalValues();
478 foreach (
479 array_diff(
480 Input\File::asMultiple(Input\File::getValue($property, $originalFields['VALUE'])),
481 Input\File::asMultiple($value),
482 Input\File::asMultiple(Input\File::getValue($property, $property['DEFAULT_VALUE']))
483 )
484 as $fileId
485 )
486 {
487 \CFile::Delete($fileId);
488 }
489 }
490 elseif ($this->getType() === 'ADDRESS' && Main\Loader::includeModule('location'))
491 {
492 if (!is_array($value))
493 {
494 return null;
495 }
496
497 $address = Address::fromArray($value);
498 $result = $address->save();
499 if (!$result->isSuccess())
500 {
501 return null;
502 }
503
504 return (int)$result->getId();
505 }
506
507 return $value;
508 }
509
515 public function getViewHtml($value)
516 {
517 return Input\Manager::getViewHtml($this->fields, $value);
518 }
519
525 public function getEditHtml(array $values)
526 {
527 $key = isset($this->property["ID"]) ? $this->getId() : "n".$values['ORDER_PROPS_ID'];
528 return Input\Manager::getEditHtml("PROPERTIES[".$key."]", $this->fields, $values['VALUE']);
529 }
530
534 public function getFields()
535 {
536 return $this->fields;
537 }
538
542 public function getId()
543 {
544 return $this->getField('ID');
545 }
546
550 public function getPersonTypeId()
551 {
552 return $this->getField('PERSON_TYPE_ID');
553 }
554
558 public function getGroupId()
559 {
560 return $this->getField('PROPS_GROUP_ID');
561 }
562
566 public function getName()
567 {
568 return $this->getField('NAME');
569 }
570
574 public function getRelations()
575 {
576 return $this->getField('RELATION');
577 }
578
582 public function getDescription()
583 {
584 return $this->getField('DESCRIPTION');
585 }
586
590 public function getType()
591 {
592 return $this->getField('TYPE');
593 }
594
598 public function isRequired()
599 {
600 return $this->getField('REQUIRED') === 'Y';
601 }
602
606 public function isUtil()
607 {
608 return $this->getField('UTIL') === 'Y';
609 }
610
614 public function getOptions()
615 {
616 return $this->getField('OPTIONS');
617 }
618
622 public function onValueDelete($value)
623 {
624 if ($this->getType() === 'FILE')
625 {
626 foreach (Input\File::asMultiple($value) as $fileId)
627 {
628 \CFile::Delete($fileId);
629 }
630 }
631 }
632}
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
Определения error.php:15
static getList(array $parameters=array())
Определения datamanager.php:431
static decode($data)
Определения json.php:50
checkRequiredValue($value)
Определения entityproperty.php:410
static getMeaningfulValues($personTypeId, $request)
Определения entityproperty.php:284
__construct(array $property, array $relation=null)
Определения entityproperty.php:137
onValueDelete($value)
Определения entityproperty.php:622
checkValue($value)
Определения entityproperty.php:337
normalizeValue($value)
Определения entityproperty.php:176
getViewHtml($value)
Определения entityproperty.php:515
getEditHtml(array $values)
Определения entityproperty.php:525
getPreparedValueForSave(EntityPropertyValue $propertyValue)
Определения entityproperty.php:444
getGroupInfo(bool $refreshData=false)
Определения entityproperty.php:98
static getRegistryType()
Определения entityproperty.php:30
getField($name)
Определения entityproperty.php:39
static getList(array $parameters=[])
Определения entityproperty.php:51
static getObjectById($propertyId)
Определения entityproperty.php:74
static getValue(array $input, $value)
Определения input.php:608
static asMultiple($value)
Определения input.php:366
static loadInfoSingle($file)
Определения input.php:1401
static loadInfo($value)
Определения input.php:1389
static getEditHtml($name, array $input, $value=null)
Определения input.php:88
static getRequiredError(array $input, $value)
Определения input.php:141
static getError(array $input, $value)
Определения input.php:123
static getViewHtml(array $input, $value=null)
Определения input.php:70
static isDeletedSingle($value)
Определения input.php:882
static getRelationsByPropertyId($propertyId)
Определения orderprops_relation.php:113
const REGISTRY_TYPE_ORDER
Определения registry.php:16
$options
Определения commerceml2.php:49
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr ><?endif?><? $propertyIndex=0;foreach( $arGlobalProperties as $propertyCode=> $propertyValue
Определения file_new.php:729
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$errors
Определения iblock_catalog_edit.php:74
$filter
Определения iblock_catalog_list.php:54
check_email($email, $strict=false, $domainCheck=false)
Определения tools.php:4571
$name
Определения menu_edit.php:35
$message
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$i
Определения factura.php:643
$error
Определения subscription_card_product.php:20
$dbRes
Определения yandex_detail.php:168