Form.php 12 KB

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