Form.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Closure;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Form\Field;
  6. use Dcat\Admin\Support\Helper;
  7. use Dcat\Admin\Traits\HasHtmlAttributes;
  8. use Illuminate\Contracts\Support\Arrayable;
  9. use Illuminate\Contracts\Support\Renderable;
  10. use Illuminate\Support\Arr;
  11. use Illuminate\Support\Collection;
  12. use Illuminate\Support\Fluent;
  13. use Illuminate\Support\Str;
  14. use Illuminate\Support\Traits\Macroable;
  15. /**
  16. * Class Form.
  17. *
  18. * @method Field\Text text($column, $label = '')
  19. * @method Field\Checkbox checkbox($column, $label = '')
  20. * @method Field\Radio radio($column, $label = '')
  21. * @method Field\Select select($column, $label = '')
  22. * @method Field\MultipleSelect multipleSelect($column, $label = '')
  23. * @method Field\Textarea textarea($column, $label = '')
  24. * @method Field\Hidden hidden($column, $label = '')
  25. * @method Field\Id id($column, $label = '')
  26. * @method Field\Ip ip($column, $label = '')
  27. * @method Field\Url url($column, $label = '')
  28. * @method Field\Color color($column, $label = '')
  29. * @method Field\Email email($column, $label = '')
  30. * @method Field\Mobile mobile($column, $label = '')
  31. * @method Field\Slider slider($column, $label = '')
  32. * @method Field\Map map($latitude, $longitude, $label = '')
  33. * @method Field\Editor editor($column, $label = '')
  34. * @method Field\Date date($column, $label = '')
  35. * @method Field\Datetime datetime($column, $label = '')
  36. * @method Field\Time time($column, $label = '')
  37. * @method Field\Year year($column, $label = '')
  38. * @method Field\Month month($column, $label = '')
  39. * @method Field\DateRange dateRange($start, $end, $label = '')
  40. * @method Field\DateTimeRange datetimeRange($start, $end, $label = '')
  41. * @method Field\TimeRange timeRange($start, $end, $label = '')
  42. * @method Field\Number number($column, $label = '')
  43. * @method Field\Currency currency($column, $label = '')
  44. * @method Field\SwitchField switch($column, $label = '')
  45. * @method Field\Display display($column, $label = '')
  46. * @method Field\Rate rate($column, $label = '')
  47. * @method Field\Divide divider()
  48. * @method Field\Password password($column, $label = '')
  49. * @method Field\Decimal decimal($column, $label = '')
  50. * @method Field\Html html($html, $label = '')
  51. * @method Field\Tags tags($column, $label = '')
  52. * @method Field\Icon icon($column, $label = '')
  53. * @method Field\Embeds embeds($column, $label = '')
  54. * @method Field\Captcha captcha($column, $label = '')
  55. * @method Field\Listbox listbox($column, $label = '')
  56. * @method Field\SelectResource selectResource($column, $label = '')
  57. * @method Field\File file($column, $label = '')
  58. * @method Field\Image image($column, $label = '')
  59. * @method Field\MultipleFile multipleFile($column, $label = '')
  60. * @method Field\MultipleImage multipleImage($column, $label = '')
  61. * @method Field\HasMany hasMany($column, \Closure $callback)
  62. * @method Field\Tree tree($column, $label = '')
  63. * @method Field\Table table($column, $callback)
  64. * @method Field\ListField list($column, $label = '')
  65. * @method Field\Timezone timezone($column, $label = '')
  66. * @method Field\KeyValue keyValue($column, $label = '')
  67. * @method Field\Tel tel($column, $label = '')
  68. * @method Field\BootstrapFile bootstrapFile($column, $label = '')
  69. * @method Field\BootstrapImage bootstrapImage($column, $label = '')
  70. * @method Field\BootstrapMultipleImage bootstrapMultipleImage($column, $label = '')
  71. * @method Field\BootstrapMultipleFile bootstrapMultipleFile($column, $label = '')
  72. */
  73. class Form implements Renderable
  74. {
  75. use HasHtmlAttributes, Macroable {
  76. __call as macroCall;
  77. }
  78. /**
  79. * @var string
  80. */
  81. protected $view = 'admin::widgets.form';
  82. /**
  83. * @var Field[]|Collection
  84. */
  85. protected $fields;
  86. /**
  87. * @var bool
  88. */
  89. protected $useAjaxSubmit = true;
  90. /**
  91. * @var Fluent
  92. */
  93. protected $data;
  94. /**
  95. * @var mixed
  96. */
  97. protected $primaryKey;
  98. /**
  99. * Available buttons.
  100. *
  101. * @var array
  102. */
  103. protected $buttons = ['reset', 'submit'];
  104. /**
  105. * @var bool
  106. */
  107. protected $useFormTag = true;
  108. /**
  109. * @var string
  110. */
  111. protected $formId;
  112. /**
  113. * @var array
  114. */
  115. protected $width = [
  116. 'label' => 2,
  117. 'field' => 8,
  118. ];
  119. /**
  120. * Form constructor.
  121. *
  122. * @param array $data
  123. * @param mixed $key
  124. */
  125. public function __construct($data = [], $key = null)
  126. {
  127. $this->data($data);
  128. $this->key($key);
  129. $this->initFields();
  130. $this->initFormAttributes();
  131. }
  132. /**
  133. * Initialize the form fields.
  134. */
  135. protected function initFields()
  136. {
  137. $this->fields = new Collection();
  138. }
  139. /**
  140. * Initialize the form attributes.
  141. */
  142. protected function initFormAttributes()
  143. {
  144. $this->setHtmlAttribute([
  145. 'method' => 'POST',
  146. 'action' => '',
  147. 'class' => 'form-horizontal',
  148. 'accept-charset' => 'UTF-8',
  149. 'pjax-container' => true,
  150. ]);
  151. }
  152. /**
  153. * Action uri of the form.
  154. *
  155. * @param string $action
  156. *
  157. * @return $this
  158. */
  159. public function action($action)
  160. {
  161. return $this->setHtmlAttribute('action', $action);
  162. }
  163. /**
  164. * @return mixed
  165. */
  166. public function getAction()
  167. {
  168. return $this->getHtmlAttribute('action');
  169. }
  170. /**
  171. * Method of the form.
  172. *
  173. * @param string $method
  174. *
  175. * @return $this
  176. */
  177. public function method($method = 'POST')
  178. {
  179. return $this->setHtmlAttribute('method', strtoupper($method));
  180. }
  181. /**
  182. * Set primary key.
  183. *
  184. * @param mixed $value
  185. *
  186. * @return $this
  187. */
  188. public function key($value)
  189. {
  190. $this->primaryKey = $value;
  191. return $this;
  192. }
  193. /**
  194. * @return mixed
  195. */
  196. public function getKey()
  197. {
  198. return $this->primaryKey;
  199. }
  200. /**
  201. * @param array|Arrayable|Closure $data
  202. *
  203. * @return $this
  204. */
  205. public function data($data)
  206. {
  207. $this->data = new Fluent(Helper::array($data));
  208. return $this;
  209. }
  210. /**
  211. * @param array|Arrayable|Closure $data
  212. *
  213. * @return $this
  214. */
  215. public function fill($data)
  216. {
  217. return $this->data($data);
  218. }
  219. /**
  220. * @return Fluent
  221. */
  222. public function model()
  223. {
  224. if (!$this->data) {
  225. $this->data([]);
  226. }
  227. return $this->data;
  228. }
  229. /**
  230. * Add a fieldset to form.
  231. *
  232. * @param string $title
  233. * @param Closure $setCallback
  234. *
  235. * @return Field\Fieldset
  236. */
  237. public function fieldset(string $title, Closure $setCallback)
  238. {
  239. $fieldset = new Field\Fieldset();
  240. $this->html($fieldset->start($title))->plain();
  241. $setCallback($this);
  242. $this->html($fieldset->end())->plain();
  243. return $fieldset;
  244. }
  245. /**
  246. * Get specify field.
  247. *
  248. * @param string|Field $name
  249. *
  250. * @return Field|null
  251. */
  252. public function field($name)
  253. {
  254. foreach ($this->fields as $field) {
  255. if ($field === $name || $field->column() === $name) {
  256. return $field;
  257. }
  258. }
  259. }
  260. /**
  261. * @return Field[]|Collection
  262. */
  263. public function fields()
  264. {
  265. return $this->fields;
  266. }
  267. /**
  268. * Disable Pjax.
  269. *
  270. * @return $this
  271. */
  272. public function disablePjax()
  273. {
  274. $this->forgetHtmlAttribute('pjax-container');
  275. return $this;
  276. }
  277. /**
  278. * Disable form tag.
  279. *
  280. * @return $this;
  281. */
  282. public function disableFormTag()
  283. {
  284. $this->useFormTag = false;
  285. return $this;
  286. }
  287. /**
  288. * Disable reset button.
  289. *
  290. * @return $this
  291. */
  292. public function disableResetButton()
  293. {
  294. array_delete($this->buttons, 'reset');
  295. return $this;
  296. }
  297. /**
  298. * Disable submit button.
  299. *
  300. * @return $this
  301. */
  302. public function disableSubmitButton()
  303. {
  304. array_delete($this->buttons, 'submit');
  305. return $this;
  306. }
  307. /**
  308. * Set field and label width in current form.
  309. *
  310. * @param int $fieldWidth
  311. * @param int $labelWidth
  312. *
  313. * @return $this
  314. */
  315. public function setWidth($fieldWidth = 8, $labelWidth = 2)
  316. {
  317. $this->width = [
  318. 'label' => $labelWidth,
  319. 'field' => $fieldWidth,
  320. ];
  321. $this->fields->each(function ($field) use ($fieldWidth, $labelWidth) {
  322. /* @var Field $field */
  323. $field->setWidth($fieldWidth, $labelWidth);
  324. });
  325. return $this;
  326. }
  327. /**
  328. * Find field class with given name.
  329. *
  330. * @param string $method
  331. *
  332. * @return bool|string
  333. */
  334. public static function findFieldClass($method)
  335. {
  336. $class = Arr::get(\Dcat\Admin\Form::getExtensions(), $method);
  337. if (class_exists($class)) {
  338. return $class;
  339. }
  340. return false;
  341. }
  342. /**
  343. * Add a form field to form.
  344. *
  345. * @param Field $field
  346. *
  347. * @return $this
  348. */
  349. public function pushField(Field &$field)
  350. {
  351. $this->fields->push($field);
  352. $field->setForm($this);
  353. $field->setWidth($this->width['field'], $this->width['label']);
  354. $field::collectAssets();
  355. return $this;
  356. }
  357. /**
  358. * Get variables for render form.
  359. *
  360. * @return array
  361. */
  362. protected function getVariables()
  363. {
  364. $this->setHtmlAttribute('id', $this->getFormId());
  365. foreach ($this->fields as $field) {
  366. $field->fill($this->model()->toArray());
  367. }
  368. return [
  369. 'start' => $this->open(),
  370. 'end' => $this->close(),
  371. 'fields' => $this->fields,
  372. 'method' => $this->getHtmlAttribute('method'),
  373. 'buttons' => $this->buttons,
  374. ];
  375. }
  376. /**
  377. * @return string
  378. */
  379. protected function open()
  380. {
  381. return <<<HTML
  382. <form {$this->formatHtmlAttributes()}>
  383. HTML;
  384. }
  385. /**
  386. * @return string
  387. */
  388. protected function close()
  389. {
  390. return '</form>';
  391. }
  392. /**
  393. * Determine if form fields has files.
  394. *
  395. * @return bool
  396. */
  397. public function hasFile()
  398. {
  399. foreach ($this->fields as $field) {
  400. if ($field instanceof Field\File) {
  401. return true;
  402. }
  403. }
  404. return false;
  405. }
  406. /**
  407. * @param $id
  408. *
  409. * @return $this
  410. */
  411. public function setFormId($id)
  412. {
  413. $this->formId = $id;
  414. return $this;
  415. }
  416. /**
  417. * @return string
  418. */
  419. public function getFormId()
  420. {
  421. return $this->formId ?: ($this->formId = 'form-'.Str::random(8));
  422. }
  423. /**
  424. * Generate a Field object and add to form builder if Field exists.
  425. *
  426. * @param string $method
  427. * @param array $arguments
  428. *
  429. * @return Field|null
  430. */
  431. public function __call($method, $arguments)
  432. {
  433. if ($className = static::findFieldClass($method)) {
  434. $name = Arr::get($arguments, 0, '');
  435. $element = new $className($name, array_slice($arguments, 1));
  436. $this->pushField($element);
  437. return $element;
  438. }
  439. if (static::hasMacro($method)) {
  440. return $this->macroCall($method, $arguments);
  441. }
  442. }
  443. /**
  444. * Disable submit with ajax.
  445. *
  446. * @param bool $disable
  447. *
  448. * @return $this
  449. */
  450. public function disableAjaxSubmit(bool $disable = true)
  451. {
  452. $this->useAjaxSubmit = !$disable;
  453. return $this;
  454. }
  455. /**
  456. * @return bool
  457. */
  458. public function allowAjaxSubmit()
  459. {
  460. return $this->useAjaxSubmit === true;
  461. }
  462. protected function setupSubmitScript()
  463. {
  464. Admin::script(
  465. <<<JS
  466. (function () {
  467. var f = $('#{$this->getFormId()}');
  468. f.find('[type="submit"]').click(function () {
  469. var t = $(this);
  470. LA.Form({
  471. \$form: f,
  472. before: function () {
  473. f.validator('validate');
  474. if (f.find('.has-error').length > 0) {
  475. return false;
  476. }
  477. t.button('loading');
  478. },
  479. after: function () {
  480. t.button('reset');
  481. }
  482. });
  483. return false;
  484. });
  485. })()
  486. JS
  487. );
  488. }
  489. /**
  490. * Render the form.
  491. *
  492. * @return string
  493. */
  494. public function render()
  495. {
  496. if ($this->allowAjaxSubmit()) {
  497. $this->setupSubmitScript();
  498. }
  499. return view($this->view, $this->getVariables())->render();
  500. }
  501. /**
  502. * Output as string.
  503. *
  504. * @return string
  505. */
  506. public function __toString()
  507. {
  508. return $this->render();
  509. }
  510. }