File.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Contracts\UploadField as UploadFieldInterface;
  4. use Dcat\Admin\Form\Field;
  5. use Dcat\Admin\Support\Helper;
  6. use Dcat\Admin\Support\JavaScript;
  7. use Illuminate\Support\Arr;
  8. use Illuminate\Support\Facades\Validator;
  9. class File extends Field implements UploadFieldInterface
  10. {
  11. use WebUploader,
  12. UploadField;
  13. public function __construct($column, $arguments = [])
  14. {
  15. parent::__construct($column, $arguments);
  16. $this->setupDefaultOptions();
  17. }
  18. public function setElementName($name)
  19. {
  20. $this->mergeOptions(['elementName' => $name]);
  21. return parent::setElementName($name);
  22. }
  23. /**
  24. * @return mixed
  25. */
  26. public function defaultDirectory()
  27. {
  28. return config('admin.upload.directory.file');
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getValidator(array $input)
  34. {
  35. if (request()->has(static::FILE_DELETE_FLAG)) {
  36. return false;
  37. }
  38. if ($this->validator) {
  39. return $this->validator->call($this, $input);
  40. }
  41. if (! Arr::has($input, $this->column)) {
  42. return false;
  43. }
  44. $value = Arr::get($input, $this->column);
  45. $value = array_filter(is_array($value) ? $value : explode(',', $value));
  46. $fileLimit = $this->options['fileNumLimit'] ?? 1;
  47. if ($fileLimit < count($value)) {
  48. $this->form->responseValidationMessages(
  49. $this->column,
  50. trans('admin.uploader.max_file_limit', ['attribute' => $this->label, 'max' => $fileLimit])
  51. );
  52. return false;
  53. }
  54. $rules = $attributes = [];
  55. $requiredIf = null;
  56. if (! $this->hasRule('required') && ! $requiredIf = $this->getRule('required_if*')) {
  57. return false;
  58. }
  59. $rules[$this->column] = $requiredIf ?: 'required';
  60. $attributes[$this->column] = $this->label;
  61. return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. protected function prepareInputValue($file)
  67. {
  68. if (request()->has(static::FILE_DELETE_FLAG)) {
  69. return $this->destroy();
  70. }
  71. $this->destroyIfChanged($file);
  72. return $file;
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function setNestedFormRelation(array $options = [])
  78. {
  79. $this->options['formData']['_relation'] = [$options['relation'], $options['key']];
  80. return $this;
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. public function disable(bool $value = true)
  86. {
  87. $this->options['disabled'] = $value;
  88. return $this;
  89. }
  90. protected function formatFieldData($data)
  91. {
  92. return Helper::array(Arr::get($data, $this->normalizeColumn()));
  93. }
  94. /**
  95. * @return array
  96. */
  97. protected function initialPreviewConfig()
  98. {
  99. $previews = [];
  100. foreach (Helper::array($this->value()) as $value) {
  101. $previews[] = [
  102. 'id' => $value,
  103. 'path' => basename($value),
  104. 'url' => $this->objectUrl($value),
  105. ];
  106. }
  107. return $previews;
  108. }
  109. protected function forceOptions()
  110. {
  111. $this->options['fileNumLimit'] = 1;
  112. }
  113. /**
  114. * {@inheritDoc}
  115. */
  116. public function render()
  117. {
  118. $this->setDefaultServer();
  119. if (! empty($this->value())) {
  120. $this->setupPreviewOptions();
  121. }
  122. $this->forceOptions();
  123. $this->formatValue();
  124. $this->addVariables([
  125. 'fileType' => $this->options['isImage'] ? '' : 'file',
  126. 'showUploadBtn' => ($this->options['autoUpload'] ?? false) ? false : true,
  127. 'options' => JavaScript::format($this->options),
  128. ]);
  129. return parent::render();
  130. }
  131. /**
  132. * @return void
  133. */
  134. protected function formatValue()
  135. {
  136. if ($this->value !== null) {
  137. $this->value = implode(',', Helper::array($this->value));
  138. } elseif (is_array($this->default)) {
  139. $this->default = implode(',', $this->default);
  140. }
  141. }
  142. }