Bitrix-D7 22.2
Загрузка...
Поиск...
Не найдено
entitycollection.php
См. документацию.
1<?php
3
6
11abstract class EntityCollection
12 extends CollectionBase
13{
14 private $index = -1;
15
16 protected $isClone = false;
17
18 protected $anyItemDeleted = false;
19 protected $anyItemAdded = false;
20
24 protected function __construct() {}
25
35 public function onItemModify(CollectableEntity $item, $name = null, $oldValue = null, $value = null)
36 {
37 return new Sale\Result();
38 }
39
44 public static function getRegistryType()
45 {
47 }
48
56 public function deleteItem($index)
57 {
58 if (!isset($this->collection[$index]))
59 {
60 throw new Main\ArgumentOutOfRangeException("collection item index wrong");
61 }
62
63 $oldItem = $this->collection[$index];
64
65 $eventManager = Main\EventManager::getInstance();
66 $eventsList = $eventManager->findEventHandlers('sale', 'OnBeforeCollectionDeleteItem');
67 if (!empty($eventsList))
68 {
70 $event = new Main\Event('sale', 'OnBeforeCollectionDeleteItem', array(
71 'COLLECTION' => $this->collection,
72 'ENTITY' => $oldItem,
73 ));
74 $event->send();
75 }
76
77 unset($this->collection[$index]);
78 $this->setAnyItemDeleted(true);
79
80 return $oldItem;
81 }
82
88 protected function addItem(CollectableEntity $item)
89 {
90 $index = $this->createIndex();
91 $item->setInternalIndex($index);
92
93 $this->collection[$index] = $item;
94 $this->setAnyItemAdded(true);
95
96 $eventManager = Main\EventManager::getInstance();
97 $eventsList = $eventManager->findEventHandlers('sale', 'OnCollectionAddItem');
98 if (!empty($eventsList))
99 {
101 $event = new Main\Event('sale', 'OnCollectionAddItem', array(
102 'COLLECTION' => $this->collection,
103 'ENTITY' => $item,
104 ));
105 $event->send();
106 }
107
108 return $item;
109 }
110
114 protected function createIndex()
115 {
116 $this->index++;
117 return $this->index;
118 }
119
124 public function clearCollection()
125 {
126 $this->callEventOnBeforeCollectionClear();
127
129 foreach ($this->getDeletableItems() as $item)
130 {
131 $item->delete();
132 }
133 }
134
138 protected function getDeletableItems()
139 {
140 return $this->collection;
141 }
142
143
147 protected function callEventOnBeforeCollectionClear()
148 {
149 $eventManager = Main\EventManager::getInstance();
150
151 $eventsList = $eventManager->findEventHandlers('sale', 'OnBeforeCollectionClear');
152 if (!empty($eventsList))
153 {
155 $event = new Main\Event('sale', 'OnBeforeCollectionClear', array(
156 'COLLECTION' => $this->collection,
157 ));
158 $event->send();
159 }
160 }
161
168 public function getItemById($id)
169 {
170 if (intval($id) <= 0)
171 {
172 throw new Main\ArgumentNullException('id');
173 }
174
175 $index = $this->getIndexById($id);
176 if ($index === null)
177 {
178 return null;
179 }
180
181 if (isset($this->collection[$index]))
182 {
183 return $this->collection[$index];
184 }
185
186 return null;
187 }
188
189
196 public function getIndexById($id)
197 {
198 if (intval($id) <= 0)
199 {
200 throw new Main\ArgumentNullException('id');
201 }
202
204 foreach ($this->collection as $item)
205 {
206 if ($item->getId() > 0 && $id == $item->getId())
207 {
208 return $item->getInternalIndex();
209 }
210 }
211 return null;
212 }
213
220 public function getItemByIndex($index)
221 {
222 if (intval($index) < 0)
223 {
224 throw new Main\ArgumentNullException('id');
225 }
226
228 foreach ($this->collection as $item)
229 {
230 if ($item->getInternalIndex() == $index)
231 {
232 return $item;
233 }
234 }
235 return null;
236 }
237
241 abstract protected function getEntityParent();
242
247 public function isStartField($isMeaningfulField = false)
248 {
249 $parent = $this->getEntityParent();
250 if ($parent === null)
251 {
252 return false;
253 }
254
255 return $parent->isStartField($isMeaningfulField);
256 }
257
261 public function clearStartField()
262 {
263 $parent = $this->getEntityParent();
264 if ($parent === null)
265 {
266 return false;
267 }
268
269 return $parent->clearStartField();
270 }
271
275 public function hasMeaningfulField()
276 {
277 $parent = $this->getEntityParent();
278 if ($parent === null)
279 {
280 return false;
281 }
282
283 return $parent->hasMeaningfulField();
284 }
285
290 public function doFinalAction($hasMeaningfulField = false)
291 {
292 $parent = $this->getEntityParent();
293 if ($parent === null)
294 {
295 return new Sale\Result();
296 }
297
298 return $parent->doFinalAction($hasMeaningfulField);
299 }
300
304 public function isMathActionOnly()
305 {
306 $parent = $this->getEntityParent();
307 if ($parent === null)
308 {
309 return false;
310 }
311
312 return $parent->isMathActionOnly();
313 }
314
319 public function setMathActionOnly($value = false)
320 {
321 $parent = $this->getEntityParent();
322 if ($parent == null)
323 {
324 return false;
325 }
326
327 return $parent->setMathActionOnly($value);
328 }
329
333 public function isChanged()
334 {
335 if (count($this->collection) > 0)
336 {
338 foreach ($this->collection as $item)
339 {
340 if ($item->isChanged())
341 {
342 return true;
343 }
344 }
345 }
346
347 return $this->isAnyItemDeleted() || $this->isAnyItemAdded();
348 }
349
353 public function verify()
354 {
355 return new Sale\Result();
356 }
357
361 public function isClone()
362 {
363 return $this->isClone;
364 }
365
366
370 public function isAnyItemDeleted()
371 {
373 }
374
380 protected function setAnyItemDeleted($value)
381 {
382 return $this->anyItemDeleted = ($value === true);
383 }
384
388 public function isAnyItemAdded()
389 {
390 return $this->anyItemAdded;
391 }
392
398 protected function setAnyItemAdded($value)
399 {
400 return $this->anyItemAdded = ($value === true);
401 }
402
406 public function clearChanged()
407 {
408 if (!empty($this->collection))
409 {
410 foreach ($this->collection as $entityItem)
411 {
412 if ($entityItem instanceof Entity)
413 {
414 $entityItem->clearChanged();
415 }
416 }
417 }
418 }
419
426 public function createClone(\SplObjectStorage $cloneEntity)
427 {
428 if ($this->isClone() && $cloneEntity->contains($this))
429 {
430 return $cloneEntity[$this];
431 }
432
433 $entityClone = clone $this;
434 $entityClone->isClone = true;
435
436 if (!$cloneEntity->contains($this))
437 {
438 $cloneEntity[$this] = $entityClone;
439 }
440
445 foreach ($entityClone->collection as $key => $entity)
446 {
447 if (!$cloneEntity->contains($entity))
448 {
449 $cloneEntity[$entity] = $entity->createClone($cloneEntity);
450 }
451
452 $entityClone->collection[$key] = $cloneEntity[$entity];
453 }
454
455 return $entityClone;
456 }
457}
doFinalAction($hasMeaningfulField=false)
isStartField($isMeaningfulField=false)
onItemModify(CollectableEntity $item, $name=null, $oldValue=null, $value=null)