19 foreach (
$fields as $name => $value)
69 static $fieldsMap = [];
71 if (!isset($fieldsMap[static::class]))
73 $fieldsMap[static::class] = array_fill_keys(static::getAvailableFields(),
true);
76 return $fieldsMap[static::class];
86 static $mapFields = [];
88 if (!isset($mapFields[static::class]))
90 $mapFields[static::class] = [];
92 $fields = static::getFieldsDescription();
95 $mapFields[static::class][$field[
'CODE']] = $field[
'CODE'];
99 return $mapFields[static::class];
110 $map = static::getFieldsMap();
111 foreach ($map as $key => $value)
113 if (is_array($value) && !isset($value[
'expression']))
117 'TYPE' => $value[
'data_type']
122 $result[$value->getName()] = [
123 'CODE' => $value->getName(),
124 'TYPE' => $value->getDataType(),
157 return $this->fields->get($name);
173 public function setField($name, $value)
177 $value = $this->normalizeValue($name, $value);
179 if ($this->eventName ===
null)
181 $this->eventName = static::getEntityEventName();
184 if ($this->eventName)
186 $eventManager = Main\EventManager::getInstance();
187 if ($eventsList = $eventManager->findEventHandlers(
'sale',
'OnBefore'.$this->eventName.
'SetField'))
190 $event =
new Main\Event(
'sale',
'OnBefore'.$this->eventName.
'SetField', array(
197 if ($event->getResults())
200 foreach($event->getResults() as $eventResult)
202 if($eventResult->getType() == Main\EventResult::SUCCESS)
204 if ($eventResultData = $eventResult->getParameters())
206 if (isset($eventResultData[
'VALUE']) && $eventResultData[
'VALUE'] != $value)
208 $value = $eventResultData[
'VALUE'];
212 elseif($eventResult->getType() == Main\EventResult::ERROR)
215 $errorMsg =
new ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_ON_BEFORE_'.mb_strtoupper($this->eventName).
'_SET_FIELD_ERROR'),
'SALE_EVENT_ON_BEFORE_'.mb_strtoupper($this->eventName).
'_SET_FIELD_ERROR');
217 if ($eventResultData = $eventResult->getParameters())
219 if (isset($eventResultData) && $eventResultData instanceof ResultError)
222 $errorMsg = $eventResultData;
226 $result->addError($errorMsg);
231 if (!$result->isSuccess())
239 $availableFields = static::getAvailableFieldsMap();
240 if (!isset($availableFields[$name]))
242 throw new Main\ArgumentOutOfRangeException(
"name=$name");
245 $oldValue = $this->fields->get($name);
246 if ($oldValue != $value || ($oldValue ===
null && $value !==
null))
249 if (!$r->isSuccess())
251 $result->addErrors($r->getErrors());
255 if ($this->eventName)
257 if ($eventsList = $eventManager->findEventHandlers(
'sale',
'On'.$this->eventName.
'SetField'))
259 $event =
new Main\Event(
'sale',
'On'.$this->eventName.
'SetField', array(
263 'OLD_VALUE' => $oldValue,
267 if ($event->getResults())
270 foreach($event->getResults() as $eventResult)
272 if($eventResult->getType() == Main\EventResult::SUCCESS)
274 if ($eventResultData = $eventResult->getParameters())
276 if (isset($eventResultData[
'VALUE']) && $eventResultData[
'VALUE'] != $value)
278 $value = $eventResultData[
'VALUE'];
287 $isStartField = $this->
isStartField(in_array($name, static::getMeaningfulFields()));
289 $this->fields->set($name, $value);
294 if ($result->isSuccess() && $this->eventName)
296 $event =
new Main\Event(
'sale',
'OnAfter'.$this->eventName.
'SetField', array(
300 'OLD_VALUE' => $oldValue,
305 if ($result->isSuccess())
307 static::addChangesToHistory($name, $oldValue, $value);
310 catch (\Exception $e)
312 $this->fields->set($name, $oldValue);
316 if (!$result->isSuccess())
318 $this->fields->set($name, $oldValue);
328 if (!$r->isSuccess())
330 $result->addErrors($r->getErrors());
334 if (($data = $r->getData())
335 && !empty($data) && is_array($data))
337 $result->setData($result->getData() + $data);
399 $allFields = static::getAllFields();
400 if (!isset($allFields[$name]))
407 $oldValue = $this->fields->get($name);
409 if ($oldValue != $value || ($oldValue ===
null && $value !==
null))
411 $this->fields->set($name, $value);
412 static::addChangesToHistory($name, $oldValue, $value);
424 public function setFields(array $values)
426 $resultData = array();
430 foreach ($values as $key => $value)
432 $oldValues[$key] = $this->fields->get($key);
435 if ($this->eventName ===
null)
437 $this->eventName = static::getEntityEventName();
440 if ($this->eventName)
442 $eventManager = Main\EventManager::getInstance();
443 if ($eventsList = $eventManager->findEventHandlers(
'sale',
'OnBefore'.$this->eventName.
'SetFields'))
445 $event =
new Main\Event(
'sale',
'OnBefore'.$this->eventName.
'SetFields', array(
448 'OLD_VALUES' => $oldValues
452 if ($event->getResults())
455 foreach($event->getResults() as $eventResult)
457 if($eventResult->getType() == Main\EventResult::SUCCESS)
459 if ($eventResultData = $eventResult->getParameters())
461 if (isset($eventResultData[
'VALUES']))
463 $values = $eventResultData[
'VALUES'];
467 elseif($eventResult->getType() == Main\EventResult::ERROR)
469 $errorMsg =
new ResultError(Main\Localization\
Loc::getMessage(
'SALE_EVENT_ON_BEFORE_'.mb_strtoupper($this->eventName).
'_SET_FIELDS_ERROR'),
'SALE_EVENT_ON_BEFORE_'.mb_strtoupper($this->eventName).
'_SET_FIELDS_ERROR');
471 if ($eventResultData = $eventResult->getParameters())
473 if (isset($eventResultData) && $eventResultData instanceof ResultError)
476 $errorMsg = $eventResultData;
480 $result->addError($errorMsg);
487 if (!$result->isSuccess())
496 foreach ($values as $key => $value)
498 $r = $this->setField($key, $value);
499 if (!$r->isSuccess())
501 $data = $r->getData();
502 if (!empty($data) && is_array($data))
504 $resultData = array_merge($resultData, $data);
506 $result->addErrors($r->getErrors());
508 elseif ($r->hasWarnings())
510 $result->addWarnings($r->getWarnings());
514 if (!empty($resultData))
516 $result->setData($resultData);
525 if (!$r->isSuccess())
527 $result->addErrors($r->getErrors());
530 if (($data = $r->getData())
531 && !empty($data) && is_array($data))
533 $result->setData(array_merge($result->getData(), $data));
557 foreach ($values as $key => $value)
572 $allFields = static::getAllFields();
573 if (!isset($allFields[$name]))
578 $this->fields->init($name, $value);
589 foreach ($values as $key => $value)
600 return $this->fields->getValues();
609 return $this->fields;
657 return get_called_class();
665 return (
bool)$this->fields->getChangedValues();
674 $fields = static::getCustomizableFields();
679 'SALE_INTERNALS_ENTITY_FIELD_IS_NOT_CUSTOMIZABLE',
685 $this->fields->markCustom($name);
694 $fields = static::getCustomizableFields();
699 'SALE_INTERNALS_ENTITY_FIELD_IS_NOT_CUSTOMIZABLE',
705 $this->fields->unmarkCustom($name);
714 return $this->fields->isMarkedCustom($name);
730 $this->fields->clearChanged();
static getMessage($code, $replace=null, $language=null)