/**
  * Build a data definition and store it into the environments container.
  *
  * @param \ContaoCommunityAlliance\DcGeneral\DataDefinition\ContainerInterface $container
  *
  * @return void
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function build(ContainerInterface $container, BuildDataDefinitionEvent $event)
 {
     if (!$this->loadDca($container->getName(), $this->getDispatcher())) {
         return;
     }
     if ($container->hasDefinition(PalettesDefinitionInterface::NAME)) {
         $palettesDefinition = $container->getDefinition(PalettesDefinitionInterface::NAME);
     } else {
         $palettesDefinition = new DefaultPalettesDefinition();
         $container->setDefinition(PalettesDefinitionInterface::NAME, $palettesDefinition);
     }
     $parser = new LegacyPalettesParser();
     $selectorFieldNames = (array) $this->getFromDca('palettes/__selector__');
     $palettesDca = (array) $this->getFromDca('metapalettes');
     $subPalettesDca = (array) $this->getFromDca('metasubpalettes');
     $subSelectPalettesDca = (array) $this->getFromDca('metasubselectpalettes');
     // extend the selector field names with subpalettes field names
     $selectorFieldNames = array_merge($selectorFieldNames, array_keys($subPalettesDca));
     $subSelectPalettes = $this->parseSubSelectPalettes($subSelectPalettesDca);
     $subPalettes = $this->parseSubPalettes($parser, $subPalettesDca, $selectorFieldNames);
     $palettes = $this->parsePalettes($palettesDefinition, $parser, $palettesDca, $subPalettes, $subSelectPalettes, $selectorFieldNames);
     if (empty($palettes)) {
         return;
     }
     $palettesDefinition->addPalettes($palettes);
 }
 /**
  * Build the field sets.
  *
  * @param WidgetManager    $widgetManager  The widget manager in use.
  *
  * @param PaletteInterface $palette        The palette to use.
  *
  * @param PropertyValueBag $propertyValues The property values.
  *
  * @return array
  */
 private function buildFieldSet($widgetManager, $palette, $propertyValues)
 {
     $propertyDefinitions = $this->definition->getPropertiesDefinition();
     $isAutoSubmit = $this->environment->getInputProvider()->getValue('SUBMIT_TYPE') === 'auto';
     $fieldSets = [];
     $first = true;
     foreach ($palette->getLegends() as $legend) {
         $legendName = $this->translator->translate($legend->getName() . '_legend', $this->definition->getName());
         $fields = [];
         $hidden = [];
         $properties = $legend->getProperties($this->model, $propertyValues);
         if (!$properties) {
             continue;
         }
         foreach ($properties as $property) {
             $propertyName = $property->getName();
             $this->ensurePropertyExists($propertyName, $propertyDefinitions);
             // If this property is invalid, fetch the error.
             if (!$isAutoSubmit && $propertyValues && $propertyValues->hasPropertyValue($propertyName) && $propertyValues->isPropertyValueInvalid($propertyName)) {
                 $this->errors = array_merge($this->errors, $propertyValues->getPropertyValueErrors($propertyName));
             }
             $fields[] = $widgetManager->renderWidget($propertyName, $isAutoSubmit, $propertyValues);
             $hidden[] = sprintf('<input type="hidden" name="FORM_INPUTS[]" value="%s">', $propertyName);
         }
         $fieldSet['label'] = $legendName;
         $fieldSet['class'] = $first ? 'tl_tbox' : 'tl_box';
         $fieldSet['palette'] = implode('', $hidden) . implode('', $fields);
         $fieldSet['legend'] = $legend->getName();
         $fieldSets[] = $fieldSet;
         $first = false;
     }
     return $fieldSets;
 }
