diff --git a/app/Http/Controllers/MediaController.php b/app/Http/Controllers/MediaController.php index c6b40ad..cdd3061 100644 --- a/app/Http/Controllers/MediaController.php +++ b/app/Http/Controllers/MediaController.php @@ -863,6 +863,82 @@ class MediaController extends Controller return json_encode($response); } + /** + * Duplique un média sans l'image + */ + + public function duplicateMedia(Request $request) + { + Auth::user()->authorizeRoles([config('roles.const.ADMIN'), config('roles.const.REDACTEUR'), config('roles.const.CONTRIBUTEUR')]); + $media_id = $request->input('media_id'); + + $media = Media::where('id', $media_id)->first(); + if ($media){ + if ($media->isLocked === 1){ + return json_encode(array('status' => false, 'error' => "Le média est en attente d'un retour d'Arcadia.")); + } + + $n = $media->replicate(); + $n->push(); + + foreach ($media->articles as $article) { + $n->articles()->attach($article); + } + + foreach ($media->marques as $marque) { + $n->marques()->attach($marque); + } + + foreach ($media->societes as $societe) { + $n->societes()->attach($societe); + } + + foreach ($media->tags as $tag) { + $n->tags()->attach($tag); + } + + $destinationPath = $this->get_mediaFolder() . "/"; + $uid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); + $filename = $uid . ".jpg"; + Storage::copy("images/default.jpg", $destinationPath . $filename); + + $n->arcadia_id = NULL; + $n->libelle = $n->libelle . '_copy'; + $n->folder = $destinationPath; + $n->uid = $uid; + $n->extension = 'jpg'; + $n->taille = $this->formatBytes($this->get_file_size($destinationPath . $filename)); + + $im = new \imagick($destinationPath . $filename); + $resolutions = $im->getImageResolution(); + $n->resolution = round($resolutions['x'] * 2.54, 2); + $imageprops = $im->getImageGeometry(); + $n->largeur = $imageprops['width']; + $n->hauteur = $imageprops['height']; + $n->largeur_mm = ceil(($n->largeur * 2.54 / $n->resolution) * 10); + $n->hauteur_mm = ceil(($n->hauteur * 2.54 / $n->resolution) * 10); + + $n->duree = ''; + $n->version = 1; + $n->isDefault = 1; + $n->statut = 0; + $n->created_at = NOW(); + $n->updated_at = NOW(); + $n->created_by = Auth::user()->id; + $n->save(); + + // ---- Creation des vignettes de base + $this->create_sys_thumb($n); + // ---- Creation des vignettes en lien avec le sujet du média + $this->create_thumb($n); + + $media = Media::find($n->id); + $media->addToIndex(); + + return json_encode(array('status' => true, 'sujet_id' => $media->sujet_id, 'datas' => $media)); + } + } + /** * Sauvegarde toutes les informations d'un média * diff --git a/public/js/app.js b/public/js/app.js index da61300..de1f3c9 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -12340,6 +12340,62 @@ $(document).ready(function () { } }).trigger("click"); } + }); + $('.action_duplicate_media').on('click', function (e) { + e.preventDefault(); + var medias = []; + $('.media_container .imageItem').each(function (index) { + if ($(this).attr("aria-grabbed") === "true") { + medias.push($(this).attr("data-id")); + } + }); + + if (medias.length === 0) { + $.fancyMessage({ + title: "Dupliquer un média", + message: "Veuillez sélectionner un média.", + type: "info" + }); + } else if (medias.length > 1) { + $.fancyMessage({ + title: "Dupliquer un média", + message: "Vous ne pouvez dupliquer qu'un média à la fois.", + type: "info" + }); + } else { + $(".media_container .loader").show(); + $.ajax({ + url: '/ajax/duplicate-media', + method: 'POST', + dataType: 'json', + data: { + media_id: medias[0] + }, + success: function success(result) { + if (result.status === true) { + openEditor({ + id: result.datas.id, + preview: result.datas.folder + result.datas.uid + "_preview." + result.datas.extension, + src: result.datas.folder + result.datas.uid + "." + result.datas.extension, + depot: 0, + typemedia: result.datas.typemedia_id, + ext: result.datas.extension + }, drPlugin, viewer); + $("#form_media .form_media_edit").trigger("click"); // On replace l'offset pour recharger + + $(window).data('offset', $(window).data('offset') - $(window).data('limit')); + loadMedia(result.sujet_id); + $(".media_container .loader").hide(); + } else { + $.fancyMessage({ + title: "Dupliquer un média", + message: result.error, + type: "error" + }); + } + } + }); + } }); // ---- Bouton pour remplacer un média par un autre $('.form_media_replace').on('click', function (event) { @@ -13267,6 +13323,7 @@ window.create_UUID = function () { }; window.openEditor = function (obj, drPlugin, viewer) { + var tab = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; console.log("openEditor Called"); // si le plugin de retouche a déjà été chargé if (drPlugin != null) { @@ -13575,7 +13632,11 @@ window.openEditor = function (obj, drPlugin, viewer) { $linkItem.find('.link_libelle').html(vignette.libelle + " " + vignette.extension + " " + vignette.resolution); $linkItem.find('.link_input input').val(vignette.url); $(".tab_links .table_links tbody").append($linkItem); - }); // affiche la popin + }); + /*if (tab && tab == 'edit'){ + $("#form_media .form_media_edit").trigger("click"); + }*/ + // affiche la popin $("#form_media").fadeIn('fast'); }, diff --git a/resources/js/media.js b/resources/js/media.js index db055b2..d8650d4 100644 --- a/resources/js/media.js +++ b/resources/js/media.js @@ -911,6 +911,53 @@ $(document).ready(function() { } }); + $('.action_duplicate_media').on('click', function (e) { + e.preventDefault(); + + let medias = []; + $('.media_container .imageItem').each(function (index) { + if( $(this ).attr("aria-grabbed") === "true" ){ + medias.push($(this ).attr("data-id")); + } + }); + + if (medias.length === 0){ + $.fancyMessage({title : "Dupliquer un média", message : "Veuillez sélectionner un média.", type : "info"}); + }else if(medias.length > 1){ + $.fancyMessage({title : "Dupliquer un média", message : "Vous ne pouvez dupliquer qu'un média à la fois.", type : "info"}); + }else{ + $(".media_container .loader").show(); + $.ajax({ + url: '/ajax/duplicate-media', + method: 'POST', + dataType : 'json', + data: { + media_id : medias[0], + }, + success: function(result){ + if (result.status === true){ + openEditor({ + id: result.datas.id, + preview: result.datas.folder + result.datas.uid + "_preview." + result.datas.extension, + src: result.datas.folder + result.datas.uid + "." + result.datas.extension, + depot: 0, + typemedia: result.datas.typemedia_id, + ext: result.datas.extension, + }, drPlugin, viewer); + $("#form_media .form_media_edit").trigger("click"); + // On replace l'offset pour recharger + $(window).data('offset', $(window).data('offset')-$(window).data('limit')); + loadMedia(result.sujet_id); + $(".media_container .loader").hide(); + }else{ + $.fancyMessage({title : "Dupliquer un média", message : result.error, type : "error"}); + } + } + }); + } + + }); + // ---- Bouton pour remplacer un média par un autre $('.form_media_replace').on('click',function(event){ event.preventDefault(); @@ -1782,7 +1829,7 @@ window.create_UUID = function(){ return uuid; } -window.openEditor = function (obj, drPlugin, viewer) { +window.openEditor = function (obj, drPlugin, viewer, tab = false) { console.log("openEditor Called"); // si le plugin de retouche a déjà été chargé @@ -1826,6 +1873,7 @@ window.openEditor = function (obj, drPlugin, viewer) { // Mémorise le média courant $(window).data('current_edit_media', obj.id); + // sélectionne la tab preview à l'ouverture $("#form_media .form_media_preview").trigger("click"); @@ -2070,6 +2118,10 @@ window.openEditor = function (obj, drPlugin, viewer) { $(".tab_links .table_links tbody").append($linkItem); }); + /*if (tab && tab == 'edit'){ + $("#form_media .form_media_edit").trigger("click"); + }*/ + // affiche la popin $("#form_media").fadeIn('fast'); }, diff --git a/resources/views/media_manager.blade.php b/resources/views/media_manager.blade.php index b35d8be..0e2f082 100644 --- a/resources/views/media_manager.blade.php +++ b/resources/views/media_manager.blade.php @@ -93,6 +93,9 @@ @if (Auth::user()->hasRole([config('roles.const.ADMIN'), config('roles.const.REDACTEUR')])) Supprimer @endif + @if (Auth::user()->hasRole([config('roles.const.CONTRIBUTEUR'), config('roles.const.ADMIN'), config('roles.const.REDACTEUR')])) + Dupliquer + @endif