74 lines
2.2 KiB
PHP
74 lines
2.2 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Ardent\ParameterBundle\Controller;
|
||
|
|
||
|
use App\Ardent\ParameterBundle\Service\ParameterService;
|
||
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||
|
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'];
|
||
|
$data[$name] = $param->get($name, false);
|
||
|
}
|
||
|
|
||
|
// 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'];
|
||
|
if ($result[$name]) {
|
||
|
$param->set($name, $result[$name]);
|
||
|
}
|
||
|
}
|
||
|
$this->addFlash('success', 'Updating parameters successful');
|
||
|
|
||
|
return $this->redirect($request->getRequestUri());
|
||
|
}
|
||
|
|
||
|
return $this->render('@ArdentParameter/form.html.twig', [
|
||
|
'parameter_form' => $form->createView(),
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param $config
|
||
|
* @param FormBuilderInterface $formBuilder
|
||
|
*/
|
||
|
private function configParser($config, &$formBuilder)
|
||
|
{
|
||
|
$type = $config['type'];
|
||
|
$options = [];
|
||
|
switch ($type) {
|
||
|
case ChoiceType::class:
|
||
|
$options['choices'] = $config['choices'];
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
$formBuilder->add($config['name'], $type, $options);
|
||
|
}
|
||
|
}
|