1291 lines
46 KiB
PHP
1291 lines
46 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Traits\ArcadiaLink;
|
|
use Elasticquent\ElasticquentTrait;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use App\Models\Sujet;
|
|
use App\Models\Setting;
|
|
use App\Models\Format;
|
|
use App\Models\Media;
|
|
use App\Models\Typemedia;
|
|
use App\Models\Langue;
|
|
use App\Models\Copyright;
|
|
use App\Models\Etiquette;
|
|
use App\Models\Societe;
|
|
use App\Models\Marque;
|
|
use App\Models\Dossier;
|
|
use App\Models\Tag;
|
|
use App\Models\Article;
|
|
use App\Models\Recherche;
|
|
use App\Models\Historique;
|
|
use App\Models\Panier;
|
|
use App\Http\Traits\MediaTools;
|
|
use App\Models\FormatRestriction;
|
|
|
|
class MediaController extends Controller
|
|
{
|
|
|
|
use MediaTools;
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$filters = "";
|
|
$sujets = Auth::user()->sujets;
|
|
$paniers = Auth::user()->paniersOwner;
|
|
$typemedias = Typemedia::all();
|
|
$langues = Langue::all();
|
|
$copyrights = Copyright::all();
|
|
$etiquettes = Etiquette::all();
|
|
$societes = Societe::orderBy('libelle')->get();
|
|
$marques = Marque::all();
|
|
|
|
$dossier_id = $request->get('d') !== null ? $request->get('d') : -1;
|
|
$sujet_id = $request->get('s') !== null ? $request->get('s') : 1;
|
|
$textSearch = $request->get('t');
|
|
$recherche_id = $request->get('r');
|
|
|
|
// ---- Recherche type texte
|
|
if ($textSearch != "" && $sujet_id != "") {
|
|
// on créé un object filtre
|
|
$filters = (object)[
|
|
"search" => array(
|
|
'type' => "search",
|
|
'legend' => "Recherche : ",
|
|
'value' => $textSearch,
|
|
'libelle' => array($textSearch),
|
|
)
|
|
];
|
|
$filters = json_encode($filters);
|
|
|
|
// ---- Recherche favorite
|
|
} elseif ($recherche_id != "") {
|
|
$recherche = Recherche::where('id', $recherche_id)->first();
|
|
if ($recherche) {
|
|
$filters = $recherche->filters;
|
|
$sujet_id = $recherche->sujet_id;
|
|
$dossier_id = $recherche->dossier_id;
|
|
}
|
|
}
|
|
|
|
return view('media_manager')->with([
|
|
'sujets' => $sujets,
|
|
'typemedias' => $typemedias,
|
|
'langues' => $langues,
|
|
'copyrights' => $copyrights,
|
|
'etiquettes' => $etiquettes,
|
|
'societes' => $societes,
|
|
'marques' => $marques,
|
|
'currentFolder' => $dossier_id,
|
|
'currentSujet' => $sujet_id,
|
|
'currentFilters' => $filters,
|
|
'paniers' => $paniers,
|
|
'textSearch' => $textSearch
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show media
|
|
*
|
|
* @param Media $media
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function show(Media $media)
|
|
{
|
|
return redirect()->route('media_manager')->with('autoOpen', $media);
|
|
}
|
|
|
|
/**
|
|
* Chargement des médias
|
|
*
|
|
*/
|
|
public function loadMedia(Request $request)
|
|
{
|
|
$response = array();
|
|
$userId = Auth::user()->id;
|
|
$dossier_id = $request->folder;
|
|
$sujet_id = $request->sujet;
|
|
$filters = $request->filters;
|
|
$offset = $request->offset;
|
|
$limit = $request->limit;
|
|
$saveSearch = $request->saveSearch;
|
|
$isDepot = 0;
|
|
|
|
$order = array_search('order', array_column($filters, 'type'));
|
|
|
|
$orderField = $filters[$order]['value'];
|
|
$order = $filters[$order]['order'];
|
|
|
|
$esMust = [];
|
|
$esMustNot = [];
|
|
$esShould = [];
|
|
$esFilters = [];
|
|
|
|
if ($dossier_id == "0") { // dossier racine
|
|
$esMust = array_merge($esMust, [
|
|
[
|
|
'bool' => [
|
|
'should' => [
|
|
'match' => [
|
|
'sujet_id' => $sujet_id
|
|
]
|
|
]
|
|
]
|
|
]
|
|
]);
|
|
$esMustNot = array_merge($esMustNot, [
|
|
'exists' => [
|
|
'field' => 'dossier_id'
|
|
]
|
|
]);
|
|
$esShould = array_merge($esShould, [
|
|
[
|
|
'match' => [
|
|
'statut' => config('services.statut.public_etendu')
|
|
]
|
|
],
|
|
[
|
|
'match' => [
|
|
'statut' => config('services.statut.public')
|
|
]
|
|
],
|
|
[
|
|
'match' => [
|
|
'created_by' => $userId
|
|
]
|
|
]
|
|
]);
|
|
|
|
} elseif ($dossier_id == "-1") { // recherche global
|
|
|
|
$esMust = array_merge($esMust, [
|
|
[
|
|
'bool' => [
|
|
'must' => [
|
|
[
|
|
'match' => [
|
|
'sujet_id' => $sujet_id
|
|
]
|
|
]
|
|
]
|
|
]
|
|
]
|
|
]);
|
|
|
|
$esShould = array_merge($esShould, [
|
|
[
|
|
'match' => [
|
|
'created_by' => $userId
|
|
],
|
|
'match' => [
|
|
'statut' => config('services.statut.public_etendu')
|
|
],
|
|
'match' => [
|
|
'statut' => config('services.statut.public')
|
|
]
|
|
]
|
|
]);
|
|
|
|
} else {
|
|
$dossier = Dossier::where('id', $dossier_id)->first();
|
|
if ($dossier->parent_id != 1 && $dossier->id != 1) { // dossier classic
|
|
|
|
$esMust = array_merge($esMust, [
|
|
[
|
|
'bool' => [
|
|
'must' => [
|
|
[
|
|
'match' => [
|
|
'sujet_id' => $sujet_id
|
|
]
|
|
],
|
|
[
|
|
'match' => [
|
|
'dossier_id' => $dossier_id
|
|
]
|
|
]
|
|
]
|
|
]
|
|
]
|
|
]);
|
|
|
|
$esFilters = array_merge($esFilters, array(
|
|
[
|
|
'bool' => [
|
|
'should' => [
|
|
[
|
|
'bool' => [
|
|
'must_not' => [
|
|
[
|
|
'term' => [
|
|
'statut' => [
|
|
'value' => config('services.statut.brouillon'),
|
|
'boost' => 1.0
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'adjust_pure_negative' => true,
|
|
'boost' => 1.0
|
|
]
|
|
],
|
|
[
|
|
'term' => [
|
|
'created_by' => [
|
|
'value' => $userId,
|
|
'boost' => 1.0
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'adjust_pure_negative' => true,
|
|
'boost' => 1.0
|
|
]
|
|
]
|
|
));
|
|
|
|
} else { // dossier dépôt
|
|
$isDepot = 1;
|
|
$esMust = array_merge($esMust, [
|
|
[
|
|
'match' => [
|
|
'dossier_id' => $dossier_id
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
}
|
|
|
|
// Filtre recherche texte
|
|
$tmp = array_search('search', array_column($filters, 'type'));
|
|
if ($tmp !== false) {
|
|
$tmpFilters = [];
|
|
$search = strtolower(trim($filters[$tmp]['value']));
|
|
|
|
if (strpos($search, '=') !== false){
|
|
$tmpSearch = explode('=', $search);
|
|
$key = $tmpSearch[0];
|
|
$needle = $tmpSearch[1];
|
|
|
|
switch ($key){
|
|
case 'ca':
|
|
$isCAQuery = true;
|
|
$tmpFilters = [
|
|
[
|
|
'bool' => [
|
|
'must' => [
|
|
'match' => [
|
|
'articles.noArt' => $needle
|
|
]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
break;
|
|
case 'fv':
|
|
$tmpFilters = [
|
|
[
|
|
'bool' => [
|
|
'must' => [
|
|
'match' => [
|
|
'articles.codFamVte' => $needle
|
|
]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
break;
|
|
case 'gca':
|
|
$tmpFilters = [
|
|
[
|
|
'bool' => [
|
|
'must' => [
|
|
'match' => [
|
|
'articles.codGencodAdm' => $needle
|
|
]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
break;
|
|
case 'gcs':
|
|
$tmpFilters = [
|
|
[
|
|
'bool' => [
|
|
'must' => [
|
|
'match' => [
|
|
'articles.codGencodSymb' => $needle
|
|
]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
break;
|
|
case 'gcp':
|
|
$tmpFilters = [
|
|
[
|
|
'bool' => [
|
|
'must' => [
|
|
'match' => [
|
|
'articles.codGencodPds' => $needle
|
|
]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
break;
|
|
case 'cd': // media id
|
|
$tmpFilters = [
|
|
[
|
|
'bool' => [
|
|
'must' => [
|
|
'match' => [
|
|
'id' => $needle
|
|
]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
break;
|
|
case 'cv': // media arcadia_id
|
|
$tmpFilters = [
|
|
[
|
|
'bool' => [
|
|
'must' => [
|
|
'match' => [
|
|
'arcadia_id' => $needle
|
|
]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
break;
|
|
}
|
|
}else if (strpos($search, '+') !== false){
|
|
$tmpSearch = explode('+', $search);
|
|
|
|
$mustLib = [];
|
|
foreach ($tmpSearch as $search) {
|
|
array_push($mustLib, [
|
|
'wildcard' => [
|
|
'libelle.keyword' => [
|
|
'wildcard' => "*$search*",
|
|
'boost' => 1.0
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
|
|
$mustArt = [];
|
|
foreach ($tmpSearch as $search) {
|
|
array_push($mustArt, [
|
|
'wildcard' => [
|
|
'articles.libCcial.keyword' => [
|
|
'wildcard' => "*$search*",
|
|
'boost' => 1.0
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
|
|
$tmpFilters = [
|
|
[
|
|
'bool' => [
|
|
'should' => [
|
|
[
|
|
'bool' => [
|
|
'must' => $mustLib
|
|
]
|
|
],
|
|
[
|
|
'bool' => [
|
|
'must' => $mustArt
|
|
]
|
|
]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
|
|
}else{
|
|
$tmpFilters = [
|
|
[
|
|
'multi_match' => [
|
|
'query' => $search,
|
|
"type" => "most_fields",
|
|
'fields' => [
|
|
'libelle^3',
|
|
'commentaire^2',
|
|
'arcadia_id',
|
|
'articles.libCcial',
|
|
'articles.libMarq',
|
|
'articles.libFamVte',
|
|
'tags.libelle'
|
|
],
|
|
'operator' => 'or',
|
|
'fuzziness' => 'AUTO'
|
|
]
|
|
]
|
|
];
|
|
}
|
|
|
|
$esMust = array_merge($esMust, $tmpFilters);
|
|
|
|
// ---- Sauvegarde de la recherche utilisateur
|
|
if ($saveSearch == 1) {
|
|
$recherche = Recherche::where('libelle', $search)->first();
|
|
if ($recherche) {
|
|
$recherche->user_id = $userId;
|
|
$recherche->sujet_id = $sujet_id;
|
|
$recherche->dossier_id = $dossier_id;
|
|
$recherche->filters = json_encode($filters);
|
|
$recherche->save();
|
|
} else {
|
|
$recherche = new Recherche();
|
|
$recherche->user_id = $userId;
|
|
$recherche->sujet_id = $sujet_id;
|
|
$recherche->dossier_id = $dossier_id;
|
|
$recherche->filters = json_encode($filters);
|
|
$recherche->libelle = $search;
|
|
$recherche->save();
|
|
|
|
// Purge des anciennes recherches
|
|
$count = Recherche::where('user_id', $userId)->count();
|
|
Recherche::where('user_id', $userId)->orderBy("created_at", "desc")->skip(10)->take($count)->get()->each(function ($row) {
|
|
$row->delete();
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Filtre type de média
|
|
$tmp = array_search('typemedia_id', array_column($filters, 'type'));
|
|
if ($tmp !== false) {
|
|
$oTmp = [];
|
|
foreach ($filters[$tmp]['value'] as $type) {
|
|
$oTmp = array_merge($oTmp, [
|
|
[
|
|
'match' => [
|
|
'typemedia_id' => $type['codeTypemedia']
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
$esTypeMedia = [
|
|
[
|
|
'bool' => [
|
|
'should' => $oTmp
|
|
]
|
|
]
|
|
];
|
|
$esMust = array_merge($esMust, $esTypeMedia);
|
|
}
|
|
|
|
// Filtre type de vue
|
|
$tmp = array_search('typevue_id', array_column($filters, 'type'));
|
|
if ($tmp !== false) {
|
|
$oTmp = [];
|
|
foreach ($filters[$tmp]['value'] as $type) {
|
|
$oTmp = array_merge($oTmp, [
|
|
[
|
|
'match' => [
|
|
'typevue_id' => $type['codeTypevue']
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
$esTypeVue = [
|
|
[
|
|
'bool' => [
|
|
'should' => $oTmp
|
|
]
|
|
]
|
|
];
|
|
$esMust = array_merge($esMust, $esTypeVue);
|
|
}
|
|
|
|
// Filtre liason avec Arcadia
|
|
$tmp = array_search('arcadia_id', array_column($filters, 'type'));
|
|
if ($tmp !== false) {
|
|
$esArcadia = [
|
|
[
|
|
'exists' => [
|
|
'field' => 'arcadia_id'
|
|
]
|
|
]
|
|
];
|
|
$esMust = array_merge($esMust, $esArcadia);
|
|
}
|
|
|
|
// Filtre etiquette
|
|
$tmp = array_search('etiquette_id', array_column($filters, 'type'));
|
|
if ($tmp !== false) {
|
|
$oTmp = [];
|
|
foreach ($filters[$tmp]['value'] as $item) {
|
|
$oTmp = array_merge($oTmp, [
|
|
[
|
|
'match' => [
|
|
'etiquette_id' => $item['etiquette_id']
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
$esEtiquette = [
|
|
[
|
|
'bool' => [
|
|
'should' => $oTmp
|
|
]
|
|
]
|
|
];
|
|
$esMust = array_merge($esMust, $esEtiquette);
|
|
}
|
|
|
|
$query = array(
|
|
'bool' => array(
|
|
'must' => $esMust,
|
|
'must_not' => $esMustNot,
|
|
'should' => $esShould,
|
|
'filter' => $esFilters
|
|
),
|
|
);
|
|
|
|
$sort = [
|
|
[
|
|
$orderField => [
|
|
'order' => $order
|
|
]
|
|
]
|
|
];
|
|
|
|
$response['data'] = $query = Media::searchByQuery($query, null, null, $limit, $offset, $sort);
|
|
|
|
if (Auth::user()->hasRole([config('roles.const.ADMIN'), config('roles.const.REDACTEUR')])) {
|
|
$response['draggable'] = true;
|
|
} else {
|
|
$response['draggable'] = false;
|
|
}
|
|
$response['isDepot'] = $isDepot;
|
|
|
|
if (isset($isCAQuery)){
|
|
$response['isCAQuery'] = $isCAQuery;
|
|
}
|
|
|
|
$response['role'] = Auth::user()->role;
|
|
|
|
return json_encode($response);
|
|
}
|
|
|
|
/**
|
|
* Déplacement des médias
|
|
*
|
|
*/
|
|
public function moveMedia(Request $request)
|
|
{
|
|
Auth::user()->authorizeRoles([config('roles.const.ADMIN'), config('roles.const.REDACTEUR')]);
|
|
$response = false;
|
|
$dossier_id = $request->input('dossier_id');
|
|
$medias = $request->input('medias');
|
|
|
|
$user_id = Auth::user()->id;
|
|
$libelle = "";
|
|
|
|
if ($dossier_id > 0) {
|
|
$dossier = Dossier::where('id', $dossier_id)->first();
|
|
if ($dossier) {
|
|
$libelle = $dossier->libelle;
|
|
}
|
|
}
|
|
|
|
foreach ($medias as $media) {
|
|
$media = Media::where('id', $media['id'])->first();
|
|
if ($media) {
|
|
$historique = new historique();
|
|
$historique->user_id = $user_id;
|
|
$historique->media_id = $media->id;
|
|
$historique->type = "Déplacement";
|
|
|
|
if ($dossier_id > 0) {
|
|
$media->dossier_id = $dossier_id;
|
|
$historique->description = "Déplacement dans le dossier " . $libelle;
|
|
} else {
|
|
$media->dossier_id = null;
|
|
$historique->description = "Média déplacé à la racine";
|
|
}
|
|
$media->save();
|
|
|
|
// Elasticsearch
|
|
$id = $media->id;
|
|
$media = Media::find($id);
|
|
$media->removeFromIndex();
|
|
$media->addToIndex();
|
|
|
|
// Lien Arcadia
|
|
ArcadiaLink::update($media);
|
|
|
|
$historique->save();
|
|
}
|
|
}
|
|
|
|
return json_encode($response);
|
|
}
|
|
|
|
/**
|
|
* supprimer des médias
|
|
*
|
|
*/
|
|
public function removeMedia(Request $request)
|
|
{
|
|
$deletedMediasList = array();
|
|
Auth::user()->authorizeRoles([config('roles.const.ADMIN'), config('roles.const.REDACTEUR')]);
|
|
$user_id = Auth::user()->id;
|
|
$deleteMedias = $request->input('medias');
|
|
|
|
if (Auth::user()->hasRole(config('roles.const.ADMIN'))){
|
|
$medias = Media::whereIn('id', $deleteMedias)->get();
|
|
}else{
|
|
$medias = Media::whereIn('id', $deleteMedias)->whereNull('arcadia_id')->get();
|
|
}
|
|
|
|
foreach ($medias as $media) {
|
|
$deletedMediasList[] = $media->id;
|
|
|
|
// Historique des actions
|
|
$historique = new historique();
|
|
$historique->user_id = $user_id;
|
|
$historique->media_id = $media->id;
|
|
$historique->type = "Suppression";
|
|
$historique->description = "Le média a été supprimé";
|
|
$historique->save();
|
|
|
|
// Elasticsearch
|
|
$media->removeFromIndex();
|
|
|
|
$media->delete();
|
|
|
|
// Lien Arcadia
|
|
ArcadiaLink::delete($media);
|
|
}
|
|
|
|
return json_encode($deletedMediasList);
|
|
}
|
|
|
|
/**
|
|
* Récupération d'un média
|
|
*/
|
|
|
|
public function recoverMedia(Request $request)
|
|
{
|
|
$arcadia_id = $request->arcadia_id;
|
|
$media = Media::onlyTrashed()->where('arcadia_id', '=', $arcadia_id)->first();
|
|
$media->restore();
|
|
$media->isLocked = 1;
|
|
$media->save();
|
|
|
|
// Elasticsearch
|
|
$id = $media->id;
|
|
$media = Media::find($id);
|
|
$media->addToIndex();
|
|
|
|
// Lien Arcadia
|
|
ArcadiaLink::reactivate($media);
|
|
|
|
return json_encode(true);
|
|
}
|
|
|
|
/**
|
|
* Obtenir toutes les informations d'un média
|
|
*
|
|
*/
|
|
public function getOneMedia(Request $request)
|
|
{
|
|
$host = $request->getSchemeAndHttpHost();
|
|
$response = array();
|
|
$media_id = $request->input('media_id');
|
|
$media = Media::where('id', $media_id)->with('sujet', 'societes', 'marques', 'articles', 'tags:libelle')->first();
|
|
if ($media) {
|
|
// stock le média
|
|
$response['data'] = $media;
|
|
// stock les tags
|
|
foreach ($media->tags as $tag) {
|
|
$response['tags'][] = $tag->libelle;
|
|
}
|
|
// stock les vignettes existantes
|
|
$files = Storage::files($media->folder . "/");
|
|
$filepath = preg_quote($media->folder . $media->uid, '/');
|
|
$matchingFiles = preg_grep('/^' . $filepath . '/', $files);
|
|
foreach ($matchingFiles as $matchingFile) {
|
|
$path_parts = pathinfo($matchingFile);
|
|
$fileInfo = array();
|
|
$fileInfo['url'] = $host . "/" . $matchingFile;
|
|
$fileInfo['libelle'] = $path_parts['filename'];
|
|
$fileInfo['extension'] = $path_parts['extension'];
|
|
$fileInfo['resolution'] = "";
|
|
|
|
if (in_array($path_parts['extension'], array("gif", "jpeg", "jpg", "png", "pdf", "ai", "psd"))) {
|
|
$im = new \imagick($matchingFile);
|
|
$im->setIteratorIndex(0);
|
|
$imageprops = $im->getImageGeometry();
|
|
$fileInfo['resolution'] = $imageprops['width'] . "x" . $imageprops['height'] . "px";
|
|
}
|
|
|
|
|
|
if (strpos($fileInfo['libelle'], '_')) {
|
|
$fileInfo['libelle'] = "Format " . substr($fileInfo['libelle'], strpos($fileInfo['libelle'], '_') + 1);
|
|
} else {
|
|
$fileInfo['libelle'] = "Format original";
|
|
}
|
|
$response['vignettes'][] = $fileInfo;
|
|
}
|
|
}
|
|
|
|
return json_encode($response);
|
|
}
|
|
|
|
/**
|
|
* Sauvegarde toutes les informations d'un média
|
|
*
|
|
*/
|
|
public function saveMedia(Request $request)
|
|
{
|
|
Auth::user()->authorizeRoles([config('roles.const.ADMIN'), config('roles.const.REDACTEUR'), config('roles.const.CONTRIBUTEUR')]);
|
|
$user_id = Auth::user()->id;
|
|
$mediaUpdate = (object)$request->input('media');
|
|
|
|
$media = Media::where('id', $mediaUpdate->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."));
|
|
}
|
|
|
|
$media->libelle = mb_substr($mediaUpdate->libelle, 0, 50, "UTF-8");
|
|
$media->typevue_id = $mediaUpdate->typevue_id;
|
|
$media->variante_id = $mediaUpdate->variante_id;
|
|
$media->langue_id = $mediaUpdate->langue_id;
|
|
$media->commentaire = $mediaUpdate->commentaire;
|
|
$media->version = $mediaUpdate->version;
|
|
$media->published_at = $mediaUpdate->published_at;
|
|
$media->statut = $mediaUpdate->statut;
|
|
$media->copyright_id = $mediaUpdate->copyright_id;
|
|
|
|
|
|
// ---- Historique des actions (etiquette seulement)
|
|
if ($media->etiquette_id != $mediaUpdate->etiquette_id) {
|
|
$curEtiquette = Etiquette::where('etiquette_id', $media->etiquette_id)->first();
|
|
$newEtiquette = Etiquette::where('etiquette_id', $mediaUpdate->etiquette_id)->first();
|
|
$historique = new historique();
|
|
$historique->user_id = $user_id;
|
|
$historique->media_id = $media->id;
|
|
$historique->type = "Etiquette";
|
|
$historique->description = $curEtiquette->libelle . " -> " . $newEtiquette->libelle;
|
|
$historique->save();
|
|
$media->etiquette_id = $mediaUpdate->etiquette_id;
|
|
}
|
|
|
|
// Si lié à Arcadia on bloque le media en attente du retour
|
|
if ($media->arcadia_id !== null)
|
|
$media->isLocked = 1;
|
|
|
|
$media->save();
|
|
|
|
// MAJ des tags
|
|
$media->tags()->detach();
|
|
if (isset($mediaUpdate->tags)) {
|
|
foreach ($mediaUpdate->tags as $tag) {
|
|
$oTag = Tag::where('libelle', $tag['libelle'])->first();
|
|
if (!$oTag) {
|
|
$oTag = new Tag();
|
|
$oTag->libelle = $tag['libelle'];
|
|
$oTag->save();
|
|
}
|
|
$media->tags()->attach($oTag->id);
|
|
}
|
|
}
|
|
|
|
|
|
// ---- Les sociétés
|
|
$media->societes()->detach();
|
|
$sSocietes = $mediaUpdate->societes;
|
|
if (!empty($sSocietes)) {
|
|
foreach ($sSocietes as $societeId) {
|
|
$media->societes()->attach($societeId['id']);
|
|
}
|
|
}
|
|
|
|
// ---- Les marques
|
|
$media->marques()->detach();
|
|
$sMarques = $mediaUpdate->marques;
|
|
if (!empty($sMarques)) {
|
|
foreach ($sMarques as $marqueId) {
|
|
$media->marques()->attach($marqueId['codMarq']);
|
|
}
|
|
}
|
|
|
|
// ---- Les articles
|
|
$media->articles()->detach();
|
|
$sArticles = $mediaUpdate->articles;
|
|
if (!empty($sArticles)) {
|
|
foreach ($sArticles as $articleId) {
|
|
$media->articles()->attach($articleId['id']);
|
|
}
|
|
}
|
|
|
|
// ---- Index dans Elasticsearch
|
|
$id = $media->id;
|
|
$media = Media::find($id);
|
|
$media->removeFromIndex();
|
|
$media->addToIndex();
|
|
|
|
// Lien Arcadia
|
|
ArcadiaLink::update($media);
|
|
|
|
// ---- Historique des actions
|
|
$historique = new historique();
|
|
$historique->user_id = $user_id;
|
|
$historique->media_id = $media->id;
|
|
$historique->type = "Modification";
|
|
$historique->description = "Les informations du média ont été modifiées";
|
|
$historique->save();
|
|
|
|
}
|
|
|
|
$response = array('status' => true, 'sujet_id' => $media->sujet_id, 'datas' => $media);
|
|
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("assets/images/default.jpg", $destinationPath . $filename);
|
|
|
|
$n->arcadia_id = NULL;
|
|
$n->libelle = (strlen($n->libelle) + 5) >= 50 ? substr($n->libelle, 0, -((strlen($n->libelle) - 50) + 5)) . '_copy' : $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);
|
|
//pour dev sous windows (pour cause de bug sur imagick avec les chemins relatifs)
|
|
//$im = new \imagick(realpath($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
|
|
*
|
|
*/
|
|
public function applySticker(Request $request)
|
|
{
|
|
Auth::user()->authorizeRoles([config('roles.const.ADMIN'), config('roles.const.REDACTEUR'), config('roles.const.CONTRIBUTEUR')]);
|
|
$medias = $request->input('medias');
|
|
$etiquette_id = $request->input('etiquette_id');
|
|
$user_id = Auth::user()->id;
|
|
|
|
$etiquette = Etiquette::where('etiquette_id', $etiquette_id)->first();
|
|
$response = $etiquette;
|
|
if ($etiquette) {
|
|
foreach ($medias as $item) {
|
|
$media_id = $item['id'];
|
|
$media = Media::where('id', $media_id)->first();
|
|
if ($media) {
|
|
// ---- Historique des actions
|
|
$curEtiquette = Etiquette::where('etiquette_id', $media->etiquette_id)->first();
|
|
$historique = new historique();
|
|
$historique->user_id = $user_id;
|
|
$historique->media_id = $media->id;
|
|
$historique->type = "Etiquette";
|
|
$historique->description = $curEtiquette->libelle . " -> " . $etiquette->libelle;
|
|
$historique->save();
|
|
|
|
// ---- Sauvegarde de la nouvelle étiquette
|
|
$media->etiquette_id = $etiquette_id;
|
|
$media->save();
|
|
|
|
// ---- Index dans Elasticsearch
|
|
$id = $media->id;
|
|
$media = Media::find($id);
|
|
$media->removeFromIndex();
|
|
$media->addToIndex();
|
|
|
|
// Lien Arcadia
|
|
ArcadiaLink::update($media);
|
|
}
|
|
}
|
|
}
|
|
return json_encode($response);
|
|
}
|
|
|
|
/**
|
|
* Vérification des clés pour liaison arcadia avant création, mise à jour
|
|
*
|
|
*/
|
|
public function checkRefKey(Request $request)
|
|
{
|
|
Auth::user()->authorizeRoles([config('roles.const.ADMIN')]);
|
|
$media_id = $request->input('media_id');
|
|
$media = Media::find($media_id);
|
|
//il faut check les dimensions de l'image
|
|
//pour les cas sujet, type vue, type media qui nous interesse
|
|
$code_sujet = $media->sujet_id;
|
|
$type_vue = $media->typevue_id;
|
|
$type_media = $media->typemedia_id;
|
|
$extension = $media->extension;
|
|
$largeur = $media->largeur;
|
|
$hauteur = $media->hauteur;
|
|
|
|
//on récupère les restrictions par défaut
|
|
$defaultwidth = Setting::where('key', 'default_size_restriction_width')->first();
|
|
if(empty($defaultwidth->value)) {
|
|
$checkwidth = null;
|
|
} else {
|
|
$checkwidth = $defaultwidth->value;
|
|
}
|
|
|
|
$defaultheight = Setting::where('key', 'default_size_restriction_height')->first();
|
|
if(empty($defaultheight->value)) {
|
|
$checkheight = null;
|
|
} else {
|
|
$checkheight = $defaultheight->value;
|
|
}
|
|
|
|
$defaultmime = Setting::where('key', 'default_mime_restriction')->first();
|
|
if(empty($defaultmime->value)) {
|
|
$checkmime = array();
|
|
} else {
|
|
$checkmime = explode(',', $defaultmime->value);
|
|
}
|
|
|
|
//si on est sur un sujet arcadia (donc pas le sujet Groupe par exemple)
|
|
if ($media->sujet->sujet_arcadia) {
|
|
//le check est requis
|
|
$format_restriction = FormatRestriction::query();
|
|
$format_restriction = $format_restriction->selectRaw(
|
|
'*,(CASE WHEN ISNULL(sujet_id) THEN 0 ELSE 3 END) + (CASE WHEN ISNULL(typevues_id) THEN 0 ELSE 2 END) + (CASE WHEN ISNULL(typemedias_id) THEN 0 ELSE 1 END) AS wght')
|
|
->where(
|
|
function ($q) use ($code_sujet) {
|
|
$q->where('sujet_id', $code_sujet)
|
|
->orWhereNull('sujet_id');
|
|
})
|
|
->where(
|
|
function ($q) use ($type_vue) {
|
|
$q->where('typevues_id', $type_vue)
|
|
->orWhereNull('typevues_id');
|
|
})
|
|
->where(
|
|
function ($q) use ($type_media) {
|
|
$q->where('typemedias_id', $type_media)
|
|
->orWhereNull('typemedias_id');
|
|
})
|
|
->orderBy('wght', 'DESC');
|
|
|
|
$format_restriction = $format_restriction->first();
|
|
|
|
if(!$format_restriction) {
|
|
//aucune nouvelle restriction trouvée
|
|
} else {
|
|
$checkwidth = $format_restriction->in_width;
|
|
$checkheight = $format_restriction->in_height;
|
|
if(empty($format_restriction->in_mimes)) {
|
|
$checkmime = array();
|
|
} else {
|
|
$checkmime = explode(',', $format_restriction->in_mimes);
|
|
}
|
|
|
|
}
|
|
|
|
//vérification du mime
|
|
if(in_array($extension, $checkmime) || empty($checkmime)) {
|
|
//pass
|
|
//verification de la largeur
|
|
if($largeur == $checkwidth || $largeur == 1800 || is_null($checkwidth)) {
|
|
//pass
|
|
//vérification hauteur
|
|
if($hauteur == $checkheight || $hauteur == 1800 || is_null($checkheight)) {
|
|
//pass
|
|
//tout est OK
|
|
} else {
|
|
//fail hauteur
|
|
return json_encode(array('status' => false, 'error' => "Les dimensions requises sont ${checkwidth}px par ${checkheight}px, veuillez charger un autre média"));
|
|
}
|
|
} else {
|
|
//fail largeur
|
|
return json_encode(array('status' => false, 'error' => "Les dimensions requises sont ${checkwidth}px par ${checkheight}px, veuillez charger un autre média"));
|
|
}
|
|
} else {
|
|
//fail type de fichier
|
|
return json_encode(array('status' => false, 'error' => "Le type de fichier doit être ". implode(', ', $checkmime) .", veuillez charger un autre média"));
|
|
}
|
|
}
|
|
|
|
if ($media) {
|
|
$refs = ArcadiaLink::getRefKey($media);
|
|
if (count($refs) > 0){
|
|
return 'true';
|
|
}else{
|
|
return 'false';
|
|
}
|
|
}
|
|
return 'false';
|
|
}
|
|
|
|
/**
|
|
* Applique un Id Arcadia à un média
|
|
*
|
|
*/
|
|
public function setIdArcdadia(Request $request)
|
|
{
|
|
Auth::user()->authorizeRoles([config('roles.const.ADMIN')]);
|
|
$response = false;
|
|
$media_id = $request->input('media_id');
|
|
|
|
$historique = new historique();
|
|
$historique->user_id = Auth::user()->id;
|
|
$historique->media_id = $media_id;
|
|
$historique->type = "Modification";
|
|
$historique->description = "Ajout de la liaison Arcadia";
|
|
|
|
$media = Media::where('id', $media_id)->first();
|
|
if ($media) {
|
|
$media->isLocked = 1;
|
|
$media->save();
|
|
|
|
// Lien Arcadia
|
|
ArcadiaLink::create($media);
|
|
|
|
// ---- Index dans Elasticsearch
|
|
$id = $media->id;
|
|
$media = Media::find($id);
|
|
$media->removeFromIndex();
|
|
$media->addToIndex();
|
|
|
|
$response = true;
|
|
$historique->save();
|
|
}
|
|
|
|
return json_encode($response);
|
|
}
|
|
|
|
/**
|
|
* Renvoie un article par ID Arcadia
|
|
*
|
|
*/
|
|
public function getArticle(Request $request)
|
|
{
|
|
if (!$user = Auth::user()) abort(401, 'Cette action n\'est pas autorisée.');
|
|
$response = array();
|
|
$arcadia_id = $request->input('arcadia_id');
|
|
|
|
$article = Article::where('noArt', $arcadia_id)->first();
|
|
if ($article) {
|
|
$response['article'] = $article;
|
|
} else {
|
|
$response = false;
|
|
}
|
|
return json_encode($response);
|
|
}
|
|
|
|
/**
|
|
* Téléchargement des médias
|
|
*
|
|
*/
|
|
public function downloadMedia(Request $request)
|
|
{
|
|
if (!$user = Auth::user()) abort(404, 'Erreur lors du téléchargement');
|
|
$deleteFileAfterSend = false;
|
|
// obtention du format
|
|
$formatId = $request->input('format');
|
|
|
|
if ($formatId != "original") {
|
|
$format = Format::withoutGlobalScope('App\Scopes\SystemScope')->where('id', $formatId)->first();
|
|
if (!$format) abort(404, 'Erreur lors du téléchargement');
|
|
} else {
|
|
$format = "original";
|
|
}
|
|
|
|
$mediasId = $request->input('medias');
|
|
$medias = explode(",", $mediasId);
|
|
if (sizeof($medias) == 1) {
|
|
$media = Media::where('id', $mediasId)->first();
|
|
if ($media) {
|
|
if ($format === 'original') {
|
|
$fp = public_path() . '/' . $media->folder . $media->uid . '.' . $media->extension;
|
|
}else{
|
|
$fp = $this->get_mediaPath($media, $format);
|
|
}
|
|
|
|
if (!file_exists($fp))
|
|
abort(404, 'Fichier inconnu.');
|
|
$dlFileName = Str::slug($media->libelle);
|
|
if ($format === 'original') {
|
|
return response()->download($fp, str_replace(' ', '_', $dlFileName)."_original_". $media->largeur ."x". $media->hauteur ."px.".pathinfo($fp, PATHINFO_EXTENSION))->deleteFileAfterSend($deleteFileAfterSend);
|
|
}else{
|
|
return response()->download($fp, str_replace(' ', '_', $dlFileName)."_". $format->libelle ."_". $format->largeur ."x". $format->hauteur ."px.".pathinfo($fp, PATHINFO_EXTENSION))->deleteFileAfterSend($deleteFileAfterSend);
|
|
}
|
|
}
|
|
} else {
|
|
// create zip archive
|
|
$zip_file = public_path() . '/uploads/DAM_' . uniqid() . '_' . date("Ymd") . '.zip';
|
|
$zip = new \ZipArchive();
|
|
$zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
|
|
|
|
// add file to archive
|
|
foreach ($medias as $mediaId) {
|
|
$media = Media::where('id', $mediaId)->first();
|
|
if ($media) {
|
|
|
|
if ($format === 'original') {
|
|
$fp = public_path() . '/' . $media->folder . $media->uid . '.' . $media->extension;
|
|
}else{
|
|
$fp = $this->get_mediaPath($media, $format);
|
|
}
|
|
if (!file_exists($fp)) {
|
|
$zip->close();
|
|
unlink($zip_file);
|
|
abort(404, 'Fichier(s) inconnu(s).');
|
|
}
|
|
$dlFileName = Str::slug($media->libelle);
|
|
if ($format === 'original') {
|
|
$zip->addFile($fp, str_replace(' ', '_', $dlFileName)."_original_". $media->largeur ."x". $media->hauteur ."px.".pathinfo($fp, PATHINFO_EXTENSION));
|
|
}else{
|
|
$zip->addFile($fp, str_replace(' ', '_', $dlFileName)."_". $format->libelle ."_". $format->largeur ."x". $format->hauteur ."px.".pathinfo($fp, PATHINFO_EXTENSION));
|
|
}
|
|
}
|
|
}
|
|
$deleteFileAfterSend = true;
|
|
$zip->close();
|
|
$fp = $zip_file;
|
|
return response()->download($fp)->deleteFileAfterSend($deleteFileAfterSend);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Renvoie les infos complémentaires d'un média
|
|
*
|
|
*/
|
|
public function getMediaOtherInfos(Request $request)
|
|
{
|
|
$media_id = $request->input('media_id');
|
|
if (!$user = Auth::user()) abort(401, 'Cette action n\'est pas autorisée.');
|
|
$media = Media::find($media_id);
|
|
if(!$media)
|
|
abort('404', "Ce média n'existe pas.");
|
|
$response = array();
|
|
$history = Historique::where('media_id', $media_id)->with('user')->orderBy("created_date", "DESC")->get();
|
|
$response['history'] = $history;
|
|
$response['vignettes'] = $this->getVignettesByMedia($media);
|
|
return json_encode($response);
|
|
}
|
|
|
|
private function getVignettesByMedia($media)
|
|
{
|
|
$vignettes = [];
|
|
|
|
$vignettes['original'] = [
|
|
'url' => config('app.url') . "/" . $media->folder . $media->uid . "." . $media->extension,
|
|
'extension' => $media->extension,
|
|
'largeur' => $media->largeur,
|
|
'hauteur' => $media->hauteur
|
|
];
|
|
|
|
if(in_array($media->extension, array("pdf", "ai", "psd")) ){
|
|
$sys_extension = "png";
|
|
}else{
|
|
$sys_extension = $media->extension;
|
|
}
|
|
|
|
$vignettes['preview'] = [
|
|
'url' => config('app.url') . "/" . $media->folder . $media->uid . "_preview" . "." . $sys_extension,
|
|
'extension' => $sys_extension,
|
|
'largeur' => '1800',
|
|
'hauteur' => '1800'
|
|
];
|
|
$vignettes['thumb'] = [
|
|
'url' => config('app.url') . "/" . $media->folder . $media->uid . "_thumb" . "." . $sys_extension,
|
|
'extension' => $sys_extension,
|
|
'largeur' => '200',
|
|
'hauteur' => '200'
|
|
];
|
|
|
|
$formats = $media->sujet->formats;
|
|
foreach ($formats as $format)
|
|
$vignettes[$format->libelle] = [
|
|
'url' => config('app.url') . "/" . $media->folder . $media->uid . "_" . $format->libelle . "." . $format->extension,
|
|
'extension' => $format->extension,
|
|
'largeur' => $format->largeur,
|
|
'hauteur' => $format->hauteur
|
|
];
|
|
return $vignettes;
|
|
}
|
|
}
|