add renderWithOutTable & renderInputTagsOnly for OptionForm

This commit is contained in:
printempw 2016-12-29 13:11:46 +08:00
parent 1d7dc13e18
commit 228c17ca68
6 changed files with 99 additions and 72 deletions

View File

@ -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'));
}
/**

View File

@ -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[] = "<div class='callout callout-$type'>$msg</div>";
}
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;
}
}

View File

@ -23,28 +23,7 @@
</div>
<div class="col-md-6">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">站点公告</h3>
</div><!-- /.box-header -->
<form method="post">
<input type="hidden" name="option" value="announcement">
<div class="box-body">
<?php
if (isset($_POST['option']) && ($_POST['option'] == "announcement")) {
Option::set('announcement', $_POST['announcement']);
echo '<div class="callout callout-success">设置已保存。</div>';
} ?>
<textarea name="announcement" class="form-control" rows="3">{{ option('announcement') }}</textarea>
<p class="description">可使用 Markdown 进行排版</p>
</div><!-- /.box-body -->
<div class="box-footer">
<button type="submit" name="submit" class="btn btn-primary">提交</button>
</div>
</form>
</div>
{!! $forms['announcement']->render() !!}
{!! $forms['cache']->render() !!}
</div>

View File

@ -1,3 +1,3 @@
<label for="{{ $id }}">
<input {!! $value ? 'checked="true"' : '' !!} type="checkbox" id="{{ $id }}" name="{{ $id }}" value="1"> {{ $label }}
<input {!! $value ? 'checked="true"' : '' !!} type="checkbox" id="{{ $id }}" name="{{ $id }}" value="on"> {{ $label }}
</label>

View File

@ -1,10 +0,0 @@
<tr>
<td class="key">{{ $item->name }} {!! $item->hint or '' !!}</td>
<td class="value">
{!! $item->render() !!}
@if ($item->description != "")
<p class="description">{!! $item->description !!}</p>
@endif
</td>
</tr>

View File

@ -15,7 +15,28 @@
<table class="table">
<tbody>
@foreach($items as $item)
@include('vendor.option-form.item', compact('item'))
@unless ($renderWithOutTable)
<tr>
@unless ($renderInputTagsOnly)
<td class="key">{{ $item->name }} {!! $item->hint or '' !!}</td>
@endunless
<td class="value">
@endunless
{!! $item->render() !!}
@if ($item->description)
<p class="description">{!! $item->description !!}</p>
@endif
@unless ($renderWithOutTable)
</td>
</tr>
@endunless
@endforeach
</tbody>
</table>