Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
commonelementtable.php
1<?php
9namespace Bitrix\Iblock\ORM;
10
21use CIBlock;
22use CIBlockProperty;
23
28abstract class CommonElementTable extends DataManager
29{
33 public static function getEntityClass()
34 {
35 return ElementEntity::class;
36 }
37
38 public static function getQueryClass()
39 {
40 return Query::class;
41 }
42
43 public static function setDefaultScope($query)
44 {
45 return $query->where("IBLOCK_ID", static::getEntity()->getIblock()->getId());
46 }
47
48 public static function getTableName()
49 {
51 }
52
53 public static function getMap()
54 {
55 return ElementTable::getMap();
56 }
57
65 public static function getEntity()
66 {
67 $class = static::getEntityClass()::normalizeEntityClass(get_called_class());
68
69 if (!isset(static::$entity[$class]))
70 {
72 $parentClass = get_parent_class(get_called_class());
73
74 if (!in_array(
75 $parentClass,
76 [ElementV1Table::class, ElementV2Table::class]
77 ))
78 {
79 static::$entity[$class] = clone $parentClass::getEntity();
80 static::$entity[$class]->reinitialize($class);
81 }
82 else
83 {
84 static::$entity[$class] = parent::getEntity();
85 }
86 }
87
88 return static::$entity[$class];
89 }
90
99 public static function addMulti($rows, $ignoreEvents = false)
100 {
101 $result = null;
102
103 foreach ($rows as $row)
104 {
105 if (!empty($row['__object']))
106 {
107 $result = $row['__object']->save();
108 }
109 else
110 {
111 $result = static::add($row);
112 }
113 }
114
115 return $result;
116 }
117
118 public static function onBeforeAdd(Event $event)
119 {
120 $object = $event->getParameter('object');
121 $fields = static::getEntity()->getFields();
122
123 $result = new EventResult;
124
125 foreach ($fields as $field)
126 {
127 // check required properties
128 $hasEmptyRequiredValue = false;
129
130 if ($field instanceof PropertyReference || $field instanceof PropertyOneToMany)
131 {
132 $property = $field->getIblockElementProperty();
133
134 if ($property->getIsRequired())
135 {
137 $valueContainer = $object->get($field->getName());
138
139 if (empty($valueContainer))
140 {
141 $hasEmptyRequiredValue = true;
142 }
143
144 // check with GetLength
145 if ($valueContainer instanceof ValueStorage)
146 {
147 $userType = CIBlockProperty::GetUserType($property->getUserType());
148
149 if(array_key_exists("GetLength", $userType))
150 {
151 $length = call_user_func_array(
152 $userType["GetLength"],
153 [
154 $property->collectValues(),
155 ["VALUE" => $valueContainer->getValue()]
156 ]
157 );
158 }
159 else
160 {
161 $length = mb_strlen($valueContainer->getValue());
162 }
163
164 $hasEmptyRequiredValue = ($length <= 0);
165 }
166
167
168 if ($hasEmptyRequiredValue)
169 {
170 $result->addError(new FieldError(
171 $field,
173 "MAIN_ENTITY_FIELD_REQUIRED",
174 ["#FIELD#" => $property->getName()]
175 ),
176 FieldError::EMPTY_REQUIRED
177 ));
178 }
179 }
180 }
181 }
182
183 return $result;
184 }
185
186 public static function onAfterAdd(Event $event)
187 {
188 $elementId = (int) end($event->getParameters()['primary']);
189 $iblockId = static::getEntity()->getIblock()->getId();
190
191 // clear tag cache
192 CIBlock::clearIblockTagCache($iblockId);
193
194 // update index
195 Manager::updateElementIndex($iblockId, $elementId);
196 }
197
198 public static function onAfterUpdate(Event $event)
199 {
200 $elementId = (int) end($event->getParameters()['primary']);
201 $iblockId = static::getEntity()->getIblock()->getId();
202
203 // clear tag cache
204 CIBlock::clearIblockTagCache($iblockId);
205
206 // update index
207 Manager::updateElementIndex($iblockId, $elementId);
208 }
209
210 public static function onAfterDelete(Event $event)
211 {
212 parent::onAfterDelete($event);
213
214 $elementId = (int) end($event->getParameters()['primary']);
215 $iblockId = static::getEntity()->getIblock()->getId();
216 $connection = static::getEntity()->getConnection();
217
218 // delete property values
219 $tables = [static::getEntity()->getSingleValueTableName(), static::getEntity()->getMultiValueTableName()];
220
221 foreach (array_unique($tables) as $table)
222 {
223 $connection->query("DELETE FROM {$table} WHERE IBLOCK_ELEMENT_ID = {$elementId}");
224 }
225
226 // clear tag cache
227 CIBlock::clearIblockTagCache($iblockId);
228
229 // delete index
230 Manager::deleteElementIndex($iblockId, $elementId);
231 }
232}
static addMulti($rows, $ignoreEvents=false)
static deleteElementIndex($iblockId, $elementId)
Definition manager.php:237
static updateElementIndex($iblockId, $elementId)
Definition manager.php:264
getParameter($key)
Definition event.php:80
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static onBeforeAdd(Event $event)