diff --git a/classes/UploadStorage.php b/classes/UploadStorage.php index ccc8c7b..82c011f 100644 --- a/classes/UploadStorage.php +++ b/classes/UploadStorage.php @@ -30,14 +30,22 @@ class UploadStorage { $uploadedFile->storeAs('', $sourceFile->path, $this->disk); $sourceFile->save(); - $attachment = $sourceFile->attachments()->create([ + return $this->storeAttachment($sourceFile, [ 'title' => $this->data['title'], - 'description' => $this->data['description'] ?? '' + 'description' => $this->data['description'] ?? '', + 'crop' => $this->data['crop'] + ]); + } + + public function storeAttachment($sourceFile, $data) { + $attachment = $sourceFile->attachments()->create([ + 'title' => $data['title'], + 'description' => $data['description'] ]); Queue::push(CompressJob::class, [ 'attachment_id' => $attachment->id, - 'crop' => $this->data['crop'] + 'crop' => $data['crop'] ]); return $attachment; @@ -46,13 +54,13 @@ class UploadStorage { public function getFileData($id) { $attachment = Attachment::find($id); - return [ + return $attachment ? [ 'url' => $this->storage()->url($attachment->source->path), 'type' => $this->storage()->mimeType($attachment->source->path), 'size' => $this->storage()->size($attachment->source->path), 'name' => $attachment->slug, 'id' => $id - ]; + ] : null; } public function removeAttachment($id) { diff --git a/formwidgets/Responsiveimage.php b/formwidgets/Responsiveimage.php index 73d1335..76b6ec6 100644 --- a/formwidgets/Responsiveimage.php +++ b/formwidgets/Responsiveimage.php @@ -15,6 +15,7 @@ use Exception; use Aweos\Resizer\Traits\ResponsiveSaver; use Aweos\Resizer\Traits\ResponsiveWidget; use Aweos\Resizer\Classes\UploadStorage; +use Aweos\Resizer\Models\SourceFile; /** * File upload field @@ -102,6 +103,10 @@ class Responsiveimage extends FormWidgetBase return $value; } + public function getLoadValue() { + return is_numeric(parent::getLoadValue()) ? parent::getLoadValue() : null; + } + /** * Upload handler for the server-side processing of uploaded files */ @@ -127,4 +132,47 @@ class Responsiveimage extends FormWidgetBase app(UploadStorage::class)->removeAttachment(Input::get('file_id')); } + public function onOpenDatabase() { + $files = (new SourceFile)->newQuery(); + + if ($aspectRatio = Input::get('databasePopup.crop.aspectRatio', null)) { + $files->where('aspect_ratio', json_encode($aspectRatio)); + } + if ($minWidth = Input::get('databasePopup.crop.minWidth', null)) { + $files->where('min_width', '>=', $minWidth); + } + + return $this->makePartial('database', [ + 'images' => $files->get() + ]); + } + + public function onSelectDatabase() { + $v = Validator::make(Input::get(), [ + 'image' => 'required|exists:responsive_source_files,id', + 'title' => 'required' + ], [ + 'image.required' => 'Bitte ein Bild auswählen', + 'title.required' => 'Bitte einen Titel angeben' + ]); + + if ($v->fails()) { + throw new ValidationException($v); + } + + $attachment = app(UploadStorage::class)->storeAttachment(SourceFile::find(Input::get('image')), [ + 'title' => Input::get('title', ''), + 'description' => Input::get('description', ''), + 'crop' => null + ]); + + return Response::json([ + 'id' => $attachment->id, + 'url' => $attachment->url, + 'name' => $attachment->slug, + 'type' => $attachment->source->type, + 'size' => $attachment->source->size + ]); + } + } diff --git a/formwidgets/responsiveimage/assets/css/main.css b/formwidgets/responsiveimage/assets/css/main.css index 9493fe0..4198cb3 100644 --- a/formwidgets/responsiveimage/assets/css/main.css +++ b/formwidgets/responsiveimage/assets/css/main.css @@ -218,3 +218,30 @@ } } } + +.responsive-database-grid { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); + grid-gap: 1.5rem; + margin-bottom: 1.5rem; + label { + position: relative; + display: block; + cursor: pointer; + input { + visibility: hidden; + z-index: -999; + position: relative; + & + img { + max-width: 100%; + height: auto; + } + } + input:checked + img { + box-shadow: 3px 3px 1px hsl(200.9, 78.6%, 45.9%), + -3px 3px 1px hsl(200.9, 78.6%, 45.9%), + 3px -3px 1px hsl(200.9, 78.6%, 45.9%), + -3px -3px 1px hsl(200.9, 78.6%, 45.9%); + } + } +} diff --git a/formwidgets/responsiveimage/assets/src/App.vue b/formwidgets/responsiveimage/assets/src/App.vue index c239c80..c2f17ba 100644 --- a/formwidgets/responsiveimage/assets/src/App.vue +++ b/formwidgets/responsiveimage/assets/src/App.vue @@ -125,8 +125,24 @@ export default { this.addVisible = true; }); }, - openDatabase() { - + openDatabase(e) { + var _cls = this; + + var data = { + file_id: this.value, + crop: this.cropOptions + }; + + $(e.target).popup({ handler: this.handlers.onOpenDatabase, size: 'giant', extraData: { databasePopup: data } }).on('complete.oc.popup', function(e) { + $(e.relatedTarget).on('ajaxSuccess', 'form', function(event, context, data) { + _cls.content = data.id; + _cls.addVisible = false; + var file = { size: data.size, name: data.name, type: data.type }; + _cls.$refs.dropzone.manuallyAddFile(file, data.url); + + $(e.relatedTarget).popup('hide'); + }); + }); }, onSend(file, xhr, formData) { formData.append('extraData', JSON.stringify({ diff --git a/formwidgets/responsiveimage/partials/_database.htm b/formwidgets/responsiveimage/partials/_database.htm new file mode 100644 index 0000000..e331565 --- /dev/null +++ b/formwidgets/responsiveimage/partials/_database.htm @@ -0,0 +1,30 @@ +
diff --git a/formwidgets/responsiveimage/partials/_responsiveimage.htm b/formwidgets/responsiveimage/partials/_responsiveimage.htm index 15cd741..4e69511 100644 --- a/formwidgets/responsiveimage/partials/_responsiveimage.htm +++ b/formwidgets/responsiveimage/partials/_responsiveimage.htm @@ -7,6 +7,7 @@