Example #3
0
 /**
  * Check if the contect of the event is a allowed one.
  *
  * @param ContainerInterface $dataDefinition The data definition from the environment.
  *
  * @param string             $propertyName   The current property name.
  *
  * @param ModelInterface     $model          The current model.
  *
  * @return bool True => It is a allowed one | False => nope
  */
 protected function isAllowedContext($dataDefinition, $propertyName, $model)
 {
     // Check the name of the data def.
     if ($dataDefinition->getName() !== 'tl_metamodel_filtersetting') {
         return false;
     }
     // Check the name of the property.
     if ($propertyName !== 'attr_id2') {
         return false;
     }
     // Check the type.
     if ($model->getProperty('type') !== 'range' && $model->getProperty('type') !== 'rangedate') {
         return false;
     }
     // At the end, return true.
     return true;
 }
 /**
  * This method parses all data provider related information from Contao legacy data container arrays.
  *
  * @param ContainerInterface $container The container where the data shall be stored.
  *
  * @return void
  */
 protected function parseDataProvider(ContainerInterface $container)
 {
     if ($container->hasDataProviderDefinition()) {
         $config = $container->getDataProviderDefinition();
     } else {
         $config = new DefaultDataProviderDefinition();
         $container->setDataProviderDefinition($config);
     }
     // If mode is 5, we need to define tree view.
     if ($this->getFromDca('list/sorting/mode') === 5) {
         if (!$container->getBasicDefinition()->getRootDataProvider()) {
             $container->getBasicDefinition()->setRootDataProvider($container->getName());
         }
     }
     if (($parentTable = $this->getFromDca('config/ptable')) !== null) {
         // Check config if it already exists, if not, add it.
         if (!$config->hasInformation($parentTable)) {
             $providerInformation = new ContaoDataProviderInformation();
             $providerInformation->setName($parentTable);
             $config->addInformation($providerInformation);
         } else {
             $providerInformation = $config->getInformation($parentTable);
         }
         if ($providerInformation instanceof ContaoDataProviderInformation) {
             $initializationData = (array) $providerInformation->getInitializationData();
             $providerInformation->setTableName($parentTable)->setInitializationData(array_merge(array('source' => $parentTable, 'name' => $parentTable), $initializationData));
             if (!$container->getBasicDefinition()->getRootDataProvider()) {
                 $container->getBasicDefinition()->setRootDataProvider($parentTable);
             }
             if (!$container->getBasicDefinition()->getParentDataProvider()) {
                 $container->getBasicDefinition()->setParentDataProvider($parentTable);
             }
         }
     }
     $providerName = $container->getBasicDefinition()->getDataProvider() ?: $container->getName();
     // Check config if it already exists, if not, add it.
     if (!$config->hasInformation($providerName)) {
         $providerInformation = new ContaoDataProviderInformation();
         $providerInformation->setName($providerName);
         $config->addInformation($providerInformation);
     } else {
         $providerInformation = $config->getInformation($providerName);
     }
     if ($providerInformation instanceof ContaoDataProviderInformation) {
         $initializationData = (array) $providerInformation->getInitializationData();
         if (!isset($initializationData['source'])) {
             $providerInformation->setTableName($providerName)->setInitializationData(array_merge(array('source' => $providerName, 'name' => $providerName), $initializationData));
         }
         $providerInformation->setVersioningEnabled((bool) $this->getFromDca('config/enableVersioning'));
         if (!$container->getBasicDefinition()->getDataProvider()) {
             $container->getBasicDefinition()->setDataProvider($providerName);
         }
     }
 }
 /**
  * Parse a single data provider information and prepare the definition object for it.
  *
  * @param ContainerInterface              $container   The container where the data shall be stored.
  *
  * @param DataProviderDefinitionInterface $providers   The data provider container.
  *
  * @param array                           $information The information for the data provider to be parsed.
  *
  * @param string|null                     $name        The name of the data provider to be used within the container.
  *
  * @return ContaoDataProviderInformation|null
  */
 protected function parseSingleDataProvider(ContainerInterface $container, DataProviderDefinitionInterface $providers, array $information, $name)
 {
     if (isset($information['factory'])) {
         $factoryClass = new \ReflectionClass($information['factory']);
         $factory = $factoryClass->newInstance();
         $providerInformation = $factory->build($information);
     } else {
         // Determine the name.
         if ($name && !$this->isSpecialName($name)) {
             $providerName = $name;
         } elseif ($name === 'default') {
             $providerName = $container->getName();
         } elseif (isset($information['source'])) {
             $providerName = $information['source'];
         } else {
             $providerName = $container->getName();
         }
         // Check config if it already exists, if not, add it.
         if (!$providers->hasInformation($providerName)) {
             $providerInformation = new ContaoDataProviderInformation();
             $providerInformation->setName($providerName);
             $providers->addInformation($providerInformation);
         } else {
             $providerInformation = $providers->getInformation($providerName);
         }
         if (!$providerInformation->getTableName()) {
             if (isset($information['source'])) {
                 $providerInformation->setTableName($information['source']);
             } else {
                 $providerInformation->setTableName($providerName);
             }
         }
         if (isset($information['class'])) {
             $providerInformation->setClassName($information['class']);
         }
     }
     return $providerInformation;
 }