/** * Called on the prePersist event. * * @param LifecycleEventArgs $eventArgs */ public function prePersist(LifecycleEventArgs $eventArgs) { $object = $eventArgs->getObject(); // only handle our model if (!$object instanceof Group) { return; } $categories = [$object->getFrontendUserCategory(), $object->getFrontendModCategory(), $object->getACPCategory()]; $topLevelCategory = new OptionCategory(); $topLevelCategory->setCategories($categories); $this->groupService->setOptionsFor($object->getRoleName(), $topLevelCategory); }
/** * Tests the convertToArray method. */ public function testConvertToArray() { // simple case $optionCategory = new OptionCategory(); $mainCategory = new OptionCategory('twomartens.core'); $options = [new Option(0, 'one', 'string', 'hello'), new Option(0, 'two', 'boolean', false), new Option(0, 'three', 'array', [1, 2, 3])]; $mainCategory->setOptions($options); $optionCategory->setCategories([$mainCategory]); $yamlData = ConfigUtil::convertToArray($optionCategory); $expectedYamlData = ['twomartens.core' => ['one' => 'hello', 'two' => false, 'three' => [1, 2, 3]]]; $this->assertEquals($expectedYamlData, $yamlData); // complex case $optionCategory = new OptionCategory(); $acpCategory = new OptionCategory('acp'); $ourCategory = new OptionCategory('twomartens.core'); $acpCategory->setCategories([$ourCategory]); $optionCategory->setCategories([$acpCategory]); $options = [new Option(0, 'one', 'boolean', true), new Option(0, 'two', 'string', 'foo'), new Option(0, 'three', 'array', [4, 5, 6])]; $ourCategory->setOptions($options); $yamlData = ConfigUtil::convertToArray($optionCategory); $expectedYamlData = ['acp' => ['twomartens.core' => ['one' => true, 'two' => 'foo', 'three' => [4, 5, 6]]]]; $this->assertEquals($expectedYamlData, $yamlData); }
/** * Shows the options main page. * * @param Request $request * * @return Response */ public function indexAction(Request $request) { /** @var OptionServiceInterface $optionService */ $optionService = $this->container->get('twomartens.core.option'); $this->options = $optionService->getOptions(); $form = $this->createForm(OptionConfigurationType::class, $this->options); $form->handleRequest($request); $this->assignVariables(); if ($form->isValid()) { // save changed options to file $submittedData = $request->request->all(); $submittedData = $submittedData['option_configuration']; $categories = $this->options->getCategories(); foreach ($categories as $category) { $categoryName = $category->getName(); $options = $category->getOptions(); foreach ($options as $option) { $optionName = $option->getName(); $optionType = $option->getType(); $fieldName = str_replace('.', '_', $categoryName) . '_' . $optionName; if (!isset($submittedData[$fieldName])) { // should be the case only for checkbox $fieldValue = false; } else { $fieldValue = $submittedData[$fieldName]; } settype($fieldValue, $optionType); $option->setValue($fieldValue); } } $optionService->setOptions($this->options); $this->templateVariables['success'] = true; } $this->templateVariables['form'] = $form->createView(); return $this->render('TwoMartensCoreBundle:ACPOption:index.html.twig', $this->templateVariables); }
/** * {@inheritdoc} */ public function setOptionsFor($groupRoleName, OptionCategory $options) { $this->optionData[$groupRoleName] = $options; // updating options $categories = $options->getCategories(); foreach ($categories as $category) { $name = $category->getName(); if (!isset($this->options[$groupRoleName][$name])) { $this->options[$groupRoleName][$name] = []; } $_categories = $category->getCategories(); foreach ($_categories as $_category) { $_name = $_category->getName(); if (!isset($this->options[$groupRoleName][$name][$_name])) { $this->options[$groupRoleName][$name][$_name] = []; } $_options = $_category->getOptions(); foreach ($_options as $_option) { $this->options[$groupRoleName][$name][$_name][$_option->getName()] = $_option; } } } }
/** * Updates the options of the given category and returns the roles. * * @param OptionCategory $category * @param Form $form * * @return string[] */ private function updateOptions(OptionCategory $category, Form $form) { $categories = $category->getCategories(); $superCategoryName = $category->getName(); $roles = []; foreach ($categories as $category) { $categoryName = $category->getName(); $options = $category->getOptions(); foreach ($options as $option) { $optionName = $option->getName(); $optionType = $option->getType(); $fieldName = $superCategoryName . '_' . str_replace('.', '_', $categoryName) . '_' . $optionName; if (!$form->has($fieldName)) { // should be the case only for checkbox $fieldValue = false; } else { $fieldValue = $form->get($fieldName); } settype($fieldValue, $optionType); if ($optionType == 'boolean' && $fieldValue) { $roles[] = 'ROLE_' . strtoupper($superCategoryName) . '_' . strtoupper($categoryName) . '_' . strtoupper($optionName); } $option->setValue($fieldValue); } } return $roles; }
/** * Converts the given values into options. * * @param string $key * @param array|null $values * * @return OptionCategory */ private static function convertValues($key, $values) { $returnCategory = new OptionCategory(); if ($values === null) { return $returnCategory; } if (self::isAssoc($values)) { $options = []; $categories = []; foreach ($values as $_key => $value) { // case: value = [...] if (is_array($value)) { // recursive call $subCategory = self::convertValues($_key, $value); $subOptions = $subCategory->getOptions(); $subCategories = $subCategory->getCategories(); // case: value = [a, b, c] if (count($subOptions) == 1 && empty($subCategories) && !self::isAssoc($value)) { $options[] = $subOptions[0]; // case: value = [[a: b], [c: d, e: f]] || [a: b, c: d, e: [...]] } else { $category = new OptionCategory($_key); $category->setOptions($subOptions); $category->setCategories($subCategories); $categories[] = $category; } // case: value = b } else { $type = str_replace('double', 'float', gettype($value)); $type = str_replace('NULL', 'null', $type); $options[] = new Option(0, $_key, $type, $value); } } $returnCategory->setOptions($options); $returnCategory->setCategories($categories); } else { $options = []; $_values = []; foreach ($values as $_key => $value) { if (is_array($value)) { $subCategory = self::convertValues($_key, $value); $subOptions = $subCategory->getOptions(); $subCategories = $subCategory->getCategories(); // case: value = [a: b] if (count($subOptions) == 1 && empty($subCategories)) { $option = $subOptions[0]; $options[] = $option; // case: value = [a: b, d: e] } elseif (count($subOptions) > 1) { $option = new Option(0, '', 'array'); $optionValues = []; foreach ($subOptions as $subOption) { $optionValues[$subOption->getName()] = $subOption->getValue(); } $option->setValue($optionValues); $options[] = $option; } } else { $_values[] = $value; } } // case: values = [a, b, c, d] if (!empty($_values)) { $options[] = new Option(0, $key, 'array', $_values); } $returnCategory->setOptions($options); } return $returnCategory; }
/** * Tests setting values. */ public function testSet() { $optionCategory = new OptionCategory(); // set category name $optionCategory->setName('hansolo'); $this->assertEquals('hansolo', $optionCategory->getName()); $optionCategory->setName(''); $this->assertTrue(empty($optionCategory->getName())); $optionCategory->setName('123'); $this->assertTrue('123' === $optionCategory->getName()); // set options $options = [new Option(), new Option(0, 'testV', 'string', 'elephant'), new Option(0, 'samba', 'array', [1, 2, 3])]; $optionCategory->setOptions($options); $this->assertEquals($options, $optionCategory->getOptions()); // set categories $categories = [new OptionCategory('testv1'), new OptionCategory('testv0')]; $optionCategory->setCategories($categories); $this->assertEquals($categories, $optionCategory->getCategories()); }