Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
event.php
1<?php
9namespace Bitrix\Main\ORM;
10
14
16{
17 protected $entity;
19
26 public function __construct(Entity $entity, $type, array $parameters = array(), $withNamespace = false)
27 {
28 if ($withNamespace)
29 {
30 $eventName = $entity->getNamespace() . $entity->getName() . '::' . $type;
31 $this->entityEventType = $type;
32 }
33 else
34 {
35 $eventName = $entity->getName().$type;
36 }
37
38 parent::__construct($entity->getModule(), $eventName, $parameters);
39
40 $this->entity = $entity;
41 }
42
46 public function getEntity()
47 {
48 return $this->entity;
49 }
50
58 public function getErrors(Result $result)
59 {
60 $hasErrors = false;
61
63 foreach($this->getResults() as $evenResult)
64 {
65 if($evenResult->getType() === EventResult::ERROR)
66 {
67 $hasErrors = true;
68 $result->addErrors($evenResult->getErrors());
69 }
70 }
71 return $hasErrors;
72 }
73
81 public function mergeFields(array $data)
82 {
83 if ($this->getResults() != null)
84 {
86 foreach($this->getResults() as $evenResult)
87 {
88 $removed = $evenResult->getUnset();
89 foreach($removed as $val)
90 {
91 unset($data[$val]);
92 }
93
94 $modified = $evenResult->getModified();
95 if(!empty($modified))
96 {
97 $data = array_merge($data, $modified);
98 }
99 }
100 }
101 return $data;
102 }
103
104 public function mergeObjectFields(EntityObject $object)
105 {
106 if ($this->getResults() != null)
107 {
109 foreach($this->getResults() as $evenResult)
110 {
111 $removed = $evenResult->getUnset();
112 foreach($removed as $fieldName)
113 {
114 // sometimes data array can be used for storing non-entity data
115 if ($object->entity->hasField($fieldName))
116 {
117 $object->unset($fieldName);
118 }
119 }
120
121 $modified = $evenResult->getModified();
122 if(!empty($modified))
123 {
124 foreach ($modified as $fieldName => $value)
125 {
126 $object->set($fieldName, $value);
127 }
128 }
129 }
130 }
131 }
132
133 public function send($sender = null)
134 {
135 static $events = array(
136 DataManager::EVENT_ON_BEFORE_ADD => true,
137 DataManager::EVENT_ON_ADD => true,
138 DataManager::EVENT_ON_AFTER_ADD => true,
139 DataManager::EVENT_ON_BEFORE_UPDATE => true,
140 DataManager::EVENT_ON_UPDATE => true,
141 DataManager::EVENT_ON_AFTER_UPDATE => true,
142 DataManager::EVENT_ON_BEFORE_DELETE => true,
143 DataManager::EVENT_ON_DELETE => true,
144 DataManager::EVENT_ON_AFTER_DELETE => true,
145 );
146
147 if(isset($events[$this->entityEventType]))
148 {
149 //The event handler function name magically equals to the event type (e.g. "OnBeforeAdd").
150 //There are emtpy handlers in the DataManager class.
151 $result = call_user_func_array(array($this->entity->getDataClass(), $this->entityEventType), array($this));
152 if (($result !== null) && !($result instanceof EventResult))
153 {
154 $result = new EventResult();
155 }
156 if($result !== null)
157 {
158 $this->addResult($result);
159 }
160 }
161
162 parent::send($sender);
163 }
164}
addResult(EventResult $result)
Definition event.php:129
send($sender=null)
Definition event.php:133
__construct(Entity $entity, $type, array $parameters=array(), $withNamespace=false)
Definition event.php:26
addErrors(array $errors)
Definition result.php:98