Ejemplo n.º 1
0
 /**
  * Add the filter for the item with the given id from the parent data provider to the given config.
  *
  * @param ModelIdInterface $idParent The id of the parent item.
  *
  * @param ConfigInterface  $config   The config to add the filter to.
  *
  * @return ConfigInterface
  *
  * @throws DcGeneralRuntimeException When the parent item is not found.
  */
 private function addParentFilter($idParent, $config)
 {
     $environment = $this->getEnvironment();
     $definition = $environment->getDataDefinition();
     $providerName = $definition->getBasicDefinition()->getDataProvider();
     $parentProviderName = $idParent->getDataProviderName();
     $parentProvider = $environment->getDataProvider($parentProviderName);
     if ($definition->getBasicDefinition()->getParentDataProvider() !== $parentProviderName) {
         throw new DcGeneralRuntimeException('Unexpected parent provider ' . $parentProviderName . ' (expected ' . $definition->getBasicDefinition()->getParentDataProvider() . ')');
     }
     if ($parentProvider) {
         $objParent = $parentProvider->fetch($parentProvider->getEmptyConfig()->setId($idParent->getId()));
         if (!$objParent) {
             throw new DcGeneralRuntimeException('Parent item ' . $idParent->getSerialized() . ' not found in ' . $parentProviderName);
         }
         $condition = $definition->getModelRelationshipDefinition()->getChildCondition($parentProviderName, $providerName);
         if ($condition) {
             $arrBaseFilter = $config->getFilter();
             $arrFilter = $condition->getFilter($objParent);
             if ($arrBaseFilter) {
                 $arrFilter = array_merge($arrBaseFilter, $arrFilter);
             }
             $config->setFilter(array(array('operation' => 'AND', 'children' => $arrFilter)));
         }
     }
     return $config;
 }
Ejemplo n.º 2
0
 /**
  * Fetch the model.
  *
  * @param ModelIdInterface $modelId The model id.
  *
  * @return ModelInterface
  *
  * @throws DcGeneralRuntimeException If no model are found.
  */
 protected function fetchModel(ModelIdInterface $modelId)
 {
     $dataProvider = $this->getEnvironment()->getDataProvider($modelId->getDataProviderName());
     $model = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId()));
     if (!$model || !$model->getId()) {
         throw new DcGeneralRuntimeException('Could not load model with id ' . $modelId->getSerialized());
     }
     return $model;
 }
 /**
  * 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;
 }
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function equals(ModelIdInterface $modelId)
 {
     // It is exactly the same id
     if ($this === $modelId) {
         return true;
     }
     return !($this->getDataProviderName() !== $modelId->getDataProviderName() || $this->getId() !== $modelId->getId());
 }
Ejemplo n.º 5
0
 /**
  * Delete all deep models.
  *
  * @param ModelIdInterface $modelId The Model Id.
  *
  * @return void
  *
  * @SuppressWarnings(PHPMD.LongVariableName)
  */
 protected function deepDelete(ModelIdInterface $modelId)
 {
     $environment = $this->getEnvironment();
     $dataDefinition = $environment->getDataDefinition();
     /** @var DefaultModelRelationshipDefinition $relationships */
     $relationships = $dataDefinition->getDefinition('model-relationships');
     $childConditions = $relationships->getChildConditions($modelId->getDataProviderName());
     // delete child element before delete parent element
     /** @var ParentChildConditionInterface $childCondition */
     foreach ($childConditions as $childCondition) {
         $destinationChildConditions = $relationships->getChildConditions($childCondition->getDestinationName());
         if (empty($destinationChildConditions)) {
             continue;
         }
         $dataProvider = $environment->getDataProvider($modelId->getDataProviderName());
         $model = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId()));
         $destinationChildDataProvider = $environment->getDataProvider($childCondition->getDestinationName());
         $filters = $childCondition->getFilter($model);
         /** @var DefaultCollection $destinationChildModels */
         $destinationChildModels = $destinationChildDataProvider->fetchAll($dataProvider->getEmptyConfig()->setFilter($filters));
         if ($destinationChildModels->count() < 1) {
             continue;
         }
         foreach ($destinationChildModels as $destinationChildModel) {
             $this->deepDelete(ModelId::fromModel($destinationChildModel));
         }
     }
     foreach ($childConditions as $childCondition) {
         $dataProvider = $environment->getDataProvider($modelId->getDataProviderName());
         $model = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId()));
         $childDataProvider = $environment->getDataProvider($childCondition->getDestinationName());
         $filters = $childCondition->getFilter($model);
         /** @var DefaultCollection $childModels */
         $childModels = $childDataProvider->fetchAll($dataProvider->getEmptyConfig()->setFilter($filters));
         if ($childModels->count() < 1) {
             continue;
         }
         foreach ($childModels as $childModel) {
             // Trigger event before the model will be deleted.
             $event = new PreDeleteModelEvent($this->getEnvironment(), $childModel);
             $environment->getEventDispatcher()->dispatch($event::NAME, $event);
             $childDataProvider->delete($childModel);
             // Trigger event after the model is deleted.
             $event = new PostDeleteModelEvent($environment, $childModel);
             $environment->getEventDispatcher()->dispatch($event::NAME, $event);
         }
     }
 }
Ejemplo n.º 6
0
 /**
  * Copy each children.
  *
  * @param ModelIdInterface     $modelId     The model id.
  * @param ModelInterface       $copiedModel The copied model.
  * @param EnvironmentInterface $environment The environment.
  *
  * @return void
  *
  * @SuppressWarnings(PHPMD.LongVariableName)
  */
 protected function copyEachChilds(ModelIdInterface $modelId, ModelInterface $copiedModel, EnvironmentInterface $environment)
 {
     $childDataProviderName = $environment->getInputProvider()->getParameter('ctable');
     $dataProvider = $environment->getDataProvider();
     $childDataProvider = $environment->getDataProvider($childDataProviderName);
     $modelRelationship = $environment->getParentDataDefinition()->getModelRelationshipDefinition();
     $childCondition = $modelRelationship->getChildCondition($copiedModel->getProviderName(), $childDataProviderName);
     if (!$childCondition) {
         return;
     }
     $parentModel = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId()));
     $parentFilters = $childCondition->getFilter($parentModel);
     $copiedFilters = $childCondition->getFilter($copiedModel);
     $filter = array();
     // Todo can many filter has operation equal
     foreach ($parentFilters as $index => $parentFilter) {
         if ($parentFilter['operation'] !== '=') {
             continue;
         }
         $filter = array('parent' => $parentFilter, 'copied' => $copiedFilters[$index]);
     }
     $childModels = $childDataProvider->fetchAll($childDataProvider->getEmptyConfig()->setFilter(array($filter['parent'])));
     if ($childModels->count() < 1) {
         return;
     }
     foreach ($childModels->getModelIds() as $index => $modelId) {
         $childModel = $childModels->get($index);
         $copyModelId = ModelId::fromModel($childModel);
         $copiedChildModel = null;
         if ($environment->getDataDefinition()->getName() !== $copyModelId->getDataProviderName()) {
             $copiedChildModel = $this->copyParent($copyModelId, $environment);
         }
         if ($environment->getDataDefinition()->getName() === $copyModelId->getDataProviderName() && !$copiedChildModel) {
             $copiedChildModel = $this->copyHandler($copyModelId, $environment);
         }
         $copiedChildModel->setProperty($filter['copied']['property'], $filter['copied']['value']);
         $childDataProvider->save($copiedChildModel);
     }
 }