HasFiles.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace Dcat\Admin\Form\Concerns;
  3. use Dcat\Admin\Contracts\UploadField as UploadFieldInterface;
  4. use Dcat\Admin\Form\Builder;
  5. use Dcat\Admin\Form\Field;
  6. use Dcat\Admin\Form\NestedForm;
  7. use Dcat\Admin\Support\WebUploader;
  8. use Symfony\Component\HttpFoundation\File\UploadedFile;
  9. use Symfony\Component\HttpFoundation\Response;
  10. /**
  11. * @property Builder $builder
  12. */
  13. trait HasFiles
  14. {
  15. /**
  16. * 文件上传操作.
  17. *
  18. * @param array $data
  19. *
  20. * @return Response|void
  21. */
  22. protected function handleUploadFile($data)
  23. {
  24. $column = $data['upload_column'] ?? null;
  25. $file = app('admin.web-uploader')->getCompleteUploadedFile() ?: ($data[WebUploader::FILE_NAME] ?? null);
  26. if (! $column || ! $file instanceof UploadedFile) {
  27. return;
  28. }
  29. $relation = $data['_relation'] ?? null;
  30. if (empty($relation)) {
  31. $field = $this->findFieldByName($column);
  32. } else {
  33. // hasMany表单文件上传
  34. $relation = explode(',', $relation)[0];
  35. $field = $this->getFieldByRelationName($relation, $column);
  36. }
  37. if ($field && $field instanceof UploadFieldInterface) {
  38. if (($results = $this->callUploading($field, $file)) && $results instanceof Response) {
  39. return $results;
  40. }
  41. $response = $field->upload($file);
  42. if (($results = $this->callUploaded($field, $file, $response)) && $results instanceof Response) {
  43. return $results;
  44. }
  45. return $response;
  46. }
  47. }
  48. /**
  49. * 根据字段名称查找字段.
  50. *
  51. * @param string|null $column
  52. *
  53. * @return Field|null
  54. */
  55. public function findFieldByName(?string $column)
  56. {
  57. if ($field = $this->builder->field($column)) {
  58. return $field;
  59. }
  60. return $this->builder->field($column) ?: $this->builder->stepField($column);
  61. }
  62. /**
  63. * 新增之前删除文件操作.
  64. *
  65. * @param array $data
  66. *
  67. * @return \Illuminate\Http\JsonResponse|void
  68. */
  69. protected function handleFileDeleteBeforeCreate(array $data)
  70. {
  71. if (! array_key_exists(Field::FILE_DELETE_FLAG, $data)) {
  72. return;
  73. }
  74. $column = $data['_column'] ?? null;
  75. $file = $data['key'] ?? null;
  76. $relation = $data['_relation'] ?? null;
  77. if (! $column && ! $file) {
  78. return;
  79. }
  80. if (empty($relation)) {
  81. $field = $this->builder->field($column) ?: $this->builder->stepField($column);
  82. } else {
  83. $field = $this->getFieldByRelationName($relation[0], $column);
  84. }
  85. if ($field && $field instanceof UploadFieldInterface) {
  86. $field->deleteFile($file);
  87. return response()->json(['status' => true]);
  88. }
  89. }
  90. /**
  91. * 获取hasMany的子表单字段.
  92. *
  93. * @param string $relation
  94. * @param string $column
  95. *
  96. * @return mixed
  97. */
  98. protected function getFieldByRelationName($relation, $column)
  99. {
  100. $relation = $this->findFieldByName($relation);
  101. if ($relation && $relation instanceof Field\HasMany) {
  102. return $relation->buildNestedForm()->fields()->first(function ($field) use ($column) {
  103. return $field->column() === $column;
  104. });
  105. }
  106. }
  107. /**
  108. * @param array $input
  109. *
  110. * @return void
  111. */
  112. public function deleteFilesWhenCreating(array $input)
  113. {
  114. $this->builder
  115. ->fields()
  116. ->filter(function ($field) {
  117. return $field instanceof UploadFieldInterface;
  118. })
  119. ->each(function (UploadFieldInterface $file) use ($input) {
  120. $file->setOriginal($input);
  121. $file->destroy();
  122. });
  123. }
  124. /**
  125. * 根据传入数据删除文件.
  126. *
  127. * @param array $data
  128. * @param bool $forceDelete
  129. */
  130. public function deleteFiles($data, $forceDelete = false)
  131. {
  132. // If it's a soft delete, the files in the data will not be deleted.
  133. if (! $forceDelete && $this->isSoftDeletes) {
  134. return;
  135. }
  136. $this->builder->fields()->filter(function ($field) {
  137. return $field instanceof Field\BootstrapFile
  138. || $field instanceof UploadFieldInterface;
  139. })->each(function (UploadFieldInterface $file) use ($data) {
  140. $file->setOriginal($data);
  141. $file->destroy();
  142. });
  143. }
  144. /**
  145. * 编辑页面删除上传文件操作.
  146. *
  147. * @param array $input
  148. *
  149. * @return array
  150. */
  151. protected function handleFileDelete(array $input = [])
  152. {
  153. if (! array_key_exists(Field::FILE_DELETE_FLAG, $input)) {
  154. return $input;
  155. }
  156. $input[Field::FILE_DELETE_FLAG] = $input['key'];
  157. if (! empty($input['_column'])) {
  158. if (empty($input['_relation'])) {
  159. $input[$input['_column']] = '';
  160. } else {
  161. [$relation, $relationKey] = $input['_relation'];
  162. $keyName = $this->builder()->field($relation)->getKeyName();
  163. $input[$relation] = [
  164. $relationKey => [
  165. $keyName => $relationKey,
  166. $input['_column'] => '',
  167. NestedForm::REMOVE_FLAG_NAME => null,
  168. ],
  169. ];
  170. }
  171. }
  172. unset($input['key'], $input['_column'], $input['_relation']);
  173. $this->request->replace($input);
  174. return $input;
  175. }
  176. /**
  177. * @param array $input
  178. *
  179. * @return \Illuminate\Http\JsonResponse
  180. */
  181. protected function handleFileDeleteWhenCreating(array $input)
  182. {
  183. $input = $this->handleFileDelete($input);
  184. $column = $input['_column'] ?? null;
  185. if (isset($input[Field::FILE_DELETE_FLAG]) && $column) {
  186. $this->builder->fields()->filter(function ($field) use ($column) {
  187. /* @var Field $field */
  188. return $column === $field->column() && $field instanceof UploadFieldInterface;
  189. })->each(function (UploadFieldInterface $file) use ($input) {
  190. $file->deleteFile($input[Field::FILE_DELETE_FLAG]);
  191. });
  192. return \response()->json(['status' => true]);
  193. }
  194. }
  195. }