From a6d7454427f82d479db7f1820ca86166a2822434 Mon Sep 17 00:00:00 2001 From: Motch Julien Date: Mon, 27 Aug 2018 10:19:15 +0200 Subject: [PATCH] multiple selector --- .../Assets/js/components/MultipleMedia.vue | 123 ++++++++++++++++++ .../Assets/js/mixins/MultipleFileSelector.js | 27 ++++ .../Http/Controllers/Api/MediaController.php | 18 +++ Modules/Media/Http/apiRoutes.php | 5 + 4 files changed, 173 insertions(+) create mode 100644 Modules/Media/Assets/js/components/MultipleMedia.vue create mode 100644 Modules/Media/Assets/js/mixins/MultipleFileSelector.js diff --git a/Modules/Media/Assets/js/components/MultipleMedia.vue b/Modules/Media/Assets/js/components/MultipleMedia.vue new file mode 100644 index 000000000..710dd9b49 --- /dev/null +++ b/Modules/Media/Assets/js/components/MultipleMedia.vue @@ -0,0 +1,123 @@ + + + + \ No newline at end of file diff --git a/Modules/Media/Assets/js/mixins/MultipleFileSelector.js b/Modules/Media/Assets/js/mixins/MultipleFileSelector.js new file mode 100644 index 000000000..9c0e49f8d --- /dev/null +++ b/Modules/Media/Assets/js/mixins/MultipleFileSelector.js @@ -0,0 +1,27 @@ +export default { + methods: { + selectMultipleFile(event, model) { + if (!this[model].medias_multi) { + this[model].medias_multi = {}; + } + if (!this[model].medias_multi[event.zone]) { + this[model].medias_multi[event.zone] = { files: [] }; + } + this[model].medias_multi[event.zone].files.push(event.id); + }, + unselectFile(event, model) { + if (!this[model].medias_multi) { + this[model].medias_multi = {}; + } + if (!this[model].medias_multi[event.zone]) { + this[model].medias_multi[event.zone] = { files: [] }; + if (this.$refs['multiple-media'] !== undefined && this.$refs['multiple-media'].selectedMedia !== undefined && !_.isEmpty(this.$refs['multiple-media'].selectedMedia)) { + _.forEach(this.$refs['multiple-media'].selectedMedia, (file, key) => { + this[model].medias_multi[event.zone].files.push(file.id); + }); + } + } + this[model].medias_multi[event.zone].files = _.reject(this[model].medias_multi[event.zone].files, media => media === event.id); + }, + }, +}; \ No newline at end of file diff --git a/Modules/Media/Http/Controllers/Api/MediaController.php b/Modules/Media/Http/Controllers/Api/MediaController.php index e5b2d0b9d..552f115e6 100644 --- a/Modules/Media/Http/Controllers/Api/MediaController.php +++ b/Modules/Media/Http/Controllers/Api/MediaController.php @@ -93,6 +93,24 @@ public function findFirstByZoneEntity(Request $request) return new MediaTransformer($file); } + /** + * Get a media collection by zone and entity object. Require some params that were passed to request: entity (Full class name of entity), entity_id and zone + * + * @param Request $request + * @return JsonResponse|\Illuminate\Http\Resources\Json\AnonymousResourceCollection + */ + public function getByZoneEntity(Request $request) + { + $entityName = (string)$request->get('entity'); + $entityModel = new $entityName; + $entity = $entityModel::find($request->get('entity_id')); + if ($entity && in_array('Modules\Media\Support\Traits\MediaRelation', class_uses($entity)) && $entity->files()->count()) { + $files = $this->file->findMultipleFilesByZoneForEntity($request->get('zone'), $entity); + return MediaTransformer::collection($files); + } + return response()->json(['data' => null]); + } + /** * Store a newly created resource in storage. * diff --git a/Modules/Media/Http/apiRoutes.php b/Modules/Media/Http/apiRoutes.php index 14762968c..0d1adda11 100644 --- a/Modules/Media/Http/apiRoutes.php +++ b/Modules/Media/Http/apiRoutes.php @@ -70,6 +70,11 @@ 'as' => 'api.media.find-first-by-zone-and-entity', ]); + $router->get('media/get-by-zone-and-entity', [ + 'uses' => 'MediaController@getByZoneEntity', + 'as' => 'api.media.get-by-zone-and-entity', + ]); + $router->get('media/{media}', [ 'uses' => 'MediaController@find', 'as' => 'api.media.media.find',