123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212 |
- <?php
- namespace Dcat\Admin\Form;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Form;
- use Dcat\Admin\Support\Helper;
- use Dcat\Admin\Traits\HasBuilderEvents;
- use Dcat\Admin\Widgets\Form as WidgetForm;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Fluent;
- use Illuminate\Support\Str;
- use Illuminate\Support\Traits\Macroable;
- /**
- * Class Field.
- */
- class Field implements Renderable
- {
- use Macroable,
- Form\Concerns\HasFieldValidator,
- HasBuilderEvents;
- const FILE_DELETE_FLAG = '_file_del_';
- const FIELD_CLASS_PREFIX = 'field_';
- /**
- * Element id.
- *
- * @var array|string
- */
- protected $id;
- /**
- * Element value.
- *
- * @var mixed
- */
- protected $value;
- /**
- * Data of all original columns of value.
- *
- * @var mixed
- */
- protected $data;
- /**
- * Field original value.
- *
- * @var mixed
- */
- protected $original;
- /**
- * Field default value.
- *
- * @var mixed
- */
- protected $default;
- /**
- * Element label.
- *
- * @var string
- */
- protected $label = '';
- /**
- * Column name.
- *
- * @var string|array
- */
- protected $column = '';
- /**
- * Form element name.
- *
- * @var string|array
- */
- protected $elementName = [];
- /**
- * Form element classes.
- *
- * @var array
- */
- protected $elementClass = [];
- /**
- * Variables of elements.
- *
- * @var array
- */
- protected $variables = [];
- /**
- * Options for specify elements.
- *
- * @var array
- */
- protected $options = [];
- /**
- * Checked for specify elements.
- *
- * @var array
- */
- protected $checked = [];
- /**
- * Css required by this field.
- *
- * @var array
- */
- protected static $css = [];
- /**
- * Js required by this field.
- *
- * @var array
- */
- protected static $js = [];
- /**
- * Script for field.
- *
- * @var string
- */
- protected $script = '';
- /**
- * Element attributes.
- *
- * @var array
- */
- protected $attributes = [];
- /**
- * Parent form.
- *
- * @var Form|WidgetForm
- */
- protected $form = null;
- /**
- * View for field to render.
- *
- * @var string
- */
- protected $view = '';
- /**
- * Help block.
- *
- * @var array
- */
- protected $help = [];
- /**
- * Key for errors.
- *
- * @var string|array
- */
- protected $errorKey;
- /**
- * Placeholder for this field.
- *
- * @var string|array
- */
- protected $placeholder;
- /**
- * Width for label and field.
- *
- * @var array
- */
- protected $width = [
- 'label' => 2,
- 'field' => 8,
- ];
- /**
- * If the form horizontal layout.
- *
- * @var bool
- */
- protected $horizontal = true;
- /**
- * column data format.
- *
- * @var \Closure
- */
- protected $customFormat = null;
- /**
- * @var bool
- */
- protected $display = true;
- /**
- * @var array
- */
- protected $labelClass = [];
- /**
- * @var \Closure
- */
- protected $savingCallback;
- /**
- * Field constructor.
- *
- * @param string|array $column
- * @param array $arguments
- */
- public function __construct($column, $arguments = [])
- {
- $this->column = $column;
- $this->label = $this->formatLabel($arguments);
- $this->id = $this->formatId($column);
- $this->callResolving();
- }
- /**
- * Get the field element id.
- *
- * @return string|array
- */
- public function getElementId()
- {
- return $this->id;
- }
- /**
- * Format the field element id.
- *
- * @param string|array $column
- *
- * @return string|array
- */
- protected function formatId($column)
- {
- $random = Str::random(5);
- if (is_array($column)) {
- $id = [];
- foreach (str_replace('.', '-', $column) as $k => $v) {
- $id[$k] = "{$v}-{$random}";
- }
- return $id;
- }
- return 'form-field-'.str_replace('.', '-', $column).'-'.$random;
- }
- /**
- * @param string|null $relationName
- * @param string $relationPrimaryKey
- *
- * @return $this
- */
- public function setNestedFormRelation(?string $relationName, $relationPrimaryKey)
- {
- if (is_array($this->id)) {
- $this->id = array_map(function ($v) {
- return $v.NestedForm::DEFAULT_KEY_NAME;
- }, $this->id);
- } else {
- $this->id .= NestedForm::DEFAULT_KEY_NAME;
- }
- return $this;
- }
- /**
- * Format the label value.
- *
- * @param array $arguments
- *
- * @return string
- */
- protected function formatLabel($arguments = [])
- {
- $column = is_array($this->column) ? current($this->column) : $this->column;
- $label = isset($arguments[0]) ? $arguments[0] : admin_trans_field($column);
- return str_replace(['.', '_'], ' ', $label);
- }
- /**
- * Format the name of the field.
- *
- * @param string $column
- *
- * @return array|mixed|string
- */
- protected function formatName($column)
- {
- if (is_string($column)) {
- $name = explode('.', $column);
- if (count($name) == 1) {
- return $name[0];
- }
- $html = array_shift($name);
- foreach ($name as $piece) {
- $html .= "[$piece]";
- }
- return $html;
- }
- if (is_array($this->column)) {
- $names = [];
- foreach ($this->column as $key => $name) {
- $names[$key] = $this->formatName($name);
- }
- return $names;
- }
- return '';
- }
- /**
- * Set form element name.
- *
- * @param string|array $name
- *
- * @return $this
- *
- * @author Edwin Hui
- */
- public function setElementName($name)
- {
- $this->elementName = $name;
- return $this;
- }
- /**
- * Get form element name.
- *
- * @return array|mixed|string
- */
- public function getElementName()
- {
- return $this->elementName ?: $this->formatName($this->column);
- }
- /**
- * Fill data to the field.
- *
- * @param array $data
- *
- * @return void
- */
- final public function fill($data)
- {
- $data = Helper::array($data);
- $this->data($data);
- $this->value = $this->formatFieldData($data);
- $this->callCustomFormatter();
- }
- /**
- * Format field data.
- *
- * @param array $data
- *
- * @return mixed
- */
- protected function formatFieldData($data)
- {
- if (is_array($this->column)) {
- $value = [];
- foreach ($this->column as $key => $column) {
- $value[$key] = Arr::get($data, $column);
- }
- return $value;
- }
- return Arr::get($data, $this->column, $this->value);
- }
- /**
- * custom format form column data when edit.
- *
- * @param \Closure $call
- *
- * @return $this
- */
- public function customFormat(\Closure $call)
- {
- $this->customFormat = $call;
- return $this;
- }
- /**
- * Set original value to the field.
- *
- * @param array $data
- *
- * @return void
- */
- final public function setOriginal($data)
- {
- $data = Helper::array($data);
- $this->original = $this->formatFieldData($data);
- $this->callCustomFormatter('original', new Fluent($data));
- }
- /**
- * @param string $key
- * @param Fluent|null $dataremoveField
- */
- protected function callCustomFormatter($key = 'value', Fluent $data = null)
- {
- if ($this->customFormat) {
- $this->{$key} = $this->customFormat
- ->call(
- $data ?: $this->data(),
- $this->{$key},
- $this->column,
- $this
- );
- }
- }
- /**
- * @param Form|WidgetForm $form
- *
- * @return $this
- */
- public function setForm($form = null)
- {
- $this->form = $form;
- return $this;
- }
- /**
- * @return Fluent
- */
- public function values()
- {
- return $this->form ? $this->form->model() : new Fluent();
- }
- /**
- * Set width for field and label.
- *
- * @param int $field
- * @param int $label
- *
- * @return $this
- */
- public function width($field = 8, $label = 2)
- {
- $this->width = [
- 'label' => $label,
- 'field' => $field,
- ];
- return $this;
- }
- /**
- * Set the field options.
- *
- * @param array $options
- *
- * @return $this
- */
- public function options($options = [])
- {
- if ($options instanceof Arrayable) {
- $options = $options->toArray();
- }
- if (is_array($this->options)) {
- $this->options = array_merge($this->options, $options);
- } else {
- $this->options = $options;
- }
- return $this;
- }
- /**
- * Set the field option checked.
- *
- * @param array $checked
- *
- * @return $this
- */
- public function checked($checked = [])
- {
- if ($checked instanceof Arrayable) {
- $checked = $checked->toArray();
- }
- $this->checked = array_merge($this->checked, (array) $checked);
- return $this;
- }
- /**
- * Set key for error message.
- *
- * @param string|array $key
- *
- * @return $this
- */
- public function setErrorKey($key)
- {
- $this->errorKey = $key;
- return $this;
- }
- /**
- * Get key for error message.
- *
- * @return string
- */
- public function getErrorKey()
- {
- return $this->errorKey ?: $this->column;
- }
- /**
- * Set or get value of the field.
- *
- * @param null $value
- *
- * @return mixed
- */
- public function value($value = null)
- {
- if (is_null($value)) {
- if (
- $this->value === null
- || (is_array($this->value) && empty($this->value))
- ) {
- return $this->default();
- }
- return $this->value;
- }
- $this->value = $value;
- return $this;
- }
- /**
- * Set or get data.
- *
- * @param array $data
- *
- * @return $this|Fluent
- */
- public function data(array $data = null)
- {
- if (is_null($data)) {
- if (! $this->data || is_array($this->data)) {
- $this->data = new Fluent((array) $this->data);
- }
- return $this->data;
- }
- $this->data = new Fluent($data);
- return $this;
- }
- /**
- * Get or set default value for field.
- *
- * @param $default
- *
- * @return $this|mixed
- */
- public function default($default = null)
- {
- if ($default === null) {
- if (
- $this->form
- && method_exists($this->form, 'isCreating')
- && ! $this->form->isCreating()
- ) {
- return;
- }
- if ($this->default instanceof \Closure) {
- return call_user_func($this->default, $this->form);
- }
- return $this->default;
- }
- $this->default = $default;
- return $this;
- }
- /**
- * Set help block for current field.
- *
- * @param string $text
- * @param string $icon
- *
- * @return $this
- */
- public function help($text = '', $icon = 'feather icon-help-circle')
- {
- $this->help = compact('text', 'icon');
- return $this;
- }
- /**
- * Get column of the field.
- *
- * @return string|array
- */
- public function column()
- {
- return $this->column;
- }
- /**
- * Get or set label of the field.
- *
- * @param null $label
- *
- * @return $this|string
- */
- public function label($label = null)
- {
- if ($label == null) {
- return $this->label;
- }
- if ($label instanceof \Closure) {
- $label = $label($this->label);
- }
- $this->label = $label;
- return $this;
- }
- /**
- * @return mixed
- */
- public function old()
- {
- return old($this->column, $this->value());
- }
- /**
- * Get original value of the field.
- *
- * @return mixed
- */
- public function original()
- {
- return $this->original;
- }
- /**
- * Sanitize input data.
- *
- * @param array $input
- * @param string $column
- *
- * @return array
- */
- protected function sanitizeInput($input, $column)
- {
- if ($this instanceof Field\MultipleSelect) {
- $value = Arr::get($input, $column);
- Arr::set($input, $column, array_filter($value));
- }
- return $input;
- }
- /**
- * Add html attributes to elements.
- *
- * @param array|string $attribute
- * @param mixed $value
- *
- * @return $this
- */
- public function attribute($attribute, $value = null)
- {
- if (is_array($attribute)) {
- $this->attributes = array_merge($this->attributes, $attribute);
- } else {
- $this->attributes[$attribute] = (string) $value;
- }
- return $this;
- }
- /**
- * @param string $key
- *
- * @return bool
- */
- public function hasAttribute(string $key)
- {
- return array_key_exists($key, $this->attributes);
- }
- /**
- * Specifies a regular expression against which to validate the value of the input.
- *
- * @param string $error
- * @param string $regexp
- *
- * @return $this
- */
- public function pattern($regexp, $error = null)
- {
- if ($error) {
- $this->attribute('data-pattern-error', $error);
- }
- return $this->attribute('pattern', $regexp);
- }
- /**
- * set the input filed required.
- *
- * @param bool $isLabelAsterisked
- *
- * @return $this
- */
- public function required($isLabelAsterisked = true)
- {
- if ($isLabelAsterisked) {
- $this->setLabelClass(['asterisk']);
- }
- $this->rules('required');
- return $this->attribute('required', true);
- }
- /**
- * Set the field automatically get focus.
- *
- * @return $this
- */
- public function autofocus()
- {
- return $this->attribute('autofocus', true);
- }
- /**
- * Set the field as readonly mode.
- *
- * @return $this
- */
- public function readOnly()
- {
- return $this->attribute('readonly', true);
- }
- /**
- * Set field as disabled.
- *
- * @return $this
- */
- public function disable()
- {
- return $this->attribute('disabled', true);
- }
- /**
- * Get or set field placeholder.
- *
- * @param string $placeholder
- *
- * @return $this|string
- */
- public function placeholder($placeholder = null)
- {
- if ($placeholder === null) {
- return $this->placeholder ?: trans('admin.input').' '.$this->label;
- }
- $this->placeholder = $placeholder;
- return $this;
- }
- /**
- * @param mixed $value
- *
- * @return mixed
- */
- protected function prepareInputValue($value)
- {
- return $value;
- }
- /**
- * @param \Closure $closure
- *
- * @return $this
- */
- public function saving(\Closure $closure)
- {
- $this->savingCallback = $closure;
- return $this;
- }
- /**
- * Prepare for a field value before update or insert.
- *
- * @param mixed $value
- *
- * @return mixed
- */
- final public function prepare($value)
- {
- $value = $this->prepareInputValue($value);
- if ($handler = $this->savingCallback) {
- $handler->bindTo($this->data());
- return $handler($value);
- }
- return $value;
- }
- /**
- * Format the field attributes.
- *
- * @return string
- */
- protected function formatAttributes()
- {
- $html = [];
- foreach ($this->attributes as $name => $value) {
- $html[] = $name.'="'.e($value).'"';
- }
- return implode(' ', $html);
- }
- /**
- * @return $this
- */
- public function disableHorizontal()
- {
- $this->horizontal = false;
- return $this;
- }
- /**
- * @return array
- */
- public function getViewElementClasses()
- {
- if ($this->horizontal) {
- return [
- 'label' => "col-md-{$this->width['label']} {$this->getLabelClass()} text-capitalize",
- 'field' => "col-md-{$this->width['field']}",
- 'form-group' => 'form-group row form-field',
- ];
- }
- return ['label' => $this->getLabelClass().' text-capitalize', 'field' => '', 'form-group' => 'form-field'];
- }
- /**
- * Set element class.
- *
- * @param string|array $class
- *
- * @return $this
- */
- public function setElementClass($class)
- {
- $this->elementClass = array_merge($this->elementClass, (array) $class);
- return $this;
- }
- /**
- * Get element class.
- *
- * @return array
- */
- public function getElementClass()
- {
- if (! $this->elementClass) {
- $name = $this->getElementName();
- $this->elementClass = array_map(function ($v) {
- return static::FIELD_CLASS_PREFIX.$v;
- }, (array) str_replace(['[', ']'], '_', $name));
- }
- return $this->elementClass;
- }
- /**
- * Get element class string.
- *
- * @return mixed
- */
- public function getElementClassString()
- {
- $elementClass = $this->getElementClass();
- if (Arr::isAssoc($elementClass)) {
- $classes = [];
- foreach ($elementClass as $index => $class) {
- $classes[$index] = is_array($class) ? implode(' ', $class) : $class;
- }
- return $classes;
- }
- return implode(' ', $elementClass);
- }
- /**
- * Get element class selector.
- *
- * @return string|array
- */
- public function getElementClassSelector()
- {
- $elementClass = $this->getElementClass();
- $formId = $this->getFormElementId();
- $formId = $formId ? '#'.$formId : '';
- if (Arr::isAssoc($elementClass)) {
- $classes = [];
- foreach ($elementClass as $index => $class) {
- $classes[$index] = $formId.' .'.(is_array($class) ? implode('.', $class) : $class);
- }
- return $classes;
- }
- return $formId.' .'.implode('.', $elementClass);
- }
- /**
- * @return $this
- */
- public function hideInDialog()
- {
- if (
- $this->form instanceof Form
- && $this->form->inDialog()
- ) {
- $this->display(false);
- }
- return $this;
- }
- /**
- * @return string|null
- */
- protected function getFormElementId()
- {
- return $this->form ? $this->form->getElementId() : null;
- }
- /**
- * Add the element class.
- *
- * @param $class
- *
- * @return $this
- */
- public function addElementClass($class)
- {
- $this->elementClass = array_unique(
- array_merge($this->elementClass, (array) $class)
- );
- return $this;
- }
- /**
- * Remove element class.
- *
- * @param $class
- *
- * @return $this
- */
- public function removeElementClass($class)
- {
- $delClass = [];
- if (is_string($class) || is_array($class)) {
- $delClass = (array) $class;
- }
- foreach ($delClass as $del) {
- if (($key = array_search($del, $this->elementClass))) {
- unset($this->elementClass[$key]);
- }
- }
- return $this;
- }
- /**
- * Add variables to field view.
- *
- * @param array $variables
- *
- * @return $this
- */
- protected function addVariables(array $variables = [])
- {
- $this->variables = array_merge($this->variables, $variables);
- return $this;
- }
- /**
- * @param array|string $labelClass
- * @param bool $append
- *
- * @return $this|string
- */
- public function setLabelClass($labelClass, bool $append = true)
- {
- $this->labelClass = $append
- ? array_unique(array_merge($this->labelClass, (array) $labelClass))
- : (array) $labelClass;
- return $this;
- }
- /**
- * @return string
- */
- public function getLabelClass()
- {
- return implode(' ', $this->labelClass);
- }
- /**
- * Get the view variables of this field.
- *
- * @return array
- */
- public function variables()
- {
- return array_merge($this->variables, [
- 'id' => $this->id,
- 'name' => $this->getElementName(),
- 'help' => $this->help,
- 'class' => $this->getElementClassString(),
- 'value' => $this->value(),
- 'label' => $this->label,
- 'viewClass' => $this->getViewElementClasses(),
- 'column' => $this->column,
- 'errorKey' => $this->getErrorKey(),
- 'attributes' => $this->formatAttributes(),
- 'placeholder' => $this->placeholder(),
- 'disabled' => $this->attributes['disabled'] ?? false,
- 'formId' => $this->getFormElementId(),
- ]);
- }
- /**
- * Get view of this field.
- *
- * @return string
- */
- public function view()
- {
- return $this->view ?: 'admin::form.'.strtolower(class_basename(static::class));
- }
- /**
- * Set view of current field.
- *
- * @return string
- */
- public function setView($view)
- {
- $this->view = $view;
- return $this;
- }
- /**
- * Get script of current field.
- *
- * @return string
- */
- public function getScript()
- {
- return $this->script;
- }
- /**
- * Set script of current field.
- *
- * @return self
- */
- public function script($script)
- {
- $this->script = $script;
- return $this;
- }
- /**
- * To set this field should render or not.
- *
- * @return self
- */
- public function display(bool $display)
- {
- $this->display = $display;
- return $this;
- }
- /**
- * If this field should render.
- *
- * @return bool
- */
- protected function shouldRender()
- {
- return $this->display;
- }
- /**
- * Collect assets required by this field.
- */
- public static function collectAssets()
- {
- static::$js && Admin::js(static::$js);
- static::$css && Admin::css(static::$css);
- }
- /**
- * Render this filed.
- *
- * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
- */
- public function render()
- {
- if (! $this->shouldRender()) {
- return '';
- }
- $this->callComposing();
- Admin::script($this->script);
- return view($this->view(), $this->variables());
- }
- /**
- * @return string
- */
- public function __toString()
- {
- $view = $this->render();
- return $view instanceof Renderable ? $view->render() : (string) $view;
- }
- }
|