/** * Retrieve the provider name of the model from this item. * * @return string */ public function getDataProviderName() { if ($this->modelId) { return $this->modelId->getDataProviderName(); } return $this->providerName; }
/** * 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; }
/** * 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; }
/** * Guard that the data container is not in edit only mode. * * @param ModelIdInterface $modelId The model id. * * @return void * * @throws EditOnlyModeException If data container is in edit only mode. */ protected function guardNotEditOnly(ModelIdInterface $modelId) { if ($this->getEnvironment()->getDataDefinition()->getBasicDefinition()->isEditOnlyMode()) { throw new EditOnlyModeException($modelId->getDataProviderName()); } }
/** * 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; }
/** * Redirect to edit mask. * * @param EnvironmentInterface $environment The environment. * @param ModelIdInterface $copiedModelId The model id. * * @return void */ protected function redirect($environment, $copiedModelId) { // Build a clean url to remove the copy related arguments instad of using the AddToUrlEvent. $url = new BackendUrlBuilder(); $url->setPath('contao/main.php')->setQueryParameter('do', $environment->getInputProvider()->getParameter('do'))->setQueryParameter('table', $copiedModelId->getDataProviderName())->setQueryParameter('act', 'edit')->setQueryParameter('id', $copiedModelId->getSerialized()); $redirectEvent = new RedirectEvent($url->getUrl()); $environment->getEventDispatcher()->dispatch(ContaoEvents::CONTROLLER_REDIRECT, $redirectEvent); }
/** * {@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()); }
/** * 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); } } }