From 325ea187e03af5cf2ffb04c81e548e84c580b83c Mon Sep 17 00:00:00 2001 From: printempw Date: Sun, 11 Dec 2016 22:26:58 +0800 Subject: [PATCH] fix rendering content of option form --- app/Http/Controllers/AdminController.php | 4 +-- app/Services/OptionForm.php | 38 +++++++++++++++------ resources/views/vendor/option-form/main.tpl | 2 +- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index a044e11c..14881281 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -45,7 +45,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 中。'); @@ -58,7 +58,7 @@ class AdminController extends Controller $form->checkbox('auto_del_invalid_texture', '失效材质', '自动删除失效材质')->hint('自动从皮肤库中删除文件不存在的材质记录'); - $form->textarea('comment_script', '评论代码', option('comment_script'), function($textarea) { + $form->textarea('comment_script', '评论代码', function($textarea) { $textarea->setRows(6); $textarea->setDescription('评论代码内可使用占位符,{tid} 将会被自动替换为材质的 id,{name} 会被替换为材质名称,{url} 会被替换为当前页面地址。'); }); diff --git a/app/Services/OptionForm.php b/app/Services/OptionForm.php index a1e051da..8be00362 100644 --- a/app/Services/OptionForm.php +++ b/app/Services/OptionForm.php @@ -51,11 +51,11 @@ class OptionForm return $select; } - public function textarea($id, $name, $value, $callback) + public function textarea($id, $name, $callback) { $item = $this->addItem('textarea', $id, $name); - $textarea = new OptionFormTextarea($id, $value); + $textarea = new OptionFormTextarea($id); call_user_func($callback, $textarea); @@ -119,7 +119,7 @@ class OptionForm } } - $this->success = true; + session()->flash($this->id.'.status', 'success'); } return $this; @@ -282,10 +282,14 @@ class OptionFormTextarea protected $description = ""; - public function __construct($id, $value) + public function __construct($id) { - $this->id = $id; - $this->value = $value; + $this->id = $id; + } + + public function setContent($content) + { + $this->value = $content; } public function setRows($rows) @@ -300,6 +304,10 @@ class OptionFormTextarea public function render() { + if (is_null($this->value)) { + $this->value = option($this->id); + } + return view('vendor.option-form.textarea')->with([ 'rows' => $this->rows, 'id' => $this->id, @@ -320,18 +328,28 @@ class OptionFormGroup $this->id = $id; } - public function text($id, $value) + public function text($id, $value = null) { - $this->items[] = view('vendor.option-form.text')->withId($id)->withValue($value); + $this->items[] = ['type' => 'text', 'id' => $id, 'value' => $value]; } public function addon($value) { - $this->items[] = view('vendor.option-form.addon')->withValue($value); + $this->items[] = ['type' => 'addon', 'id' => null, 'value' => $value]; } public function render() { - return view('vendor.option-form.group')->with('items', $this->items); + $rendered = []; + + foreach ($this->items as $item) { + if ($item['id'] && !$item['value']) { + $item['value'] = option($item['id']); + } + + $rendered[] = view('vendor.option-form.'.$item['type'])->withId($item['id'])->withValue($item['value']); + } + + return view('vendor.option-form.group')->with('items', $rendered); } } diff --git a/resources/views/vendor/option-form/main.tpl b/resources/views/vendor/option-form/main.tpl index 055d5709..85ace27a 100644 --- a/resources/views/vendor/option-form/main.tpl +++ b/resources/views/vendor/option-form/main.tpl @@ -5,7 +5,7 @@
- @if ($success) + @if (session("$id.status") == 'success')
设置已保存。
@endif