Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entityproperty.php
1<?php
2
3namespace Bitrix\Sale;
4
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 }
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 {
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 $this->fields['DEFAULT_VALUE'] = $this->normalizeValue($this->fields['DEFAULT_VALUE']);
163 }
164
173 public function normalizeValue($value)
174 {
175 if ($this->fields['TYPE'] === 'FILE')
176 {
177 return Input\File::loadInfo($value);
178 }
179 elseif ($this->fields['TYPE'] === 'ADDRESS' && Main\Loader::includeModule('location'))
180 {
181 if (is_array($value))
182 {
186 return $value;
187 }
188 elseif (is_numeric($value))
189 {
193 $address = Address::load((int)$value);
194
195 $value = ($address instanceof Address) ? $address->toArray() : null;
196 }
197 elseif (is_string($value) && !empty($value))
198 {
203 try
204 {
205 $result = Main\Web\Json::decode(
206 Main\Text\Encoding::convertEncoding(
207 $value,
208 SITE_CHARSET,
209 'UTF-8'
210 )
211 );
212 }
213 catch (\Exception $exception)
214 {
215 $result = (new Address(LANGUAGE_ID))
216 ->setFieldValue(Address\FieldType::ADDRESS_LINE_2, $value)
217 ->toArray();
218 }
219
220 return $result;
221 }
222 }
223 elseif ($this->fields['TYPE'] === "STRING")
224 {
225 if ($this->fields['IS_EMAIL'] === "Y" && !empty($value))
226 {
227 $value = trim((string)$value);
228 }
229
230 if (Input\StringInput::isMultiple($value))
231 {
232 foreach ($value as $key => $data)
233 {
234 if (Input\StringInput::isDeletedSingle($data))
235 {
236 unset($value[$key]);
237 }
238 }
239 }
240
241 return $value;
242 }
243
244 return $value;
245 }
246
253 protected function loadOptions()
254 {
255 $options = array();
256
257 $dbRes = Internals\OrderPropsVariantTable::getList([
258 'select' => ['VALUE', 'NAME'],
259 'filter' => ['ORDER_PROPS_ID' => $this->getId()],
260 'order' => ['SORT' => 'ASC']
261 ]);
262
263 while ($data = $dbRes->fetch())
264 {
265 $options[$data['VALUE']] = $data['NAME'];
266 }
267
268 return $options;
269 }
270
274 protected function loadRelations()
275 {
276 return Internals\OrderPropsRelationTable::getRelationsByPropertyId($this->getId());
277 }
278
287 public static function getMeaningfulValues($personTypeId, $request)
288 {
289 $result = [];
290
291 $personTypeId = intval($personTypeId);
292 if ($personTypeId <= 0 || !is_array($request))
293 {
294 return [];
295 }
296
297 $dbRes = static::getList([
298 'select' => [
299 'ID',
300 'IS_LOCATION',
301 'IS_EMAIL',
302 'IS_PROFILE_NAME',
303 'IS_PAYER',
304 'IS_LOCATION4TAX',
305 'IS_ZIP',
306 'IS_PHONE',
307 'IS_ADDRESS',
308 'IS_ADDRESS_FROM',
309 'IS_ADDRESS_TO',
310 ],
311 'filter' => [
312 '=ACTIVE' => 'Y',
313 '=UTIL' => 'N',
314 '=PERSON_TYPE_ID' => $personTypeId
315 ]
316 ]);
317
318 while ($row = $dbRes->fetch())
319 {
320 if (array_key_exists($row["ID"], $request))
321 {
322 foreach ($row as $key => $value)
323 {
324 if (($value === "Y") && (mb_substr($key, 0, 3) === "IS_"))
325 {
326 $result[mb_substr($key, 3)] = $request[$row["ID"]];
327 }
328 }
329 }
330 }
331
332 return $result;
333 }
334
340 public function checkValue($value)
341 {
342 $result = new Result();
343
344 static $errors = [];
345
346 if (
347 $this->getField('TYPE') === "STRING"
348 && (int)$this->getField('MAXLENGTH') <= 0
349 )
350 {
351 $this->fields['MAXLENGTH'] = 500;
352 }
353
354 $error = Input\Manager::getError($this->fields, $value);
355
356 if (!is_array($error))
357 {
358 $error = array($error);
359 }
360
361 foreach ($error as $item)
362 {
363 if (!is_array($item))
364 {
365 $item = [$item];
366 }
367
368 foreach ($item as $message)
369 {
370 if (isset($errorsList[$this->getId()]) && in_array($message, $errors[$this->getId()]))
371 {
372 continue;
373 }
374
375 $result->addError(
376 new Main\Error(
378 "SALE_PROPERTY_ERROR",
379 ["#PROPERTY_NAME#" => $this->getField('NAME'), "#ERROR_MESSAGE#" => $message]
380 )
381 )
382 );
383 }
384 }
385
386 if (!is_array($value) && isset($value))
387 {
388 $value = trim((string)$value);
389
390 if ($value !== '' && $this->getField('IS_EMAIL') === 'Y')
391 {
392 if (!check_email($value, true))
393 {
394 $result->addError(
395 new Main\Error(
396 Loc::getMessage("SALE_GOPE_WRONG_EMAIL")
397 )
398 );
399 }
400 }
401 }
402
403 return $result;
404 }
405
413 public function checkRequiredValue($value)
414 {
415 static $errors = [];
416
417 $result = new Result();
418
419 $errorList = Input\Manager::getRequiredError($this->fields, $value);
420
421 foreach ($errorList as $error)
422 {
423 if (is_array($error))
424 {
425 foreach ($error as $message)
426 {
427 $result->addError(new ResultError($this->getField('NAME').' '.$message));
428 $errors[$this->getId()][] = $message;
429 }
430 }
431 else
432 {
433 $result->addError(new ResultError($this->getName().' '.$error));
434 $errors[$this->getId()][] = $error;
435 }
436 }
437
438 return $result;
439 }
440
447 public function getPreparedValueForSave(EntityPropertyValue $propertyValue)
448 {
449 $value = $propertyValue->getField('VALUE');
450
451 if ($this->getType() == 'FILE')
452 {
453 $value = Input\File::asMultiple($value);
454
455 foreach ($value as $i => $file)
456 {
457 if (Input\File::isDeletedSingle($file))
458 {
459 unset($value[$i]);
460 }
461 else
462 {
463 if (Input\File::isUploadedSingle($file))
464 {
465 $fileId = \CFile::SaveFile(array('MODULE_ID' => 'sale') + $file, 'sale/order/properties');
466 if (is_numeric($fileId))
467 {
468 $file = $fileId;
469 }
470 }
471
472 $value[$i] = Input\File::loadInfoSingle($file);
473 }
474 }
475
476 $property = $this->getFields();
477 $propertyValue->setField('VALUE', $value);
478 $value = Input\File::getValue($property, $value);
479
480 $originalFields = $propertyValue->getFields()->getOriginalValues();
481 foreach (
482 array_diff(
483 Input\File::asMultiple(Input\File::getValue($property, $originalFields['VALUE'])),
484 Input\File::asMultiple($value),
485 Input\File::asMultiple(Input\File::getValue($property, $property['DEFAULT_VALUE']))
486 )
487 as $fileId
488 )
489 {
490 \CFile::Delete($fileId);
491 }
492 }
493 elseif ($this->getType() === 'ADDRESS' && Main\Loader::includeModule('location'))
494 {
495 if (!is_array($value))
496 {
497 return null;
498 }
499
500 $address = Address::fromArray($value);
501 $result = $address->save();
502 if (!$result->isSuccess())
503 {
504 return null;
505 }
506
507 return (int)$result->getId();
508 }
509
510 return $value;
511 }
512
518 public function getViewHtml($value)
519 {
520 return Input\Manager::getViewHtml($this->fields, $value);
521 }
522
528 public function getEditHtml(array $values)
529 {
530 $key = isset($this->property["ID"]) ? $this->getId() : "n".$values['ORDER_PROPS_ID'];
531 return Input\Manager::getEditHtml("PROPERTIES[".$key."]", $this->fields, $values['VALUE']);
532 }
533
537 public function getFields()
538 {
539 return $this->fields;
540 }
541
545 public function getId()
546 {
547 return $this->getField('ID');
548 }
549
553 public function getPersonTypeId()
554 {
555 return $this->getField('PERSON_TYPE_ID');
556 }
557
561 public function getGroupId()
562 {
563 return $this->getField('PROPS_GROUP_ID');
564 }
565
569 public function getName()
570 {
571 return $this->getField('NAME');
572 }
573
577 public function getRelations()
578 {
579 return $this->getField('RELATION');
580 }
581
585 public function getDescription()
586 {
587 return $this->getField('DESCRIPTION');
588 }
589
593 public function getType()
594 {
595 return $this->getField('TYPE');
596 }
597
601 public function isRequired()
602 {
603 return $this->getField('REQUIRED') === 'Y';
604 }
605
609 public function isUtil()
610 {
611 return $this->getField('UTIL') === 'Y';
612 }
613
617 public function getOptions()
618 {
619 return $this->getField('OPTIONS');
620 }
621
625 public function onValueDelete($value)
626 {
627 if ($this->getType() === 'FILE')
628 {
629 foreach (Input\File::asMultiple($value) as $fileId)
630 {
631 \CFile::Delete($fileId);
632 }
633 }
634 }
635}
static fromArray(array $arrayData)
Definition address.php:298
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())
static getMeaningfulValues($personTypeId, $request)
__construct(array $property, array $relation=null)
getPreparedValueForSave(EntityPropertyValue $propertyValue)
getGroupInfo(bool $refreshData=false)
static getList(array $parameters=[])
static getObjectById($propertyId)