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

338 lines
7.1 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;
}
public function textarea($id, $name, $value, $callback)
{
$item = $this->addItem('textarea', $id, $name);
$textarea = new OptionFormTextarea($id, $value);
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;
2016-11-12 23:50:41 +08:00
if ($_POST[$item->id] != option($item->id)) {
Option::set($item->id, $_POST[$item->id]);
}
}
$this->success = true;
}
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 = "";
public function __construct($id, $value)
{
$this->id = $id;
$this->value = $value;
}
public function setRows($rows)
{
$this->rows = $rows;
}
public function setDescription($description)
{
$this->description = $description;
}
public function render()
{
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;
}
public function text($id, $value)
{
$this->items[] = view('vendor.option-form.text')->withId($id)->withValue($value);
}
public function addon($value)
{
$this->items[] = view('vendor.option-form.addon')->withValue($value);
}
public function render()
{
return view('vendor.option-form.group')->with('items', $this->items);
}
}