Builder.php 16 KB

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