public function testClone() { $palette = new Palette(); $condition = new DefaultPaletteCondition(); $palette->setCondition($condition); $legend = new Legend('legend'); $palette->addLegend($legend); $legend->addProperty(new Property('prop1')); $legend->addProperty(new Property('prop2')); $palette2 = clone $palette; $this->assertNotSame($palette, $palette2); $condition2 = $palette2->getCondition(); $this->assertInstanceOf('ContaoCommunityAlliance\\DcGeneral\\DataDefinition\\Palette\\Condition\\Palette\\DefaultPaletteCondition', $condition2); $this->assertNotSame($condition, $condition2); $legend2 = $palette2->getLegend('legend'); $this->assertNotNull($legend2); $this->assertNotSame($legend, $legend2); $this->assertSame($legend->getName(), $legend2->getName()); $properties1 = $legend->getProperties(); $properties2 = $legend2->getProperties(); $this->assertNotSame($properties1[0], $properties2[0]); $this->assertSame($properties1[0]->getName(), $properties2[0]->getName()); $this->assertNotSame($properties1[1], $properties2[1]); $this->assertSame($properties1[1]->getName(), $properties2[1]->getName()); }
/** * Create a new palette from a list of legends. * * @param string $name The name of the palette, can be omitted (deprecated). * * @param array $legend A list of legends. Can be multiple arrays and arrays of arrays. * * @param array $_ A list of legends. Can be multiple arrays and arrays of arrays. * * @return PaletteInterface */ public static function createPalette($name = null, $legend = null, $_ = null) { $palette = new Palette(); $args = func_get_args(); if (is_string($args[0])) { $name = array_shift($args); $palette->setName($name); } static::fillPalette($palette, $args); return $palette; }
/** * Parse a single palette. * * @param string $paletteSelector The selector for the palette. * * @param string $fields The fields contained within the palette. * * @param array(string => PropertyInterface) $subPaletteProperties The sub palette properties [optional]. * * @param array(string) $selectorFieldNames The names of all properties being used as * selectors [optional]. * * @param PaletteInterface $palette The palette to be populated [optional]. * * @return Palette */ public function parsePalette($paletteSelector, $fields, array $subPaletteProperties = array(), array $selectorFieldNames = array(), PaletteInterface $palette = null) { if (!$palette) { $palette = new Palette(); $palette->setName($paletteSelector); } $condition = $this->createPaletteCondition($paletteSelector, $selectorFieldNames); $palette->setCondition($condition); // We ignore the difference between field set (separated by ";") and fields (separated by ","). $fields = preg_split('~[;,]~', $fields); $fields = array_map('trim', $fields); $fields = array_filter($fields); $legend = null; foreach ($fields as $field) { // TODO what about :hide? this is currently not supported by LegendInterface. if (preg_match('~^\\{(.*?)(_legend)?(:hide)?\\}$~', $field, $matches)) { $name = $matches[1]; if ($palette->hasLegend($name)) { $legend = $palette->getLegend($name); } else { $legend = new Legend($matches[1]); $palette->addLegend($legend); } } else { // Fallback for incomplete palettes without legend, // Create an empty legend. if (!$legend) { $name = 'unnamed'; if ($palette->hasLegend($name)) { $legend = $palette->getLegend($name); } else { $legend = new Legend($matches[1]); $palette->addLegend($legend); } } // Add the current field to the legend. $property = new Property($field); $legend->addProperty($property); // Add sub palette fields to the legend. if (isset($subPaletteProperties[$field])) { foreach ($subPaletteProperties[$field] as $property) { $legend->addProperty(clone $property); } } } } return $palette; }
protected function parsePalettes(PalettesDefinitionInterface $palettesDefinition, LegacyPalettesParser $parser, array $palettesDca, array $subPalettes, array $subSelectPalettes, array $selectorFieldNames) { $palettes = array(); if (is_array($palettesDca)) { foreach ($palettesDca as $selector => $legendPropertyNames) { if (preg_match('#^(\\w+) extends (\\w+)$#', $selector, $matches)) { $parentSelector = $matches[2]; $selector = $matches[1]; if (isset($palettes[$parentSelector])) { $palette = clone $palettes[$parentSelector]; $palette->setName($selector); } else { if ($palettesDefinition->hasPaletteByName($parentSelector)) { $palette = clone $palettesDefinition->getPaletteByName($parentSelector); } else { $palette = null; } } if (!$palette) { throw new \RuntimeException('Parent palette ' . $parentSelector . ' does not exists'); } // We MUST NOT retain the DefaultPaletteCondition. $palette->setCondition($parser->createPaletteCondition($selector, $selectorFieldNames)); $extended = true; } else { $palette = new Palette(); $palette->setName($selector); $palette->setCondition($parser->createPaletteCondition($selector, $selectorFieldNames)); $extended = false; } $paletteCallbacks = array(); foreach ($legendPropertyNames as $legendName => $propertyNames) { if ($propertyNames instanceof \Closure) { $paletteCallbacks[] = $propertyNames; continue; } $additive = false; $subtractive = false; $insert = false; $refLegendName = null; if ($extended) { // add properties to existing legend if ($legendName[0] == '+') { $additive = true; $legendName = substr($legendName, 1); } else { // subtract properties from existing legend if ($legendName[0] == '-') { $subtractive = true; $legendName = substr($legendName, 1); } } if (preg_match('#^(\\w+) (before|after) (\\w+)$#', $legendName, $matches)) { $legendName = $matches[1]; $insert = $matches[2]; $refLegendName = $matches[3]; } } if ($palette->hasLegend($legendName)) { $legend = $palette->getLegend($legendName); } else { $legend = new Legend($legendName); // insert a legend before or after another one if ($insert) { $existingLegends = $palette->getLegends(); $refLegend = null; // search the referenced legend /** @var LegendInterface $existingLegend */ reset($existingLegends); while ($existingLegend = next($existingLegends)) { if ($existingLegend->getName() == $refLegendName) { if ($insert == 'after') { // if insert after, get to next $refLegend = next($existingLegends); } else { $refLegend = $existingLegend; } break; } } $palette->addLegend($legend, $refLegend); } else { // just append the legend $palette->addLegend($legend); } } // if extend a palette, but not add or remove fields, clear the legend but only when we have fields. if ($extended && !($additive || $subtractive) && count($propertyNames)) { $legend->clearProperties(); } $legendCallbacks = array(); foreach ($propertyNames as $propertyName) { if ($propertyName instanceof \Closure) { $legendCallbacks[] = $propertyName; continue; } if ($propertyName[0] == ':') { if ('hide' === substr($propertyName, 1)) { $legend->setInitialVisibility(false); } // skip other modifiers continue; } if ($additive || $subtractive) { $action = $additive ? 'add' : 'sub'; $insert = false; $refPropertyName = null; if ($propertyName[0] == '+') { $action = 'add'; $propertyName = substr($propertyName, 1); } else { if ($propertyName[0] == '-') { $action = 'sub'; $propertyName = substr($propertyName, 1); } } if (preg_match('#^(\\w+) (before|after) (\\w+)$#', $propertyName, $matches)) { $propertyName = $matches[1]; $insert = $matches[2]; $refPropertyName = $matches[3]; } if ($action == 'add') { $property = new Property($propertyName); if ($insert) { $existingProperties = $legend->getProperties(); $refProperty = null; reset($existingProperties); $existingProperty = current($existingProperties); /** @var PropertyInterface $existingProperty */ while ($existingProperty !== false) { if ($existingProperty->getName() == $refPropertyName) { if ($insert == 'after') { $refProperty = next($existingProperties); if ($refProperty === false) { $refProperty = null; } } else { $refProperty = $existingProperty; } break; } $existingProperty = next($existingProperties); } $legend->addProperty($property, $refProperty); } else { $legend->addProperty($property); } } else { /** @var PropertyInterface $property */ foreach ($legend->getProperties() as $property) { if ($property->getName() == $propertyName) { $legend->removeProperty($property); break; } } } } else { $property = new Property($propertyName); $legend->addProperty($property); } // add sub palette properties if (isset($subPalettes[$propertyName])) { $legend->addProperties($subPalettes[$propertyName]); } // add sub select properties for unspecified legend names. $this->addSubSelectProperties($legend, $subSelectPalettes, $propertyName); } foreach ($legendCallbacks as $callback) { call_user_func($callback, $legendName, $legend, $palette, $palettesDefinition); } } foreach ($paletteCallbacks as $callback) { call_user_func($callback, $palette, $palettesDefinition); } $palettes[$selector] = $palette; } } // now add sub select properties that are for specific legend names. foreach ($subSelectPalettes as $propertyName => $legendInformation) { $subpaletteCallbacks = array(); foreach ($legendInformation as $properties) { if ($properties instanceof \Closure) { $subpaletteCallbacks[] = $properties; } } foreach ($legendInformation as $fullLegendName => $properties) { if ($fullLegendName === '') { continue; } foreach ($palettes as $palette) { if (preg_match('#^(\\w+) (before|after) (\\w+)$#', $fullLegendName, $matches)) { $legendName = $matches[1]; $insert = $matches[2]; $refName = $matches[3]; } else { $legendName = $fullLegendName; $insert = ''; $refName = null; } /** @var Palette $palette */ if (!$palette->hasLegend($legendName)) { $palette->addLegend(new Legend($legendName)); } /** @var Legend $legend */ $legend = $palette->getLegend($legendName); $this->addSubSelectProperties($legend, $subSelectPalettes, $propertyName, $fullLegendName, $insert, $refName); foreach ($subpaletteCallbacks as $callback) { call_user_func($callback, $legendName, $properties, $legend, $palette, $palettesDefinition); } } } } return $palettes; }
/** * Parse the palettes from the input screen into the data container. * * @param IMetaModelDataDefinition $container The data container. * * @return void */ protected function parsePalettes(IMetaModelDataDefinition $container) { $inputScreen = $this->getInputScreenDetails(); $metaModel = $this->getMetaModel(); if ($container->hasDefinition(PalettesDefinitionInterface::NAME)) { $palettesDefinition = $container->getDefinition(PalettesDefinitionInterface::NAME); } else { $palettesDefinition = new DefaultPalettesDefinition(); $container->setDefinition(PalettesDefinitionInterface::NAME, $palettesDefinition); } $palette = new Palette(); $palette->setName('default')->setCondition(new DefaultPaletteCondition()); $palettesDefinition->addPalette($palette); foreach ($inputScreen->getLegends() as $legendName => $legend) { $paletteLegend = new Legend($legendName); $paletteLegend->setInitialVisibility(isset($legend['visible']) && (bool) $legend['visible']); $palette->addLegend($paletteLegend); $this->translator->setValue($legendName . '_legend', isset($legend['name']) ? $legend['name'] : '', $container->getName()); foreach ($legend['properties'] as $propertyName) { $property = new Property($propertyName); $paletteLegend->addProperty($property); $propInfo = $inputScreen->getProperty($propertyName); $chain = new PropertyConditionChain(); $property->setEditableCondition($chain); $chain->addCondition(new BooleanCondition(!(isset($propInfo['info']['readonly']) && $propInfo['info']['readonly']))); if ($metaModel->hasVariants() && !$metaModel->getAttribute($propertyName)->get('isvariant')) { $chain->addCondition(new PropertyValueCondition('varbase', 1)); } $extra = $propInfo['info']; $chain = new PropertyConditionChain(); $property->setVisibleCondition($chain); $chain->addCondition(new BooleanCondition(!(isset($extra['doNotShow']) && $extra['doNotShow'] || isset($extra['hideInput']) && $extra['hideInput']))); $propertyConditions = $inputScreen->getConditionsFor($propertyName); if ($propertyConditions !== null) { $chain->addCondition($propertyConditions); } // If variants, do show only if allowed. if ($metaModel->hasVariants()) { $chain->addCondition(new IsVariantAttribute()); } } } }