Builder.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <?php
  2. namespace Dcat\Admin\Form\Step;
  3. use Closure;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Form as ParentForm;
  6. use Dcat\Admin\Form\StepForm as Form;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Collection;
  9. class Builder
  10. {
  11. const CURRENT_VALIDATION_STEP = 'CURRENT_VALIDATION_STEP';
  12. const ALL_STEPS = 'ALL_STEPS';
  13. /**
  14. * @var ParentForm
  15. */
  16. protected $form;
  17. /**
  18. * @var Form[]
  19. */
  20. protected $stepForms = [];
  21. /**
  22. * @var Done
  23. */
  24. protected $done;
  25. /**
  26. * @var array
  27. */
  28. protected $options = [
  29. 'selected' => 0,
  30. 'width' => '1000px',
  31. 'padding' => '30px 18px 30px',
  32. 'remember' => false,
  33. 'shown' => [],
  34. 'leaving' => [],
  35. ];
  36. public function __construct(ParentForm $form)
  37. {
  38. $this->form = $form->saved(function () {
  39. $this->flushStash();
  40. });
  41. $this->collectAssets();
  42. }
  43. /**
  44. * @param string|Form|Form[] $title
  45. * @param \Closure|null $callback
  46. *
  47. * @return $this
  48. */
  49. public function add($title, ?\Closure $callback = null)
  50. {
  51. if (is_array($title)) {
  52. foreach ($title as $key => $form) {
  53. $this->addForm($form, $callback);
  54. }
  55. return $this;
  56. }
  57. $form = $title instanceof Form ? $title : new Form($this->form, $title);
  58. $this->addForm($form, $callback);
  59. return $this;
  60. }
  61. /**
  62. * @param Form $form
  63. * @param \Closure|null $callback
  64. *
  65. * @return void
  66. */
  67. protected function addForm(Form $form, ?\Closure $callback = null)
  68. {
  69. $form->setIndex(count($this->stepForms));
  70. $this->stepForms[] = $form;
  71. if ($callback) {
  72. $callback($form);
  73. }
  74. }
  75. /**
  76. * Get all step forms.
  77. *
  78. * @return Form[]
  79. */
  80. public function all()
  81. {
  82. return $this->stepForms;
  83. }
  84. /**
  85. * @return ParentForm\Field[]|Collection
  86. */
  87. public function fields()
  88. {
  89. $fields = new Collection();
  90. foreach ($this->all() as $form) {
  91. $fields = $fields->merge($form->fields());
  92. }
  93. return $fields;
  94. }
  95. /**
  96. * Counts all step forms.
  97. *
  98. * @return int
  99. */
  100. public function count()
  101. {
  102. return count($this->stepForms);
  103. }
  104. /**
  105. * Set options.
  106. *
  107. * @param string|array $key
  108. * @param mixed $value
  109. *
  110. * @return $this
  111. */
  112. public function option($key, $value = null)
  113. {
  114. if (is_array($key)) {
  115. $this->options = array_merge($this->options, $key);
  116. } else {
  117. $this->options[$key] = $value;
  118. }
  119. return $this;
  120. }
  121. /**
  122. * Get options.
  123. *
  124. * @param string|null $key
  125. * @param null $default
  126. *
  127. * @return array|mixed|null
  128. */
  129. public function getOption($key = null, $default = null)
  130. {
  131. if ($key === null) {
  132. return $this->options;
  133. }
  134. return $this->options[$key] ?? $default;
  135. }
  136. /**
  137. * @param int $index
  138. *
  139. * @return $this
  140. */
  141. public function select(int $index)
  142. {
  143. return $this->option('selected', $index);
  144. }
  145. /**
  146. * Set padding for container.
  147. *
  148. * @param string $padding
  149. *
  150. * @return $this
  151. */
  152. public function padding(string $padding)
  153. {
  154. return $this->option('padding', $padding);
  155. }
  156. /**
  157. * Set max width for container.
  158. *
  159. * @param string $width
  160. *
  161. * @return $this
  162. */
  163. public function width(string $width)
  164. {
  165. return $this->option('width', $width);
  166. }
  167. /**
  168. * Remember input data.
  169. *
  170. * @param bool $value
  171. *
  172. * @return $this
  173. */
  174. public function remember(bool $value = true)
  175. {
  176. return $this->option('remember', $value);
  177. }
  178. /**
  179. * @param string|Closure $title
  180. * @param Closure|null $callback
  181. *
  182. * @return $this|Done
  183. */
  184. public function done($title = null, Closure $callback = null)
  185. {
  186. if ($title === null && $callback === null) {
  187. if (! $this->done) {
  188. $this->makeDefaultDonePage();
  189. }
  190. return $this->done;
  191. }
  192. if ($title instanceof Closure) {
  193. $callback = $title;
  194. $title = trans('admin.done');
  195. }
  196. $this->done = new Done($this->form, $title, $callback);
  197. return $this;
  198. }
  199. /**
  200. * @return void
  201. */
  202. protected function makeDefaultDonePage()
  203. {
  204. $this->done(function () {
  205. $resource = $this->form->getResource(0);
  206. $data = [
  207. 'title' => trans('admin.save_succeeded'),
  208. 'description' => '',
  209. 'createUrl' => $resource.'/create',
  210. 'backUrl' => $resource,
  211. ];
  212. return view('admin::form.done-step', $data);
  213. });
  214. }
  215. /**
  216. * Stash input data.
  217. *
  218. * @param array $data
  219. * @param bool $merge
  220. *
  221. * @return void
  222. */
  223. public function stash(array $data, bool $merge = false)
  224. {
  225. if (! $this->options['remember']) {
  226. return;
  227. }
  228. if ($merge) {
  229. $data = array_merge($this->fetchStash(), $data);
  230. }
  231. session()->put($this->getStashKey(), $data);
  232. }
  233. /**
  234. * Fetch input data.
  235. *
  236. * @return array
  237. */
  238. public function fetchStash()
  239. {
  240. if (! $this->options['remember']) {
  241. return [];
  242. }
  243. return session()->get($this->getStashKey()) ?: [];
  244. }
  245. /**
  246. * Flush input data.
  247. *
  248. * @return void
  249. */
  250. public function flushStash()
  251. {
  252. if (! $this->options['remember']) {
  253. return;
  254. }
  255. session()->remove($this->getStashKey());
  256. }
  257. /**
  258. * Forget input data by keys.
  259. *
  260. * @param string|array $keys
  261. *
  262. * @return void
  263. */
  264. public function forgetStash($keys)
  265. {
  266. $data = $this->fetchStash();
  267. Arr::forget($data, $keys);
  268. $this->stash($data);
  269. }
  270. /**
  271. * @param string|\Dcat\Admin\Form\Field $field
  272. *
  273. * @return void
  274. */
  275. public function stashIndexByField($field)
  276. {
  277. if (! $this->options['remember']) {
  278. return;
  279. }
  280. $data = $this->fetchStash();
  281. $data[self::CURRENT_VALIDATION_STEP] = ($this->fieldIndex($field) ?: 0) - 1;
  282. unset($data[self::ALL_STEPS]);
  283. $this->stash($data);
  284. }
  285. /**
  286. * @return string
  287. */
  288. protected function getStashKey()
  289. {
  290. return 'step-form-input:'.admin_controller_slug();
  291. }
  292. /**
  293. * @return void
  294. */
  295. protected function collectAssets()
  296. {
  297. Admin::collectAssets('@smart-wizard');
  298. }
  299. /**
  300. * @return void
  301. */
  302. protected function selectStep()
  303. {
  304. if (! $this->options['remember'] || ! $input = $this->fetchStash()) {
  305. return;
  306. }
  307. $current = $input[static::CURRENT_VALIDATION_STEP] ?? null;
  308. $allStep = $input[static::ALL_STEPS] ?? null;
  309. unset($input[static::CURRENT_VALIDATION_STEP], $input[static::ALL_STEPS]);
  310. if ($current !== null && $current !== '' && ! empty($input)) {
  311. $this->select((int) ($current + 1));
  312. }
  313. if (! empty($allStep) && ! empty($input)) {
  314. $this->select($this->count() - 1);
  315. }
  316. }
  317. /**
  318. * @return string
  319. */
  320. public function build()
  321. {
  322. $this->selectStep();
  323. $this->prepareForm();
  324. return $this->renderFields();
  325. }
  326. /**
  327. * @return void
  328. */
  329. protected function prepareForm()
  330. {
  331. foreach ($this->stepForms as $step) {
  332. $step->action($this->form->action());
  333. foreach ($step->fields() as $field) {
  334. $field->setForm($this->form);
  335. }
  336. }
  337. }
  338. /**
  339. * @return string
  340. */
  341. public function renderFields()
  342. {
  343. $html = '';
  344. foreach ($this->stepForms as $step) {
  345. $html .= (string) $step->render();
  346. }
  347. return $html;
  348. }
  349. /**
  350. * Register the "showStep" event listener.
  351. *
  352. * @param string $script
  353. *
  354. * @return $this
  355. */
  356. public function shown($script)
  357. {
  358. $script = value($script);
  359. $this->options['shown'][] = <<<JS
  360. function (args) {
  361. {$script}
  362. }
  363. JS;
  364. return $this;
  365. }
  366. /**
  367. * Register the "leaveStep" event listener.
  368. *
  369. * @param string $script
  370. *
  371. * @return $this
  372. */
  373. public function leaving($script)
  374. {
  375. $script = value($script);
  376. $this->options['leaving'][] = <<<JS
  377. function (args) {
  378. {$script}
  379. }
  380. JS;
  381. return $this;
  382. }
  383. /**
  384. * @param string|\Dcat\Admin\Form\Field $column
  385. *
  386. * @return false|int
  387. */
  388. public function fieldIndex($column)
  389. {
  390. foreach ($this->stepForms as $index => $form) {
  391. if ($form->field($column)) {
  392. return $index;
  393. }
  394. }
  395. return false;
  396. }
  397. }