blessing-skin-server/app/Services/OptionForm.php

356 lines
7.6 KiB
PHP
Raw Normal View History

2016-11-12 23:50:41 +08:00
<?php
namespace App\Services;
use Option;
use Illuminate\Support\Arr;
2016-11-18 21:57:47 +08:00
use Illuminate\Support\Str;
2016-11-12 23:50:41 +08:00
class OptionForm
{
public $id;
public $title;
protected $hint;
protected $items;
protected $success = false;
protected $messages = [];
protected $alwaysCallback = null;
2016-11-12 23:50:41 +08:00
public function __construct($id, $title)
{
$this->id = $id;
$this->title = $title;
}
public function text($id, $name, $value = null)
{
2016-11-18 21:57:47 +08:00
return $this->addItem('text', $id, $name)->set('value', $value);
2016-11-12 23:50:41 +08:00
}
public function checkbox($id, $name, $label, $checked = null)
{
2016-11-18 21:57:47 +08:00
$checkbox = $this->addItem('checkbox', $id, $name)->set('label', $label);
2016-11-12 23:50:41 +08:00
return $checkbox;
}
public function select($id, $name, $callback)
{
$item = $this->addItem('select', $id, $name);
$select = new OptionFormSelect($id);
call_user_func($callback, $select);
2016-11-18 21:57:47 +08:00
$item->set('view', $select);
2016-11-12 23:50:41 +08:00
return $select;
}
2016-12-11 22:26:58 +08:00
public function textarea($id, $name, $callback)
2016-11-12 23:50:41 +08:00
{
$item = $this->addItem('textarea', $id, $name);
2016-12-11 22:26:58 +08:00
$textarea = new OptionFormTextarea($id);
2016-11-12 23:50:41 +08:00
call_user_func($callback, $textarea);
2016-11-18 21:57:47 +08:00
$item->set('view', $textarea);
2016-11-12 23:50:41 +08:00
return $textarea;
}
public function group($id, $name, $callback)
{
$item = $this->addItem('group', $id, $name);
$group = new OptionFormGroup($id);
call_user_func($callback, $group);
2016-11-18 21:57:47 +08:00
$item->set('view', $group);
2016-11-12 23:50:41 +08:00
return $item;
}
public function addItem($type, $id, $name)
{
$item = new OptionFormItem($id, $name, $type);
$this->items[] = $item;
return $item;
}
public function hint($hint_content)
{
$this->hint = view('vendor.option-form.hint')->with('hint', $hint_content)->render();
return $this;
}
public function addMessage($msg, $type = "info")
{
$this->messages[] = "<div class='callout callout-$type'>$msg</div>";
}
2016-11-12 23:50:41 +08:00
public function handle($callback = null)
{
if (Arr::get($_POST, 'option') == $this->id) {
if (!is_null($callback)) {
call_user_func($callback, $this);
2016-11-12 23:50:41 +08:00
}
foreach ($this->items as $item) {
if ($item->type == "checkbox" && !isset($_POST[$item->id])) {
2016-11-18 21:57:47 +08:00
// preset value for checkboxes which are not checked
2016-11-12 23:50:41 +08:00
$_POST[$item->id] = "0";
}
2016-11-18 21:57:47 +08:00
if (Str::is('*[*]', $item->id))
continue;
if ($_POST[$item->id] != option($item->id, null, false)) {
2016-11-12 23:50:41 +08:00
Option::set($item->id, $_POST[$item->id]);
}
}
2016-12-11 22:26:58 +08:00
session()->flash($this->id.'.status', 'success');
2016-11-12 23:50:41 +08:00
}
return $this;
}
public function always($callback)
{
$this->alwaysCallback = $callback;
return $this;
}
2016-11-12 23:50:41 +08:00
public function render()
{
if (!is_null($this->alwaysCallback)) {
call_user_func($this->alwaysCallback, $this);
}
2016-11-12 23:50:41 +08:00
foreach ($this->items as $item) {
2016-11-18 21:57:47 +08:00
$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);
}
}
2016-11-12 23:50:41 +08:00
switch ($item->type) {
case 'text':
$view = view('vendor.option-form.text')->with(compact('id', 'value'));
break;
case 'checkbox':
$view = view('vendor.option-form.checkbox')->with([
'id' => $id,
'label' => $item->get('label'),
'checked' => (bool) $value
]);
break;
case 'select':
case 'textarea':
case 'group':
2016-11-18 21:57:47 +08:00
$view = $item->get('view')->render();
2016-11-12 23:50:41 +08:00
break;
}
$item->setContent($view->render());
}
return view('vendor.option-form.main')->with(get_object_vars($this))->render();
2016-11-12 23:50:41 +08:00
}
}
class OptionFormItem
{
public $id;
public $type;
public $title;
protected $data;
protected $hint;
protected $content;
public function __construct($id, $title, $type = "")
{
$this->id = $id;
$this->type = $type;
$this->title = $title;
}
public function hint($hint_content)
{
$this->hint = view('vendor.option-form.hint')->with('hint', $hint_content)->render();
return $this;
}
public function setContent($content)
{
$this->content = $content;
return $this;
}
public function set($key, $value)
{
$this->data[$key] = $value;
2016-11-18 21:57:47 +08:00
return $this;
2016-11-12 23:50:41 +08:00
}
public function get($key)
{
return Arr::get($this->data, $key);
}
public function render()
{
return view('vendor.option-form.item')->with([
'title' => $this->title,
'content' => $this->content,
'hint' => $this->hint
]);
}
}
class OptionFormSelect
{
protected $id;
protected $items;
2016-11-18 21:57:47 +08:00
protected $selected = null;
2016-11-12 23:50:41 +08:00
public function __construct($id)
{
$this->id = $id;
}
public function add($id, $name)
{
$this->items[] = [$id, $name];
}
public function setSelected($id)
{
$this->selected = $id;
}
public function render()
{
2016-11-18 21:57:47 +08:00
if (is_null($this->selected)) {
$this->selected = option($this->id);
}
2016-11-12 23:50:41 +08:00
return view('vendor.option-form.select')->with([
'id' => $this->id,
'items' => $this->items,
'selected' => $this->selected
]);
}
}
class OptionFormTextarea
{
protected $id;
protected $value;
protected $rows = 3;
protected $description = "";
2016-12-11 22:26:58 +08:00
public function __construct($id)
2016-11-12 23:50:41 +08:00
{
2016-12-11 22:26:58 +08:00
$this->id = $id;
}
public function setContent($content)
{
$this->value = $content;
2016-11-12 23:50:41 +08:00
}
public function setRows($rows)
{
$this->rows = $rows;
}
public function setDescription($description)
{
$this->description = $description;
}
public function render()
{
2016-12-11 22:26:58 +08:00
if (is_null($this->value)) {
$this->value = option($this->id);
}
2016-11-12 23:50:41 +08:00
return view('vendor.option-form.textarea')->with([
'rows' => $this->rows,
'id' => $this->id,
'value' => $this->value,
'description' => $this->description
]);
}
}
class OptionFormGroup
{
protected $id;
protected $items = [];
public function __construct($id)
{
$this->id = $id;
}
2016-12-11 22:26:58 +08:00
public function text($id, $value = null)
2016-11-12 23:50:41 +08:00
{
2016-12-11 22:26:58 +08:00
$this->items[] = ['type' => 'text', 'id' => $id, 'value' => $value];
2016-11-12 23:50:41 +08:00
}
public function addon($value)
{
2016-12-11 22:26:58 +08:00
$this->items[] = ['type' => 'addon', 'id' => null, 'value' => $value];
2016-11-12 23:50:41 +08:00
}
public function render()
{
2016-12-11 22:26:58 +08:00
$rendered = [];
foreach ($this->items as $item) {
if ($item['id'] && is_null($item['value'])) {
$item['value'] = option($item['id'], null, false);
2016-12-11 22:26:58 +08:00
}
$rendered[] = view('vendor.option-form.'.$item['type'])->withId($item['id'])->withValue($item['value']);
}
return view('vendor.option-form.group')->with('items', $rendered);
2016-11-12 23:50:41 +08:00
}
}