NestedForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Contracts\UploadField;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Widgets\Form as WidgetForm;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Collection;
  9. /**
  10. * Class Form.
  11. *
  12. * @method Field\Text text($column, $label = '')
  13. * @method Field\Checkbox checkbox($column, $label = '')
  14. * @method Field\Radio radio($column, $label = '')
  15. * @method Field\Select select($column, $label = '')
  16. * @method Field\MultipleSelect multipleSelect($column, $label = '')
  17. * @method Field\Textarea textarea($column, $label = '')
  18. * @method Field\Hidden hidden($column, $label = '')
  19. * @method Field\Id id($column, $label = '')
  20. * @method Field\Ip ip($column, $label = '')
  21. * @method Field\Url url($column, $label = '')
  22. * @method Field\Email email($column, $label = '')
  23. * @method Field\Mobile mobile($column, $label = '')
  24. * @method Field\Slider slider($column, $label = '')
  25. * @method Field\Map map($latitude, $longitude, $label = '')
  26. * @method Field\Editor editor($column, $label = '')
  27. * @method Field\Date date($column, $label = '')
  28. * @method Field\Datetime datetime($column, $label = '')
  29. * @method Field\Time time($column, $label = '')
  30. * @method Field\Year year($column, $label = '')
  31. * @method Field\Month month($column, $label = '')
  32. * @method Field\DateRange dateRange($start, $end, $label = '')
  33. * @method Field\DateTimeRange datetimeRange($start, $end, $label = '')
  34. * @method Field\TimeRange timeRange($start, $end, $label = '')
  35. * @method Field\Number number($column, $label = '')
  36. * @method Field\Currency currency($column, $label = '')
  37. * @method Field\SwitchField switch($column, $label = '')
  38. * @method Field\Display display($column, $label = '')
  39. * @method Field\Rate rate($column, $label = '')
  40. * @method Field\Divide divider()
  41. * @method Field\Password password($column, $label = '')
  42. * @method Field\Decimal decimal($column, $label = '')
  43. * @method Field\Html html($html, $label = '')
  44. * @method Field\Tags tags($column, $label = '')
  45. * @method Field\Icon icon($column, $label = '')
  46. * @method Field\Embeds embeds($column, $label = '')
  47. * @method Field\Captcha captcha()
  48. * @method Field\Listbox listbox($column, $label = '')
  49. * @method Field\SelectResource selectResource($column, $label = '')
  50. * @method Field\File file($column, $label = '')
  51. * @method Field\Image image($column, $label = '')
  52. * @method Field\MultipleFile multipleFile($column, $label = '')
  53. * @method Field\MultipleImage multipleImage($column, $label = '')
  54. * @method Field\HasMany hasMany($column, $labelOrCallback, $callback = null)
  55. * @method Field\Tree tree($column, $label = '')
  56. * @method Field\Table table($column, $labelOrCallback, $callback = null)
  57. * @method Field\ListField list($column, $label = '')
  58. * @method Field\Timezone timezone($column, $label = '')
  59. * @method Field\KeyValue keyValue($column, $label = '')
  60. * @method Field\Tel tel($column, $label = '')
  61. */
  62. class NestedForm
  63. {
  64. const DEFAULT_KEY_NAME = '__LA_KEY__';
  65. const REMOVE_FLAG_NAME = '_remove_';
  66. const REMOVE_FLAG_CLASS = 'fom-removed';
  67. /**
  68. * @var string
  69. */
  70. protected $relationName;
  71. /**
  72. * NestedForm key.
  73. *
  74. * @var
  75. */
  76. protected $key;
  77. /**
  78. * Fields in form.
  79. *
  80. * @var Collection
  81. */
  82. protected $fields;
  83. /**
  84. * Original data for this field.
  85. *
  86. * @var array
  87. */
  88. protected $original = [];
  89. /**
  90. * @var Form|WidgetForm
  91. */
  92. protected $form;
  93. /**
  94. * Create a new NestedForm instance.
  95. *
  96. * NestedForm constructor.
  97. *
  98. * @param string $relation
  99. * @param null $key
  100. */
  101. public function __construct($relation, $key = null)
  102. {
  103. $this->relationName = $relation;
  104. $this->key = $key;
  105. $this->fields = new Collection();
  106. }
  107. /**
  108. * Set Form.
  109. *
  110. * @param Form|WidgetForm $form
  111. *
  112. * @return $this
  113. */
  114. public function setForm($form = null)
  115. {
  116. $this->form = $form;
  117. return $this;
  118. }
  119. /**
  120. * Get form.
  121. *
  122. * @return Form
  123. */
  124. public function getForm()
  125. {
  126. return $this->form;
  127. }
  128. /**
  129. * Set original values for fields.
  130. *
  131. * @param array $data
  132. * @param string $relatedKeyName
  133. *
  134. * @return $this
  135. */
  136. public function setOriginal($data, $relatedKeyName)
  137. {
  138. if (empty($data)) {
  139. return $this;
  140. }
  141. foreach ($data as $value) {
  142. if (! isset($value[$relatedKeyName])) {
  143. continue;
  144. }
  145. /*
  146. * like $this->original[30] = [ id = 30, .....]
  147. */
  148. $this->original[$value[$relatedKeyName]] = $value;
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Prepare for insert or update.
  154. *
  155. * @param array $input
  156. *
  157. * @return mixed
  158. */
  159. public function prepare($input)
  160. {
  161. foreach ($input as $key => $record) {
  162. if (! array_key_exists(static::REMOVE_FLAG_NAME, $record)) {
  163. continue;
  164. }
  165. $this->setFieldOriginalValue($key);
  166. $input[$key] = $this->prepareRecord($record);
  167. }
  168. return $input;
  169. }
  170. /**
  171. * Get key for current form.
  172. *
  173. * @return string
  174. */
  175. public function getKey()
  176. {
  177. return $this->key;
  178. }
  179. /**
  180. * Set key for current form.
  181. *
  182. * @param mixed $key
  183. *
  184. * @return $this
  185. */
  186. public function setKey($key)
  187. {
  188. $this->key = $key;
  189. return $this;
  190. }
  191. /**
  192. * Set original data for each field.
  193. *
  194. * @param string $key
  195. *
  196. * @return void
  197. */
  198. protected function setFieldOriginalValue($key)
  199. {
  200. $values = [];
  201. if (array_key_exists($key, $this->original)) {
  202. $values = $this->original[$key];
  203. }
  204. $this->fields->each(function (Field $field) use ($values) {
  205. $field->setOriginal($values);
  206. });
  207. }
  208. /**
  209. * Do prepare work before store and update.
  210. *
  211. * @param array $record
  212. *
  213. * @return array
  214. */
  215. protected function prepareRecord($record)
  216. {
  217. if ($record[static::REMOVE_FLAG_NAME] == 1) {
  218. return $record;
  219. }
  220. $prepared = [];
  221. /* @var Field $field */
  222. foreach ($this->fields as $field) {
  223. $columns = $field->column();
  224. $value = $this->fetchColumnValue($record, $columns);
  225. if (is_null($value)) {
  226. continue;
  227. }
  228. if (method_exists($field, 'prepare')) {
  229. $value = $field->prepare($value);
  230. }
  231. if (($field instanceof Form\Field\Hidden) || $value != $field->original()) {
  232. if (is_array($columns)) {
  233. foreach ($columns as $name => $column) {
  234. Arr::set($prepared, $column, $value[$name]);
  235. }
  236. } elseif (is_string($columns)) {
  237. Arr::set($prepared, $columns, $value);
  238. }
  239. }
  240. }
  241. $prepared[static::REMOVE_FLAG_NAME] = $record[static::REMOVE_FLAG_NAME];
  242. return $prepared;
  243. }
  244. /**
  245. * Fetch value in input data by column name.
  246. *
  247. * @param array $data
  248. * @param string|array $columns
  249. *
  250. * @return array|mixed
  251. */
  252. protected function fetchColumnValue($data, $columns)
  253. {
  254. if (is_string($columns)) {
  255. return Arr::get($data, $columns);
  256. }
  257. if (is_array($columns)) {
  258. $value = [];
  259. foreach ($columns as $name => $column) {
  260. if (! Arr::has($data, $column)) {
  261. continue;
  262. }
  263. $value[$name] = Arr::get($data, $column);
  264. }
  265. return $value;
  266. }
  267. }
  268. /**
  269. * @param Field $field
  270. *
  271. * @return $this
  272. */
  273. public function pushField(Field $field)
  274. {
  275. $this->fields->push($field);
  276. if (method_exists($this->form, 'builder')) {
  277. $this->form->builder()->fields()->push($field);
  278. $field->attribute(Builder::BUILD_IGNORE, true);
  279. }
  280. if ($field instanceof UploadField) {
  281. $field->setRelation($this->relationName);
  282. }
  283. $field::collectAssets();
  284. return $this;
  285. }
  286. /**
  287. * Get fields of this form.
  288. *
  289. * @return Collection
  290. */
  291. public function fields()
  292. {
  293. return $this->fields;
  294. }
  295. /**
  296. * Fill data to all fields in form.
  297. *
  298. * @param array $data
  299. *
  300. * @return $this
  301. */
  302. public function fill(array $data)
  303. {
  304. /* @var Field $field */
  305. foreach ($this->fields() as $field) {
  306. $field->fill($data);
  307. }
  308. return $this;
  309. }
  310. /**
  311. * Get the html and script of template.
  312. *
  313. * @return array
  314. */
  315. public function getTemplateHtmlAndScript()
  316. {
  317. $html = '';
  318. $scripts = [];
  319. /* @var Field $field */
  320. foreach ($this->fields() as $field) {
  321. //when field render, will push $script to Admin
  322. $html .= $field->render();
  323. /*
  324. * Get and remove the last script of Admin::$script stack.
  325. */
  326. if ($field->getScript()) {
  327. $scripts[] = array_pop(Admin::asset()->script);
  328. }
  329. }
  330. return [$html, implode("\r\n", $scripts)];
  331. }
  332. /**
  333. * Set `errorKey` `elementName` `elementClass` for fields inside hasmany fields.
  334. *
  335. * @param Field $field
  336. *
  337. * @return Field
  338. */
  339. protected function formatField(Field $field)
  340. {
  341. $column = $field->column();
  342. $elementName = $elementClass = $errorKey = [];
  343. $key = $this->key ?: 'new_'.static::DEFAULT_KEY_NAME;
  344. if (is_array($column)) {
  345. foreach ($column as $k => $name) {
  346. $errorKey[$k] = sprintf('%s.%s.%s', $this->relationName, $key, $name);
  347. $elementName[$k] = sprintf('%s[%s][%s]', $this->relationName, $key, $name);
  348. $elementClass[$k] = [$this->relationName, $name];
  349. }
  350. } else {
  351. $errorKey = sprintf('%s.%s.%s', $this->relationName, $key, $column);
  352. $elementName = sprintf('%s[%s][%s]', $this->relationName, $key, $column);
  353. $elementClass = [$this->relationName, $column];
  354. }
  355. return $field->setErrorKey($errorKey)
  356. ->setElementName($elementName)
  357. ->setElementClass($elementClass);
  358. }
  359. /**
  360. * Add nested-form fields dynamically.
  361. *
  362. * @param string $method
  363. * @param array $arguments
  364. *
  365. * @return mixed
  366. */
  367. public function __call($method, $arguments)
  368. {
  369. if ($className = Form::findFieldClass($method)) {
  370. $column = Arr::get($arguments, 0, '');
  371. /* @var Field $field */
  372. $field = new $className($column, array_slice($arguments, 1));
  373. $field->setForm($this->form);
  374. $field = $this->formatField($field);
  375. $this->pushField($field);
  376. return $field;
  377. }
  378. return $this;
  379. }
  380. }