StepBuilder.php 8.3 KB

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