[CONTROLLER][ADMIN][CONFIG] Fix form to use static strings as keys and add labels seperately; convert input from string to appropriate type

This commit is contained in:
Hugo Sales 2020-07-25 14:43:43 +00:00 committed by Hugo Sales
parent b772702895
commit c26ffe09b6
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -33,10 +33,11 @@
namespace App\Controller; namespace App\Controller;
use App\Core\Controller; use App\Core\Controller;
use App\Core\DB\DB;
use App\Core\DB\DefaultSettings; use App\Core\DB\DefaultSettings;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; 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\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
@ -56,29 +57,35 @@ class AdminPanel extends Controller
} }
$form = Form::create([ $form = Form::create([
[_m('Setting'), ChoiceType::class, ['choices' => $options]], ['setting', ChoiceType::class, ['label' => _m('Setting'), 'choices' => $options]],
[_m('Value'), TextType::class], ['value', TextType::class, ['label' => _m('Value')]],
['save', SubmitType::class, ['label' => _m('Set site setting')]], ['save', SubmitType::class, ['label' => _m('Set site setting')]],
]); ]);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted()) { if ($form->isSubmitted()) {
$data = $form->getData(); $data = $form->getData();
if ($form->isValid() && array_key_exists(_m('Setting'), $data)) { if ($form->isValid() && array_key_exists('setting', $data)) {
list($section, $setting) = explode(':', $data[_m('Setting')]); list($section, $setting) = explode(':', $data['setting']);
$value = $data[_m('Value')]; $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]; $default = $defaults[$section][$setting];
// Sanity check // Sanity check
if (gettype($default) === gettype($value)) { if (gettype($default) === gettype($value)) {
$conf = DB::find('config', ['section' => $section, 'setting' => $setting]); $old_value = Common::config($section, $setting);
$old_value = unserialize($conf->getValue()); Common::setConfig($section, $setting, $value);
$conf->setValue(serialize($value));
DB::flush();
return [ return [
'_template' => 'config/admin.html.twig', '_template' => 'config/admin.html.twig',
'form' => $form->createView(), 'form' => $form->createView(),
'old_value' => $old_value, 'old_value' => Formatting::toString($old_value),
'default' => $default, 'default' => Formatting::toString($default),
]; ];
} }
} else { } else {