EmbeddedForm.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Collection;
  7. /**
  8. * Class EmbeddedForm.
  9. *
  10. * @method Field\Text text($column, $label = '')
  11. * @method Field\Checkbox checkbox($column, $label = '')
  12. * @method Field\Radio radio($column, $label = '')
  13. * @method Field\Select select($column, $label = '')
  14. * @method Field\MultipleSelect multipleSelect($column, $label = '')
  15. * @method Field\Textarea textarea($column, $label = '')
  16. * @method Field\Hidden hidden($column, $label = '')
  17. * @method Field\Id id($column, $label = '')
  18. * @method Field\Ip ip($column, $label = '')
  19. * @method Field\Url url($column, $label = '')
  20. * @method Field\Color color($column, $label = '')
  21. * @method Field\Email email($column, $label = '')
  22. * @method Field\Mobile mobile($column, $label = '')
  23. * @method Field\Slider slider($column, $label = '')
  24. * @method Field\Map map($latitude, $longitude, $label = '')
  25. * @method Field\Editor editor($column, $label = '')
  26. * @method Field\Date date($column, $label = '')
  27. * @method Field\Datetime datetime($column, $label = '')
  28. * @method Field\Time time($column, $label = '')
  29. * @method Field\Year year($column, $label = '')
  30. * @method Field\Month month($column, $label = '')
  31. * @method Field\DateRange dateRange($start, $end, $label = '')
  32. * @method Field\DateTimeRange datetimeRange($start, $end, $label = '')
  33. * @method Field\TimeRange timeRange($start, $end, $label = '')
  34. * @method Field\Number number($column, $label = '')
  35. * @method Field\Currency currency($column, $label = '')
  36. * @method Field\HasMany hasMany($relationName, $callback)
  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($column, $label = '')
  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\Tree tree($column, $label = '')
  55. *
  56. * @method Field\BootstrapFile bootstrapFile($column, $label = '')
  57. * @method Field\BootstrapImage bootstrapImage($column, $label = '')
  58. * @method Field\BootstrapMultipleImage bootstrapMultipleImage($column, $label = '')
  59. * @method Field\BootstrapMultipleFile bootstrapMultipleFile($column, $label = '')
  60. */
  61. class EmbeddedForm
  62. {
  63. /**
  64. * @var Form
  65. */
  66. protected $parent = null;
  67. /**
  68. * Fields in form.
  69. *
  70. * @var Collection
  71. */
  72. protected $fields;
  73. /**
  74. * Original data for this field.
  75. *
  76. * @var array
  77. */
  78. protected $original = [];
  79. /**
  80. * Column name for this form.
  81. *
  82. * @var string
  83. */
  84. protected $column;
  85. /**
  86. * EmbeddedForm constructor.
  87. *
  88. * @param string $column
  89. */
  90. public function __construct($column)
  91. {
  92. $this->column = $column;
  93. $this->fields = new Collection();
  94. }
  95. /**
  96. * Get all fields in current form.
  97. *
  98. * @return Collection
  99. */
  100. public function fields()
  101. {
  102. return $this->fields;
  103. }
  104. /**
  105. * Set parent form for this form.
  106. *
  107. * @param Form $parent
  108. *
  109. * @return $this
  110. */
  111. public function setParent(Form $parent)
  112. {
  113. $this->parent = $parent;
  114. return $this;
  115. }
  116. /**
  117. * Set original values for fields.
  118. *
  119. * @param array $data
  120. *
  121. * @return $this
  122. */
  123. public function setOriginal($data)
  124. {
  125. if (empty($data)) {
  126. return $this;
  127. }
  128. if (is_string($data)) {
  129. $data = json_decode($data, true);
  130. }
  131. $this->original = $data;
  132. return $this;
  133. }
  134. /**
  135. * Prepare for insert or update.
  136. *
  137. * @param array $input
  138. *
  139. * @return mixed
  140. */
  141. public function prepare($input)
  142. {
  143. foreach ($input as $key => $record) {
  144. $this->setFieldOriginalValue($key);
  145. $input[$key] = $this->prepareValue($key, $record);
  146. }
  147. return $input;
  148. }
  149. /**
  150. * Do prepare work for each field.
  151. *
  152. * @param string $key
  153. * @param string $record
  154. *
  155. * @return mixed
  156. */
  157. protected function prepareValue($key, $record)
  158. {
  159. $field = $this->fields->first(function (Field $field) use ($key) {
  160. return in_array($key, (array) $field->column());
  161. });
  162. if (method_exists($field, 'prepareInputValue')) {
  163. return $field->prepareInputValue($record);
  164. }
  165. return $record;
  166. }
  167. /**
  168. * Set original data for each field.
  169. *
  170. * @param string $key
  171. *
  172. * @return void
  173. */
  174. protected function setFieldOriginalValue($key)
  175. {
  176. if (array_key_exists($key, $this->original)) {
  177. $values = $this->original[$key];
  178. $this->fields->each(function (Field $field) use ($values) {
  179. $field->setOriginal($values);
  180. });
  181. }
  182. }
  183. /**
  184. * Fill data to all fields in form.
  185. *
  186. * @param array $data
  187. *
  188. * @return $this
  189. */
  190. public function fill(array $data)
  191. {
  192. $this->fields->each(function (Field $field) use ($data) {
  193. $field->fill($data);
  194. });
  195. return $this;
  196. }
  197. /**
  198. * Format form, set `element name` `error key` and `element class`.
  199. *
  200. * @param Field $field
  201. *
  202. * @return Field
  203. */
  204. protected function formatField(Field $field)
  205. {
  206. $jsonKey = $field->column();
  207. $elementName = $elementClass = $errorKey = [];
  208. if (is_array($jsonKey)) {
  209. foreach ($jsonKey as $index => $name) {
  210. $elementName[$index] = "{$this->column}[$name]";
  211. $errorKey[$index] = "{$this->column}.$name";
  212. $elementClass[$index] = "{$this->column}_$name";
  213. }
  214. } else {
  215. $elementName = "{$this->column}[$jsonKey]";
  216. $errorKey = "{$this->column}.$jsonKey";
  217. $elementClass = "{$this->column}_$jsonKey";
  218. }
  219. $field->setElementName($elementName)
  220. ->setErrorKey($errorKey)
  221. ->setElementClass($elementClass);
  222. return $field;
  223. }
  224. /**
  225. * Add a field to form.
  226. *
  227. * @param Field $field
  228. *
  229. * @return $this
  230. */
  231. public function pushField(Field $field)
  232. {
  233. $field = $this->formatField($field);
  234. $this->fields->push($field);
  235. $field::collectAssets();
  236. return $this;
  237. }
  238. /**
  239. * Add nested-form fields dynamically.
  240. *
  241. * @param string $method
  242. * @param array $arguments
  243. *
  244. * @return Field|$this
  245. */
  246. public function __call($method, $arguments)
  247. {
  248. if ($className = Form::findFieldClass($method)) {
  249. $column = Arr::get($arguments, 0, '');
  250. /** @var Field $field */
  251. $field = new $className($column, array_slice($arguments, 1));
  252. $field->setForm($this->parent);
  253. $this->pushField($field);
  254. return $field;
  255. }
  256. return $this;
  257. }
  258. }