HasFiles.php 3.5 KB

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