Builder.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. return $field === $name || $field->column() == $name;
  435. });
  436. if (! $field) {
  437. $field = $this->stepField($name);
  438. }
  439. return $field;
  440. }
  441. /**
  442. * @param string $name
  443. *
  444. * @return Field|null
  445. */
  446. public function stepField($name)
  447. {
  448. if (! $builder = $this->stepBuilder()) {
  449. return;
  450. }
  451. foreach ($builder->all() as $step) {
  452. if ($field = $step->field($name)) {
  453. return $field;
  454. }
  455. }
  456. }
  457. /**
  458. * @return Field[]|Collection
  459. */
  460. public function stepFields()
  461. {
  462. $fields = new Collection();
  463. if (! $builder = $this->stepBuilder()) {
  464. return $fields;
  465. }
  466. foreach ($builder->all() as $step) {
  467. $fields = $fields->merge($step->fields());
  468. }
  469. return $fields;
  470. }
  471. /**
  472. * @param $column
  473. *
  474. * @return void
  475. */
  476. public function removeField($column)
  477. {
  478. $this->fields = $this->fields->filter(function (Field $field) use ($column) {
  479. return $field->column() != $column;
  480. });
  481. }
  482. /**
  483. * If the parant form has rows.
  484. *
  485. * @return bool
  486. */
  487. public function hasRows()
  488. {
  489. return ! empty($this->form->rows());
  490. }
  491. /**
  492. * Get field rows of form.
  493. *
  494. * @return array
  495. */
  496. public function rows()
  497. {
  498. return $this->form->rows();
  499. }
  500. /**
  501. * @return Form
  502. */
  503. public function form()
  504. {
  505. return $this->form;
  506. }
  507. /**
  508. * @return array
  509. */
  510. public function hiddenFields()
  511. {
  512. return $this->hiddenFields;
  513. }
  514. /**
  515. * @param Field $field
  516. *
  517. * @return void
  518. */
  519. public function addHiddenField(Field $field)
  520. {
  521. $this->hiddenFields[] = $field;
  522. }
  523. /**
  524. * Add or get options.
  525. *
  526. * @param array $options
  527. *
  528. * @return array|null
  529. */
  530. public function options($options = [])
  531. {
  532. if (empty($options)) {
  533. return $this->options;
  534. }
  535. $this->options = array_merge($this->options, $options);
  536. }
  537. /**
  538. * Get or set option.
  539. *
  540. * @param string $option
  541. * @param mixed $value
  542. *
  543. * @return void
  544. */
  545. public function option($option, $value = null)
  546. {
  547. if (func_num_args() == 1) {
  548. return Arr::get($this->options, $option);
  549. }
  550. $this->options[$option] = $value;
  551. }
  552. /**
  553. * @param bool $disable
  554. *
  555. * @return void
  556. */
  557. public function disableHeader(bool $disable = true)
  558. {
  559. $this->showHeader = ! $disable;
  560. }
  561. /**
  562. * @param bool $disable
  563. *
  564. * @return void
  565. */
  566. public function disableFooter(bool $disable = true)
  567. {
  568. $this->showFooter = ! $disable;
  569. }
  570. /**
  571. * @param $id
  572. *
  573. * @return void
  574. */
  575. public function setElementId($id)
  576. {
  577. $this->elementId = $id;
  578. }
  579. /**
  580. * @return string
  581. */
  582. public function getElementId()
  583. {
  584. return $this->elementId ?: ($this->elementId = 'form-'.Str::random(8));
  585. }
  586. /**
  587. * Determine if form fields has files.
  588. *
  589. * @return bool
  590. */
  591. public function hasFile()
  592. {
  593. foreach ($this->fields() as $field) {
  594. if (
  595. $field instanceof UploadField
  596. || $field instanceof Form\Field\BootstrapFile
  597. ) {
  598. return true;
  599. }
  600. }
  601. return false;
  602. }
  603. /**
  604. * Add field for store redirect url after update or store.
  605. *
  606. * @return void
  607. */
  608. protected function addRedirectUrlField()
  609. {
  610. $previous = Helper::getPreviousUrl();
  611. if (! $previous || $previous == URL::current()) {
  612. return;
  613. }
  614. if (
  615. Str::contains($previous, url($this->getResource()))
  616. && ! Helper::urlHasQuery($previous, [IFrameGrid::QUERY_NAME, DialogForm::QUERY_NAME])
  617. ) {
  618. $this->addHiddenField(
  619. (new Hidden(static::PREVIOUS_URL_KEY))->value($previous)
  620. );
  621. }
  622. }
  623. /**
  624. * Open up a new HTML form.
  625. *
  626. * @param array $options
  627. *
  628. * @return string
  629. */
  630. public function open($options = [])
  631. {
  632. $attributes = [];
  633. if ($this->isMode(static::MODE_EDIT)) {
  634. $this->addHiddenField((new Hidden('_method'))->value('PUT'));
  635. }
  636. $this->addRedirectUrlField();
  637. $attributes['id'] = $this->getElementId();
  638. $attributes['action'] = $this->action();
  639. $attributes['method'] = Arr::get($options, 'method', 'post');
  640. $attributes['accept-charset'] = 'UTF-8';
  641. $attributes['data-toggle'] = 'validator';
  642. $attributes['class'] = Arr::get($options, 'class');
  643. if ($this->hasFile()) {
  644. $attributes['enctype'] = 'multipart/form-data';
  645. }
  646. $html = [];
  647. foreach ($attributes as $name => $value) {
  648. $html[] = "$name=\"$value\"";
  649. }
  650. return '<form '.implode(' ', $html).' pjax-container>';
  651. }
  652. /**
  653. * Close the current form.
  654. *
  655. * @return string
  656. */
  657. public function close()
  658. {
  659. $this->form = null;
  660. $this->fields = null;
  661. return '</form>';
  662. }
  663. /**
  664. * 移除需要忽略的字段.
  665. *
  666. * @return void
  667. */
  668. protected function removeIgnoreFields()
  669. {
  670. $this->fields = $this->fields()->reject(function (Field $field) {
  671. return $field->hasAttribute(static::BUILD_IGNORE);
  672. });
  673. }
  674. /**
  675. * Remove reserved fields like `id` `created_at` `updated_at` in form fields.
  676. *
  677. * @return void
  678. */
  679. protected function removeReservedFields()
  680. {
  681. if (! $this->isMode(static::MODE_CREATE)) {
  682. return;
  683. }
  684. $reservedColumns = [
  685. $this->form->keyName(),
  686. $this->form->createdAtColumn(),
  687. $this->form->updatedAtColumn(),
  688. ];
  689. $this->fields = $this->fields()->reject(function (Field $field) use (&$reservedColumns) {
  690. return in_array($field->column(), $reservedColumns)
  691. && $field instanceof Form\Field\Display;
  692. });
  693. }
  694. /**
  695. * Render form header tools.
  696. *
  697. * @return string
  698. */
  699. public function renderTools()
  700. {
  701. return $this->tools->render();
  702. }
  703. /**
  704. * Render form footer.
  705. *
  706. * @return string
  707. */
  708. public function renderFooter()
  709. {
  710. if (! $this->showFooter) {
  711. return;
  712. }
  713. return $this->footer->render();
  714. }
  715. /**
  716. * Render form.
  717. *
  718. * @return string
  719. */
  720. public function render()
  721. {
  722. $this->removeIgnoreFields();
  723. $this->removeReservedFields();
  724. $tabObj = $this->form->getTab();
  725. if (! $tabObj->isEmpty()) {
  726. $tabObj->addScript();
  727. }
  728. if ($this->form->allowAjaxSubmit() && empty($this->stepBuilder)) {
  729. $this->addSubmitScript();
  730. }
  731. $open = $this->open(['class' => 'form-horizontal']);
  732. $data = [
  733. 'form' => $this,
  734. 'tabObj' => $tabObj,
  735. 'width' => $this->width,
  736. 'elementId' => $this->getElementId(),
  737. 'showHeader' => $this->showHeader,
  738. 'steps' => $this->stepBuilder,
  739. ];
  740. if ($this->layout->hasColumns()) {
  741. $content = $this->doWrap(view($this->view, $data));
  742. } else {
  743. $this->layout->prepend(
  744. $this->defaultBlockWidth,
  745. $this->doWrap(view($this->view, $data))
  746. );
  747. $content = $this->layout->build();
  748. }
  749. return <<<EOF
  750. {$open} {$content} {$this->close()}
  751. EOF;
  752. }
  753. /**
  754. * @param Renderable $view
  755. *
  756. * @return string
  757. */
  758. protected function doWrap(Renderable $view)
  759. {
  760. if ($wrapper = $this->wrapper) {
  761. return $wrapper($view);
  762. }
  763. return "<div class='card dcat-box'>{$view->render()}</div>";
  764. }
  765. /**
  766. * @return void
  767. */
  768. protected function addSubmitScript()
  769. {
  770. $confirm = json_encode($this->confirm);
  771. Admin::script(
  772. <<<JS
  773. $('#{$this->getElementId()}').form({
  774. validate: true,
  775. confirm: {$confirm},
  776. });
  777. JS
  778. );
  779. }
  780. }