| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- namespace Dcat\Admin\Form\Field;
- use Dcat\Admin\Form\Field;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Validator;
- use Symfony\Component\HttpFoundation\File\UploadedFile;
- class BootstrapFile extends Field
- {
- use BootstrapUploadField;
- /**
- * Css.
- *
- * @var array
- */
- protected static $css = [
- '/vendor/dcat-admin/bootstrap-fileinput/css/fileinput.min.css?v=4.5.2',
- ];
- /**
- * Js.
- *
- * @var array
- */
- protected static $js = [
- '/vendor/dcat-admin/bootstrap-fileinput/js/plugins/canvas-to-blob.min.js',
- '/vendor/dcat-admin/bootstrap-fileinput/js/fileinput.min.js?v=4.5.2',
- ];
- /**
- * Create a new File instance.
- *
- * @param string $column
- * @param array $arguments
- */
- public function __construct($column, $arguments = [])
- {
- $this->initStorage();
- parent::__construct($column, $arguments);
- }
- /**
- * Default directory for file to upload.
- *
- * @return mixed
- */
- public function defaultDirectory()
- {
- return config('admin.upload.directory.file');
- }
- /**
- * {@inheritdoc}
- */
- public function getValidator(array $input)
- {
- if (request()->has(static::FILE_DELETE_FLAG)) {
- return false;
- }
- if ($this->validator) {
- return $this->validator->call($this, $input);
- }
- /*
- * If has original value, means the form is in edit mode,
- * then remove required rule from rules.
- */
- if ($this->original()) {
- $this->removeRule('required');
- }
- /*
- * Make input data validatable if the column data is `null`.
- */
- if (Arr::has($input, $this->column) && is_null(Arr::get($input, $this->column))) {
- $input[$this->column] = '';
- }
- $rules = $attributes = [];
- if (!$fieldRules = $this->getRules()) {
- return false;
- }
- $rules[$this->column] = $fieldRules;
- $attributes[$this->column] = $this->label;
- return Validator::make($input, $rules, $this->getValidationMessages(), $attributes);
- }
- /**
- * Prepare for saving.
- *
- * @param UploadedFile|array $file
- *
- * @return mixed|string
- */
- protected function prepareToSave($file)
- {
- if (request()->has(static::FILE_DELETE_FLAG)) {
- return $this->destroy();
- }
- $this->name = $this->getStoreName($file);
- return $this->uploadAndDeleteOriginal($file);
- }
- /**
- * Upload file and delete original file.
- *
- * @param UploadedFile $file
- *
- * @return mixed
- */
- protected function uploadAndDeleteOriginal(UploadedFile $file)
- {
- $this->renameIfExists($file);
- $path = null;
- if (!is_null($this->storagePermission)) {
- $path = $this->storage->putFileAs($this->getDirectory(), $file, $this->name, $this->storagePermission);
- } else {
- $path = $this->storage->putFileAs($this->getDirectory(), $file, $this->name);
- }
- $this->destroy();
- return $path;
- }
- /**
- * Preview html for file-upload plugin.
- *
- * @return string
- */
- protected function preview()
- {
- return $this->objectUrl($this->value());
- }
- /**
- * Initialize the caption.
- *
- * @param string $caption
- *
- * @return string
- */
- protected function initialCaption($caption)
- {
- return basename($caption);
- }
- /**
- * @return array
- */
- protected function initialPreviewConfig()
- {
- return [
- ['caption' => basename($this->value()), 'key' => 0],
- ];
- }
- /**
- * Render file upload field.
- *
- * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
- */
- public function render()
- {
- $this->options(['overwriteInitial' => true]);
- $this->setupDefaultOptions();
- if (!empty($this->value)) {
- $this->attribute('data-initial-preview', $this->preview());
- $this->attribute('data-initial-caption', $this->initialCaption($this->value));
- $this->setupPreviewOptions();
- }
- $options = json_encode($this->options);
- $this->script = <<<JS
- $("{$this->getElementClassSelector()}").fileinput({$options});
- JS;
- return parent::render();
- }
- }
|