From 228c17ca68f67bf8ae6e75cf0186658b8da89a58 Mon Sep 17 00:00:00 2001 From: printempw Date: Thu, 29 Dec 2016 13:11:46 +0800 Subject: [PATCH] add renderWithOutTable & renderInputTagsOnly for OptionForm --- app/Http/Controllers/AdminController.php | 10 +- app/Services/OptionForm.php | 103 ++++++++++++------ resources/views/admin/options.tpl | 23 +--- .../views/vendor/option-form/checkbox.tpl | 2 +- resources/views/vendor/option-form/item.tpl | 10 -- resources/views/vendor/option-form/main.tpl | 23 +++- 6 files changed, 99 insertions(+), 72 deletions(-) delete mode 100644 resources/views/vendor/option-form/item.tpl diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index 55ae9870..28f0a536 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -90,7 +90,7 @@ class AdminController extends Controller $form->group('max_upload_file_size', '最大允许上传大小') ->text('max_upload_file_size')->addon('KB') - ->hint('PHP 限制:'.ini_get('post_max_size').',定义在 php.ini 中。'); + ->hint('PHP 限制:'.ini_get('upload_max_filesize').',定义在 php.ini 中。'); $form->checkbox('allow_chinese_playername', '角色名')->label('允许中文角色名'); @@ -107,6 +107,12 @@ class AdminController extends Controller $_POST['site_url'] = substr($_POST['site_url'], 0, -1); }); + $announcement = Option::form('announcement', '站点公告', function($form) + { + $form->textarea('announcement')->description('可使用 Markdown 进行排版'); + + })->renderWithOutTable()->handle(); + $cache = Option::form('cache', '资源文件配置', function($form) { $form->checkbox('force_ssl', '强制 SSL')->label('强制使用 HTTPS 协议加载资源')->hint('请确认 SSL 可用后再开启'); @@ -117,7 +123,7 @@ class AdminController extends Controller })->type('warning')->hint('如果启用了 CDN 缓存请适当修改这些配置')->handle(); - return view('admin.options')->with('forms', compact('general', 'cache')); + return view('admin.options')->with('forms', compact('general', 'cache', 'announcement')); } /** diff --git a/app/Services/OptionForm.php b/app/Services/OptionForm.php index df770ed9..978a91d4 100644 --- a/app/Services/OptionForm.php +++ b/app/Services/OptionForm.php @@ -8,8 +8,8 @@ use Illuminate\Support\Str; class OptionForm { - public $id; - public $title; + protected $id; + protected $title; protected $hint; protected $type = 'primary'; @@ -21,6 +21,9 @@ class OptionForm protected $alwaysCallback = null; + protected $renderWithOutTable = false; + protected $renderInputTagsOnly = false; + public function __construct($id, $title) { $this->id = $id; @@ -55,9 +58,13 @@ class OptionForm return $this; } - public function setValues(array $values) + public function with($key, $value = null) { - $this->values = array_merge($this->values, $values); + if (is_array($key)) { + $this->values = array_merge($this->values, $values); + } else { + $this->values[$key] = $value; + } return $this; } @@ -67,6 +74,29 @@ class OptionForm $this->messages[] = "
$msg
"; } + public function always($callback) + { + $this->alwaysCallback = $callback; + + return $this; + } + + protected function parseIdWithOffset($id) + { + // detect if id is formatted as *[*] + // array option is stored as unserialized string + preg_match('/(.*)\[(.*)\]/', $id, $matches); + + if (isset($matches[2])) { + return [ + 'id' => $matches[1], + 'offset' => $matches[2] + ]; + } + + return false; + } + public function handle($callback = null) { if (Arr::get($_POST, 'option') == $this->id) { @@ -104,36 +134,13 @@ class OptionForm return $this; } - public function always($callback) - { - $this->alwaysCallback = $callback; - - return $this; - } - - protected function parseIdWithOffset($id) - { - // detect if id is formatted as *[*] - // array option is stored as unserialized string - preg_match('/(.*)\[(.*)\]/', $id, $matches); - - if (isset($matches[2])) { - return [ - 'id' => $matches[1], - 'offset' => $matches[2] - ]; - } - - return false; - } - /** * Load value from $this->values & options. * * @param string $id * @return mixed */ - protected function loadValueFromId($id) + protected function getValueById($id) { if (false === ($result = $this->parseIdWithOffset($id))) { return option($id); @@ -149,7 +156,7 @@ class OptionForm } } - public function render() + protected function assignValues() { if (!is_null($this->alwaysCallback)) { call_user_func($this->alwaysCallback, $this); @@ -160,18 +167,37 @@ class OptionForm if ($item instanceof OptionFormGroup) { foreach ($item->items as $groupItem) { if ($groupItem['id'] && is_null($groupItem['value'])) { - $groupItem['value'] = $this->loadValueFromId($groupItem['id']); + $groupItem['value'] = $this->getValueById($groupItem['id']); } } continue; } if (is_null($item->value)) { - $item->value = $this->loadValueFromId($item->id); + $item->value = $this->getValueById($item->id); } } + } - return view('vendor.option-form.main')->with(get_object_vars($this))->render(); + public function renderWithOutTable() + { + $this->renderWithOutTable = true; + + return $this; + } + + public function renderInputTagsOnly() + { + $this->renderInputTagsOnly = true; + + return $this; + } + + public function render() + { + $this->assignValues(); + + return view('vendor.option-form.main')->with(array_merge(get_object_vars($this)))->render(); } } @@ -181,13 +207,13 @@ class OptionFormItem public $name; - public $value = null; - public $hint; + public $value = null; + public $description; - public function __construct($id, $name) + public function __construct($id, $name = null) { $this->id = $id; $this->name = $name; @@ -214,9 +240,14 @@ class OptionFormItem return $this; } + /** + * Render option item. Should be extended. + * + * @return \Illuminate\View\View|string + */ public function render() { - // + return; } } diff --git a/resources/views/admin/options.tpl b/resources/views/admin/options.tpl index 698743ec..675da70b 100644 --- a/resources/views/admin/options.tpl +++ b/resources/views/admin/options.tpl @@ -23,28 +23,7 @@
-
-
-

站点公告

-
-
- -
- 设置已保存。
'; - } ?> - - -

可使用 Markdown 进行排版

- -
- - -
+ {!! $forms['announcement']->render() !!} {!! $forms['cache']->render() !!} diff --git a/resources/views/vendor/option-form/checkbox.tpl b/resources/views/vendor/option-form/checkbox.tpl index f57babfb..07068495 100644 --- a/resources/views/vendor/option-form/checkbox.tpl +++ b/resources/views/vendor/option-form/checkbox.tpl @@ -1,3 +1,3 @@ diff --git a/resources/views/vendor/option-form/item.tpl b/resources/views/vendor/option-form/item.tpl deleted file mode 100644 index 2dd3818b..00000000 --- a/resources/views/vendor/option-form/item.tpl +++ /dev/null @@ -1,10 +0,0 @@ - - {{ $item->name }} {!! $item->hint or '' !!} - - {!! $item->render() !!} - - @if ($item->description != "") -

{!! $item->description !!}

- @endif - - diff --git a/resources/views/vendor/option-form/main.tpl b/resources/views/vendor/option-form/main.tpl index 2c37f2df..960f5c9d 100644 --- a/resources/views/vendor/option-form/main.tpl +++ b/resources/views/vendor/option-form/main.tpl @@ -15,7 +15,28 @@ @foreach($items as $item) - @include('vendor.option-form.item', compact('item')) + + + @unless ($renderWithOutTable) + + @unless ($renderInputTagsOnly) + + @endunless + + + + @endunless + @endforeach
{{ $item->name }} {!! $item->hint or '' !!} + @endunless + + {!! $item->render() !!} + + @if ($item->description) +

{!! $item->description !!}

+ @endif + + @unless ($renderWithOutTable) +