Field.php 25 KB

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