| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268 |
- <?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 $savingCallbacks = [];
- /**
- * 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);
- }
- /**
- * @param string $key
- *
- * @return mixed|null
- */
- public function getAttribute(string $key)
- {
- return $this->attributes[$key] ?? null;
- }
- /**
- * 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->savingCallbacks[] = $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 ($this->savingCallbacks) {
- foreach ($this->savingCallbacks as $callback) {
- $value = $callback->call($this->data());
- }
- }
- 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(),
- ]);
- }
- protected function isCreating()
- {
- return request()->isMethod('POST');
- }
- protected function isEditing()
- {
- return request()->isMethod('PUT');
- }
- /**
- * 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;
- }
- public function saveAsJson($option = 0)
- {
- return $this->saving(function ($value) use ($option) {
- if (! $value || is_scalar($value)) {
- return $value;
- }
- return json_encode($value, $option);
- });
- }
- public function saveAsJoin(string $glue = ',')
- {
- return $this->saving(function ($value) use ($glue) {
- if (! $value || is_scalar($value)) {
- return $value;
- }
- return implode($glue, (array) $value);
- });
- }
- public function saveAsString()
- {
- return $this->saving(function ($value) {
- return (string) $value;
- });
- }
- public function saveAsInteger()
- {
- return $this->saving(function ($value) {
- return (int) $value;
- });
- }
- /**
- * 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;
- }
- }
|