EmbeddedForm.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Support\Helper;
  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\Email email($column, $label = '')
  21. * @method Field\Mobile mobile($column, $label = '')
  22. * @method Field\Slider slider($column, $label = '')
  23. * @method Field\Map map($latitude, $longitude, $label = '')
  24. * @method Field\Editor editor($column, $label = '')
  25. * @method Field\Date date($column, $label = '')
  26. * @method Field\Datetime datetime($column, $label = '')
  27. * @method Field\Time time($column, $label = '')
  28. * @method Field\Year year($column, $label = '')
  29. * @method Field\Month month($column, $label = '')
  30. * @method Field\DateRange dateRange($start, $end, $label = '')
  31. * @method Field\DateTimeRange datetimeRange($start, $end, $label = '')
  32. * @method Field\TimeRange timeRange($start, $end, $label = '')
  33. * @method Field\Number number($column, $label = '')
  34. * @method Field\Currency currency($column, $label = '')
  35. * @method Field\SwitchField switch($column, $label = '')
  36. * @method Field\Display display($column, $label = '')
  37. * @method Field\Rate rate($column, $label = '')
  38. * @method Field\Divide divider(string $title = null)
  39. * @method Field\Password password($column, $label = '')
  40. * @method Field\Decimal decimal($column, $label = '')
  41. * @method Field\Html html($html, $label = '')
  42. * @method Field\Tags tags($column, $label = '')
  43. * @method Field\Icon icon($column, $label = '')
  44. * @method Field\Embeds embeds($column, $label = '')
  45. * @method Field\Captcha captcha()
  46. * @method Field\Listbox listbox($column, $label = '')
  47. * @method Field\File file($column, $label = '')
  48. * @method Field\Image image($column, $label = '')
  49. * @method Field\MultipleFile multipleFile($column, $label = '')
  50. * @method Field\MultipleImage multipleImage($column, $label = '')
  51. * @method Field\Tree tree($column, $label = '')
  52. * @method Field\Table table($column, $labelOrCallback, $callback = null)
  53. * @method Field\ListField list($column, $label = '')
  54. * @method Field\Timezone timezone($column, $label = '')
  55. * @method Field\KeyValue keyValue($column, $label = '')
  56. * @method Field\Tel tel($column, $label = '')
  57. * @method Field\Markdown markdown($column, $label = '')
  58. * @method Field\Range range($start, $end, $label = '')
  59. * @method Field\Color color($column, $label = '')
  60. * @method Field\ArrayField array($column, $labelOrCallback, $callback = null)
  61. * @method Field\SelectTable selectTable($column, $label = '')
  62. * @method Field\MultipleSelectTable multipleSelectTable($column, $label = '')
  63. */
  64. class EmbeddedForm
  65. {
  66. /**
  67. * @var Form
  68. */
  69. protected $parent = null;
  70. /**
  71. * Fields in form.
  72. *
  73. * @var Collection
  74. */
  75. protected $fields;
  76. /**
  77. * Original data for this field.
  78. *
  79. * @var array
  80. */
  81. protected $original = [];
  82. /**
  83. * Column name for this form.
  84. *
  85. * @var string
  86. */
  87. protected $column;
  88. /**
  89. * EmbeddedForm constructor.
  90. *
  91. * @param string $column
  92. */
  93. public function __construct($column)
  94. {
  95. $this->column = $column;
  96. $this->fields = new Collection();
  97. }
  98. /**
  99. * Get all fields in current form.
  100. *
  101. * @return Collection
  102. */
  103. public function fields()
  104. {
  105. return $this->fields;
  106. }
  107. /**
  108. * Set parent form for this form.
  109. *
  110. * @param Form $parent
  111. *
  112. * @return $this
  113. */
  114. public function setParent($parent)
  115. {
  116. $this->parent = $parent;
  117. return $this;
  118. }
  119. public function getKey()
  120. {
  121. return $this->parent->getKey();
  122. }
  123. public function model()
  124. {
  125. return $this->parent->model();
  126. }
  127. /**
  128. * Set original values for fields.
  129. *
  130. * @param array $data
  131. *
  132. * @return $this
  133. */
  134. public function setOriginal($data)
  135. {
  136. if (empty($data)) {
  137. return $this;
  138. }
  139. if (is_string($data)) {
  140. $data = json_decode($data, true);
  141. }
  142. $this->original = $data;
  143. return $this;
  144. }
  145. /**
  146. * Prepare for insert or update.
  147. *
  148. * @param array $input
  149. *
  150. * @return mixed
  151. */
  152. public function prepare($input)
  153. {
  154. foreach ($input as $key => $record) {
  155. $this->setFieldOriginalValue($key);
  156. $input[$key] = $this->prepareValue($key, $record);
  157. }
  158. return $input;
  159. }
  160. /**
  161. * Do prepare work for each field.
  162. *
  163. * @param string $key
  164. * @param string $record
  165. *
  166. * @return mixed
  167. */
  168. protected function prepareValue($key, $record)
  169. {
  170. $field = $this->fields->first(function (Field $field) use ($key) {
  171. return in_array($key, (array) $field->column());
  172. });
  173. if (method_exists($field, 'prepare')) {
  174. return $field->prepare($record);
  175. }
  176. return $record;
  177. }
  178. /**
  179. * Set original data for each field.
  180. *
  181. * @param string $key
  182. *
  183. * @return void
  184. */
  185. protected function setFieldOriginalValue($key)
  186. {
  187. if (Helper::keyExists($key, $this->original)) {
  188. $values = $this->original[$key];
  189. $this->fields->each(function (Field $field) use ($values) {
  190. $field->setOriginal($values);
  191. });
  192. }
  193. }
  194. /**
  195. * Fill data to all fields in form.
  196. *
  197. * @param array $data
  198. *
  199. * @return $this
  200. */
  201. public function fill(array $data)
  202. {
  203. $this->fields->each(function (Field $field) use ($data) {
  204. $field->fill($data);
  205. });
  206. return $this;
  207. }
  208. /**
  209. * Format form, set `element name` `error key` and `element class`.
  210. *
  211. * @param Field $field
  212. *
  213. * @return Field
  214. */
  215. protected function formatField(Field $field)
  216. {
  217. $jsonKey = $field->column();
  218. $elementName = $elementClass = $errorKey = [];
  219. if (is_array($jsonKey)) {
  220. foreach ($jsonKey as $index => $name) {
  221. $elementName[$index] = $this->formatName("{$this->column}.{$name}");
  222. $errorKey[$index] = "{$this->column}.$name";
  223. $elementClass[$index] = $this->formatClass("{$this->column}_$name");
  224. }
  225. } else {
  226. $elementName = $this->formatName("{$this->column}.$jsonKey");
  227. $errorKey = "{$this->column}.$jsonKey";
  228. $elementClass = $this->formatClass("{$this->column}_$jsonKey");
  229. }
  230. $field->setElementName($elementName)
  231. ->setErrorKey($errorKey)
  232. ->setElementClass($elementClass);
  233. return $field;
  234. }
  235. protected function formatName($name)
  236. {
  237. return Helper::formatElementName($name);
  238. }
  239. protected function formatClass(string $column)
  240. {
  241. return str_replace('.', '-', $column);
  242. }
  243. /**
  244. * Add a field to form.
  245. *
  246. * @param Field $field
  247. *
  248. * @return $this
  249. */
  250. public function pushField(Field $field)
  251. {
  252. $field = $this->formatField($field);
  253. $this->fields->push($field);
  254. $field::requireAssets();
  255. return $this;
  256. }
  257. /**
  258. * Add nested-form fields dynamically.
  259. *
  260. * @param string $method
  261. * @param array $arguments
  262. *
  263. * @return Field|$this
  264. */
  265. public function __call($method, $arguments)
  266. {
  267. if ($className = Form::findFieldClass($method)) {
  268. $column = Arr::get($arguments, 0, '');
  269. /** @var Field $field */
  270. $field = new $className($column, array_slice($arguments, 1));
  271. $field->setForm($this->parent);
  272. $this->pushField($field);
  273. return $field;
  274. }
  275. return $this;
  276. }
  277. }