Exemplo n.º 1
0
 /**
  * Computes the difference of the collection.
  *
  * @param CollectionInterface $collection The collection to compute the difference for.
  *
  * @return CollectionInterface The collection containing all the entries from this collection that are not present
  *                             in the given collection.
  */
 public function diff($collection)
 {
     $diff = new DefaultCollection();
     /** @var ModelInterface $localModel */
     foreach ($this as $localModel) {
         /** @var ModelInterface $otherModel */
         foreach ($collection as $otherModel) {
             if ($localModel->getProviderName() == $otherModel->getProviderName() && $localModel->getId() == $otherModel->getId()) {
                 continue;
             }
             $diff->push($localModel);
         }
     }
     return $diff;
 }
 /**
  * Apply sorting and persist all models.
  *
  * @param array            $actions       The actions collection.
  * @param ModelIdInterface $after         The previous model id.
  * @param ModelIdInterface $into          The hierarchical parent model id.
  * @param ModelIdInterface $parentModelId The parent model id.
  * @param array            $items         Write-back clipboard items.
  *
  * @return DefaultCollection|ModelInterface[]
  *
  * @throws DcGeneralRuntimeException When the parameters for the pasting destination are invalid.
  */
 private function sortAndPersistModels(array $actions, ModelIdInterface $after = null, ModelIdInterface $into = null, ModelIdInterface $parentModelId = null, array &$items = array())
 {
     $environment = $this->getEnvironment();
     $dataDefinition = $environment->getDataDefinition();
     $manualSorting = ViewHelpers::getManualSortingProperty($environment);
     /** @var DefaultCollection|ModelInterface[] $models */
     $models = new DefaultCollection();
     foreach ($actions as $action) {
         $models->push($action['model']);
         $items[] = $action['item'];
     }
     // Trigger for each model the pre persist event.
     foreach ($models as $model) {
         $event = new PrePasteModelEvent($environment, $model);
         $environment->getEventDispatcher()->dispatch($event::NAME, $event);
     }
     if ($after && $after->getId()) {
         $dataProvider = $environment->getDataProvider($after->getDataProviderName());
         $previous = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($after->getId()));
         $this->pasteAfter($previous, $models, $manualSorting);
     } elseif ($into && $into->getId()) {
         $dataProvider = $environment->getDataProvider($into->getDataProviderName());
         $parent = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($into->getId()));
         $this->pasteInto($parent, $models, $manualSorting);
     } elseif ($after && $after->getId() == '0' || $into && $into->getId() == '0') {
         if ($dataDefinition->getBasicDefinition()->getMode() === BasicDefinitionInterface::MODE_HIERARCHICAL) {
             foreach ($models as $model) {
                 // Paste top means root in hierarchical mode!
                 $this->setRootModel($model);
             }
         }
         $this->pasteTop($models, $manualSorting, $parentModelId);
     } elseif ($parentModelId) {
         if ($manualSorting) {
             $this->pasteTop($models, $manualSorting, $parentModelId);
         } else {
             $dataProvider = $environment->getDataProvider();
             $dataProvider->saveEach($models);
         }
     } else {
         throw new DcGeneralRuntimeException('Invalid parameters.');
     }
     // Trigger for each model the past persist event.
     foreach ($models as $model) {
         $event = new PostPasteModelEvent($environment, $model);
         $environment->getEventDispatcher()->dispatch($event::NAME, $event);
     }
     return $models;
 }
 /**
  * @param VersionModel[] $versions
  *
  * @return DefaultCollection
  */
 public function mapVersions($versions, $activeVersion = false)
 {
     $collection = new DefaultCollection();
     foreach ($versions as $version) {
         $collection->push($this->mapVersion($version, $activeVersion));
     }
     return $collection;
 }