/** * This method parses the parent-child conditions. * * @param ContainerInterface $container The container where the data shall be stored. * * @return void */ protected function parseParentChildConditions(ContainerInterface $container) { if ($container->hasDefinition(ModelRelationshipDefinitionInterface::NAME)) { $definition = $container->getDefinition(ModelRelationshipDefinitionInterface::NAME); } else { $definition = new DefaultModelRelationshipDefinition(); } // If mode is 5, we need to define tree view. if ($this->getFromDca('list/sorting/mode') === 5) { $rootProvider = $this->getRootProviderName($container); if (($relationship = $definition->getRootCondition()) === null) { $relationship = new RootCondition(); $relationship->setSourceName($rootProvider); $definition->setRootCondition($relationship); $builder = FilterBuilder::fromArrayForRoot()->getFilter(); } else { $builder = FilterBuilder::fromArrayForRoot($relationship->getFilterArray())->getFilter(); } $relationship->setSetters(array_merge_recursive(array(array('property' => 'pid', 'value' => '0'))), $relationship->getSetters()); $builder->andPropertyEquals('pid', '0'); $relationship->setFilterArray($builder->getAllAsArray()); if (($relationship = $definition->getChildCondition($rootProvider, $rootProvider)) === null) { $relationship = new ParentChildCondition(); $relationship->setSourceName($rootProvider)->setDestinationName($rootProvider); $definition->addChildCondition($relationship); $builder = FilterBuilder::fromArray()->getFilter(); } else { $builder = FilterBuilder::fromArray($relationship->getFilterArray())->getFilter(); } $relationship->setSetters(array_merge_recursive(array(array('to_field' => 'pid', 'from_field' => 'id'))), $relationship->getSetters()); $builder->andRemotePropertyEquals('pid', 'id'); $relationship->setFilterArray($builder->getAllAsArray()); $container->setDefinition(ModelRelationshipDefinitionInterface::NAME, $definition); } // If ptable defined and no root setter we need to add (Contao default id=>pid mapping). if ($this->getFromDca('config/ptable') !== null) { $rootProvider = $this->getRootProviderName($container); if (($relationship = $definition->getRootCondition()) === null) { $relationship = new RootCondition(); $relationship->setSourceName($rootProvider); $definition->setRootCondition($relationship); } if (!$relationship->getSetters()) { $relationship->setSetters(array_merge_recursive(array(array('property' => 'pid', 'value' => '0'))), $relationship->getSetters()); } $container->setDefinition(ModelRelationshipDefinitionInterface::NAME, $definition); } }
/** * Render the current values for listing. * * @return array */ public function renderItemsPlain() { $values = array(); $value = $this->varValue; $idProperty = $this->idProperty ?: 'id'; if (is_array($value) && !empty($value)) { $environment = $this->getEnvironment(); $dataDriver = $environment->getDataProvider(); $config = $environment->getBaseConfigRegistry()->getBaseConfig(); $filter = FilterBuilder::fromArrayForRoot()->getFilter()->andPropertyValueIn($idProperty, $value)->getAllAsArray(); $config->setFilter($filter); // Set the sort field. if ($this->orderField && $dataDriver->fieldExists($this->orderField)) { $config->setSorting(array($this->orderField => 'ASC')); } $collection = $dataDriver->fetchAll($config); if ($collection->length() > 0) { foreach ($collection as $model) { $formatted = $this->formatModel($model, false); $idValue = $model->getProperty($idProperty); $values[$idValue] = $formatted[0]['content']; } } // Apply a custom sort order. $values = $this->sortValues($values); } return $values; }
/** * Parse the correct conditions for a MetaModel with variant support. * * @param IMetaModelDataDefinition $container The data container. * * @param ModelRelationshipDefinitionInterface $definition The relationship container. * * @return void */ protected function calculateConditionsWithVariants(IMetaModelDataDefinition $container, $definition) { // Basic conditions. $this->addHierarchicalConditions($container, $definition); $this->addParentCondition($container, $definition); // Conditions for metamodels variants. $relationship = $this->getRootCondition($container, $definition); $relationship->setSetters(array_merge_recursive(array(array('property' => 'varbase', 'value' => '1')), $relationship->getSetters())); $builder = FilterBuilder::fromArrayForRoot((array) $relationship->getFilterArray())->getFilter(); $builder->andPropertyEquals('varbase', 1); $relationship->setFilterArray($builder->getAllAsArray()); $setter = array(array('to_field' => 'varbase', 'value' => '0'), array('to_field' => 'vargroup', 'from_field' => 'vargroup')); $inverse = array(); /** @var ParentChildConditionInterface $relationship */ $relationship = $definition->getChildCondition($container->getName(), $container->getName()); if ($relationship === null) { $relationship = new ParentChildCondition(); $relationship->setSourceName($container->getName())->setDestinationName($container->getName()); $definition->addChildCondition($relationship); } else { $setter = array_merge_recursive($setter, $relationship->getSetters()); $inverse = array_merge_recursive($inverse, $relationship->getInverseFilterArray()); } $relationship->setFilterArray(FilterBuilder::fromArray($relationship->getFilterArray())->getFilter()->getBuilder()->encapsulateOr()->andRemotePropertyEquals('vargroup', 'vargroup')->andRemotePropertyEquals('vargroup', 'id')->andRemotePropertyEquals('varbase', 0, true)->getAllAsArray())->setSetters($setter)->setInverseFilterArray($inverse); }
/** * Parse the root condition. * * @param ContainerInterface $container The container where the data shall be stored. * * @param ModelRelationshipDefinitionInterface $definition The relationship definition. * * @return void * * @throws DcGeneralRuntimeException If no root data provider is defined. */ protected function parseRootCondition(ContainerInterface $container, ModelRelationshipDefinitionInterface $definition) { if (($rootCondition = $this->getFromDca('dca_config/rootEntries')) !== null) { $rootProvider = $container->getBasicDefinition()->getRootDataProvider(); if (!$rootProvider) { throw new DcGeneralRuntimeException('Root data provider name not specified in DCA but rootEntries section specified.'); } if (!$container->getDataProviderDefinition()->hasInformation($rootProvider)) { throw new DcGeneralRuntimeException('Unknown root data provider but rootEntries section specified.'); } if (isset($rootCondition[$rootProvider])) { $rootCondition = $rootCondition[$rootProvider]; $mySetter = $rootCondition['setOn']; if (($relationship = $definition->getRootCondition()) === null) { $relationship = new RootCondition(); $setter = $mySetter; $builder = FilterBuilder::fromArrayForRoot()->getFilter(); } else { /** @var RootConditionInterface $relationship */ if ($relationship->getSetters()) { $setter = array_merge_recursive($mySetter, $relationship->getSetters()); } else { $setter = $mySetter; } $builder = FilterBuilder::fromArrayForRoot($relationship->getFilterArray())->getFilter(); } $builder->append(FilterBuilder::fromArrayForRoot((array) $rootCondition['filter'])); $relationship->setSourceName($rootProvider)->setFilterArray($builder->getAllAsArray())->setSetters($setter); $definition->setRootCondition($relationship); } } }
/** * Render the current values for listing. * * @return array */ public function renderItemsPlain() { $values = array(); $value = $this->varValue; if ($this->fieldType == 'radio' && !empty($value) && !is_array($value)) { $value = array($value); } if (is_array($value) && !empty($value)) { $environment = $this->getEnvironment(); $dataDriver = $environment->getDataProvider(); $config = $environment->getController()->getBaseConfig(); $filter = FilterBuilder::fromArrayForRoot()->getFilter()->andPropertyValueIn('id', $value)->getAllAsArray(); $config->setFilter($filter); $collection = $dataDriver->fetchAll($config); if ($collection->length() > 0) { foreach ($collection as $model) { $formatted = $this->formatModel($model, false); $id = $model->getId(); $set[] = $id; $values[$id] = $formatted[0]['content']; } } // Apply a custom sort order. // TODO: this is untested. if ($this->orderField && is_array($this->{$this->strOrderField})) { $arrNew = array(); foreach ($this->{$this->strOrderField} as $i) { if (isset($values[$i])) { $arrNew[$i] = $values[$i]; unset($values[$i]); } } if (!empty($values)) { foreach ($values as $k => $v) { $arrNew[$k] = $v; } } $values = $arrNew; unset($arrNew); } } return $values; }