Field.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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. /**
  26. * Element value.
  27. *
  28. * @var mixed
  29. */
  30. protected $value;
  31. /**
  32. * Data of all original columns of value.
  33. *
  34. * @var mixed
  35. */
  36. protected $data;
  37. /**
  38. * Field original value.
  39. *
  40. * @var mixed
  41. */
  42. protected $original;
  43. /**
  44. * Field default value.
  45. *
  46. * @var mixed
  47. */
  48. protected $default;
  49. /**
  50. * @var bool
  51. */
  52. protected $allowDefaultValueInEditPage = false;
  53. /**
  54. * Element label.
  55. *
  56. * @var string
  57. */
  58. protected $label = '';
  59. /**
  60. * Column name.
  61. *
  62. * @var string|array
  63. */
  64. protected $column = '';
  65. /**
  66. * Form element name.
  67. *
  68. * @var string|array
  69. */
  70. protected $elementName = [];
  71. /**
  72. * Form element classes.
  73. *
  74. * @var array
  75. */
  76. protected $elementClass = [];
  77. /**
  78. * Options for specify elements.
  79. *
  80. * @var array
  81. */
  82. protected $options = [];
  83. /**
  84. * Checked for specify elements.
  85. *
  86. * @var array
  87. */
  88. protected $checked = [];
  89. /**
  90. * Css required by this field.
  91. *
  92. * @var array
  93. */
  94. protected static $css = [];
  95. /**
  96. * Js required by this field.
  97. *
  98. * @var array
  99. */
  100. protected static $js = [];
  101. /**
  102. * Script for field.
  103. *
  104. * @var string
  105. */
  106. protected $script = '';
  107. /**
  108. * Element attributes.
  109. *
  110. * @var array
  111. */
  112. protected $attributes = [];
  113. /**
  114. * Parent form.
  115. *
  116. * @var Form|WidgetForm
  117. */
  118. protected $form = null;
  119. /**
  120. * View for field to render.
  121. *
  122. * @var string
  123. */
  124. protected $view = '';
  125. /**
  126. * Help block.
  127. *
  128. * @var array
  129. */
  130. protected $help = [];
  131. /**
  132. * Key for errors.
  133. *
  134. * @var string|array
  135. */
  136. protected $errorKey;
  137. /**
  138. * Placeholder for this field.
  139. *
  140. * @var string|array
  141. */
  142. protected $placeholder;
  143. /**
  144. * Width for label and field.
  145. *
  146. * @var array
  147. */
  148. protected $width = [
  149. 'label' => 2,
  150. 'field' => 8,
  151. ];
  152. /**
  153. * If the form horizontal layout.
  154. *
  155. * @var bool
  156. */
  157. protected $horizontal = true;
  158. /**
  159. * column data format.
  160. *
  161. * @var \Closure
  162. */
  163. protected $customFormat = null;
  164. /**
  165. * @var bool
  166. */
  167. protected $display = true;
  168. /**
  169. * @var array
  170. */
  171. protected $labelClass = ['text-capitalize'];
  172. /**
  173. * @var array
  174. */
  175. protected $fieldClass = [];
  176. /**
  177. * @var array
  178. */
  179. protected $formGroupClass = ['form-field'];
  180. /**
  181. * @var \Closure[]
  182. */
  183. protected $savingCallbacks = [];
  184. /**
  185. * Field constructor.
  186. *
  187. * @param string|array $column
  188. * @param array $arguments
  189. */
  190. public function __construct($column, $arguments = [])
  191. {
  192. $this->column = $column;
  193. $this->label = $this->formatLabel($arguments);
  194. $this->callResolving();
  195. }
  196. /**
  197. * @param array $options
  198. *
  199. * @return $this
  200. */
  201. public function setNestedFormRelation(array $options = [])
  202. {
  203. return $this;
  204. }
  205. /**
  206. * Format the label value.
  207. *
  208. * @param array $arguments
  209. *
  210. * @return string
  211. */
  212. protected function formatLabel($arguments = [])
  213. {
  214. if (isset($arguments[0])) {
  215. return $arguments[0];
  216. }
  217. $column = is_array($this->column) ? current($this->column) : $this->column;
  218. $label = admin_trans_field($column);
  219. return str_replace('_', ' ', $label);
  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. /**
  1015. * If this field should render.
  1016. *
  1017. * @return bool
  1018. */
  1019. protected function shouldRender()
  1020. {
  1021. return $this->display;
  1022. }
  1023. public function saveAsJson($option = 0)
  1024. {
  1025. return $this->saving(function ($value) use ($option) {
  1026. if (! $value || is_scalar($value)) {
  1027. return $value;
  1028. }
  1029. return json_encode($value, $option);
  1030. });
  1031. }
  1032. public function saveAsString()
  1033. {
  1034. return $this->saving(function ($value) {
  1035. return (string) $value;
  1036. });
  1037. }
  1038. /**
  1039. * Collect assets required by this field.
  1040. */
  1041. public static function requireAssets()
  1042. {
  1043. static::$js && Admin::js(static::$js);
  1044. static::$css && Admin::css(static::$css);
  1045. }
  1046. /**
  1047. * Render this filed.
  1048. *
  1049. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
  1050. */
  1051. public function render()
  1052. {
  1053. if (! $this->shouldRender()) {
  1054. return '';
  1055. }
  1056. $this->callComposing();
  1057. $this->withScript();
  1058. return Admin::view($this->view(), $this->variables());
  1059. }
  1060. protected function withScript()
  1061. {
  1062. if ($this->script) {
  1063. Admin::script($this->script);
  1064. }
  1065. }
  1066. /**
  1067. * @return string
  1068. */
  1069. public function __toString()
  1070. {
  1071. $view = $this->render();
  1072. return $view instanceof Renderable ? $view->render() : (string) $view;
  1073. }
  1074. }