BootstrapMultipleFile.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Form\Field;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Facades\Validator;
  6. use Symfony\Component\HttpFoundation\File\UploadedFile;
  7. class BootstrapMultipleFile extends Field
  8. {
  9. use BootstrapUploadField;
  10. /**
  11. * Css.
  12. *
  13. * @var array
  14. */
  15. protected static $css = [
  16. '/vendor/dcat-admin/bootstrap-fileinput/css/fileinput.min.css?v=4.5.2',
  17. ];
  18. /**
  19. * Js.
  20. *
  21. * @var array
  22. */
  23. protected static $js = [
  24. '/vendor/dcat-admin/bootstrap-fileinput/js/plugins/canvas-to-blob.min.js',
  25. '/vendor/dcat-admin/bootstrap-fileinput/js/fileinput.min.js?v=4.5.2',
  26. ];
  27. /**
  28. * Create a new File instance.
  29. *
  30. * @param string $column
  31. * @param array $arguments
  32. */
  33. public function __construct($column, $arguments = [])
  34. {
  35. $this->initStorage();
  36. parent::__construct($column, $arguments);
  37. }
  38. /**
  39. * Default directory for file to upload.
  40. *
  41. * @return mixed
  42. */
  43. public function defaultDirectory()
  44. {
  45. return config('admin.upload.directory.file');
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getValidator(array $input)
  51. {
  52. if (request()->has(static::FILE_DELETE_FLAG)) {
  53. return false;
  54. }
  55. if ($this->validator) {
  56. return $this->validator->call($this, $input);
  57. }
  58. $attributes = [];
  59. if (! $fieldRules = $this->getRules()) {
  60. return false;
  61. }
  62. $attributes[$this->column] = $this->label;
  63. [$rules, $input] = $this->hydrateFiles(Arr::get($input, $this->column, []));
  64. return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);
  65. }
  66. /**
  67. * Hydrate the files array.
  68. *
  69. * @param array $value
  70. *
  71. * @return array
  72. */
  73. protected function hydrateFiles(array $value)
  74. {
  75. if (empty($value)) {
  76. return [[$this->column => $this->getRules()], []];
  77. }
  78. $rules = $input = [];
  79. foreach ($value as $key => $file) {
  80. $rules[$this->column.$key] = $this->getRules();
  81. $input[$this->column.$key] = $file;
  82. }
  83. return [$rules, $input];
  84. }
  85. /**
  86. * Prepare for saving.
  87. *
  88. * @param UploadedFile|array $files
  89. *
  90. * @return mixed|string
  91. */
  92. protected function prepareToSave($files)
  93. {
  94. if (request()->has(static::FILE_DELETE_FLAG)) {
  95. return $this->destroy(request(static::FILE_DELETE_FLAG));
  96. }
  97. $targets = array_map([$this, 'prepareForeach'], $files);
  98. return array_merge($this->original(), $targets);
  99. }
  100. /**
  101. * @return array|mixed
  102. */
  103. public function original()
  104. {
  105. if (empty($this->original)) {
  106. return [];
  107. }
  108. return $this->original;
  109. }
  110. protected function formatFieldData($data)
  111. {
  112. $value = Arr::get($data, $this->column);
  113. if ($value && is_string($value)) {
  114. return explode(',', $value);
  115. }
  116. return $value ? (array) $value : [];
  117. }
  118. /**
  119. * Prepare for each file.
  120. *
  121. * @param UploadedFile $file
  122. *
  123. * @return mixed|string
  124. */
  125. protected function prepareForeach(UploadedFile $file = null)
  126. {
  127. $this->name = $this->getStoreName($file);
  128. return tap($this->upload($file), function () {
  129. $this->name = null;
  130. });
  131. }
  132. /**
  133. * Preview html for file-upload plugin.
  134. *
  135. * @return array
  136. */
  137. protected function preview()
  138. {
  139. $files = $this->value ?: [];
  140. return array_map([$this, 'objectUrl'], $files);
  141. }
  142. /**
  143. * Initialize the caption.
  144. *
  145. * @param array $caption
  146. *
  147. * @return string
  148. */
  149. protected function initialCaption($caption)
  150. {
  151. if (empty($caption)) {
  152. return '';
  153. }
  154. $caption = array_map('basename', $caption);
  155. return implode(',', $caption);
  156. }
  157. /**
  158. * @return array
  159. */
  160. protected function initialPreviewConfig()
  161. {
  162. $files = $this->value ?: [];
  163. $config = [];
  164. foreach ($files as $index => $file) {
  165. $config[] = [
  166. 'caption' => basename($file),
  167. 'key' => $index,
  168. ];
  169. }
  170. return $config;
  171. }
  172. /**
  173. * Render file upload field.
  174. *
  175. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  176. */
  177. public function render()
  178. {
  179. $this->attribute('multiple', true);
  180. $this->setupDefaultOptions();
  181. if (! empty($this->value())) {
  182. $this->options(['initialPreview' => $this->preview()]);
  183. $this->setupPreviewOptions();
  184. }
  185. $options = json_encode($this->options);
  186. $this->script = <<<JS
  187. $("{$this->getElementClassSelector()}").fileinput({$options});
  188. JS;
  189. return parent::render();
  190. }
  191. /**
  192. * Destroy original files.
  193. *
  194. * @return string.
  195. */
  196. public function destroy($key)
  197. {
  198. $files = $this->original ?: [];
  199. $file = Arr::get($files, $key);
  200. $this->deleteFile($file);
  201. unset($files[$key]);
  202. return array_values($files);
  203. }
  204. }