diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index 069a706c..bc76e097 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -44,7 +44,7 @@ class AdminController extends Controller $form->group('max_upload_file_size', '最大允许上传大小', function($group) { // main textbox - $group->text('max_upload_file_size', option('max_upload_file_size')); + $group->text('max_upload_file_size'); $group->addon('KB'); })->hint('PHP 限制:'.ini_get('post_max_size').',定义在 php.ini 中。'); @@ -53,8 +53,6 @@ class AdminController extends Controller $form->select('api_type', '首选 JSON API', function($options) { $options->add('0', 'CustomSkinLoader API'); $options->add('1', 'UniversalSkinAPI'); - - $options->setSelected(option('api_type')); }); $form->checkbox('auto_del_invalid_texture', '失效材质', '自动删除失效材质')->hint('自动从皮肤库中删除文件不存在的材质记录'); diff --git a/app/Services/OptionForm.php b/app/Services/OptionForm.php index caa1e2e0..ae9e92d9 100644 --- a/app/Services/OptionForm.php +++ b/app/Services/OptionForm.php @@ -4,6 +4,7 @@ namespace App\Services; use Option; use Illuminate\Support\Arr; +use Illuminate\Support\Str; class OptionForm { @@ -23,14 +24,12 @@ class OptionForm public function text($id, $name, $value = null) { - return $this->addItem('text', $id, $name); + return $this->addItem('text', $id, $name)->set('value', $value); } public function checkbox($id, $name, $label, $checked = null) { - $checkbox = $this->addItem('checkbox', $id, $name); - - $checkbox->set('label', $label); + $checkbox = $this->addItem('checkbox', $id, $name)->set('label', $label); return $checkbox; } @@ -43,7 +42,7 @@ class OptionForm call_user_func($callback, $select); - $item->set('view', $select->render()); + $item->set('view', $select); return $select; } @@ -56,7 +55,7 @@ class OptionForm call_user_func($callback, $textarea); - $item->set('view', $textarea->render()); + $item->set('view', $textarea); return $textarea; } @@ -69,7 +68,7 @@ class OptionForm call_user_func($callback, $group); - $item->set('view', $group->render()); + $item->set('view', $group); return $item; } @@ -99,9 +98,13 @@ class OptionForm foreach ($this->items as $item) { if ($item->type == "checkbox" && !isset($_POST[$item->id])) { + // preset value for checkboxes which are not checked $_POST[$item->id] = "0"; } + if (Str::is('*[*]', $item->id)) + continue; + if ($_POST[$item->id] != option($item->id)) { Option::set($item->id, $_POST[$item->id]); } @@ -116,8 +119,19 @@ class OptionForm public function render() { foreach ($this->items as $item) { - $id = $item->id; - $value = Option::get($item->id); + $id = $item->id; + + if (!$value = $item->get('value')) { + preg_match('/(.*)\[(.*)\]/', $id, $matches); + + if (isset($matches[2])) { + $option = @unserialize(option($matches[1])); + + $value = Arr::get($option, $matches[2]); + } else { + $value = option($item->id); + } + } switch ($item->type) { case 'text': @@ -135,7 +149,7 @@ class OptionForm case 'select': case 'textarea': case 'group': - $view = $item->get('view'); + $view = $item->get('view')->render(); break; } @@ -188,6 +202,8 @@ class OptionFormItem public function set($key, $value) { $this->data[$key] = $value; + + return $this; } public function get($key) @@ -212,7 +228,7 @@ class OptionFormSelect protected $items; - protected $selected; + protected $selected = null; public function __construct($id) { @@ -231,6 +247,10 @@ class OptionFormSelect public function render() { + if (is_null($this->selected)) { + $this->selected = option($this->id); + } + return view('vendor.option-form.select')->with([ 'id' => $this->id, 'items' => $this->items, diff --git a/config/app.php b/config/app.php index 7349cc5c..418502bf 100644 --- a/config/app.php +++ b/config/app.php @@ -213,6 +213,8 @@ return [ 'URL' => Illuminate\Support\Facades\URL::class, 'Validator' => Illuminate\Support\Facades\Validator::class, 'View' => Illuminate\Support\Facades\View::class, + 'Str' => Illuminate\Support\Str::class, + 'Arr' => Illuminate\Support\Arr::class, /** * Blessing Skin