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

574 lines
14 KiB
PHP
Raw Normal View History

2016-11-12 23:50:41 +08:00
<?php
namespace App\Services;
use Option;
use ReflectionClass;
2016-11-12 23:50:41 +08:00
use Illuminate\Support\Arr;
2016-11-18 21:57:47 +08:00
use Illuminate\Support\Str;
use BadMethodCallException;
2016-11-12 23:50:41 +08:00
class OptionForm
{
2016-12-31 23:28:09 +08:00
/**
* Pass this value to tell generator to
* load text from language files automatically.
*/
const AUTO_DETECT = 0x97ab1;
protected $id;
protected $title;
2016-11-12 23:50:41 +08:00
protected $hint;
protected $type = 'primary';
protected $items = [];
2016-11-12 23:50:41 +08:00
protected $values = [];
2016-11-12 23:50:41 +08:00
protected $buttons = [];
protected $messages = [];
protected $alwaysCallback = null;
protected $renderWithOutTable = false;
protected $renderInputTagsOnly = false;
protected $renderWithOutSubmitButton = false;
/**
* Create a new option form instance.
*
* @param string $id
* @param string $title
* @return void
*/
2016-11-12 23:50:41 +08:00
public function __construct($id, $title)
{
2016-12-31 23:28:09 +08:00
$this->id = $id;
if ($title == self::AUTO_DETECT) {
$this->title = trans("options.$this->id.title");
} else {
$this->title = $title;
}
2016-11-12 23:50:41 +08:00
}
/**
* Add option item to the form dynamically.
*
* @param string $method
2016-12-31 23:28:09 +08:00
* @param array $params
* @return OptionItem
*
* @throws \BadMethodCallException
*/
2016-12-31 23:28:09 +08:00
public function __call($method, $params)
2016-11-12 23:50:41 +08:00
{
if (!in_array($method, ['text', 'checkbox', 'textarea', 'select', 'group'])) {
throw new BadMethodCallException("Method [$method] does not exist on option form.");
2016-12-28 23:28:15 +08:00
}
2016-11-12 23:50:41 +08:00
// assign name for option item
2016-12-31 23:28:09 +08:00
if (!isset($params[1]) || Arr::get($params, 1) == OptionForm::AUTO_DETECT) {
$params[1] = Arr::get(trans("options.$this->id.$params[0]"), 'title', trans("options.$this->id.$params[0]"));
2016-12-31 23:28:09 +08:00
}
$class = new ReflectionClass('App\Services\OptionForm'.Str::title($method));
2016-12-28 23:28:15 +08:00
// use ReflectionClass to create a new OptionFormItem instance
2016-12-31 23:28:09 +08:00
$item = $class->newInstanceArgs($params);
$item->setParentId($this->id);
2016-12-28 23:28:15 +08:00
$this->items[] = $item;
2016-11-12 23:50:41 +08:00
2016-12-28 23:28:15 +08:00
return $item;
2016-11-12 23:50:41 +08:00
}
/**
* Set the box type of option form.
*
* @param string $type
* @return $this
*/
2016-12-28 23:28:15 +08:00
public function type($type)
2016-11-12 23:50:41 +08:00
{
2016-12-28 23:28:15 +08:00
$this->type = $type;
2016-11-12 23:50:41 +08:00
2016-12-28 23:28:15 +08:00
return $this;
2016-11-12 23:50:41 +08:00
}
/**
* Add a hint to option form.
*
* @param array $info
* @return $this
*/
public function hint($hintContent = self::AUTO_DETECT)
2016-11-12 23:50:41 +08:00
{
2016-12-31 23:28:09 +08:00
if ($hintContent == self::AUTO_DETECT) {
$hintContent = trans("options.$this->id.hint");
}
$this->hint = view('vendor.option-form.hint')->with('hint', $hintContent)->render();
2016-11-12 23:50:41 +08:00
2016-12-28 23:28:15 +08:00
return $this;
2016-11-12 23:50:41 +08:00
}
/**
* Add a piece of data to the option form.
*
* @param string|array $key
* @param mixed $value
* @return $this
*/
public function with($key, $value = null)
2016-11-12 23:50:41 +08:00
{
if (is_array($key)) {
$this->values = array_merge($this->values, $key);
} else {
$this->values[$key] = $value;
}
2016-11-12 23:50:41 +08:00
return $this;
}
/**
* Add a button at the footer of option form.
*
* @param array $info
* @return $this
*/
public function addButton(array $info)
{
$info = array_merge([
'style' => 'default',
'class' => [],
'href' => '',
'text' => 'BUTTON',
'type' => 'button',
'name' => ''
], $info);
$classes = "btn btn-{$info['style']} ".implode(' ', (array) Arr::get($info, 'class'));
if ($info['href']) {
$this->buttons[] = "<a href='{$info['href']}' class='$classes'>{$info['text']}</a>";
} else {
$this->buttons[] = "<button type='{$info['type']}' name='{$info['name']}' class='$classes'>{$info['text']}</button>";
}
return $this;
}
/**
* Add a message to the top of option form.
*
* @param string $msg
* @param string $style
* @return $this
*/
public function addMessage($msg = self::AUTO_DETECT, $style = "info")
{
2016-12-31 23:28:09 +08:00
if ($msg == self::AUTO_DETECT) {
$msg = trans("options.$this->id.message");
}
$this->messages[] = "<div class='callout callout-$style'>$msg</div>";
return $this;
}
/**
* Add callback which will be always executed.
*
* @param callable $callback
* @return $this
*/
public function always(callable $callback)
{
$this->alwaysCallback = $callback;
return $this;
}
/**
* Parse id formatted as *[*]. Return id & offset when succeed.
*
* @param string $id
* @return bool|array
*/
protected function parseIdWithOffset($id)
{
preg_match('/(.*)\[(.*)\]/', $id, $matches);
if (isset($matches[2])) {
return [
'id' => $matches[1],
'offset' => $matches[2]
];
}
return false;
}
/**
* Handle the HTTP post request and update modified options.
*
* @param callable $callback
* @return $this
*/
public function handle(callable $callback = null)
2016-11-12 23:50:41 +08:00
{
if (Arr::get($_POST, 'option') == $this->id) {
if (!is_null($callback)) {
call_user_func($callback, $this);
2016-11-12 23:50:41 +08:00
}
$postOptionQueue = [];
$arrayOptionQueue = [];
2016-12-28 23:28:15 +08:00
2016-11-12 23:50:41 +08:00
foreach ($this->items as $item) {
if ($item instanceof OptionFormGroup) {
foreach ($item->items as $innerItem) {
if ($innerItem['type'] == "text") {
$postOptionQueue[] = new OptionFormText($innerItem['id']);
}
}
continue;
}
// push item to the queue
$postOptionQueue[] = $item;
}
foreach ($postOptionQueue as $item) {
2016-12-28 23:28:15 +08:00
if ($item instanceof OptionFormCheckbox && !isset($_POST[$item->id])) {
2016-11-18 21:57:47 +08:00
// preset value for checkboxes which are not checked
$_POST[$item->id] = "false";
2016-11-12 23:50:41 +08:00
}
2016-12-28 23:28:15 +08:00
// Str::is('*[*]', $item->id)
if (false !== ($result = $this->parseIdWithOffset($item->id))) {
// Push array option value to cache.
// Values of post ids like *[*] is collected as arrays in $_POST
// automatically by Laravel.
$arrayOptionQueue[$result['id']] = $_POST[$result['id']];
2016-11-18 21:57:47 +08:00
continue;
2016-12-28 23:28:15 +08:00
}
2016-11-18 21:57:47 +08:00
2017-01-02 10:28:10 +08:00
// compare with raw option value
if (($data = Arr::get($_POST, $item->id)) != option($item->id, null, true)) {
Option::set($item->id, $data);
2016-11-12 23:50:41 +08:00
}
}
foreach ($arrayOptionQueue as $key => $value) {
2016-12-28 23:28:15 +08:00
Option::set($key, serialize($value));
}
2016-12-31 23:28:09 +08:00
$this->addMessage(trans('options.option-saved'), 'success');
2016-11-12 23:50:41 +08:00
}
return $this;
}
2016-12-28 23:28:15 +08:00
/**
* Load value from $this->values & options by given id.
2016-12-28 23:28:15 +08:00
*
* @param string $id
* @return mixed
*/
protected function getValueById($id)
2016-12-28 23:28:15 +08:00
{
if (false === ($result = $this->parseIdWithOffset($id))) {
2016-12-30 19:35:07 +08:00
return Arr::get($this->values, $id, option($id));
2016-12-28 23:28:15 +08:00
} else {
$option = Arr::get(
$this->values,
$result['id'],
// fallback to load from options
@unserialize(option($result['id']))
);
return Arr::get($option, $result['offset']);
}
}
/**
* Assign value for option items whose value haven't been set.
*
* @return void
*/
protected function assignValues()
2016-11-12 23:50:41 +08:00
{
2016-12-28 23:28:15 +08:00
// load values for items if not set manually
2016-11-12 23:50:41 +08:00
foreach ($this->items as $item) {
2016-12-28 23:28:15 +08:00
if ($item instanceof OptionFormGroup) {
foreach ($item->items as &$groupItem) {
2016-12-28 23:28:15 +08:00
if ($groupItem['id'] && is_null($groupItem['value'])) {
$groupItem['value'] = $this->getValueById($groupItem['id']);
2016-12-28 23:28:15 +08:00
}
2016-11-18 21:57:47 +08:00
}
2016-12-28 23:28:15 +08:00
continue;
2016-11-18 21:57:47 +08:00
}
2016-11-12 23:50:41 +08:00
2016-12-28 23:28:15 +08:00
if (is_null($item->value)) {
$item->value = $this->getValueById($item->id);
2016-11-12 23:50:41 +08:00
}
}
}
public function renderWithOutTable()
{
$this->renderWithOutTable = true;
2016-11-12 23:50:41 +08:00
return $this;
}
public function renderInputTagsOnly()
{
$this->renderInputTagsOnly = true;
return $this;
}
public function renderWithOutSubmitButton()
{
$this->renderWithOutSubmitButton = true;
return $this;
}
/**
* Get the string contents of the option form.
*
* @return string
*/
public function render()
{
2016-12-30 19:35:07 +08:00
if (!is_null($this->alwaysCallback)) {
call_user_func($this->alwaysCallback, $this);
}
// attach submit button to the form
if (!$this->renderWithOutSubmitButton) {
$this->addButton([
'style' => 'primary',
'text' => trans('general.submit'),
'type' => 'submit',
'name' => 'submit'
]);
}
$this->assignValues();
return view('vendor.option-form.main')->with(array_merge(get_object_vars($this)))->render();
2016-11-12 23:50:41 +08:00
}
/**
* Get the string contents of the option form.
*
* @return string
*/
public function __toString()
{
return $this->render();
}
2016-11-12 23:50:41 +08:00
}
class OptionFormItem
{
public $id;
2016-12-28 23:28:15 +08:00
public $name;
2016-11-12 23:50:41 +08:00
2016-12-28 23:28:15 +08:00
public $hint;
public $value = null;
public $disabled;
2016-12-28 23:28:15 +08:00
public $description;
2016-11-12 23:50:41 +08:00
protected $parentId;
public function __construct($id, $name = null)
2016-11-12 23:50:41 +08:00
{
2016-12-28 23:28:15 +08:00
$this->id = $id;
$this->name = $name;
2016-11-12 23:50:41 +08:00
}
public function setParentId($id)
{
$this->parentId = $id;
return $this;
}
2016-12-28 23:28:15 +08:00
public function value($value)
2016-11-12 23:50:41 +08:00
{
2016-12-28 23:28:15 +08:00
$this->value = $value;
2016-11-12 23:50:41 +08:00
return $this;
}
public function hint($hintContent = OptionForm::AUTO_DETECT)
2016-11-12 23:50:41 +08:00
{
2016-12-31 23:28:09 +08:00
if ($hintContent == OptionForm::AUTO_DETECT) {
$hintContent = trans("options.$this->parentId.$this->id.hint");
2016-12-31 23:28:09 +08:00
}
2016-12-28 23:28:15 +08:00
$this->hint = view('vendor.option-form.hint')->with('hint', $hintContent)->render();
2016-11-12 23:50:41 +08:00
return $this;
}
public function disabled($disabled = "disabled")
{
$this->disabled = "disabled=\"$disabled\"";
return $this;
}
public function description($description = OptionForm::AUTO_DETECT)
2016-11-12 23:50:41 +08:00
{
2016-12-31 23:28:09 +08:00
if ($description == OptionForm::AUTO_DETECT) {
$description = trans("options.$this->parentId.$this->id.description");
2016-12-31 23:28:09 +08:00
}
2016-12-28 23:28:15 +08:00
$this->description = $description;
2016-11-18 21:57:47 +08:00
return $this;
2016-11-12 23:50:41 +08:00
}
/**
* Render option item. Should be extended.
*
* @return \Illuminate\View\View|string
*/
2016-12-28 23:28:15 +08:00
public function render()
2016-11-12 23:50:41 +08:00
{
return;
2016-11-12 23:50:41 +08:00
}
2016-12-28 23:28:15 +08:00
}
class OptionFormText extends OptionFormItem
{
2016-11-12 23:50:41 +08:00
public function render()
{
2016-12-28 23:28:15 +08:00
return view('vendor.option-form.text')->with([
'id' => $this->id,
'value' => $this->value,
'disabled' => $this->disabled
2016-11-12 23:50:41 +08:00
]);
}
}
2016-12-28 23:28:15 +08:00
class OptionFormCheckbox extends OptionFormItem
2016-11-12 23:50:41 +08:00
{
2016-12-28 23:28:15 +08:00
protected $label;
2016-11-12 23:50:41 +08:00
public function label($label = OptionForm::AUTO_DETECT)
2016-11-12 23:50:41 +08:00
{
2016-12-31 23:28:09 +08:00
if ($label == OptionForm::AUTO_DETECT) {
$label = trans("options.$this->parentId.$this->id.label");
2016-12-31 23:28:09 +08:00
}
2016-12-28 23:28:15 +08:00
$this->label = $label;
2016-11-12 23:50:41 +08:00
2016-12-28 23:28:15 +08:00
return $this;
2016-11-12 23:50:41 +08:00
}
public function render()
{
2016-12-28 23:28:15 +08:00
return view('vendor.option-form.checkbox')->with([
'id' => $this->id,
'value' => $this->value,
'label' => $this->label,
'disabled' => $this->disabled
2016-11-12 23:50:41 +08:00
]);
}
}
2016-12-28 23:28:15 +08:00
class OptionFormTextarea extends OptionFormItem
2016-11-12 23:50:41 +08:00
{
protected $rows = 3;
2016-12-28 23:28:15 +08:00
public function rows($rows)
2016-11-12 23:50:41 +08:00
{
$this->rows = $rows;
2016-12-28 23:28:15 +08:00
return $this;
2016-11-12 23:50:41 +08:00
}
public function render()
{
return view('vendor.option-form.textarea')->with([
2016-12-28 23:28:15 +08:00
'id' => $this->id,
'rows' => $this->rows,
'value' => $this->value,
'disabled' => $this->disabled
2016-11-12 23:50:41 +08:00
]);
}
}
2016-12-28 23:28:15 +08:00
class OptionFormSelect extends OptionFormItem
2016-11-12 23:50:41 +08:00
{
2016-12-28 23:28:15 +08:00
protected $options;
public function option($value, $name)
{
$this->options[] = compact('value', 'name');
2016-11-12 23:50:41 +08:00
2016-12-28 23:28:15 +08:00
return $this;
}
2016-11-12 23:50:41 +08:00
2016-12-28 23:28:15 +08:00
public function render()
2016-11-12 23:50:41 +08:00
{
2016-12-28 23:28:15 +08:00
return view('vendor.option-form.select')->with([
'id' => $this->id,
'options' => $this->options,
'selected' => $this->value,
'disabled' => $this->disabled
2016-12-28 23:28:15 +08:00
]);
2016-11-12 23:50:41 +08:00
}
2016-12-28 23:28:15 +08:00
}
class OptionFormGroup extends OptionFormItem
{
public $items = [];
2016-11-12 23:50:41 +08:00
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-12-28 23:28:15 +08:00
return $this;
2016-11-12 23:50:41 +08:00
}
public function addon($value = OptionForm::AUTO_DETECT)
2016-11-12 23:50:41 +08:00
{
2016-12-31 23:28:09 +08:00
if ($value == OptionForm::AUTO_DETECT) {
$value = trans("options.$this->parentId.$this->id.addon");
2016-12-31 23:28:09 +08:00
}
2016-12-11 22:26:58 +08:00
$this->items[] = ['type' => 'addon', 'id' => null, 'value' => $value];
2016-12-28 23:28:15 +08:00
return $this;
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']);
2016-12-11 22:26:58 +08:00
}
2016-12-28 23:28:15 +08:00
$rendered[] = view('vendor.option-form.'.$item['type'])->with([
'id' => $item['id'],
'value' => $item['value']
]);
2016-12-11 22:26:58 +08:00
}
return view('vendor.option-form.group')->with('items', $rendered);
2016-11-12 23:50:41 +08:00
}
}