HasFiles.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Dcat\Admin\Form\Concerns;
  3. use Symfony\Component\HttpFoundation\File\UploadedFile;
  4. use Dcat\Admin\Form\Field;
  5. trait HasFiles
  6. {
  7. /**
  8. * @param array $data
  9. */
  10. protected function handleUploadFile($data)
  11. {
  12. $column = $data['upload_column'] ?? null;
  13. $file = $data['file'] ?? null;
  14. if (!$column && !$file instanceof UploadedFile) {
  15. return;
  16. }
  17. $field = $this->builder->field($column);
  18. if (!$field || !$field instanceof Field\File) {
  19. return;
  20. }
  21. return $field->upload($file);
  22. }
  23. /**
  24. * @param array $data
  25. * @return \Illuminate\Http\JsonResponse|void
  26. */
  27. protected function handleFileDeleteBeforeCreate(array $data)
  28. {
  29. if (!array_key_exists(Field::FILE_DELETE_FLAG, $data)) {
  30. return;
  31. }
  32. $column = $data['_column'] ?? null;
  33. $file = $data['key'] ?? null;
  34. if (!$column && !$file) {
  35. return;
  36. }
  37. $field = $this->builder->field($column);
  38. if ($field && in_array(Field\UploadField::class, class_uses($field))) {
  39. $field->deleteFile($file);
  40. return response()->json(['status' => true]);
  41. }
  42. }
  43. public function deleteFilesWhenCreating(array $input)
  44. {
  45. $this->builder->fields()->filter(function ($field) {
  46. return $field instanceof Field\File;
  47. })->each(function (Field\File $file) use ($input) {
  48. $file->setOriginal($input);
  49. $file->destroy();
  50. });
  51. }
  52. /**
  53. * Remove files in record.
  54. *
  55. * @param array $data
  56. * @param bool $forceDelete
  57. */
  58. public function deleteFiles($data, $forceDelete = false)
  59. {
  60. // If it's a soft delete, the files in the data will not be deleted.
  61. if (!$forceDelete && $this->isSoftDeletes) {
  62. return;
  63. }
  64. $this->builder->fields()->filter(function ($field) {
  65. return $field instanceof Field\BootstrapFile
  66. || $field instanceof Field\File;
  67. })->each(function ($file) use ($data) {
  68. $file->setOriginal($data);
  69. $file->destroy();
  70. });
  71. }
  72. /**
  73. * @param array $input
  74. *
  75. * @return array
  76. */
  77. protected function handleFileDelete(array $input = [])
  78. {
  79. if (array_key_exists(Field::FILE_DELETE_FLAG, $input)) {
  80. $input[Field::FILE_DELETE_FLAG] = $input['key'];
  81. unset($input['key']);
  82. }
  83. request()->replace($input);
  84. return $input;
  85. }
  86. /**
  87. * @param array $input
  88. * @return \Illuminate\Http\JsonResponse
  89. */
  90. protected function handleFileDeleteWhenCreating(array $input)
  91. {
  92. $input = $this->handleFileDelete($input);
  93. if (isset($input[Field::FILE_DELETE_FLAG])) {
  94. $this->builder->fields()->filter(function ($field) {
  95. return $field instanceof Field\File;
  96. })->each(function (Field\File $file) use ($input) {
  97. $file->deleteFile($input[Field::FILE_DELETE_FLAG]);
  98. });
  99. return \response()->json(['status' => true]);
  100. }
  101. }
  102. }