From c26ffe09b6911c8b18f62a30c2cf7146065bd366 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sat, 25 Jul 2020 14:43:43 +0000 Subject: [PATCH] [CONTROLLER][ADMIN][CONFIG] Fix form to use static strings as keys and add labels seperately; convert input from string to appropriate type --- src/Controller/AdminPanel.php | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Controller/AdminPanel.php b/src/Controller/AdminPanel.php index 33d8d7ece4..2ba9bcc349 100644 --- a/src/Controller/AdminPanel.php +++ b/src/Controller/AdminPanel.php @@ -33,10 +33,11 @@ namespace App\Controller; use App\Core\Controller; -use App\Core\DB\DB; use App\Core\DB\DefaultSettings; use App\Core\Form; use function App\Core\I18n\_m; +use App\Util\Common; +use App\Util\Formatting; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextType; @@ -56,29 +57,35 @@ class AdminPanel extends Controller } $form = Form::create([ - [_m('Setting'), ChoiceType::class, ['choices' => $options]], - [_m('Value'), TextType::class], - ['save', SubmitType::class, ['label' => _m('Set site setting')]], + ['setting', ChoiceType::class, ['label' => _m('Setting'), 'choices' => $options]], + ['value', TextType::class, ['label' => _m('Value')]], + ['save', SubmitType::class, ['label' => _m('Set site setting')]], ]); $form->handleRequest($request); if ($form->isSubmitted()) { $data = $form->getData(); - 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 ($form->isValid() && array_key_exists('setting', $data)) { + list($section, $setting) = explode(':', $data['setting']); + $value = $data['value']; + if (preg_match('/^[0-9]+$/', $value)) { + $value = (int) $value; + } elseif (Formatting::toArray($value, $value)) { + // empty + } elseif (preg_match('/true|false/i', $value)) { + $value = ($value == 'true'); + } + + $default = $defaults[$section][$setting]; // Sanity check if (gettype($default) === gettype($value)) { - $conf = DB::find('config', ['section' => $section, 'setting' => $setting]); - $old_value = unserialize($conf->getValue()); - $conf->setValue(serialize($value)); - DB::flush(); + $old_value = Common::config($section, $setting); + Common::setConfig($section, $setting, $value); return [ '_template' => 'config/admin.html.twig', 'form' => $form->createView(), - 'old_value' => $old_value, - 'default' => $default, + 'old_value' => Formatting::toString($old_value), + 'default' => Formatting::toString($default), ]; } } else {