Field.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Support\Helper;
  6. use Dcat\Admin\Traits\HasBuilderEvents;
  7. use Dcat\Admin\Traits\HasVariables;
  8. use Dcat\Admin\Widgets\Form as WidgetForm;
  9. use Illuminate\Contracts\Support\Arrayable;
  10. use Illuminate\Contracts\Support\Renderable;
  11. use Illuminate\Support\Arr;
  12. use Illuminate\Support\Fluent;
  13. use Illuminate\Support\Str;
  14. use Illuminate\Support\Traits\Macroable;
  15. /**
  16. * Class Field.
  17. */
  18. class Field implements Renderable
  19. {
  20. use Macroable;
  21. use Form\Concerns\HasFieldValidator;
  22. use HasBuilderEvents;
  23. use HasVariables;
  24. const FILE_DELETE_FLAG = '_file_del_';
  25. const FIELD_CLASS_PREFIX = 'field_';
  26. /**
  27. * Element value.
  28. *
  29. * @var mixed
  30. */
  31. protected $value;
  32. /**
  33. * Data of all original columns of value.
  34. *
  35. * @var mixed
  36. */
  37. protected $data;
  38. /**
  39. * Field original value.
  40. *
  41. * @var mixed
  42. */
  43. protected $original;
  44. /**
  45. * Field default value.
  46. *
  47. * @var mixed
  48. */
  49. protected $default;
  50. /**
  51. * @var bool
  52. */
  53. protected $allowDefaultValueInEditPage = false;
  54. /**
  55. * Element label.
  56. *
  57. * @var string
  58. */
  59. protected $label = '';
  60. /**
  61. * Column name.
  62. *
  63. * @var string|array
  64. */
  65. protected $column = '';
  66. /**
  67. * Form element name.
  68. *
  69. * @var string|array
  70. */
  71. protected $elementName = [];
  72. /**
  73. * Form element classes.
  74. *
  75. * @var array
  76. */
  77. protected $elementClass = [];
  78. /**
  79. * Options for specify elements.
  80. *
  81. * @var array
  82. */
  83. protected $options = [];
  84. /**
  85. * Checked for specify elements.
  86. *
  87. * @var array
  88. */
  89. protected $checked = [];
  90. /**
  91. * Css required by this field.
  92. *
  93. * @var array
  94. */
  95. protected static $css = [];
  96. /**
  97. * Js required by this field.
  98. *
  99. * @var array
  100. */
  101. protected static $js = [];
  102. /**
  103. * Script for field.
  104. *
  105. * @var string
  106. */
  107. protected $script = '';
  108. /**
  109. * Element attributes.
  110. *
  111. * @var array
  112. */
  113. protected $attributes = [];
  114. /**
  115. * Parent form.
  116. *
  117. * @var Form|WidgetForm
  118. */
  119. protected $form = null;
  120. /**
  121. * View for field to render.
  122. *
  123. * @var string
  124. */
  125. protected $view = '';
  126. /**
  127. * Help block.
  128. *
  129. * @var array
  130. */
  131. protected $help = [];
  132. /**
  133. * Key for errors.
  134. *
  135. * @var string|array
  136. */
  137. protected $errorKey;
  138. /**
  139. * Placeholder for this field.
  140. *
  141. * @var string|array
  142. */
  143. protected $placeholder;
  144. /**
  145. * Width for label and field.
  146. *
  147. * @var array
  148. */
  149. protected $width = [
  150. 'label' => 2,
  151. 'field' => 8,
  152. ];
  153. /**
  154. * If the form horizontal layout.
  155. *
  156. * @var bool
  157. */
  158. protected $horizontal = true;
  159. /**
  160. * column data format.
  161. *
  162. * @var \Closure
  163. */
  164. protected $customFormat = null;
  165. /**
  166. * @var bool
  167. */
  168. protected $display = true;
  169. /**
  170. * @var array
  171. */
  172. protected $labelClass = ['text-capitalize'];
  173. /**
  174. * @var array
  175. */
  176. protected $fieldClass = [];
  177. /**
  178. * @var array
  179. */
  180. protected $formGroupClass = ['form-field'];
  181. /**
  182. * @var \Closure[]
  183. */
  184. protected $savingCallbacks = [];
  185. /**
  186. * Field constructor.
  187. *
  188. * @param string|array $column
  189. * @param array $arguments
  190. */
  191. public function __construct($column, $arguments = [])
  192. {
  193. $this->column = $column;
  194. $this->label = $this->formatLabel($arguments);
  195. $this->callResolving();
  196. }
  197. /**
  198. * @param array $options
  199. *
  200. * @return $this
  201. */
  202. public function setNestedFormRelation(array $options = [])
  203. {
  204. return $this;
  205. }
  206. /**
  207. * Format the label value.
  208. *
  209. * @param array $arguments
  210. *
  211. * @return string
  212. */
  213. protected function formatLabel($arguments = [])
  214. {
  215. $column = is_array($this->column) ? current($this->column) : $this->column;
  216. $label = isset($arguments[0]) ? $arguments[0] : admin_trans_field($column);
  217. return str_replace(['.', '_'], ' ', $label);
  218. }
  219. /**
  220. * Format the name of the field.
  221. *
  222. * @param string $column
  223. *
  224. * @return array|mixed|string
  225. */
  226. protected function formatName($column)
  227. {
  228. return Helper::formatElementName($column);
  229. }
  230. /**
  231. * Set form element name.
  232. *
  233. * @param string|array $name
  234. *
  235. * @return $this
  236. *
  237. * @author Edwin Hui
  238. */
  239. public function setElementName($name)
  240. {
  241. $this->elementName = $name;
  242. return $this;
  243. }
  244. /**
  245. * Get form element name.
  246. *
  247. * @return array|mixed|string
  248. */
  249. public function getElementName()
  250. {
  251. return $this->elementName ?: $this->formatName($this->column);
  252. }
  253. /**
  254. * Fill data to the field.
  255. *
  256. * @param array $data
  257. *
  258. * @return void
  259. */
  260. final public function fill($data)
  261. {
  262. $data = Helper::array($data);
  263. $this->data($data);
  264. $this->value = $this->formatFieldData($data);
  265. $this->callCustomFormatter();
  266. }
  267. /**
  268. * Format field data.
  269. *
  270. * @param array $data
  271. *
  272. * @return mixed
  273. */
  274. protected function formatFieldData($data)
  275. {
  276. if (is_array($this->column)) {
  277. $value = [];
  278. foreach ($this->column as $key => $column) {
  279. $value[$key] = Arr::get($data, $this->normalizeColumn($column));
  280. }
  281. return $value;
  282. }
  283. return Arr::get($data, $this->normalizeColumn(), $this->value);
  284. }
  285. protected function normalizeColumn(?string $column = null)
  286. {
  287. return str_replace('->', '.', $column ?: $this->column);
  288. }
  289. /**
  290. * custom format form column data when edit.
  291. *
  292. * @param \Closure $call
  293. *
  294. * @return $this
  295. */
  296. public function customFormat(\Closure $call)
  297. {
  298. $this->customFormat = $call;
  299. return $this;
  300. }
  301. /**
  302. * Set original value to the field.
  303. *
  304. * @param array $data
  305. *
  306. * @return void
  307. */
  308. final public function setOriginal($data)
  309. {
  310. $data = Helper::array($data);
  311. $this->original = $this->formatFieldData($data);
  312. $this->callCustomFormatter('original', new Fluent($data));
  313. }
  314. /**
  315. * @param string $key
  316. * @param Fluent|null $dataremoveField
  317. */
  318. protected function callCustomFormatter($key = 'value', Fluent $data = null)
  319. {
  320. if ($this->customFormat) {
  321. $this->{$key} = $this->customFormat
  322. ->call(
  323. $data ?: $this->data(),
  324. $this->{$key},
  325. $this->column,
  326. $this
  327. );
  328. }
  329. }
  330. /**
  331. * @param Form|WidgetForm $form
  332. *
  333. * @return $this
  334. */
  335. public function setForm($form = null)
  336. {
  337. $this->form = $form;
  338. return $this;
  339. }
  340. /**
  341. * @return Fluent
  342. */
  343. public function values()
  344. {
  345. return $this->form ? $this->form->model() : new Fluent();
  346. }
  347. /**
  348. * Set width for field and label.
  349. *
  350. * @param int $field
  351. * @param int $label
  352. *
  353. * @return $this
  354. */
  355. public function width($field = 8, $label = 2)
  356. {
  357. $this->width = [
  358. 'label' => $label,
  359. 'field' => $field,
  360. ];
  361. return $this;
  362. }
  363. /**
  364. * Set the field options.
  365. *
  366. * @param array $options
  367. *
  368. * @return $this
  369. */
  370. public function options($options = [])
  371. {
  372. if ($options instanceof \Closure) {
  373. $options = $options->call($this->data(), $this->value());
  374. }
  375. $this->options = array_merge($this->options, Helper::array($options));
  376. return $this;
  377. }
  378. /**
  379. * @param array $options
  380. *
  381. * @return $this
  382. */
  383. public function replaceOptions($options)
  384. {
  385. if ($options instanceof \Closure) {
  386. $options = $options->call($this->data(), $this->value());
  387. }
  388. $this->options = $options;
  389. return $this;
  390. }
  391. /**
  392. * @param array|Arrayable $options
  393. *
  394. * @return $this
  395. */
  396. public function mergeOptions($options)
  397. {
  398. return $this->options($options);
  399. }
  400. /**
  401. * Set the field option checked.
  402. *
  403. * @param array $checked
  404. *
  405. * @return $this
  406. */
  407. public function checked($checked = [])
  408. {
  409. if ($checked instanceof Arrayable) {
  410. $checked = $checked->toArray();
  411. }
  412. $this->checked = array_merge($this->checked, (array) $checked);
  413. return $this;
  414. }
  415. /**
  416. * Set key for error message.
  417. *
  418. * @param string|array $key
  419. *
  420. * @return $this
  421. */
  422. public function setErrorKey($key)
  423. {
  424. $this->errorKey = $key;
  425. return $this;
  426. }
  427. /**
  428. * Get key for error message.
  429. *
  430. * @return string
  431. */
  432. public function getErrorKey()
  433. {
  434. return $this->errorKey ?: $this->column;
  435. }
  436. /**
  437. * Set or get value of the field.
  438. *
  439. * @param null $value
  440. *
  441. * @return mixed
  442. */
  443. public function value($value = null)
  444. {
  445. if (is_null($value)) {
  446. if (
  447. $this->value === null
  448. || (is_array($this->value) && empty($this->value))
  449. ) {
  450. return $this->default();
  451. }
  452. return $this->value;
  453. }
  454. $this->value = value($value);
  455. return $this;
  456. }
  457. /**
  458. * Set or get data.
  459. *
  460. * @param array $data
  461. *
  462. * @return $this|Fluent
  463. */
  464. public function data(array $data = null)
  465. {
  466. if (is_null($data)) {
  467. if (! $this->data || is_array($this->data)) {
  468. $this->data = new Fluent((array) $this->data);
  469. }
  470. return $this->data;
  471. }
  472. $this->data = new Fluent($data);
  473. return $this;
  474. }
  475. /**
  476. * Get or set default value for field.
  477. *
  478. * @param mixed $default
  479. * @param bool $edit
  480. *
  481. * @return $this|mixed
  482. */
  483. public function default($default = null, bool $edit = false)
  484. {
  485. if ($default === null) {
  486. if (
  487. $this->form
  488. && method_exists($this->form, 'isCreating')
  489. && ! $this->form->isCreating()
  490. && ! $this->allowDefaultValueInEditPage
  491. ) {
  492. return;
  493. }
  494. if ($this->default instanceof \Closure) {
  495. $this->default->bindTo($this->data());
  496. return call_user_func($this->default, $this->form);
  497. }
  498. return $this->default;
  499. }
  500. $this->default = value($default);
  501. $this->allowDefaultValueInEditPage = $edit;
  502. return $this;
  503. }
  504. /**
  505. * Set help block for current field.
  506. *
  507. * @param string $text
  508. * @param string $icon
  509. *
  510. * @return $this
  511. */
  512. public function help($text = '', $icon = 'feather icon-help-circle')
  513. {
  514. $this->help = compact('text', 'icon');
  515. return $this;
  516. }
  517. /**
  518. * Get column of the field.
  519. *
  520. * @return string|array
  521. */
  522. public function column()
  523. {
  524. return $this->column;
  525. }
  526. /**
  527. * Get or set label of the field.
  528. *
  529. * @param null $label
  530. *
  531. * @return $this|string
  532. */
  533. public function label($label = null)
  534. {
  535. if ($label == null) {
  536. return $this->label;
  537. }
  538. if ($label instanceof \Closure) {
  539. $label = $label($this->label);
  540. }
  541. $this->label = $label;
  542. return $this;
  543. }
  544. /**
  545. * Get original value of the field.
  546. *
  547. * @return mixed
  548. */
  549. public function original()
  550. {
  551. return $this->original;
  552. }
  553. /**
  554. * Sanitize input data.
  555. *
  556. * @param array $input
  557. * @param string $column
  558. *
  559. * @return array
  560. */
  561. protected function sanitizeInput($input, $column)
  562. {
  563. if ($this instanceof Field\MultipleSelect) {
  564. $value = Arr::get($input, $column);
  565. Arr::set($input, $column, array_filter($value));
  566. }
  567. return $input;
  568. }
  569. /**
  570. * Add html attributes to elements.
  571. *
  572. * @param array|string $attribute
  573. * @param mixed $value
  574. *
  575. * @return $this
  576. */
  577. public function attribute($attribute, $value = null)
  578. {
  579. if (is_array($attribute)) {
  580. $this->attributes = array_merge($this->attributes, $attribute);
  581. } else {
  582. $this->attributes[$attribute] = (string) $value;
  583. }
  584. return $this;
  585. }
  586. /**
  587. * @param string $key
  588. *
  589. * @return bool
  590. */
  591. public function hasAttribute(string $key)
  592. {
  593. return array_key_exists($key, $this->attributes);
  594. }
  595. /**
  596. * @param string $key
  597. *
  598. * @return mixed|null
  599. */
  600. public function getAttribute(string $key)
  601. {
  602. return $this->attributes[$key] ?? null;
  603. }
  604. /**
  605. * Specifies a regular expression against which to validate the value of the input.
  606. *
  607. * @param string $error
  608. * @param string $regexp
  609. *
  610. * @return $this
  611. */
  612. public function pattern($regexp, $error = null)
  613. {
  614. if ($error) {
  615. $this->attribute('data-pattern-error', $error);
  616. }
  617. return $this->attribute('pattern', $regexp);
  618. }
  619. /**
  620. * set the input filed required.
  621. *
  622. * @param bool $isLabelAsterisked
  623. *
  624. * @return $this
  625. */
  626. public function required($isLabelAsterisked = true)
  627. {
  628. if ($isLabelAsterisked) {
  629. $this->setLabelClass(['asterisk']);
  630. }
  631. $this->rules('required');
  632. return $this->attribute('required', true);
  633. }
  634. /**
  635. * Set the field automatically get focus.
  636. *
  637. * @return $this
  638. */
  639. public function autofocus()
  640. {
  641. return $this->attribute('autofocus', true);
  642. }
  643. /**
  644. * Set the field as readonly mode.
  645. *
  646. * @return $this
  647. */
  648. public function readOnly()
  649. {
  650. return $this->attribute('readonly', true);
  651. }
  652. /**
  653. * Set field as disabled.
  654. *
  655. * @return $this
  656. */
  657. public function disable()
  658. {
  659. return $this->attribute('disabled', true);
  660. }
  661. /**
  662. * Get or set field placeholder.
  663. *
  664. * @param string $placeholder
  665. *
  666. * @return $this|string
  667. */
  668. public function placeholder($placeholder = null)
  669. {
  670. if ($placeholder === null) {
  671. return $this->placeholder ?: trans('admin.input').' '.$this->label;
  672. }
  673. $this->placeholder = $placeholder;
  674. return $this;
  675. }
  676. /**
  677. * @param mixed $value
  678. *
  679. * @return mixed
  680. */
  681. protected function prepareInputValue($value)
  682. {
  683. return $value;
  684. }
  685. /**
  686. * @param \Closure $closure
  687. *
  688. * @return $this
  689. */
  690. public function saving(\Closure $closure)
  691. {
  692. $this->savingCallbacks[] = $closure;
  693. return $this;
  694. }
  695. /**
  696. * Prepare for a field value before update or insert.
  697. *
  698. * @param mixed $value
  699. *
  700. * @return mixed
  701. */
  702. final public function prepare($value)
  703. {
  704. $value = $this->prepareInputValue($value);
  705. if ($this->savingCallbacks) {
  706. foreach ($this->savingCallbacks as $callback) {
  707. $value = $callback->call($this->data(), $value);
  708. }
  709. }
  710. return $value;
  711. }
  712. /**
  713. * Format the field attributes.
  714. *
  715. * @return string
  716. */
  717. protected function formatAttributes()
  718. {
  719. $html = [];
  720. foreach ($this->attributes as $name => $value) {
  721. $html[] = $name.'="'.e($value).'"';
  722. }
  723. return implode(' ', $html);
  724. }
  725. /**
  726. * @return $this
  727. */
  728. public function disableHorizontal()
  729. {
  730. $this->horizontal = false;
  731. return $this;
  732. }
  733. /**
  734. * @return array
  735. */
  736. public function getViewElementClasses()
  737. {
  738. if ($this->horizontal) {
  739. return [
  740. 'label' => "col-md-{$this->width['label']} {$this->getLabelClass()}",
  741. 'field' => "col-md-{$this->width['field']} {$this->getFieldClass()}",
  742. 'form-group' => "form-group row {$this->getFormGroupClass()}",
  743. ];
  744. }
  745. return [
  746. 'label' => $this->getLabelClass(),
  747. 'field' => $this->getFieldClass(),
  748. 'form-group' => $this->getFormGroupClass(),
  749. ];
  750. }
  751. /**
  752. * Set element class.
  753. *
  754. * @param string|array $class
  755. *
  756. * @return $this
  757. */
  758. public function setElementClass($class)
  759. {
  760. $this->elementClass = array_merge($this->elementClass, (array) $this->normalizeElementClass($class));
  761. return $this;
  762. }
  763. /**
  764. * Get element class.
  765. *
  766. * @return array
  767. */
  768. public function getElementClass()
  769. {
  770. if (! $this->elementClass) {
  771. $this->elementClass = $this->normalizeElementClass((array) $this->getElementName());
  772. }
  773. return $this->elementClass;
  774. }
  775. /**
  776. * @param string|array $class
  777. *
  778. * @return array|string
  779. */
  780. public function normalizeElementClass($class)
  781. {
  782. if (is_array($class)) {
  783. return array_map([$this, 'normalizeElementClass'], $class);
  784. }
  785. return static::FIELD_CLASS_PREFIX.str_replace(['[', ']', '->', '.'], '_', $class);
  786. }
  787. /**
  788. * Get element class selector.
  789. *
  790. * @return string|array
  791. */
  792. public function getElementClassSelector()
  793. {
  794. $elementClass = $this->getElementClass();
  795. $formId = $this->getFormElementId();
  796. $formId = $formId ? '#'.$formId : '';
  797. if (Arr::isAssoc($elementClass)) {
  798. $classes = [];
  799. foreach ($elementClass as $index => $class) {
  800. $classes[$index] = $formId.' .'.(is_array($class) ? implode('.', $class) : $class);
  801. }
  802. return $classes;
  803. }
  804. return $formId.' .'.implode('.', $elementClass);
  805. }
  806. /**
  807. * Get element class string.
  808. *
  809. * @return mixed
  810. */
  811. public function getElementClassString()
  812. {
  813. $elementClass = $this->getElementClass();
  814. if (Arr::isAssoc($elementClass)) {
  815. $classes = [];
  816. foreach ($elementClass as $index => $class) {
  817. $classes[$index] = is_array($class) ? implode(' ', $class) : $class;
  818. }
  819. return $classes;
  820. }
  821. return implode(' ', $elementClass);
  822. }
  823. /**
  824. * @return $this
  825. */
  826. public function hideInDialog()
  827. {
  828. if (
  829. $this->form instanceof Form
  830. && $this->form->inDialog()
  831. ) {
  832. $this->display(false);
  833. }
  834. return $this;
  835. }
  836. /**
  837. * @return string|null
  838. */
  839. protected function getFormElementId()
  840. {
  841. return $this->form ? $this->form->getElementId() : null;
  842. }
  843. /**
  844. * Add the element class.
  845. *
  846. * @param $class
  847. *
  848. * @return $this
  849. */
  850. public function addElementClass($class)
  851. {
  852. $this->elementClass = array_unique(
  853. array_merge($this->elementClass, (array) $class)
  854. );
  855. return $this;
  856. }
  857. /**
  858. * Remove element class.
  859. *
  860. * @param $class
  861. *
  862. * @return $this
  863. */
  864. public function removeElementClass($class)
  865. {
  866. $delClass = [];
  867. if (is_string($class) || is_array($class)) {
  868. $delClass = (array) $class;
  869. }
  870. foreach ($delClass as $del) {
  871. if (($key = array_search($del, $this->elementClass))) {
  872. unset($this->elementClass[$key]);
  873. }
  874. }
  875. return $this;
  876. }
  877. /**
  878. * @param array|string $labelClass
  879. * @param bool $append
  880. *
  881. * @return $this|string
  882. */
  883. public function setLabelClass($labelClass, bool $append = true)
  884. {
  885. $this->labelClass = $append
  886. ? array_unique(array_merge($this->labelClass, (array) $labelClass))
  887. : (array) $labelClass;
  888. return $this;
  889. }
  890. /**
  891. * @return string
  892. */
  893. public function getLabelClass()
  894. {
  895. return implode(' ', $this->labelClass);
  896. }
  897. /**
  898. * @param mixed $value
  899. * @param callable $callback
  900. *
  901. * @return $this|mixed
  902. */
  903. public function when($value, $callback)
  904. {
  905. if ($value) {
  906. return $callback($this, $value) ?: $this;
  907. }
  908. return $this;
  909. }
  910. public function setFormGroupClass($labelClass, bool $append = true)
  911. {
  912. $this->formGroupClass = $append
  913. ? array_unique(array_merge($this->formGroupClass, (array) $labelClass))
  914. : (array) $labelClass;
  915. return $this;
  916. }
  917. public function getFormGroupClass()
  918. {
  919. return implode(' ', $this->formGroupClass);
  920. }
  921. public function setFieldClass($labelClass, bool $append = true)
  922. {
  923. $this->fieldClass = $append
  924. ? array_unique(array_merge($this->fieldClass, (array) $labelClass))
  925. : (array) $labelClass;
  926. return $this;
  927. }
  928. public function getFieldClass()
  929. {
  930. return implode(' ', $this->fieldClass);
  931. }
  932. /**
  933. * Get the view variables of this field.
  934. *
  935. * @return array
  936. */
  937. public function defaultVariables()
  938. {
  939. return [
  940. 'name' => $this->getElementName(),
  941. 'help' => $this->help,
  942. 'class' => $this->getElementClassString(),
  943. 'value' => $this->value(),
  944. 'label' => $this->label,
  945. 'viewClass' => $this->getViewElementClasses(),
  946. 'column' => $this->column,
  947. 'errorKey' => $this->getErrorKey(),
  948. 'attributes' => $this->formatAttributes(),
  949. 'placeholder' => $this->placeholder(),
  950. 'disabled' => $this->attributes['disabled'] ?? false,
  951. 'formId' => $this->getFormElementId(),
  952. 'selector' => $this->getElementClassSelector(),
  953. 'options' => $this->options,
  954. ];
  955. }
  956. protected function isCreating()
  957. {
  958. return request()->isMethod('POST');
  959. }
  960. protected function isEditing()
  961. {
  962. return request()->isMethod('PUT');
  963. }
  964. /**
  965. * Get view of this field.
  966. *
  967. * @return string
  968. */
  969. public function view()
  970. {
  971. return $this->view ?: 'admin::form.'.strtolower(class_basename(static::class));
  972. }
  973. /**
  974. * Set view of current field.
  975. *
  976. * @return string
  977. */
  978. public function setView($view)
  979. {
  980. $this->view = $view;
  981. return $this;
  982. }
  983. /**
  984. * Get script of current field.
  985. *
  986. * @return string
  987. */
  988. public function getScript()
  989. {
  990. return $this->script;
  991. }
  992. /**
  993. * Set script of current field.
  994. *
  995. * @return self
  996. */
  997. public function script($script)
  998. {
  999. $this->script = $script;
  1000. return $this;
  1001. }
  1002. /**
  1003. * To set this field should render or not.
  1004. *
  1005. * @return self
  1006. */
  1007. public function display(bool $display)
  1008. {
  1009. $this->display = $display;
  1010. return $this;
  1011. }
  1012. /**
  1013. * If this field should render.
  1014. *
  1015. * @return bool
  1016. */
  1017. protected function shouldRender()
  1018. {
  1019. return $this->display;
  1020. }
  1021. public function saveAsJson($option = 0)
  1022. {
  1023. return $this->saving(function ($value) use ($option) {
  1024. if (! $value || is_scalar($value)) {
  1025. return $value;
  1026. }
  1027. return json_encode($value, $option);
  1028. });
  1029. }
  1030. public function saveAsString()
  1031. {
  1032. return $this->saving(function ($value) {
  1033. return (string) $value;
  1034. });
  1035. }
  1036. /**
  1037. * Collect assets required by this field.
  1038. */
  1039. public static function requireAssets()
  1040. {
  1041. static::$js && Admin::js(static::$js);
  1042. static::$css && Admin::css(static::$css);
  1043. }
  1044. /**
  1045. * Render this filed.
  1046. *
  1047. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
  1048. */
  1049. public function render()
  1050. {
  1051. if (! $this->shouldRender()) {
  1052. return '';
  1053. }
  1054. $this->callComposing();
  1055. $this->withScript();
  1056. return Admin::view($this->view(), $this->variables());
  1057. }
  1058. protected function withScript()
  1059. {
  1060. if ($this->script) {
  1061. Admin::script($this->script);
  1062. }
  1063. }
  1064. /**
  1065. * @return string
  1066. */
  1067. public function __toString()
  1068. {
  1069. $view = $this->render();
  1070. return $view instanceof Renderable ? $view->render() : (string) $view;
  1071. }
  1072. }