Form.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. <?php
  2. namespace Dcat\Admin;
  3. use Closure;
  4. use Dcat\Admin\Actions\Action;
  5. use Dcat\Admin\Contracts\Repository;
  6. use Dcat\Admin\Form\AbstractTool;
  7. use Dcat\Admin\Form\Builder;
  8. use Dcat\Admin\Form\Concerns;
  9. use Dcat\Admin\Form\Condition;
  10. use Dcat\Admin\Form\Field;
  11. use Dcat\Admin\Form\NestedForm;
  12. use Dcat\Admin\Support\Helper;
  13. use Dcat\Admin\Traits\HasBuilderEvents;
  14. use Dcat\Admin\Traits\HasFormResponse;
  15. use Dcat\Admin\Widgets\DialogForm;
  16. use Illuminate\Contracts\Support\MessageProvider;
  17. use Illuminate\Contracts\Support\Renderable;
  18. use Illuminate\Database\Eloquent\Model;
  19. use Illuminate\Http\Request;
  20. use Illuminate\Support\Arr;
  21. use Illuminate\Support\Collection;
  22. use Illuminate\Support\Fluent;
  23. use Illuminate\Support\MessageBag;
  24. use Illuminate\Support\Traits\Macroable;
  25. use Illuminate\Validation\Validator;
  26. use Symfony\Component\HttpFoundation\Response;
  27. /**
  28. * Class Form.
  29. *
  30. * @method Field\Text text($column, $label = '')
  31. * @method Field\Checkbox checkbox($column, $label = '')
  32. * @method Field\Radio radio($column, $label = '')
  33. * @method Field\Select select($column, $label = '')
  34. * @method Field\MultipleSelect multipleSelect($column, $label = '')
  35. * @method Field\Textarea textarea($column, $label = '')
  36. * @method Field\Hidden hidden($column, $label = '')
  37. * @method Field\Id id($column, $label = '')
  38. * @method Field\Ip ip($column, $label = '')
  39. * @method Field\Url url($column, $label = '')
  40. * @method Field\Email email($column, $label = '')
  41. * @method Field\Mobile mobile($column, $label = '')
  42. * @method Field\Slider slider($column, $label = '')
  43. * @method Field\Map map($latitude, $longitude, $label = '')
  44. * @method Field\Editor editor($column, $label = '')
  45. * @method Field\Date date($column, $label = '')
  46. * @method Field\Datetime datetime($column, $label = '')
  47. * @method Field\Time time($column, $label = '')
  48. * @method Field\Year year($column, $label = '')
  49. * @method Field\Month month($column, $label = '')
  50. * @method Field\DateRange dateRange($start, $end, $label = '')
  51. * @method Field\DateTimeRange datetimeRange($start, $end, $label = '')
  52. * @method Field\TimeRange timeRange($start, $end, $label = '')
  53. * @method Field\Number number($column, $label = '')
  54. * @method Field\Currency currency($column, $label = '')
  55. * @method Field\SwitchField switch($column, $label = '')
  56. * @method Field\Display display($column, $label = '')
  57. * @method Field\Rate rate($column, $label = '')
  58. * @method Field\Divide divider()
  59. * @method Field\Password password($column, $label = '')
  60. * @method Field\Decimal decimal($column, $label = '')
  61. * @method Field\Html html($html, $label = '')
  62. * @method Field\Tags tags($column, $label = '')
  63. * @method Field\Icon icon($column, $label = '')
  64. * @method Field\Embeds embeds($column, $label = '', Closure $callback = null)
  65. * @method Field\Captcha captcha()
  66. * @method Field\Listbox listbox($column, $label = '')
  67. * @method Field\SelectResource selectResource($column, $label = '')
  68. * @method Field\File file($column, $label = '')
  69. * @method Field\Image image($column, $label = '')
  70. * @method Field\MultipleFile multipleFile($column, $label = '')
  71. * @method Field\MultipleImage multipleImage($column, $label = '')
  72. * @method Field\HasMany hasMany($column, $labelOrCallback, $callback = null)
  73. * @method Field\Tree tree($column, $label = '')
  74. * @method Field\Table table($column, $labelOrCallback, $callback = null)
  75. * @method Field\ListField list($column, $label = '')
  76. * @method Field\Timezone timezone($column, $label = '')
  77. * @method Field\KeyValue keyValue($column, $label = '')
  78. * @method Field\Tel tel($column, $label = '')
  79. * @method Field\Markdown markdown($column, $label = '')
  80. * @method Field\Range range($start, $end, $label = '')
  81. * @method Field\Color color($column, $label = '')
  82. * @method Field\ArrayField array($column, $labelOrCallback, $callback = null)
  83. * @method Field\SelectTable selectTable($column, $label = '')
  84. * @method Field\MultipleSelectTable multipleSelectTable($column, $label = '')
  85. */
  86. class Form implements Renderable
  87. {
  88. use HasBuilderEvents,
  89. HasFormResponse,
  90. Concerns\HasEvents,
  91. Concerns\HasFiles,
  92. Concerns\HasSteps,
  93. Concerns\HandleCascadeFields,
  94. Concerns\HasRows,
  95. Concerns\HasTabs,
  96. Macroable {
  97. __call as macroCall;
  98. }
  99. /**
  100. * Remove flag in `has many` form.
  101. */
  102. const REMOVE_FLAG_NAME = '_remove_';
  103. const CURRENT_URL_NAME = '_current_';
  104. /**
  105. * Available fields.
  106. *
  107. * @var array
  108. */
  109. protected static $availableFields = [
  110. 'button' => Field\Button::class,
  111. 'checkbox' => Field\Checkbox::class,
  112. 'currency' => Field\Currency::class,
  113. 'date' => Field\Date::class,
  114. 'dateRange' => Field\DateRange::class,
  115. 'datetime' => Field\Datetime::class,
  116. 'datetimeRange' => Field\DatetimeRange::class,
  117. 'decimal' => Field\Decimal::class,
  118. 'display' => Field\Display::class,
  119. 'divider' => Field\Divide::class,
  120. 'embeds' => Field\Embeds::class,
  121. 'editor' => Field\Editor::class,
  122. 'email' => Field\Email::class,
  123. 'hidden' => Field\Hidden::class,
  124. 'id' => Field\Id::class,
  125. 'ip' => Field\Ip::class,
  126. 'map' => Field\Map::class,
  127. 'mobile' => Field\Mobile::class,
  128. 'month' => Field\Month::class,
  129. 'multipleSelect' => Field\MultipleSelect::class,
  130. 'number' => Field\Number::class,
  131. 'password' => Field\Password::class,
  132. 'radio' => Field\Radio::class,
  133. 'rate' => Field\Rate::class,
  134. 'select' => Field\Select::class,
  135. 'slider' => Field\Slider::class,
  136. 'switch' => Field\SwitchField::class,
  137. 'text' => Field\Text::class,
  138. 'textarea' => Field\Textarea::class,
  139. 'time' => Field\Time::class,
  140. 'timeRange' => Field\TimeRange::class,
  141. 'url' => Field\Url::class,
  142. 'year' => Field\Year::class,
  143. 'html' => Field\Html::class,
  144. 'tags' => Field\Tags::class,
  145. 'icon' => Field\Icon::class,
  146. 'captcha' => Field\Captcha::class,
  147. 'listbox' => Field\Listbox::class,
  148. 'selectResource' => Field\SelectResource::class,
  149. 'file' => Field\File::class,
  150. 'image' => Field\Image::class,
  151. 'multipleFile' => Field\MultipleFile::class,
  152. 'multipleImage' => Field\MultipleImage::class,
  153. 'hasMany' => Field\HasMany::class,
  154. 'tree' => Field\Tree::class,
  155. 'table' => Field\Table::class,
  156. 'list' => Field\ListField::class,
  157. 'timezone' => Field\Timezone::class,
  158. 'keyValue' => Field\KeyValue::class,
  159. 'tel' => Field\Tel::class,
  160. 'markdown' => Field\Markdown::class,
  161. 'range' => Field\Range::class,
  162. 'color' => Field\Color::class,
  163. 'array' => Field\ArrayField::class,
  164. 'selectTable' => Field\SelectTable::class,
  165. 'multipleSelectTable' => Field\MultipleSelectTable::class,
  166. ];
  167. /**
  168. * Collected field assets.
  169. *
  170. * @var array
  171. */
  172. protected static $collectedAssets = [];
  173. /**
  174. * Form field alias.
  175. *
  176. * @var array
  177. */
  178. public static $fieldAlias = [];
  179. /**
  180. * @var Repository
  181. */
  182. protected $repository;
  183. /**
  184. * @var Closure
  185. */
  186. protected $callback;
  187. /**
  188. * @var Request
  189. */
  190. protected $request;
  191. /**
  192. * @var bool
  193. */
  194. protected $useAjaxSubmit = true;
  195. /**
  196. * Model of the form.
  197. *
  198. * @var Fluent
  199. */
  200. protected $model;
  201. /**
  202. * @var \Illuminate\Validation\Validator
  203. */
  204. protected $validator;
  205. /**
  206. * @var Builder
  207. */
  208. protected $builder;
  209. /**
  210. * Resource path for this form page.
  211. *
  212. * @var string
  213. */
  214. protected $resource;
  215. /**
  216. * Data for save to current model from input.
  217. *
  218. * @var array
  219. */
  220. protected $updates = [];
  221. /**
  222. * Input data.
  223. *
  224. * @var array
  225. */
  226. protected $inputs = [];
  227. /**
  228. * Ignored saving fields.
  229. *
  230. * @var array
  231. */
  232. protected $ignored = [];
  233. /**
  234. * @var bool
  235. */
  236. protected $isSoftDeletes = false;
  237. /**
  238. * @var MessageBag
  239. */
  240. protected $validationMessages;
  241. /**
  242. * @var Condition[]
  243. */
  244. protected $conditions = [];
  245. /**
  246. * Create a new form instance.
  247. *
  248. * @param Repository|Model|\Illuminate\Database\Eloquent\Builder|string $model
  249. * @param \Closure $callback
  250. * @param Request $request
  251. */
  252. public function __construct($repository = null, ?Closure $callback = null, Request $request = null)
  253. {
  254. $this->repository = $repository ? Admin::repository($repository) : null;
  255. $this->callback = $callback;
  256. $this->request = $request ?: request();
  257. $this->builder = new Builder($this);
  258. $this->isSoftDeletes = $repository ? $this->repository->isSoftDeletes() : false;
  259. $this->model(new Fluent());
  260. $this->prepareDialogForm();
  261. $this->callResolving();
  262. }
  263. /**
  264. * Create a form instance.
  265. *
  266. * @param mixed ...$params
  267. *
  268. * @return $this
  269. */
  270. public static function make(...$params)
  271. {
  272. return new static(...$params);
  273. }
  274. /**
  275. * @param Field $field
  276. *
  277. * @return $this
  278. */
  279. public function pushField(Field $field)
  280. {
  281. $field->setForm($this);
  282. $this->builder->fields()->push($field);
  283. $this->builder->layout()->addField($field);
  284. $width = $this->builder->getWidth();
  285. $field->width($width['field'], $width['label']);
  286. $field::collectAssets();
  287. return $this;
  288. }
  289. /**
  290. * Get specify field.
  291. *
  292. * @param string|null $name
  293. *
  294. * @return Field|Collection|Field[]|null
  295. */
  296. public function field($name = null)
  297. {
  298. return $this->builder->field($name);
  299. }
  300. /**
  301. * @return Collection|Field[]
  302. */
  303. public function fields()
  304. {
  305. $fields = $this->builder->fields();
  306. if ($steps = $this->builder->stepBuilder()) {
  307. $fields = $fields->merge($steps->fields());
  308. }
  309. return $fields;
  310. }
  311. /**
  312. * @param $column
  313. *
  314. * @return $this
  315. */
  316. public function removeField($column)
  317. {
  318. $this->builder->removeField($column);
  319. return $this;
  320. }
  321. /**
  322. * @param string $title
  323. * @param string $content
  324. *
  325. * @return $this
  326. */
  327. public function confirm(?string $title = null, ?string $content = null)
  328. {
  329. $this->builder->confirm($title, $content);
  330. return $this;
  331. }
  332. /**
  333. * @return bool
  334. */
  335. public function isCreating()
  336. {
  337. return $this->builder->isCreating();
  338. }
  339. /**
  340. * @return bool
  341. */
  342. public function isEditing()
  343. {
  344. return $this->builder->isEditing();
  345. }
  346. /**
  347. * @return bool
  348. */
  349. public function isDeleting()
  350. {
  351. return $this->builder->isDeleting();
  352. }
  353. /**
  354. * @param Fluent $model
  355. *
  356. * @return Fluent|void
  357. */
  358. public function model(Fluent $model = null)
  359. {
  360. if ($model === null) {
  361. return $this->model;
  362. }
  363. $this->model = $model;
  364. }
  365. /**
  366. * Get resource id.
  367. *
  368. * @return mixed
  369. */
  370. public function getKey()
  371. {
  372. return $this->builder()->getResourceId();
  373. }
  374. /**
  375. * Disable submit with ajax.
  376. *
  377. * @param bool $disable
  378. *
  379. * @return $this
  380. */
  381. public function disableAjaxSubmit(bool $disable = true)
  382. {
  383. $this->useAjaxSubmit = ! $disable;
  384. return $this;
  385. }
  386. /**
  387. * @return bool
  388. */
  389. public function allowAjaxSubmit()
  390. {
  391. return $this->useAjaxSubmit === true;
  392. }
  393. /**
  394. * @param \Closure $closure
  395. *
  396. * @return $this;
  397. */
  398. public function wrap(\Closure $closure)
  399. {
  400. $this->builder->wrap($closure);
  401. return $this;
  402. }
  403. /**
  404. * @return Builder
  405. */
  406. public function builder()
  407. {
  408. return $this->builder;
  409. }
  410. /**
  411. * @return string
  412. */
  413. public function getElementId()
  414. {
  415. return $this->builder->getElementId();
  416. }
  417. /**
  418. * @return Repository
  419. */
  420. public function repository()
  421. {
  422. return $this->repository;
  423. }
  424. /**
  425. * Generate a edit form.
  426. *
  427. * @param $id
  428. *
  429. * @return $this
  430. */
  431. public function edit($id)
  432. {
  433. $this->builder->mode(Builder::MODE_EDIT);
  434. $this->builder->setResourceId($id);
  435. $this->model(new Fluent($this->repository->edit($this)));
  436. return $this;
  437. }
  438. /**
  439. * Add a fieldset to form.
  440. *
  441. * @param string $title
  442. * @param Closure $setCallback
  443. *
  444. * @return Field\Fieldset
  445. */
  446. public function fieldset(string $title, Closure $setCallback)
  447. {
  448. $fieldset = new Field\Fieldset();
  449. $this->html($fieldset->start($title))->plain();
  450. $setCallback($this);
  451. $this->html($fieldset->end())->plain();
  452. return $fieldset;
  453. }
  454. /**
  455. * Destroy data entity and remove files.
  456. *
  457. * @param $id
  458. *
  459. * @return mixed
  460. */
  461. public function destroy($id)
  462. {
  463. try {
  464. $this->builder->setResourceId($id);
  465. $this->builder->mode(Builder::MODE_DELETE);
  466. $data = $this->repository->getDataWhenDeleting($this);
  467. $this->model(new Fluent($data));
  468. $this->setFieldOriginalValue();
  469. $this->build();
  470. if (($response = $this->callDeleting()) instanceof Response) {
  471. return $response;
  472. }
  473. $result = $this->repository->destroy($this, $data);
  474. if (($response = $this->callDeleted($result)) instanceof Response) {
  475. return $response;
  476. }
  477. $response = [
  478. 'status' => $result ? true : false,
  479. 'message' => $result ? trans('admin.delete_succeeded') : trans('admin.delete_failed'),
  480. ];
  481. } catch (\Throwable $exception) {
  482. $response = Admin::makeExceptionHandler()->handle($exception);
  483. if ($response instanceof Response) {
  484. return $response;
  485. }
  486. $response = $response ?: [
  487. 'status' => false,
  488. 'message' => $exception->getMessage() ?: trans('admin.delete_failed'),
  489. ];
  490. }
  491. return response()->json($response);
  492. }
  493. /**
  494. * Store a new record.
  495. *
  496. * @param array|null $data
  497. * @param string|string $redirectTo
  498. *
  499. * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\Http\JsonResponse|Response
  500. */
  501. public function store(?array $data = null, $redirectTo = null)
  502. {
  503. if ($data) {
  504. $this->request->replace($data);
  505. }
  506. $data = $data ?: $this->request->all();
  507. if ($response = $this->beforeStore($data)) {
  508. return $response;
  509. }
  510. $this->updates = $this->prepareInsert($this->updates);
  511. $id = $this->repository->store($this);
  512. $this->builder->setResourceId($id);
  513. if (($response = $this->callSaved($id))) {
  514. return $response;
  515. }
  516. if ($response = $this->responseMultipleStepsDonePage()) {
  517. return $response;
  518. }
  519. if (! $id) {
  520. return $this->error(trans('admin.save_failed'));
  521. }
  522. return $this->redirect(
  523. $this->redirectUrl($id, $redirectTo),
  524. trans('admin.save_succeeded')
  525. );
  526. }
  527. /**
  528. * Before store.
  529. *
  530. * @param array $data
  531. *
  532. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|Response|void
  533. */
  534. protected function beforeStore(array $data)
  535. {
  536. $this->inputs = $data;
  537. $this->build();
  538. $this->prepareStepFormFields($this->inputs);
  539. if (($response = $this->callSubmitted())) {
  540. return $response;
  541. }
  542. // Validate step form.
  543. if ($this->isStepFormValidationRequest()) {
  544. return $this->validateStepForm($this->inputs);
  545. }
  546. if ($response = $this->handleUploadFile($this->inputs)) {
  547. return $response;
  548. }
  549. if ($response = $this->handleFileDeleteBeforeCreate($this->inputs)) {
  550. $this->deleteFileInStepFormStashData($this->inputs);
  551. return $response;
  552. }
  553. if ($response = $this->handleFileDeleteWhenCreating($this->inputs)) {
  554. $this->deleteFileInStepFormStashData($this->inputs);
  555. return $response;
  556. }
  557. // Handle validation errors.
  558. if ($validationMessages = $this->validationMessages($this->inputs)) {
  559. return $this->validationErrorsResponse($validationMessages);
  560. }
  561. if (($response = $this->prepare($this->inputs))) {
  562. $this->deleteFilesWhenCreating($this->inputs);
  563. return $response;
  564. }
  565. }
  566. /**
  567. * Prepare input data for insert or update.
  568. *
  569. * @param array $data
  570. *
  571. * @return Response|null
  572. */
  573. protected function prepare($data = [])
  574. {
  575. $this->inputs = $this->removeIgnoredFields($data);
  576. if (($response = $this->callSaving()) instanceof Response) {
  577. return $response;
  578. }
  579. $this->updates = $this->inputs;
  580. }
  581. /**
  582. * Remove ignored fields from input.
  583. *
  584. * @param array $input
  585. *
  586. * @return array
  587. */
  588. public function removeIgnoredFields($input)
  589. {
  590. Arr::forget($input, $this->ignored);
  591. $ignored = $this->fields()->map(function (Field $field) {
  592. if ($field instanceof Field\Display || $field->getAttribute('readonly') || $field->getAttribute('disabled')) {
  593. return $field->column();
  594. }
  595. })->filter();
  596. if (! $ignored->isEmpty()) {
  597. Arr::forget($input, $ignored->flatten()->toArray());
  598. }
  599. return $input;
  600. }
  601. /**
  602. * Get or set data for insert or update.
  603. *
  604. * @param array $updates
  605. *
  606. * @return $this|array
  607. */
  608. public function updates(array $updates = null)
  609. {
  610. if ($updates === null) {
  611. return $this->updates;
  612. }
  613. $this->updates = array_merge($this->updates, $updates);
  614. return $this;
  615. }
  616. /**
  617. * Handle orderable update.
  618. *
  619. * @param int $id
  620. * @param array $input
  621. *
  622. * @return Response
  623. */
  624. protected function handleOrderable(array $input = [])
  625. {
  626. if (array_key_exists('_orderable', $input)) {
  627. $updated = $input['_orderable'] == 1
  628. ? $this->repository->moveOrderUp()
  629. : $this->repository->moveOrderDown();
  630. return $updated
  631. ? $this->ajaxResponse(__('admin.update_succeeded'))
  632. : $this->error(__('admin.nothing_updated'));
  633. }
  634. }
  635. /**
  636. * Handle update.
  637. *
  638. * @param $id
  639. * @param array|null $data
  640. * @param string|null $redirectTo
  641. *
  642. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse||Response
  643. */
  644. public function update(
  645. $id,
  646. ?array $data = null,
  647. $redirectTo = null
  648. ) {
  649. if ($data) {
  650. $this->request->replace($data);
  651. }
  652. $data = $data ?: $this->request->all();
  653. if ($response = $this->beforeUpdate($id, $data)) {
  654. return $response;
  655. }
  656. $this->updates = $this->prepareUpdate($this->updates);
  657. $updated = $this->repository->update($this);
  658. if (($response = $this->callSaved($updated))) {
  659. return $response;
  660. }
  661. if (! $updated) {
  662. return $this->error(trans('admin.update_succeeded'));
  663. }
  664. return $this->redirect(
  665. $this->redirectUrl($id, $redirectTo),
  666. trans('admin.update_succeeded')
  667. );
  668. }
  669. /**
  670. * Before update.
  671. *
  672. * @param array $data
  673. *
  674. * @return Response|void
  675. */
  676. protected function beforeUpdate($id, array &$data)
  677. {
  678. $this->builder->setResourceId($id);
  679. $this->builder->mode(Builder::MODE_EDIT);
  680. $this->inputs = $data;
  681. $this->model(new Fluent($this->repository->getDataWhenUpdating($this)));
  682. $this->build();
  683. $this->setFieldOriginalValue();
  684. if ($response = $this->callSubmitted()) {
  685. return $response;
  686. }
  687. if ($uploadFileResponse = $this->handleUploadFile($this->inputs)) {
  688. return $uploadFileResponse;
  689. }
  690. $isEditable = $this->isEditable($this->inputs);
  691. $this->inputs = $this->handleEditable($this->inputs);
  692. $this->inputs = $this->handleFileDelete($this->inputs);
  693. $this->inputs = $this->handleHasManyValues($this->inputs);
  694. if ($response = $this->handleOrderable($this->inputs)) {
  695. return $response;
  696. }
  697. // Handle validation errors.
  698. if ($validationMessages = $this->validationMessages($this->inputs)) {
  699. return $this->validationErrorsResponse(
  700. $isEditable ? Arr::dot($validationMessages->toArray()) : $validationMessages
  701. );
  702. }
  703. if ($response = $this->prepare($this->inputs)) {
  704. return $response;
  705. }
  706. }
  707. /**
  708. * @param array $inputs
  709. *
  710. * @return array
  711. */
  712. protected function handleHasManyValues(array $inputs)
  713. {
  714. foreach ($inputs as $column => &$input) {
  715. $field = $this->builder()->field($column);
  716. if (is_array($input) && $field instanceof Field\HasMany) {
  717. $keyName = $field->getKeyName();
  718. foreach ($input as $k => &$v) {
  719. if (! array_key_exists($keyName, $v)) {
  720. $v[$keyName] = $k;
  721. }
  722. if (empty($v[NestedForm::REMOVE_FLAG_NAME])) {
  723. $v[NestedForm::REMOVE_FLAG_NAME] = null;
  724. }
  725. }
  726. }
  727. }
  728. return $inputs;
  729. }
  730. /**
  731. * @param $key
  732. * @param $redirectTo
  733. *
  734. * @return string|null
  735. */
  736. public function redirectUrl($key, $redirectTo = null)
  737. {
  738. if ($redirectTo) {
  739. return $redirectTo;
  740. }
  741. $resourcesPath = $this->builder->isCreating() ?
  742. $this->getResource(0) : $this->getResource(-1);
  743. if ($this->request->get('after-save') == 1) {
  744. // continue editing
  745. if ($this->builder->isEditing() && $this->isAjaxRequest()) {
  746. return;
  747. }
  748. return rtrim($resourcesPath, '/')."/{$key}/edit";
  749. }
  750. if ($this->request->get('after-save') == 2) {
  751. // continue creating
  752. return rtrim($resourcesPath, '/').'/create';
  753. }
  754. if ($this->request->get('after-save') == 3) {
  755. // view resource
  756. return rtrim($resourcesPath, '/')."/{$key}";
  757. }
  758. return $this->request->get(Builder::PREVIOUS_URL_KEY) ?: $resourcesPath;
  759. }
  760. /**
  761. * Check if request is from editable.
  762. *
  763. * @param array $input
  764. *
  765. * @return bool
  766. */
  767. protected function isEditable(array $input = [])
  768. {
  769. return array_key_exists('_editable', $input);
  770. }
  771. /**
  772. * Handle editable update.
  773. *
  774. * @param array $input
  775. *
  776. * @return array
  777. */
  778. protected function handleEditable(array $input = [])
  779. {
  780. if (array_key_exists('_editable', $input)) {
  781. $name = $input['name'];
  782. $value = $input['value'];
  783. Arr::forget($input, ['pk', 'value', 'name']);
  784. Arr::set($input, $name, $value);
  785. }
  786. return $input;
  787. }
  788. /**
  789. * Prepare input data for update.
  790. *
  791. * @param array $updates
  792. *
  793. * @return array
  794. */
  795. public function prepareUpdate(array $updates)
  796. {
  797. $prepared = [];
  798. /** @var Field $field */
  799. foreach ($this->builder->fields() as $field) {
  800. $columns = $field->column();
  801. // If column not in input array data, then continue.
  802. if (! Arr::has($updates, $columns)) {
  803. continue;
  804. }
  805. $value = $this->getDataByColumn($updates, $columns);
  806. $value = $field->prepare($value);
  807. if (is_array($columns)) {
  808. foreach ($columns as $name => $column) {
  809. Arr::set($prepared, $column, $value[$name]);
  810. }
  811. } elseif (is_string($columns)) {
  812. Arr::set($prepared, $columns, $value);
  813. }
  814. }
  815. return $prepared;
  816. }
  817. /**
  818. * Prepare input data for insert.
  819. *
  820. * @param $inserts
  821. *
  822. * @return array
  823. */
  824. public function prepareInsert($inserts)
  825. {
  826. Helper::prepareHasOneRelation($this->builder->fields(), $inserts);
  827. foreach ($inserts as $column => $value) {
  828. if (is_null($field = $this->field($column))) {
  829. unset($inserts[$column]);
  830. continue;
  831. }
  832. $inserts[$column] = $field->prepare($value);
  833. }
  834. $prepared = [];
  835. foreach ($inserts as $key => $value) {
  836. Arr::set($prepared, $key, $value);
  837. }
  838. return $prepared;
  839. }
  840. /**
  841. * Ignore fields to save.
  842. *
  843. * @param string|array $fields
  844. *
  845. * @return $this
  846. */
  847. public function ignore($fields)
  848. {
  849. $this->ignored = array_merge($this->ignored, (array) $fields);
  850. return $this;
  851. }
  852. /**
  853. * Get primary key name of model.
  854. *
  855. * @return string
  856. */
  857. public function keyName()
  858. {
  859. if (! $this->repository) {
  860. return 'id';
  861. }
  862. return $this->repository->getKeyName();
  863. }
  864. /**
  865. * @return string|void
  866. */
  867. public function createdAtColumn()
  868. {
  869. if (! $this->repository) {
  870. return;
  871. }
  872. return $this->repository->getCreatedAtColumn();
  873. }
  874. /**
  875. * @return string|void
  876. */
  877. public function updatedAtColumn()
  878. {
  879. if (! $this->repository) {
  880. return;
  881. }
  882. return $this->repository->getUpdatedAtColumn();
  883. }
  884. /**
  885. * @param array $data
  886. * @param string|array $columns
  887. *
  888. * @return array|mixed
  889. */
  890. protected function getDataByColumn($data, $columns)
  891. {
  892. if (is_string($columns)) {
  893. return Arr::get($data, $columns);
  894. }
  895. if (is_array($columns)) {
  896. $value = [];
  897. foreach ($columns as $name => $column) {
  898. if (! Arr::has($data, $column)) {
  899. continue;
  900. }
  901. $value[$name] = Arr::get($data, $column);
  902. }
  903. return $value;
  904. }
  905. }
  906. /**
  907. * Set original data for each field.
  908. *
  909. * @return void
  910. */
  911. protected function setFieldOriginalValue()
  912. {
  913. $data = $this->model()->toArray();
  914. $this->builder->fields()->each(function (Field $field) use ($data) {
  915. $field->setOriginal($data);
  916. });
  917. }
  918. /**
  919. * @example
  920. * $form->if(true)->then(function (Form $form) {
  921. * $form->text('name');
  922. * });
  923. *
  924. * $form->if(function (Form $form) {
  925. * return $form->model()->id > 5;
  926. * })->then(function (Form $form) {
  927. * $form->text('name');
  928. * });
  929. *
  930. * $form->if(true)->now(function (Form $form) {
  931. * $form->text('name');
  932. * });
  933. *
  934. * $form->if(true)->creating(function (Form $form) {});
  935. *
  936. * $form->if(true)->removeField('name');
  937. *
  938. * @param bool|\Closure $condition
  939. *
  940. * @return Condition
  941. */
  942. public function if($condition)
  943. {
  944. return $this->conditions[] = new Condition($condition, $this);
  945. }
  946. /**
  947. * @return void
  948. */
  949. protected function rendering()
  950. {
  951. $this->build();
  952. if ($this->isCreating()) {
  953. $this->callCreating();
  954. return;
  955. }
  956. $this->fillFields($this->model()->toArray());
  957. $this->callEditing();
  958. }
  959. /**
  960. * @param array $data
  961. *
  962. * @return void
  963. */
  964. public function fillFields(array $data)
  965. {
  966. $this->builder->fields()->each(function (Field $field) use ($data) {
  967. if (! in_array($field->column(), $this->ignored, true)) {
  968. $field->fill($data);
  969. }
  970. });
  971. }
  972. /**
  973. * @return void
  974. */
  975. protected function build()
  976. {
  977. if ($callback = $this->callback) {
  978. $callback($this);
  979. }
  980. foreach ($this->conditions as $condition) {
  981. $condition->process();
  982. }
  983. }
  984. /**
  985. * Get validation messages.
  986. *
  987. * @param array $input
  988. *
  989. * @return MessageBag|bool
  990. */
  991. public function validationMessages($input)
  992. {
  993. $failedValidators = [];
  994. /** @var Field $field */
  995. foreach ($this->builder->fields() as $field) {
  996. if (! $validator = $field->getValidator($input)) {
  997. continue;
  998. }
  999. if (($validator instanceof Validator) && ! $validator->passes()) {
  1000. $failedValidators[] = [$field, $validator];
  1001. }
  1002. }
  1003. $message = $this->mergeValidationMessages($failedValidators);
  1004. if ($message->any() && $this->builder->isCreating()) {
  1005. $this->deleteFilesWhenCreating($input);
  1006. }
  1007. return $message->any() ? $message : false;
  1008. }
  1009. /**
  1010. * @param string|array|MessageProvider $column
  1011. * @param string|array $messages
  1012. *
  1013. * @return $this
  1014. */
  1015. public function responseValidationMessages($column, $messages = null)
  1016. {
  1017. if ($column instanceof MessageProvider) {
  1018. return $this->responseValidationMessages($column->getMessageBag()->getMessages());
  1019. }
  1020. if (! $this->validationMessages) {
  1021. $this->validationMessages = new MessageBag();
  1022. }
  1023. if (! $column) {
  1024. return $this;
  1025. }
  1026. if (is_array($column)) {
  1027. foreach ($column as $k => &$v) {
  1028. $v = (array) $v;
  1029. }
  1030. $this->validationMessages->merge($column);
  1031. } elseif ($messages) {
  1032. $this->validationMessages->merge([$column => (array) $messages]);
  1033. }
  1034. return $this;
  1035. }
  1036. /**
  1037. * Merge validation messages from input validators.
  1038. *
  1039. * @param array $validators
  1040. *
  1041. * @return MessageBag
  1042. */
  1043. protected function mergeValidationMessages($validators)
  1044. {
  1045. $messageBag = new MessageBag();
  1046. foreach ($validators as $value) {
  1047. [$field, $validator] = $value;
  1048. $messageBag = $messageBag->merge($field->formatValidatorMessages($validator->messages()));
  1049. }
  1050. if ($this->validationMessages) {
  1051. return $messageBag->merge($this->validationMessages);
  1052. }
  1053. return $messageBag;
  1054. }
  1055. /**
  1056. * Get or set action for form.
  1057. *
  1058. * @param string|null $action
  1059. *
  1060. * @return $this|string
  1061. */
  1062. public function action($action = null)
  1063. {
  1064. $value = $this->builder->action($action);
  1065. if ($action === null) {
  1066. return $value;
  1067. }
  1068. return $this;
  1069. }
  1070. /**
  1071. * Set field and label width in current form.
  1072. *
  1073. * @param int $fieldWidth
  1074. * @param int $labelWidth
  1075. *
  1076. * @return $this
  1077. */
  1078. public function width($fieldWidth = 8, $labelWidth = 2)
  1079. {
  1080. $this->builder->fields()->each(function ($field) use ($fieldWidth, $labelWidth) {
  1081. /* @var Field $field */
  1082. $field->width($fieldWidth, $labelWidth);
  1083. });
  1084. $this->builder->width($fieldWidth, $labelWidth);
  1085. return $this;
  1086. }
  1087. /**
  1088. * Set view for form.
  1089. *
  1090. * @param string $view
  1091. *
  1092. * @return $this
  1093. */
  1094. public function view($view)
  1095. {
  1096. $this->builder->view($view);
  1097. return $this;
  1098. }
  1099. /**
  1100. * Get or set title for form.
  1101. *
  1102. * @param string $title
  1103. *
  1104. * @return $this
  1105. */
  1106. public function title($title = null)
  1107. {
  1108. $this->builder->title($title);
  1109. return $this;
  1110. }
  1111. /**
  1112. * Tools setting for form.
  1113. *
  1114. * @param Closure|string|AbstractTool|Renderable|Action|array $callback
  1115. *
  1116. * @return $this;
  1117. */
  1118. public function tools($callback)
  1119. {
  1120. if ($callback instanceof Closure) {
  1121. $callback->call($this, $this->builder->tools());
  1122. return $this;
  1123. }
  1124. if (! is_array($callback)) {
  1125. $callback = [$callback];
  1126. }
  1127. foreach ($callback as $tool) {
  1128. $this->builder->tools()->append($tool);
  1129. }
  1130. return $this;
  1131. }
  1132. /**
  1133. * @param bool $disable
  1134. *
  1135. * @return $this
  1136. */
  1137. public function disableHeader(bool $disable = true)
  1138. {
  1139. $this->builder->disableHeader($disable);
  1140. return $this;
  1141. }
  1142. /**
  1143. * @param bool $disable
  1144. *
  1145. * @return $this
  1146. */
  1147. public function disableFooter(bool $disable = true)
  1148. {
  1149. $this->builder->disableFooter($disable);
  1150. return $this;
  1151. }
  1152. /**
  1153. * Disable form submit.
  1154. *
  1155. * @return $this
  1156. */
  1157. public function disableSubmitButton(bool $disable = true)
  1158. {
  1159. $this->builder->footer()->disableSubmit($disable);
  1160. return $this;
  1161. }
  1162. /**
  1163. * Disable form reset.
  1164. *
  1165. * @return $this
  1166. */
  1167. public function disableResetButton(bool $disable = true)
  1168. {
  1169. $this->builder->footer()->disableReset($disable);
  1170. return $this;
  1171. }
  1172. /**
  1173. * Disable View Checkbox on footer.
  1174. *
  1175. * @return $this
  1176. */
  1177. public function disableViewCheck(bool $disable = true)
  1178. {
  1179. $this->builder->footer()->disableViewCheck($disable);
  1180. return $this;
  1181. }
  1182. /**
  1183. * Disable Editing Checkbox on footer.
  1184. *
  1185. * @return $this
  1186. */
  1187. public function disableEditingCheck(bool $disable = true)
  1188. {
  1189. $this->builder->footer()->disableEditingCheck($disable);
  1190. return $this;
  1191. }
  1192. /**
  1193. * Disable Creating Checkbox on footer.
  1194. *
  1195. * @return $this
  1196. */
  1197. public function disableCreatingCheck(bool $disable = true)
  1198. {
  1199. $this->builder->footer()->disableCreatingCheck($disable);
  1200. return $this;
  1201. }
  1202. /**
  1203. * Disable `view` tool.
  1204. *
  1205. * @return $this
  1206. */
  1207. public function disableViewButton(bool $disable = true)
  1208. {
  1209. $this->builder->tools()->disableView($disable);
  1210. return $this;
  1211. }
  1212. /**
  1213. * Disable `list` tool.
  1214. *
  1215. * @return $this
  1216. */
  1217. public function disableListButton(bool $disable = true)
  1218. {
  1219. $this->builder->tools()->disableList($disable);
  1220. return $this;
  1221. }
  1222. /**
  1223. * Disable `delete` tool.
  1224. *
  1225. * @return $this
  1226. */
  1227. public function disableDeleteButton(bool $disable = true)
  1228. {
  1229. $this->builder->tools()->disableDelete($disable);
  1230. return $this;
  1231. }
  1232. /**
  1233. * Footer setting for form.
  1234. *
  1235. * @param Closure $callback
  1236. *
  1237. * @return $this
  1238. */
  1239. public function footer(Closure $callback)
  1240. {
  1241. call_user_func($callback, $this->builder->footer());
  1242. return $this;
  1243. }
  1244. /**
  1245. * Get current resource route url.
  1246. *
  1247. * @param int $slice
  1248. *
  1249. * @return string
  1250. */
  1251. public function getResource($slice = -2)
  1252. {
  1253. $path = $this->resource ?: $this->request->getUri();
  1254. $segments = explode('/', trim($path, '/'));
  1255. if ($slice != 0) {
  1256. $segments = array_slice($segments, 0, $slice);
  1257. }
  1258. return url(implode('/', $segments));
  1259. }
  1260. /**
  1261. * Set resource path.
  1262. *
  1263. * @param string $resource
  1264. *
  1265. * @return $this
  1266. */
  1267. public function resource(string $resource)
  1268. {
  1269. if ($resource) {
  1270. $this->resource = admin_url($resource);
  1271. }
  1272. return $this;
  1273. }
  1274. /**
  1275. * Render the form contents.
  1276. *
  1277. * @return string
  1278. */
  1279. public function render()
  1280. {
  1281. try {
  1282. $this->rendering();
  1283. $this->callComposing();
  1284. return $this->builder->render();
  1285. } catch (\Throwable $e) {
  1286. return Admin::makeExceptionHandler()->handle($e);
  1287. }
  1288. }
  1289. /**
  1290. * Get or set input data.
  1291. *
  1292. * @param string $key
  1293. * @param null $value
  1294. *
  1295. * @return array|mixed
  1296. */
  1297. public function input($key, $value = null)
  1298. {
  1299. if (is_null($value)) {
  1300. return Arr::get($this->inputs, $key);
  1301. }
  1302. return Arr::set($this->inputs, $key, $value);
  1303. }
  1304. /**
  1305. * @param string|array $keys
  1306. *
  1307. * @return void
  1308. */
  1309. public function deleteInput($keys)
  1310. {
  1311. Arr::forget($this->inputs, $keys);
  1312. }
  1313. /**
  1314. * @param int $width
  1315. * @param Closure $callback
  1316. *
  1317. * @return $this
  1318. */
  1319. public function block(int $width, \Closure $callback)
  1320. {
  1321. $layout = $this->builder->layout();
  1322. $callback($form = $layout->form());
  1323. $layout->column($width, $form);
  1324. return $this;
  1325. }
  1326. /**
  1327. * @param int|float $width
  1328. * @param Closure $callback
  1329. *
  1330. * @return $this
  1331. */
  1332. public function column($width, \Closure $callback)
  1333. {
  1334. $this->builder->layout()->onlyColumn($width, function () use ($callback) {
  1335. $callback($this);
  1336. });
  1337. return $this;
  1338. }
  1339. /**
  1340. * @param int $width
  1341. *
  1342. * @return $this
  1343. */
  1344. public function setDefaultBlockWidth(int $width)
  1345. {
  1346. $this->builder->setDefaultBlockWidth($width);
  1347. return $this;
  1348. }
  1349. /**
  1350. * @return $this
  1351. */
  1352. protected function prepareDialogForm()
  1353. {
  1354. DialogForm::prepare($this);
  1355. return $this;
  1356. }
  1357. /**
  1358. * @param Closure $callback
  1359. *
  1360. * @return bool|void
  1361. */
  1362. public function inDialog(\Closure $callback = null)
  1363. {
  1364. if (! $callback) {
  1365. return DialogForm::is();
  1366. }
  1367. if (DialogForm::is()) {
  1368. $callback($this);
  1369. }
  1370. }
  1371. /**
  1372. * Create a dialog form.
  1373. *
  1374. * @param string|null $title
  1375. *
  1376. * @return DialogForm
  1377. */
  1378. public static function dialog(?string $title = null)
  1379. {
  1380. return new DialogForm($title);
  1381. }
  1382. /**
  1383. * Register custom field.
  1384. *
  1385. * @param string $abstract
  1386. * @param string $class
  1387. *
  1388. * @return void
  1389. */
  1390. public static function extend($abstract, $class)
  1391. {
  1392. static::$availableFields[$abstract] = $class;
  1393. }
  1394. /**
  1395. * @return array
  1396. */
  1397. public static function extensions()
  1398. {
  1399. return static::$availableFields;
  1400. }
  1401. /**
  1402. * Set form field alias.
  1403. *
  1404. * @param string $field
  1405. * @param string $alias
  1406. *
  1407. * @return void
  1408. */
  1409. public static function alias($field, $alias)
  1410. {
  1411. static::$fieldAlias[$alias] = $field;
  1412. }
  1413. /**
  1414. * Find field class.
  1415. *
  1416. * @param string $method
  1417. *
  1418. * @return bool|mixed
  1419. */
  1420. public static function findFieldClass($method)
  1421. {
  1422. // If alias exists.
  1423. if (isset(static::$fieldAlias[$method])) {
  1424. $method = static::$fieldAlias[$method];
  1425. }
  1426. $class = Arr::get(static::$availableFields, $method);
  1427. if (class_exists($class)) {
  1428. return $class;
  1429. }
  1430. return false;
  1431. }
  1432. /**
  1433. * Getter.
  1434. *
  1435. * @param string $name
  1436. *
  1437. * @return array|mixed
  1438. */
  1439. public function __get($name)
  1440. {
  1441. return $this->input($name);
  1442. }
  1443. /**
  1444. * Setter.
  1445. *
  1446. * @param string $name
  1447. * @param mixed $value
  1448. */
  1449. public function __set($name, $value)
  1450. {
  1451. return Arr::set($this->inputs, $name, $value);
  1452. }
  1453. /**
  1454. * Generate a Field object and add to form builder if Field exists.
  1455. *
  1456. * @param string $method
  1457. * @param array $arguments
  1458. *
  1459. * @return Field
  1460. */
  1461. public function __call($method, $arguments)
  1462. {
  1463. if (static::hasMacro($method)) {
  1464. return $this->macroCall($method, $arguments);
  1465. }
  1466. if ($className = static::findFieldClass($method)) {
  1467. $column = Arr::get($arguments, 0, '');
  1468. $element = new $className($column, array_slice($arguments, 1));
  1469. $this->pushField($element);
  1470. return $element;
  1471. }
  1472. admin_error('Error', "Field type [$method] does not exist.");
  1473. return new Field\Nullable();
  1474. }
  1475. }