public function testAddAnd() { $filter = array(array('operation' => '=', 'property' => 'prop', 'value' => '1')); $result = array_merge($filter, array(array('operation' => '=', 'property' => 'prop2', 'value' => '2'))); $builder = new FilterBuilder($filter, true); $builder->getFilter()->andPropertyEquals('prop2', '2'); $this->assertEquals($result, $builder->getAllAsArray()); }
/** * 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); } }
/** * Retrieve the children of a model (if any exist). * * @param DataProviderInterface $dataProvider The data provider. * * @param ModelInterface $model The model. * * @param ParentChildConditionInterface $childCondition The condition. * * @return CollectionInterface|null */ private function getChildrenOfModel($dataProvider, $model, $childCondition) { $childIds = $dataProvider->fetchAll($dataProvider->getEmptyConfig()->setFilter($childCondition->getFilter($model))->setIdOnly(true)); if (!$childIds) { return null; } return $dataProvider->fetchAll($dataProvider->getEmptyConfig()->setSorting(array('sorting' => 'ASC'))->setFilter(FilterBuilder::fromArray()->getFilter()->andPropertyValueIn('id', $childIds)->getAllAsArray())); }
/** * 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; }
/** * Initialize an instance with the values from the given array. * * @param array $array The initialization array. * * @param FilterBuilder $builder The builder instance. * * @return BaseFilterBuilder */ public static function fromArray($array, $builder) { $children = array(); foreach ($array['children'] as $child) { $children[] = FilterBuilder::getBuilderFromArray($child, $builder); } /** @var BaseFilterBuilder $instance */ $instance = new static($children); return $instance->setBuilder($builder); }
/** * Parse the root condition. * * @param ModelRelationshipDefinitionInterface $definition The relationship definition. * * @return void * * @throws DcGeneralRuntimeException If no root data provider is defined. */ protected function parseParentChildConditions(ModelRelationshipDefinitionInterface $definition) { if (($childConditions = $this->getFromDca('dca_config/childCondition')) !== null) { foreach ((array) $childConditions as $childCondition) { /** @var ParentChildConditionInterface $relationship */ $relationship = $definition->getChildCondition($childCondition['from'], $childCondition['to']); if (!$relationship instanceof ParentChildConditionInterface) { $relationship = new ParentChildCondition(); $relationship->setSourceName($childCondition['from'])->setDestinationName($childCondition['to']); $definition->addChildCondition($relationship); $setter = $childCondition['setOn']; $inverse = $childCondition['inverse']; } else { $setter = array_merge_recursive((array) $childCondition['setOn'], $relationship->getSetters()); $inverse = array_merge_recursive((array) $childCondition['inverse'], $relationship->getInverseFilterArray()); } $relationship->setFilterArray(FilterBuilder::fromArray($relationship->getFilterArray())->getFilter()->append(FilterBuilder::fromArray((array) $childCondition['filter']))->getAllAsArray())->setSetters($setter)->setInverseFilterArray($inverse); } } }
/** * Get all the filter array for all filters from the current filter builder. * * @return array */ public function getAllAsArray() { return $this->builder->getAllAsArray(); }
/** * {@inheritDoc} */ public function initialize(ConfigInterface $objConfig, PanelElementInterface $objElement = null) { $this->updateValue(); if ($this->getPropertyName() && $this->getValue() && $objElement !== $this) { $arrCurrent = $objConfig->getFilter(); if (!is_array($arrCurrent)) { $arrCurrent = array(); } $objConfig->setFilter(FilterBuilder::fromArray($arrCurrent)->getFilter()->andPropertyEquals($this->getPropertyName(), $this->getValue())->getAllAsArray()); } // Finally load the filter options. if ($objElement === null) { $this->loadFilterOptions(); } }
/** * 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); }
/** * 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; }