/** * 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); }
/** * Converts a top level category into the YAML representation. * * @param OptionCategory $optionCategory * * @return array */ public static function convertToArray(OptionCategory $optionCategory) { $result = []; $categories = $optionCategory->getCategories(); foreach ($categories as $category) { $options = $category->getOptions(); $_categories = $category->getCategories(); $optionsArray = self::getOptionsArray($options); foreach ($_categories as $_category) { $_options = $_category->getOptions(); $_optionsArray = self::getOptionsArray($_options); $optionsArray[$_category->getName()] = $_optionsArray; } $result[$category->getName()] = $optionsArray; } return $result; }
/** * 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()); }
/** * {@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; }