Builder.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Closure;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Contracts\UploadField;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Form\Field\Hidden;
  8. use Dcat\Admin\Form\Step\Builder as StepBuilder;
  9. use Dcat\Admin\IFrameGrid;
  10. use Dcat\Admin\Support\Helper;
  11. use Dcat\Admin\Widgets\DialogForm;
  12. use Illuminate\Contracts\Support\Renderable;
  13. use Illuminate\Support\Arr;
  14. use Illuminate\Support\Collection;
  15. use Illuminate\Support\Facades\URL;
  16. use Illuminate\Support\Str;
  17. /**
  18. * Class Builder.
  19. */
  20. class Builder
  21. {
  22. /**
  23. * 上个页面URL保存的key.
  24. */
  25. const PREVIOUS_URL_KEY = '_previous_';
  26. /**
  27. * 构建时需要忽略的字段.
  28. */
  29. const BUILD_IGNORE = 'build-ignore';
  30. /**
  31. * Modes constants.
  32. */
  33. const MODE_EDIT = 'edit';
  34. const MODE_CREATE = 'create';
  35. const MODE_DELETE = 'delete';
  36. /**
  37. * @var mixed
  38. */
  39. protected $id;
  40. /**
  41. * @var Form
  42. */
  43. protected $form;
  44. /**
  45. * @var
  46. */
  47. protected $action;
  48. /**
  49. * @var Collection
  50. */
  51. protected $fields;
  52. /**
  53. * @var array
  54. */
  55. protected $options = [];
  56. /**
  57. * Form action mode, could be create|view|edit.
  58. *
  59. * @var string
  60. */
  61. protected $mode = self::MODE_CREATE;
  62. /**
  63. * @var array
  64. */
  65. protected $hiddenFields = [];
  66. /**
  67. * @var Tools
  68. */
  69. protected $tools;
  70. /**
  71. * @var Footer
  72. */
  73. protected $footer;
  74. /**
  75. * Width for label and field.
  76. *
  77. * @var array
  78. */
  79. protected $width = [
  80. 'label' => 2,
  81. 'field' => 8,
  82. ];
  83. /**
  84. * View for this form.
  85. *
  86. * @var string
  87. */
  88. protected $view = 'admin::form.container';
  89. /**
  90. * Form title.
  91. *
  92. * @var string
  93. */
  94. protected $title;
  95. /**
  96. * @var BlockForm[]
  97. */
  98. protected $multipleForms = [];
  99. /**
  100. * @var Layout
  101. */
  102. protected $layout;
  103. /**
  104. * @var int
  105. */
  106. protected $defaultBlockWidth = 12;
  107. /**
  108. * @var string
  109. */
  110. protected $elementId;
  111. /**
  112. * @var \Closure
  113. */
  114. protected $wrapper;
  115. /**
  116. * @var bool
  117. */
  118. protected $showHeader = true;
  119. /**
  120. * @var bool
  121. */
  122. protected $showFooter = true;
  123. /**
  124. * @var StepBuilder
  125. */
  126. protected $stepBuilder;
  127. /**
  128. * @var array
  129. */
  130. protected $confirm = [];
  131. /**
  132. * Builder constructor.
  133. *
  134. * @param Form $form
  135. */
  136. public function __construct(Form $form)
  137. {
  138. $this->form = $form;
  139. $this->fields = new Collection();
  140. $this->layout = new Layout($form);
  141. $this->tools = new Tools($this);
  142. $this->footer = new Footer($this);
  143. }
  144. /**
  145. * @param \Closure $closure
  146. *
  147. * @return Layout
  148. */
  149. public function layout($closure = null)
  150. {
  151. if ($closure) {
  152. $closure($this->layout);
  153. }
  154. return $this->layout;
  155. }
  156. /**
  157. * @param Closure $closure
  158. *
  159. * @return $this;
  160. */
  161. public function wrap(Closure $closure)
  162. {
  163. $this->wrapper = $closure;
  164. return $this;
  165. }
  166. /**
  167. * @return bool
  168. */
  169. public function hasWrapper()
  170. {
  171. return $this->wrapper ? true : false;
  172. }
  173. /**
  174. * @param int $width
  175. *
  176. * @return $this
  177. */
  178. public function setDefaultBlockWidth(int $width)
  179. {
  180. $this->defaultBlockWidth = $width;
  181. return $this;
  182. }
  183. /**
  184. * @param BlockForm $form
  185. */
  186. public function addForm(BlockForm $form)
  187. {
  188. $this->multipleForms[] = $form;
  189. $form->disableResetButton();
  190. $form->disableSubmitButton();
  191. $form->disableFormTag();
  192. return $this;
  193. }
  194. /**
  195. * Get form tools instance.
  196. *
  197. * @return Tools
  198. */
  199. public function tools()
  200. {
  201. return $this->tools;
  202. }
  203. /**
  204. * Get form footer instance.
  205. *
  206. * @return Footer
  207. */
  208. public function footer()
  209. {
  210. return $this->footer;
  211. }
  212. /**
  213. * @param \Closure|StepForm[]|null $builder
  214. *
  215. * @return StepBuilder
  216. */
  217. public function multipleSteps($builder = null)
  218. {
  219. if (! $this->stepBuilder) {
  220. $this->view = 'admin::form.steps';
  221. $this->stepBuilder = new StepBuilder($this->form);
  222. }
  223. if ($builder) {
  224. if ($builder instanceof \Closure) {
  225. $builder($this->stepBuilder);
  226. } elseif (is_array($builder)) {
  227. $this->stepBuilder->add($builder);
  228. }
  229. }
  230. return $this->stepBuilder;
  231. }
  232. /**
  233. * @return StepBuilder
  234. */
  235. public function stepBuilder()
  236. {
  237. return $this->stepBuilder;
  238. }
  239. /**
  240. * @param string $title
  241. * @param string $content
  242. *
  243. * @return $this
  244. */
  245. public function confirm(?string $title = null, ?string $content = null)
  246. {
  247. $this->confirm['title'] = $title;
  248. $this->confirm['content'] = $content;
  249. return $this;
  250. }
  251. /**
  252. * Set the builder mode.
  253. *
  254. * @param string $mode
  255. *
  256. * @return void|string
  257. */
  258. public function mode(string $mode = null)
  259. {
  260. if ($mode === null) {
  261. return $this->mode;
  262. }
  263. $this->mode = $mode;
  264. }
  265. /**
  266. * Returns builder is $mode.
  267. *
  268. * @param $mode
  269. *
  270. * @return bool
  271. */
  272. public function isMode($mode)
  273. {
  274. return $this->mode == $mode;
  275. }
  276. /**
  277. * Check if is creating resource.
  278. *
  279. * @return bool
  280. */
  281. public function isCreating()
  282. {
  283. return $this->isMode(static::MODE_CREATE);
  284. }
  285. /**
  286. * Check if is editing resource.
  287. *
  288. * @return bool
  289. */
  290. public function isEditing()
  291. {
  292. return $this->isMode(static::MODE_EDIT);
  293. }
  294. /**
  295. * Check if is deleting resource.
  296. *
  297. * @return bool
  298. */
  299. public function isDeleting()
  300. {
  301. return $this->isMode(static::MODE_DELETE);
  302. }
  303. /**
  304. * Set resource Id.
  305. *
  306. * @param $id
  307. *
  308. * @return mixed|void
  309. */
  310. public function setResourceId($id)
  311. {
  312. $this->id = $id;
  313. }
  314. /**
  315. * @return mixed
  316. */
  317. public function getResourceId()
  318. {
  319. return $this->id;
  320. }
  321. /**
  322. * @return string
  323. */
  324. public function getResource($slice = null)
  325. {
  326. if ($this->mode == self::MODE_CREATE) {
  327. return $this->form->getResource(-1);
  328. }
  329. if ($slice !== null) {
  330. return $this->form->getResource($slice);
  331. }
  332. return $this->form->getResource();
  333. }
  334. /**
  335. * @param int $field
  336. * @param int $label
  337. *
  338. * @return $this
  339. */
  340. public function width($field = 8, $label = 2)
  341. {
  342. $this->width = [
  343. 'label' => $label,
  344. 'field' => $field,
  345. ];
  346. return $this;
  347. }
  348. /**
  349. * Get label and field width.
  350. *
  351. * @return array
  352. */
  353. public function getWidth()
  354. {
  355. return $this->width;
  356. }
  357. /**
  358. * Get or set action for form.
  359. *
  360. * @return string|void
  361. */
  362. public function action($action = null)
  363. {
  364. if ($action !== null) {
  365. $this->action = admin_url($action);
  366. return;
  367. }
  368. if ($this->action) {
  369. return $this->action;
  370. }
  371. if ($this->isMode(static::MODE_EDIT)) {
  372. return $this->form->getResource().'/'.$this->id;
  373. }
  374. if ($this->isMode(static::MODE_CREATE)) {
  375. return $this->form->getResource(-1);
  376. }
  377. return '';
  378. }
  379. /**
  380. * Set view for this form.
  381. *
  382. * @param string $view
  383. *
  384. * @return $this
  385. */
  386. public function view($view)
  387. {
  388. $this->view = $view;
  389. return $this;
  390. }
  391. /**
  392. * Get or set title for form.
  393. *
  394. * @param string $title
  395. *
  396. * @return $this|string
  397. */
  398. public function title($title = null)
  399. {
  400. if ($title !== null) {
  401. $this->title = $title;
  402. return $this;
  403. }
  404. if ($this->title) {
  405. return $this->title;
  406. }
  407. if ($this->mode == static::MODE_CREATE) {
  408. return trans('admin.create');
  409. }
  410. if ($this->mode == static::MODE_EDIT) {
  411. return trans('admin.edit');
  412. }
  413. return '';
  414. }
  415. /**
  416. * Get fields of this builder.
  417. *
  418. * @return Collection
  419. */
  420. public function fields()
  421. {
  422. return $this->fields;
  423. }
  424. /**
  425. * Get specify field.
  426. *
  427. * @param string|Field $name
  428. *
  429. * @return Field|null
  430. */
  431. public function field($name)
  432. {
  433. $field = $this->fields->first(function (Field $field) use ($name) {
  434. if (is_array($field->column())) {
  435. return in_array($name, $field->column(), true);
  436. }
  437. return $field === $name || $field->column() === $name;
  438. });
  439. if (! $field) {
  440. $field = $this->stepField($name);
  441. }
  442. return $field;
  443. }
  444. /**
  445. * @param string $name
  446. *
  447. * @return Field|null
  448. */
  449. public function stepField($name)
  450. {
  451. if (! $builder = $this->stepBuilder()) {
  452. return;
  453. }
  454. foreach ($builder->all() as $step) {
  455. if ($field = $step->field($name)) {
  456. return $field;
  457. }
  458. }
  459. }
  460. /**
  461. * @return Field[]|Collection
  462. */
  463. public function stepFields()
  464. {
  465. $fields = new Collection();
  466. if (! $builder = $this->stepBuilder()) {
  467. return $fields;
  468. }
  469. foreach ($builder->all() as $step) {
  470. $fields = $fields->merge($step->fields());
  471. }
  472. return $fields;
  473. }
  474. /**
  475. * @param $column
  476. *
  477. * @return void
  478. */
  479. public function removeField($column)
  480. {
  481. $this->fields = $this->fields->filter(function (Field $field) use ($column) {
  482. return $field->column() != $column;
  483. });
  484. }
  485. /**
  486. * If the parant form has rows.
  487. *
  488. * @return bool
  489. */
  490. public function hasRows()
  491. {
  492. return ! empty($this->form->rows());
  493. }
  494. /**
  495. * Get field rows of form.
  496. *
  497. * @return array
  498. */
  499. public function rows()
  500. {
  501. return $this->form->rows();
  502. }
  503. /**
  504. * @return Form
  505. */
  506. public function form()
  507. {
  508. return $this->form;
  509. }
  510. /**
  511. * @return array
  512. */
  513. public function hiddenFields()
  514. {
  515. return $this->hiddenFields;
  516. }
  517. /**
  518. * @param Field $field
  519. *
  520. * @return void
  521. */
  522. public function addHiddenField(Field $field)
  523. {
  524. $this->hiddenFields[] = $field;
  525. }
  526. /**
  527. * Add or get options.
  528. *
  529. * @param array $options
  530. *
  531. * @return array|null
  532. */
  533. public function options($options = [])
  534. {
  535. if (empty($options)) {
  536. return $this->options;
  537. }
  538. $this->options = array_merge($this->options, $options);
  539. }
  540. /**
  541. * Get or set option.
  542. *
  543. * @param string $option
  544. * @param mixed $value
  545. *
  546. * @return void
  547. */
  548. public function option($option, $value = null)
  549. {
  550. if (func_num_args() == 1) {
  551. return Arr::get($this->options, $option);
  552. }
  553. $this->options[$option] = $value;
  554. }
  555. /**
  556. * @param bool $disable
  557. *
  558. * @return void
  559. */
  560. public function disableHeader(bool $disable = true)
  561. {
  562. $this->showHeader = ! $disable;
  563. }
  564. /**
  565. * @param bool $disable
  566. *
  567. * @return void
  568. */
  569. public function disableFooter(bool $disable = true)
  570. {
  571. $this->showFooter = ! $disable;
  572. }
  573. /**
  574. * @param $id
  575. *
  576. * @return void
  577. */
  578. public function setElementId($id)
  579. {
  580. $this->elementId = $id;
  581. }
  582. /**
  583. * @return string
  584. */
  585. public function getElementId()
  586. {
  587. return $this->elementId ?: ($this->elementId = 'form-'.Str::random(8));
  588. }
  589. /**
  590. * Determine if form fields has files.
  591. *
  592. * @return bool
  593. */
  594. public function hasFile()
  595. {
  596. foreach ($this->fields() as $field) {
  597. if (
  598. $field instanceof UploadField
  599. || $field instanceof Form\Field\BootstrapFile
  600. ) {
  601. return true;
  602. }
  603. }
  604. return false;
  605. }
  606. /**
  607. * Add field for store redirect url after update or store.
  608. *
  609. * @return void
  610. */
  611. protected function addRedirectUrlField()
  612. {
  613. $previous = Helper::getPreviousUrl();
  614. if (! $previous || $previous == URL::current()) {
  615. return;
  616. }
  617. if (
  618. Str::contains($previous, url($this->getResource()))
  619. && ! Helper::urlHasQuery($previous, [IFrameGrid::QUERY_NAME, DialogForm::QUERY_NAME])
  620. ) {
  621. $this->addHiddenField(
  622. (new Hidden(static::PREVIOUS_URL_KEY))->value($previous)
  623. );
  624. }
  625. }
  626. /**
  627. * Open up a new HTML form.
  628. *
  629. * @param array $options
  630. *
  631. * @return string
  632. */
  633. public function open($options = [])
  634. {
  635. $attributes = [];
  636. if ($this->isMode(static::MODE_EDIT)) {
  637. $this->addHiddenField((new Hidden('_method'))->value('PUT'));
  638. }
  639. $this->addHiddenField((new Hidden('_token'))->value(csrf_token()));
  640. $this->addRedirectUrlField();
  641. $attributes['id'] = $this->getElementId();
  642. $attributes['action'] = $this->action();
  643. $attributes['method'] = Arr::get($options, 'method', 'post');
  644. $attributes['accept-charset'] = 'UTF-8';
  645. $attributes['data-toggle'] = 'validator';
  646. $attributes['class'] = Arr::get($options, 'class');
  647. if ($this->hasFile()) {
  648. $attributes['enctype'] = 'multipart/form-data';
  649. }
  650. $html = [];
  651. foreach ($attributes as $name => $value) {
  652. $html[] = "$name=\"$value\"";
  653. }
  654. return '<form '.implode(' ', $html).' pjax-container>';
  655. }
  656. /**
  657. * Close the current form.
  658. *
  659. * @return string
  660. */
  661. public function close()
  662. {
  663. $this->form = null;
  664. $this->fields = null;
  665. return '</form>';
  666. }
  667. /**
  668. * 移除需要忽略的字段.
  669. *
  670. * @return void
  671. */
  672. protected function removeIgnoreFields()
  673. {
  674. $this->fields = $this->fields()->reject(function (Field $field) {
  675. return $field->hasAttribute(static::BUILD_IGNORE);
  676. });
  677. }
  678. /**
  679. * Remove reserved fields like `id` `created_at` `updated_at` in form fields.
  680. *
  681. * @return void
  682. */
  683. protected function removeReservedFields()
  684. {
  685. if (! $this->isMode(static::MODE_CREATE)) {
  686. return;
  687. }
  688. $reservedColumns = [
  689. $this->form->keyName(),
  690. $this->form->createdAtColumn(),
  691. $this->form->updatedAtColumn(),
  692. ];
  693. $reject = function (Field $field) use (&$reservedColumns) {
  694. return in_array($field->column(), $reservedColumns)
  695. && $field instanceof Form\Field\Display;
  696. };
  697. $this->fields = $this->fields()->reject($reject);
  698. if ($this->form->hasTab()) {
  699. $this->form->getTab()->getTabs()->transform(function($item) use ($reject) {
  700. if (! empty($item['fields'])) {
  701. $item['fields'] = $item['fields']->reject($reject);
  702. }
  703. return $item;
  704. });
  705. }
  706. }
  707. /**
  708. * Render form header tools.
  709. *
  710. * @return string
  711. */
  712. public function renderTools()
  713. {
  714. return $this->tools->render();
  715. }
  716. /**
  717. * Render form footer.
  718. *
  719. * @return string
  720. */
  721. public function renderFooter()
  722. {
  723. if (! $this->showFooter) {
  724. return;
  725. }
  726. return $this->footer->render();
  727. }
  728. /**
  729. * Render form.
  730. *
  731. * @return string
  732. */
  733. public function render()
  734. {
  735. $this->removeIgnoreFields();
  736. $this->removeReservedFields();
  737. $tabObj = $this->form->getTab();
  738. if (! $tabObj->isEmpty()) {
  739. $tabObj->addScript();
  740. }
  741. if ($this->form->allowAjaxSubmit() && empty($this->stepBuilder)) {
  742. $this->addSubmitScript();
  743. }
  744. $open = $this->open(['class' => 'form-horizontal']);
  745. $data = [
  746. 'form' => $this,
  747. 'tabObj' => $tabObj,
  748. 'width' => $this->width,
  749. 'elementId' => $this->getElementId(),
  750. 'showHeader' => $this->showHeader,
  751. 'steps' => $this->stepBuilder,
  752. ];
  753. if ($this->layout->hasColumns()) {
  754. $content = $this->doWrap(view($this->view, $data));
  755. } else {
  756. $this->layout->prepend(
  757. $this->defaultBlockWidth,
  758. $this->doWrap(view($this->view, $data))
  759. );
  760. $content = $this->layout->build();
  761. }
  762. return <<<EOF
  763. {$open} {$content} {$this->close()}
  764. EOF;
  765. }
  766. /**
  767. * @param Renderable $view
  768. *
  769. * @return string
  770. */
  771. protected function doWrap(Renderable $view)
  772. {
  773. if ($wrapper = $this->wrapper) {
  774. return $wrapper($view);
  775. }
  776. return "<div class='card dcat-box'>{$view->render()}</div>";
  777. }
  778. /**
  779. * @return void
  780. */
  781. protected function addSubmitScript()
  782. {
  783. $confirm = json_encode($this->confirm);
  784. Admin::script(
  785. <<<JS
  786. $('#{$this->getElementId()}').form({
  787. validate: true,
  788. confirm: {$confirm},
  789. });
  790. JS
  791. );
  792. }
  793. }