[ROUTE][ADMIN][CONFIG] Add route to update values in the config table

This commit is contained in:
Hugo Sales 2020-06-23 21:24:14 +00:00 committed by Hugo Sales
parent e286f39551
commit 44eaf43ba9
No known key found for this signature in database
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 33 additions and 14 deletions

View File

@ -30,9 +30,11 @@
namespace App\Controller;
// use App\Core\GSEvent as Event;
// use App\Core\Event;
// use App\Util\Common;
use App\Core\DB\DB;
use App\Core\DB\DefaultSettings;
use App\Core\Form;
use function App\Core\I18n\_m;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
@ -44,28 +46,40 @@ class AdminConfigController extends AbstractController
{
public function __invoke(Request $request)
{
$defaults = DefaultSettings::$defaults;
$options = [];
foreach (DefaultSettings::$defaults as $key => $inner) {
foreach ($defaults as $key => $inner) {
$options[$key] = [];
foreach (array_keys($inner) as $inner_key) {
$options[_m($key)][_m($inner_key)] = "{$key}:{$inner_key}";
}
}
$form = $this->createFormBuilder(null, ['translation_domain' => false])
->add(_m('Setting'), ChoiceType::class, ['choices' => $options])
->add(_m('Value'), TextType::class)
->add('save', SubmitType::class, ['label' => _m('Set site setting')])
->getForm();
$form = Form::create([[_m('Setting'), ChoiceType::class, ['choices' => $options]],
[_m('Value'), TextType::class],
['save', SubmitType::class, ['label' => _m('Set site setting')]], ]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($form->isSubmitted()) {
$data = $form->getData();
var_dump($data);
// Stay in this page
return $this->redirect($request->getUri());
if ($form->isValid() && array_key_exists(_m('Setting'), $data)) {
list($section, $setting) = explode(':', $data[_m('Setting')]);
$value = $data[_m('Value')];
$default = $defaults[$section][$setting];
if (gettype($default) === gettype($value)) {
$conf = DB::find('\App\Entity\Config', ['section' => $section, 'setting' => $setting]);
$old_value = $conf->getValue();
$conf->setValue(serialize($value));
DB::flush();
}
return $this->render('config/admin.html.twig', [
'form' => $form->createView(),
'old_value' => unserialize($old_value),
'default' => $default,
]);
} else {
// Display error
}
}
return $this->render('config/admin.html.twig', [

View File

@ -16,5 +16,10 @@
{% block javascripts %}{% endblock %}
{{ form(form) }}
{% if old_value is defined and default is defined %}
Old value: {{ old_value }} <br>
Default value: {{ default }}
{% endif %}
</body>
</html>