2019-05-23 17:04:48 +02:00
|
|
|
<?php
|
|
|
|
|
2020-07-25 15:40:27 +02:00
|
|
|
namespace Ardent\ArdentParameterBundle\Controller;
|
2019-05-23 17:04:48 +02:00
|
|
|
|
2020-07-25 15:40:27 +02:00
|
|
|
use Ardent\ArdentParameterBundle\Service\ParameterService;
|
2019-05-23 17:04:48 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2019-06-03 11:29:44 +02:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
2019-05-23 17:04:48 +02:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
|
|
|
abstract class BaseController extends AbstractController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param ParameterService $param
|
|
|
|
* @param Request $request
|
|
|
|
* @param [] $configuration
|
|
|
|
*/
|
|
|
|
public function baseIndex(ParameterService $param, Request $request, $configuration)
|
|
|
|
{
|
|
|
|
// gather the values
|
|
|
|
$data = [];
|
|
|
|
foreach ($configuration as $config) {
|
|
|
|
$name = $config['name'];
|
2019-06-03 11:29:44 +02:00
|
|
|
$data[$name] = $this->valueParser($config['type'], $param->get($name, false));
|
2019-05-23 17:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// build the form
|
|
|
|
$formBuilder = $this->createFormBuilder($data);
|
|
|
|
foreach ($configuration as $config) {
|
|
|
|
$this->configParser($config, $formBuilder);
|
|
|
|
}
|
|
|
|
$form = $formBuilder->add('Update', SubmitType::class)->getForm();
|
|
|
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
$result = $form->getData();
|
|
|
|
|
|
|
|
foreach ($configuration as $config) {
|
|
|
|
$name = $config['name'];
|
2019-06-19 15:22:22 +02:00
|
|
|
if (null !== $result[$name] || CheckboxType::class == $config['type']) {
|
|
|
|
$param->set($name, $result[$name]);
|
|
|
|
}
|
2019-05-23 17:04:48 +02:00
|
|
|
}
|
|
|
|
$this->addFlash('success', 'Updating parameters successful');
|
|
|
|
|
|
|
|
return $this->redirect($request->getRequestUri());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render('@ArdentParameter/form.html.twig', [
|
|
|
|
'parameter_form' => $form->createView(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-06-03 11:29:44 +02:00
|
|
|
private function valueParser($type, $value)
|
|
|
|
{
|
|
|
|
switch ($type) {
|
|
|
|
case CheckboxType::class:
|
|
|
|
return boolval($value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return $value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 17:04:48 +02:00
|
|
|
/**
|
|
|
|
* @param $config
|
|
|
|
* @param FormBuilderInterface $formBuilder
|
|
|
|
*/
|
|
|
|
private function configParser($config, &$formBuilder)
|
|
|
|
{
|
|
|
|
$type = $config['type'];
|
|
|
|
$options = [];
|
|
|
|
switch ($type) {
|
|
|
|
case ChoiceType::class:
|
2019-06-19 11:45:22 +02:00
|
|
|
if (is_callable($config['choices'])) {
|
|
|
|
$options['choices'] = $config['choices']();
|
|
|
|
} else {
|
|
|
|
$options['choices'] = $config['choices'];
|
|
|
|
}
|
2019-05-23 17:04:48 +02:00
|
|
|
break;
|
2019-06-03 11:29:44 +02:00
|
|
|
case CheckboxType::class:
|
|
|
|
$options['required'] = false;
|
|
|
|
break;
|
2019-05-23 17:04:48 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$formBuilder->add($config['name'], $type, $options);
|
|
|
|
}
|
|
|
|
}
|