Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
entity.php
1<?php
3
7
8Main\Localization\Loc::loadMessages(__FILE__);
9
10abstract class Entity
11{
13 protected $fields;
14
15 protected $eventName = null;
16
17 protected function __construct(array $fields = array())
18 {
19 foreach ($fields as $name => $value)
20 {
21 $fields[$name] = $this->normalizeValue($name, $value);
22 }
23
24 $this->fields = new Fields($fields);
25 }
26
31 public static function getRegistryType()
32 {
33 throw new Main\NotImplementedException('The method '.__METHOD__.' is not overridden in '.static::class);
34 }
35
40 public static function getRegistryEntity()
41 {
42 throw new Main\NotImplementedException('The method '.__METHOD__.' is not overridden in '.static::class);
43 }
44
50 public static function getAvailableFields()
51 {
53 }
54
58 public static function getCustomizableFields() : array
59 {
60 return [];
61 }
62
67 public static function getAvailableFieldsMap()
68 {
69 static $fieldsMap = [];
70
71 if (!isset($fieldsMap[static::class]))
72 {
73 $fieldsMap[static::class] = array_fill_keys(static::getAvailableFields(), true);
74 }
75
76 return $fieldsMap[static::class];
77 }
78
84 public static function getAllFields()
85 {
86 static $mapFields = [];
87
88 if (!isset($mapFields[static::class]))
89 {
90 $mapFields[static::class] = [];
91
92 $fields = static::getFieldsDescription();
93 foreach ($fields as $field)
94 {
95 $mapFields[static::class][$field['CODE']] = $field['CODE'];
96 }
97 }
98
99 return $mapFields[static::class];
100 }
101
106 public static function getFieldsDescription()
107 {
108 $result = [];
109
110 $map = static::getFieldsMap();
111 foreach ($map as $key => $value)
112 {
113 if (is_array($value) && !isset($value['expression']))
114 {
115 $result[$key] = [
116 'CODE' => $key,
117 'TYPE' => $value['data_type']
118 ];
119 }
120 elseif ($value instanceof Main\Entity\ScalarField)
121 {
122 $result[$value->getName()] = [
123 'CODE' => $value->getName(),
124 'TYPE' => $value->getDataType(),
125 ];
126 }
127 }
128
129 return $result;
130 }
131
136 protected static function getFieldsMap()
137 {
139 }
140
146 protected static function getMeaningfulFields()
147 {
149 }
150
155 public function getField($name)
156 {
157 return $this->fields->get($name);
158 }
159
160 protected function normalizeValue($name, $value)
161 {
162 return $value;
163 }
164
173 public function setField($name, $value)
174 {
175 $result = new Result();
176
177 $value = $this->normalizeValue($name, $value);
178
179 if ($this->eventName === null)
180 {
181 $this->eventName = static::getEntityEventName();
182 }
183
184 if ($this->eventName)
185 {
186 $eventManager = Main\EventManager::getInstance();
187 if ($eventsList = $eventManager->findEventHandlers('sale', 'OnBefore'.$this->eventName.'SetField'))
188 {
190 $event = new Main\Event('sale', 'OnBefore'.$this->eventName.'SetField', array(
191 'ENTITY' => $this,
192 'NAME' => $name,
193 'VALUE' => $value,
194 ));
195 $event->send();
196
197 if ($event->getResults())
198 {
200 foreach($event->getResults() as $eventResult)
201 {
202 if($eventResult->getType() == Main\EventResult::SUCCESS)
203 {
204 if ($eventResultData = $eventResult->getParameters())
205 {
206 if (isset($eventResultData['VALUE']) && $eventResultData['VALUE'] != $value)
207 {
208 $value = $eventResultData['VALUE'];
209 }
210 }
211 }
212 elseif($eventResult->getType() == Main\EventResult::ERROR)
213 {
214
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');
216
217 if ($eventResultData = $eventResult->getParameters())
218 {
219 if (isset($eventResultData) && $eventResultData instanceof ResultError)
220 {
222 $errorMsg = $eventResultData;
223 }
224 }
225
226 $result->addError($errorMsg);
227
228 }
229 }
230
231 if (!$result->isSuccess())
232 {
233 return $result;
234 }
235 }
236 }
237 }
238
239 $availableFields = static::getAvailableFieldsMap();
240 if (!isset($availableFields[$name]))
241 {
242 throw new Main\ArgumentOutOfRangeException("name=$name");
243 }
244
245 $oldValue = $this->fields->get($name);
246 if ($oldValue != $value || ($oldValue === null && $value !== null))
247 {
248 $r = $this->checkValueBeforeSet($name, $value);
249 if (!$r->isSuccess())
250 {
251 $result->addErrors($r->getErrors());
252 return $result;
253 }
254
255 if ($this->eventName)
256 {
257 if ($eventsList = $eventManager->findEventHandlers('sale', 'On'.$this->eventName.'SetField'))
258 {
259 $event = new Main\Event('sale', 'On'.$this->eventName.'SetField', array(
260 'ENTITY' => $this,
261 'NAME' => $name,
262 'VALUE' => $value,
263 'OLD_VALUE' => $oldValue,
264 ));
265 $event->send();
266
267 if ($event->getResults())
268 {
270 foreach($event->getResults() as $eventResult)
271 {
272 if($eventResult->getType() == Main\EventResult::SUCCESS)
273 {
274 if ($eventResultData = $eventResult->getParameters())
275 {
276 if (isset($eventResultData['VALUE']) && $eventResultData['VALUE'] != $value)
277 {
278 $value = $eventResultData['VALUE'];
279 }
280 }
281 }
282 }
283 }
284 }
285 }
286
287 $isStartField = $this->isStartField(in_array($name, static::getMeaningfulFields()));
288
289 $this->fields->set($name, $value);
290 try
291 {
292 $result = $this->onFieldModify($name, $oldValue, $value);
293
294 if ($result->isSuccess() && $this->eventName)
295 {
296 $event = new Main\Event('sale', 'OnAfter'.$this->eventName.'SetField', array(
297 'ENTITY' => $this,
298 'NAME' => $name,
299 'VALUE' => $value,
300 'OLD_VALUE' => $oldValue,
301 ));
302 $event->send();
303 }
304
305 if ($result->isSuccess())
306 {
307 static::addChangesToHistory($name, $oldValue, $value);
308 }
309 }
310 catch (\Exception $e)
311 {
312 $this->fields->set($name, $oldValue);
313 throw $e;
314 }
315
316 if (!$result->isSuccess())
317 {
318 $this->fields->set($name, $oldValue);
319 }
320 else
321 {
322 if ($isStartField)
323 {
324 $hasMeaningfulFields = $this->hasMeaningfulField();
325
327 $r = $this->doFinalAction($hasMeaningfulFields);
328 if (!$r->isSuccess())
329 {
330 $result->addErrors($r->getErrors());
331 }
332 else
333 {
334 if (($data = $r->getData())
335 && !empty($data) && is_array($data))
336 {
337 $result->setData($result->getData() + $data);
338 }
339 }
340 }
341 }
342 }
343
344 return $result;
345 }
346
352 protected function checkValueBeforeSet($name, $value)
353 {
354 return new Result();
355 }
356
361 abstract public function isStartField($isMeaningfulField = false);
362
366 abstract public function clearStartField();
367
371 abstract public function hasMeaningfulField();
372
377 abstract public function doFinalAction($hasMeaningfulField = false);
378
383 abstract public function setMathActionOnly($value = false);
384
388 abstract public function isMathActionOnly();
389
397 public function setFieldNoDemand($name, $value)
398 {
399 $allFields = static::getAllFields();
400 if (!isset($allFields[$name]))
401 {
402 throw new Main\ArgumentOutOfRangeException($name);
403 }
404
405 $value = $this->normalizeValue($name, $value);
406
407 $oldValue = $this->fields->get($name);
408
409 if ($oldValue != $value || ($oldValue === null && $value !== null))
410 {
411 $this->fields->set($name, $value);
412 static::addChangesToHistory($name, $oldValue, $value);
413 }
414 }
415
424 public function setFields(array $values)
425 {
426 $resultData = array();
427 $result = new Result();
428 $oldValues = null;
429
430 foreach ($values as $key => $value)
431 {
432 $oldValues[$key] = $this->fields->get($key);
433 }
434
435 if ($this->eventName === null)
436 {
437 $this->eventName = static::getEntityEventName();
438 }
439
440 if ($this->eventName)
441 {
442 $eventManager = Main\EventManager::getInstance();
443 if ($eventsList = $eventManager->findEventHandlers('sale', 'OnBefore'.$this->eventName.'SetFields'))
444 {
445 $event = new Main\Event('sale', 'OnBefore'.$this->eventName.'SetFields', array(
446 'ENTITY' => $this,
447 'VALUES' => $values,
448 'OLD_VALUES' => $oldValues
449 ));
450 $event->send();
451
452 if ($event->getResults())
453 {
455 foreach($event->getResults() as $eventResult)
456 {
457 if($eventResult->getType() == Main\EventResult::SUCCESS)
458 {
459 if ($eventResultData = $eventResult->getParameters())
460 {
461 if (isset($eventResultData['VALUES']))
462 {
463 $values = $eventResultData['VALUES'];
464 }
465 }
466 }
467 elseif($eventResult->getType() == Main\EventResult::ERROR)
468 {
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');
470
471 if ($eventResultData = $eventResult->getParameters())
472 {
473 if (isset($eventResultData) && $eventResultData instanceof ResultError)
474 {
476 $errorMsg = $eventResultData;
477 }
478 }
479
480 $result->addError($errorMsg);
481 }
482 }
483 }
484 }
485 }
486
487 if (!$result->isSuccess())
488 {
489 return $result;
490 }
491
492 $values = $this->onBeforeSetFields($values);
493
494 $isStartField = $this->isStartField();
495
496 foreach ($values as $key => $value)
497 {
498 $r = $this->setField($key, $value);
499 if (!$r->isSuccess())
500 {
501 $data = $r->getData();
502 if (!empty($data) && is_array($data))
503 {
504 $resultData = array_merge($resultData, $data);
505 }
506 $result->addErrors($r->getErrors());
507 }
508 elseif ($r->hasWarnings())
509 {
510 $result->addWarnings($r->getWarnings());
511 }
512 }
513
514 if (!empty($resultData))
515 {
516 $result->setData($resultData);
517 }
518
519 if ($isStartField)
520 {
521 $hasMeaningfulFields = $this->hasMeaningfulField();
522
524 $r = $this->doFinalAction($hasMeaningfulFields);
525 if (!$r->isSuccess())
526 {
527 $result->addErrors($r->getErrors());
528 }
529
530 if (($data = $r->getData())
531 && !empty($data) && is_array($data))
532 {
533 $result->setData(array_merge($result->getData(), $data));
534 }
535 }
536
537 return $result;
538 }
539
544 protected function onBeforeSetFields(array $values)
545 {
546 return $values;
547 }
548
555 public function setFieldsNoDemand(array $values)
556 {
557 foreach ($values as $key => $value)
558 {
559 $this->setFieldNoDemand($key, $value);
560 }
561 }
562
570 public function initField($name, $value)
571 {
572 $allFields = static::getAllFields();
573 if (!isset($allFields[$name]))
574 {
575 throw new Main\ArgumentOutOfRangeException($name);
576 }
577
578 $this->fields->init($name, $value);
579 }
580
587 public function initFields(array $values)
588 {
589 foreach ($values as $key => $value)
590 {
591 $this->initField($key, $value);
592 }
593 }
594
598 public function getFieldValues()
599 {
600 return $this->fields->getValues();
601 }
602
607 public function getFields()
608 {
609 return $this->fields;
610 }
611
618 protected function onFieldModify($name, $oldValue, $value)
619 {
620 return new Result();
621 }
622
626 public function getId()
627 {
628 return (int)$this->getField("ID");
629 }
630
636 protected function addChangesToHistory($name, $oldValue = null, $value = null)
637 {
638 return;
639 }
640
647 public static function getEntityEventName()
648 {
649 throw new Main\NotImplementedException(static::class . ':' . __FUNCTION__ . ' is not implemented');
650 }
651
655 public static function getClassName()
656 {
657 return get_called_class();
658 }
659
663 public function isChanged()
664 {
665 return (bool)$this->fields->getChangedValues();
666 }
667
672 public function markFieldCustom(string $name)
673 {
674 $fields = static::getCustomizableFields();
675 if (!isset($fields[$name]))
676 {
678 Main\Localization\Loc::getMessage(
679 'SALE_INTERNALS_ENTITY_FIELD_IS_NOT_CUSTOMIZABLE',
680 ['#FIELD#' => $name]
681 )
682 );
683 }
684
685 $this->fields->markCustom($name);
686 }
687
692 public function unmarkFieldCustom(string $name)
693 {
694 $fields = static::getCustomizableFields();
695 if (!isset($fields[$name]))
696 {
698 Main\Localization\Loc::getMessage(
699 'SALE_INTERNALS_ENTITY_FIELD_IS_NOT_CUSTOMIZABLE',
700 ['#FIELD#' => $name]
701 )
702 );
703 }
704
705 $this->fields->unmarkCustom($name);
706 }
707
712 public function isMarkedFieldCustom(string $name) : bool
713 {
714 return $this->fields->isMarkedCustom($name);
715 }
716
720 public function verify()
721 {
722 return new Result();
723 }
724
728 public function clearChanged()
729 {
730 $this->fields->clearChanged();
731 }
732
733 public function toArray() : array
734 {
735 return $this->getFieldValues();
736 }
737}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
onFieldModify($name, $oldValue, $value)
Definition entity.php:618
setMathActionOnly($value=false)
isMarkedFieldCustom(string $name)
Definition entity.php:712
checkValueBeforeSet($name, $value)
Definition entity.php:352
markFieldCustom(string $name)
Definition entity.php:672
unmarkFieldCustom(string $name)
Definition entity.php:692
doFinalAction($hasMeaningfulField=false)
initField($name, $value)
Definition entity.php:570
isStartField($isMeaningfulField=false)
initFields(array $values)
Definition entity.php:587
__construct(array $fields=array())
Definition entity.php:17
onBeforeSetFields(array $values)
Definition entity.php:544
addChangesToHistory($name, $oldValue=null, $value=null)
Definition entity.php:636
setFieldNoDemand($name, $value)
Definition entity.php:397
normalizeValue($name, $value)
Definition entity.php:160
setFieldsNoDemand(array $values)
Definition entity.php:555