Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
commonelement.php
1<?php
10
19
24abstract class CommonElement extends EO_CommonElement
25{
35 public function setIblockSectionId($iblockSectionId)
36 {
37 $newIblockSectionId = (int) $iblockSectionId;
38 $actualIblockSectionId = 0;
39
40 if ($this->state !== State::RAW)
41 {
42 $this->fill('IBLOCK_SECTION_ID');
43 $actualIblockSectionId = $this->remindActual('IBLOCK_SECTION_ID');
44 }
45
46 if ($newIblockSectionId !== $actualIblockSectionId)
47 {
48 // remove old
49 if ($actualIblockSectionId > 0)
50 {
51 $oldSection = SectionTable::wakeUpObject($actualIblockSectionId);
52 $this->removeFrom('SECTIONS', $oldSection);
53 }
54
55 // add new
56 if ($newIblockSectionId > 0)
57 {
58 $newSection = SectionTable::wakeUpObject($newIblockSectionId);
59 $this->addTo('SECTIONS', $newSection);
60 }
61
62 // rewrite value
63 parent::sysSetValue('IBLOCK_SECTION_ID', $newIblockSectionId);
64 }
65
66 return $this;
67 }
68
79 public function sysSetValue($fieldName, $value)
80 {
81 $field = $this->entity->getField($fieldName);
82
83 if ($field instanceof PropertyReference)
84 {
85 // convert PropertyValue to regular reference
86 $value = $this->sysConvertPropertyValue($value, $field);
87
89 $currentValue = $this->get($fieldName);
90
91 if (empty($currentValue))
92 {
93 parent::sysSetValue($fieldName, $value);
94 }
95 else
96 {
97 // set value directly
98 $currentValue->set('VALUE', $value->get('VALUE'));
99
100 if ($this->entity->hasField('DESCRIPTION') && $value->sysHasValue('DESCRIPTION'))
101 {
102 $currentValue->set('DESCRIPTION', $value->get('DESCRIPTION'));
103 }
104 }
105
106 // mark current object as changed, or else save() will be skipped
107 if ($this->state === State::ACTUAL)
108 {
109 $this->sysChangeState(State::CHANGED);
110 }
111
112 return $this;
113 }
114
115 return parent::sysSetValue($fieldName, $value);
116 }
117
128 public function sysAddToCollection($fieldName, $remoteObject)
129 {
130 $fieldName = StringHelper::strtoupper($fieldName);
131 $field = $this->entity->getField($fieldName);
132
133 if ($field instanceof PropertyOneToMany)
134 {
135 // convert PropertyValue to regular relation object
136 $remoteObject = $this->sysConvertPropertyValue($remoteObject, $field);
137
138 // check for duplicates
139 if (in_array(
140 $field->getIblockElementProperty()->getPropertyType(),
142 true
143 ))
144 {
145 // it's ok to have duplicates for this type
146 }
147 else
148 {
149 // check for duplicates and skip ot
151 $collection = $this->get($fieldName);
152
153 // we need filled collection to check value in it
154 if (empty($collection) || !$collection->sysIsFilled())
155 {
156 $collection = $this->fill($fieldName);
157 }
158
159 foreach ($collection as $refObject)
160 {
161 // we have original scalar in VALUE
162 if ($refObject->get('VALUE') === $remoteObject->get('VALUE'))
163 {
164 // skip duplicate
165 return false;
166 }
167 }
168 }
169 }
170
171 return parent::sysAddToCollection($fieldName, $remoteObject);
172 }
173
184 public function sysRemoveFromCollection($fieldName, $remoteObject)
185 {
186 $fieldName = StringHelper::strtoupper($fieldName);
187 $field = $this->entity->getField($fieldName);
188
189 if ($field instanceof PropertyOneToMany)
190 {
191 // convert PropertyValue to regular relation object
192 $valueObject = $this->sysConvertPropertyValue($remoteObject, $field);
193
194 if ($valueObject->sysHasPrimary())
195 {
196 // existing object. nothing to do, just call parent remove
197 $remoteObject = $valueObject;
198 }
199 else
200 {
201 // find relation object by value and remove it
203 $collection = $this->get($fieldName);
204
205 // we need filled collection to check value in it
206 if (empty($collection) || !$collection->sysIsFilled())
207 {
208 $collection = $this->fill($fieldName);
209 }
210
211 $foundValue = false;
212
213 foreach ($collection as $refObject)
214 {
215 // find original object with that value
216 if ($valueObject->get('VALUE') === $refObject->get('VALUE'))
217 {
218 $foundValue = true;
219 $remoteObject = $refObject;
220 break;
221 }
222 }
223
224 if (!$foundValue)
225 {
226 // nothing to do, value was not found in collection
227 return false;
228 }
229 }
230 }
231
232 return parent::sysRemoveFromCollection($fieldName, $remoteObject);
233 }
234
238 public function sysSaveCurrentReferences()
239 {
240 return;
241 }
242
251 protected function sysConvertPropertyValue($value, $field)
252 {
253 $valueEntity = $field->getRefEntity();
254 $valueObjectClass = $valueEntity->getObjectClass();
255
256 if ($value instanceof $valueObjectClass)
257 {
258 // nothing to do
259 return $value;
260 }
261
263 $valueObject = $valueEntity->createObject();
264
265 // if we don't have primary right now, repeat setter later
266 if ($this->state == State::RAW)
267 {
268 $this->sysAddOnPrimarySetListener(function (EntityObject $localObject) use ($valueObject) {
269 $valueObject->set('IBLOCK_ELEMENT_ID', $localObject->get('ID'));
270 });
271 }
272 else
273 {
274 // set base fields
275 $valueObject->set('IBLOCK_ELEMENT_ID', $this->get('ID'));
276 }
277
278 if ($valueEntity->hasField('IBLOCK_PROPERTY_ID'))
279 {
280 $valueObject->set('IBLOCK_PROPERTY_ID', $field->getIblockElementProperty()->getId());
281 }
282
283 // set value fields
284 if ($value instanceof PropertyValue)
285 {
286 $valueObject->set('VALUE', $value->getValue());
287
288 if ($value->hasDescription())
289 {
290 $valueObject->set('DESCRIPTION', $value->getDescription());
291 }
292 }
293 else
294 {
295 $valueObject->set('VALUE', $value);
296 }
297
298 return $valueObject;
299 }
300}
setIblockSectionId($iblockSectionId)