public function testMatchesRemoteValue()
 {
     $parent = new DefaultModel();
     $parent->setId(1);
     $child = new DefaultModel();
     $child->setPropertyRaw('pid', 1);
     $child->setId(2);
     $condition = new ParentChildCondition();
     $condition->setFilterArray(array(array('local' => 'id', 'operation' => '=', 'remote_value' => '2')));
     $this->assertTrue($condition->matches($parent, $child));
 }
 /**
  * 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);
     }
 }
 /**
  * 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);
         }
     }
 }
Beispiel #4
0
 /**
  * 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);
 }