fix rendering of option forms

This commit is contained in:
printempw 2016-11-18 21:57:47 +08:00
parent b84a137cf7
commit a91da6cf3c
3 changed files with 34 additions and 14 deletions

View File

@ -44,7 +44,7 @@ class AdminController extends Controller
$form->group('max_upload_file_size', '最大允许上传大小', function($group) { $form->group('max_upload_file_size', '最大允许上传大小', function($group) {
// main textbox // main textbox
$group->text('max_upload_file_size', option('max_upload_file_size')); $group->text('max_upload_file_size');
$group->addon('KB'); $group->addon('KB');
})->hint('PHP 限制:'.ini_get('post_max_size').',定义在 php.ini 中。'); })->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) { $form->select('api_type', '首选 JSON API', function($options) {
$options->add('0', 'CustomSkinLoader API'); $options->add('0', 'CustomSkinLoader API');
$options->add('1', 'UniversalSkinAPI'); $options->add('1', 'UniversalSkinAPI');
$options->setSelected(option('api_type'));
}); });
$form->checkbox('auto_del_invalid_texture', '失效材质', '自动删除失效材质')->hint('自动从皮肤库中删除文件不存在的材质记录'); $form->checkbox('auto_del_invalid_texture', '失效材质', '自动删除失效材质')->hint('自动从皮肤库中删除文件不存在的材质记录');

View File

@ -4,6 +4,7 @@ namespace App\Services;
use Option; use Option;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class OptionForm class OptionForm
{ {
@ -23,14 +24,12 @@ class OptionForm
public function text($id, $name, $value = null) 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) public function checkbox($id, $name, $label, $checked = null)
{ {
$checkbox = $this->addItem('checkbox', $id, $name); $checkbox = $this->addItem('checkbox', $id, $name)->set('label', $label);
$checkbox->set('label', $label);
return $checkbox; return $checkbox;
} }
@ -43,7 +42,7 @@ class OptionForm
call_user_func($callback, $select); call_user_func($callback, $select);
$item->set('view', $select->render()); $item->set('view', $select);
return $select; return $select;
} }
@ -56,7 +55,7 @@ class OptionForm
call_user_func($callback, $textarea); call_user_func($callback, $textarea);
$item->set('view', $textarea->render()); $item->set('view', $textarea);
return $textarea; return $textarea;
} }
@ -69,7 +68,7 @@ class OptionForm
call_user_func($callback, $group); call_user_func($callback, $group);
$item->set('view', $group->render()); $item->set('view', $group);
return $item; return $item;
} }
@ -99,9 +98,13 @@ class OptionForm
foreach ($this->items as $item) { foreach ($this->items as $item) {
if ($item->type == "checkbox" && !isset($_POST[$item->id])) { if ($item->type == "checkbox" && !isset($_POST[$item->id])) {
// preset value for checkboxes which are not checked
$_POST[$item->id] = "0"; $_POST[$item->id] = "0";
} }
if (Str::is('*[*]', $item->id))
continue;
if ($_POST[$item->id] != option($item->id)) { if ($_POST[$item->id] != option($item->id)) {
Option::set($item->id, $_POST[$item->id]); Option::set($item->id, $_POST[$item->id]);
} }
@ -116,8 +119,19 @@ class OptionForm
public function render() public function render()
{ {
foreach ($this->items as $item) { foreach ($this->items as $item) {
$id = $item->id; $id = $item->id;
$value = Option::get($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) { switch ($item->type) {
case 'text': case 'text':
@ -135,7 +149,7 @@ class OptionForm
case 'select': case 'select':
case 'textarea': case 'textarea':
case 'group': case 'group':
$view = $item->get('view'); $view = $item->get('view')->render();
break; break;
} }
@ -188,6 +202,8 @@ class OptionFormItem
public function set($key, $value) public function set($key, $value)
{ {
$this->data[$key] = $value; $this->data[$key] = $value;
return $this;
} }
public function get($key) public function get($key)
@ -212,7 +228,7 @@ class OptionFormSelect
protected $items; protected $items;
protected $selected; protected $selected = null;
public function __construct($id) public function __construct($id)
{ {
@ -231,6 +247,10 @@ class OptionFormSelect
public function render() public function render()
{ {
if (is_null($this->selected)) {
$this->selected = option($this->id);
}
return view('vendor.option-form.select')->with([ return view('vendor.option-form.select')->with([
'id' => $this->id, 'id' => $this->id,
'items' => $this->items, 'items' => $this->items,

View File

@ -213,6 +213,8 @@ return [
'URL' => Illuminate\Support\Facades\URL::class, 'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class, 'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class, 'View' => Illuminate\Support\Facades\View::class,
'Str' => Illuminate\Support\Str::class,
'Arr' => Illuminate\Support\Arr::class,
/** /**
* Blessing Skin * Blessing Skin