/** * {@inheritdoc} */ public function setPalette(PaletteInterface $palette = null) { if ($this->palette) { $this->palette->removeLegend($this); } $this->palette = $palette; return $this; }
/** * Add a custom condition to last created palette. * * @param PaletteConditionInterface $condition The condition to add. * * @return void * * @throws DcGeneralRuntimeException If the palette is missing. */ protected function addPaletteCondition(PaletteConditionInterface $condition) { if (!$this->palette) { throw new DcGeneralRuntimeException('Palette is missing, please create a palette first'); } $event = new AddConditionEvent($condition, $this->palette, $this); $this->dispatchEvent($event); $condition = $event->getCondition(); $previousCondition = $this->palette->getCondition(); if (!$previousCondition) { $this->palette->setCondition($condition); } elseif ($previousCondition instanceof PropertyConditionChain) { $previousCondition->addCondition($condition); } else { $chain = $this->paletteConditionChainClass->newInstance(); $chain->addCondition($previousCondition); $chain->addCondition($condition); $this->palette->setCondition($chain); } }
/** * Retrieve the legend with the given name. * * @param string $name Name of the legend. * * @param PaletteInterface $palette The palette. * * @param LegendInterface $prevLegend The previous legend. * * @return LegendInterface */ protected function getLegend($name, $palette, $prevLegend = null) { if (!$palette->hasLegend($name)) { $palette->addLegend(new Legend($name), $prevLegend); } return $palette->getLegend($name); }
/** * Build the field sets. * * @param ContaoWidgetManager $widgetManager The widget manager in use. * * @param PaletteInterface $palette The palette to use. * * @param PropertyValueBag $propertyValues The property values. * * @return array */ protected function buildFieldSet($widgetManager, $palette, $propertyValues) { $environment = $this->getEnvironment(); $definition = $this->getDataDefinition(); $translator = $environment->getTranslator(); $propertyDefinitions = $definition->getPropertiesDefinition(); $isAutoSubmit = $environment->getInputProvider()->getValue('SUBMIT_TYPE') === 'auto'; $legendStates = $this->getLegendStates(); $fieldSets = array(); $first = true; foreach ($palette->getLegends() as $legend) { $legendName = $translator->translate($legend->getName() . '_legend', $definition->getName()); $fields = array(); $properties = $legend->getProperties($this->model, $propertyValues); if (!$properties) { continue; } $legendVisible = $this->isLegendVisible($legend, $legendStates); foreach ($properties as $property) { $this->ensurePropertyExists($property->getName(), $propertyDefinitions); // If this property is invalid, fetch the error. if (!$isAutoSubmit && $propertyValues && $propertyValues->hasPropertyValue($property->getName()) && $propertyValues->isPropertyValueInvalid($property->getName())) { $this->errors = array_merge($this->errors, $propertyValues->getPropertyValueErrors($property->getName())); // Force legend open on error. $legendVisible = true; } $fields[] = $widgetManager->renderWidget($property->getName(), $isAutoSubmit, $propertyValues); } $fieldSet['label'] = $legendName; $fieldSet['class'] = $this->getLegendClass($first, $legendVisible); $fieldSet['palette'] = implode('', $fields); $fieldSet['legend'] = $legend->getName(); $fieldSets[] = $fieldSet; $first = false; } return $fieldSets; }
/** * Fill a palette from a multidimensional array of legends. * * @param PaletteInterface $palette The palette. * * @param array $legends The legends. * * @return void * * @throws DcGeneralInvalidArgumentException When an invalid legend has been passed. */ public static function fillPalette(PaletteInterface $palette, array $legends) { foreach ($legends as $legend) { if ($legend instanceof LegendInterface) { $palette->addLegend($legend); } elseif (is_array($legend)) { static::fillPalette($palette, $legend); } else { $type = is_object($legend) ? get_class($legend) : gettype($legend); throw new DcGeneralInvalidArgumentException('Legend [' . $type . '] does not implement LegendInterface'); } } }
/** * 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; }
/** * Retrieve the legend with the given name. * * @param string $name Name of the legend. * * @param PaletteInterface $palette The palette. * * @param LegendInterface $prevLegend The previous legend. * * @return LegendInterface */ public function getLegend($name, $palette, $prevLegend = null) { if ($name[0] == '+') { $name = substr($name, 1); } if (!$palette->hasLegend($name)) { $palette->addLegend(new Legend($name), $prevLegend); } return $palette->getLegend($name); }