1306 lines
45 KiB
PHP
1306 lines
45 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 App\Models\Sujet;
|
|
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;
|
|
|
|
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');
|
|
$sujet_id = $request->get('s');
|
|
$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,
|
|
]);
|
|
}
|
|
|
|
public function show(Request $request) {
|
|
$uid = $request->uid;
|
|
$media = Media::where('uid', $uid)->first();
|
|
if (!$media)
|
|
abort(404);
|
|
$infos = [
|
|
'id' => $media->id,
|
|
'preview' => $media->folder . $media->uid . '_preview.' . $media->extension,
|
|
'src' => $media->folder . $media->uid . $media->extension,
|
|
'depot' => 0,
|
|
'typemedia' => $media->typemedia_id,
|
|
'ext' => $media->extension,
|
|
'dossier_id' => $media->dossier_id
|
|
];
|
|
return redirect()->route('media_manager')->with('autoOpen', $infos);
|
|
}
|
|
|
|
/**
|
|
* Chargement des médias
|
|
*
|
|
*/
|
|
public function loadMedia(Request $request)
|
|
{
|
|
$response = array();
|
|
$userId = Auth::user()->id;
|
|
$dossier_id = $request->input('dossier_id');
|
|
$sujet_id = $request->input('sujet_id');
|
|
$filters = json_decode($request->input('filters'));
|
|
$offset = $request->input('offset');
|
|
$limit = $request->input('limit');
|
|
$orderField = $request->input('orderField');
|
|
$order = $request->input('order');
|
|
$saveSearch = $request->input('saveSearch');
|
|
$isDepot = 0;
|
|
|
|
$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
|
|
]
|
|
]
|
|
]);
|
|
|
|
/*$query= Media::where('sujet_id', $sujet_id)
|
|
->whereNull('dossier_id')
|
|
->where(function ($query) use ( $userId ) {
|
|
$query->where('statut', config('services.statut.public_etendu'))
|
|
->orWhere('statut', config('services.statut.public'))
|
|
->orWhere('created_by', $userId);
|
|
}); */
|
|
} elseif ($dossier_id == "-1") { // recherche global
|
|
|
|
$esMust = array_merge($esMust, [
|
|
[
|
|
'bool' => [
|
|
'should' => [
|
|
[
|
|
'match' => [
|
|
'sujet_id' => $sujet_id
|
|
]
|
|
],
|
|
[
|
|
'match' => [
|
|
'statut' => config('services.statut.public_etendu')
|
|
]
|
|
]
|
|
]
|
|
]
|
|
]
|
|
]);
|
|
|
|
$esShould = array_merge($esShould, [
|
|
[
|
|
'match' => [
|
|
'created_by' => $userId
|
|
]
|
|
]
|
|
]);
|
|
|
|
/*
|
|
$query = Media::where('sujet_id', $sujet_id)
|
|
->where(function ($query) use ( $userId ) {
|
|
$query->where('statut', config('services.statut.public_etendu'))
|
|
->orWhere('created_by', $userId);
|
|
});*/
|
|
} 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
|
|
]
|
|
]
|
|
));
|
|
|
|
/*
|
|
$query = Media::where('sujet_id', $sujet_id)
|
|
->where('dossier_id', $dossier_id)
|
|
->where(function ($query) use ( $userId ) {
|
|
$query->where('statut', '<>', config('services.statut.brouillon'))
|
|
->orWhere('created_by', $userId);
|
|
});*/
|
|
} else { // dossier dépôt
|
|
$isDepot = 1;
|
|
$esMust = array_merge($esMust, [
|
|
[
|
|
'match' => [
|
|
'dossier_id' => $dossier_id
|
|
]
|
|
]
|
|
]);
|
|
|
|
//$query= Media::where('dossier_id', $dossier_id);
|
|
}
|
|
}
|
|
|
|
// Filtre recherche texte
|
|
if (property_exists($filters, 'search')) {
|
|
$tmpFilters = [];
|
|
$search = strtolower(trim($filters->search->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;
|
|
}
|
|
}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
|
|
]
|
|
]
|
|
]
|
|
]
|
|
]
|
|
];
|
|
|
|
//dd($tmpFilters);exit;
|
|
}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);
|
|
|
|
/*
|
|
$query->where(function ($query) use ( $search ) {
|
|
$query->whereRaw("LCASE(libelle) LIKE '%".$search."%'")
|
|
->orWhere('arcadia_id', $search)
|
|
->orWhereHas("articles", function($query) use ($search){
|
|
$query->where("noArt", $search)
|
|
->orWhereRaw("LCASE(libCcial) LIKE '%".$search."%'")
|
|
->orWhereRaw("LCASE(libMarq) LIKE '%".$search."%'")
|
|
->orWhereRaw("LCASE(libFamVte) LIKE '%".$search."%'")
|
|
->orWhere('codGencodAdm', $search)
|
|
->orWhere('codGencodSymb', $search)
|
|
->orWhere('codGencodPds', $search);
|
|
})
|
|
->orWhereHas("tags", function($query) use ($search){
|
|
$query->WhereRaw("LCASE(libelle) LIKE '%".$search."%'");
|
|
});
|
|
});
|
|
*/
|
|
|
|
// ---- 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
|
|
if (property_exists($filters, 'typemedia_id')) {
|
|
$tmp = [];
|
|
foreach ($filters->typemedia_id->value as $type) {
|
|
$tmp = array_merge($tmp, [
|
|
[
|
|
'match' => [
|
|
'typemedia_id' => $type
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
$esTypeMedia = [
|
|
[
|
|
'bool' => [
|
|
'should' => $tmp
|
|
]
|
|
]
|
|
];
|
|
$esMust = array_merge($esMust, $esTypeMedia);
|
|
|
|
//$query->whereIn('typemedia_id', $filters->typemedia_id->value);
|
|
}
|
|
|
|
// Filtre type de vue
|
|
if (property_exists($filters, 'typevue_id')) {
|
|
$tmp = [];
|
|
foreach ($filters->typevue_id->value as $type) {
|
|
$tmp = array_merge($tmp, [
|
|
[
|
|
'match' => [
|
|
'typevue_id' => $type
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
$esTypeVue = [
|
|
[
|
|
'bool' => [
|
|
'should' => $tmp
|
|
]
|
|
]
|
|
];
|
|
$esMust = array_merge($esMust, $esTypeVue);
|
|
|
|
//$query->whereIn('typevue_id', $filters->typevue_id->value);
|
|
}
|
|
|
|
// Filtre liason avec Arcadia
|
|
if (property_exists($filters, 'arcadia_id')) {
|
|
$esArcadia = [
|
|
[
|
|
'exists' => [
|
|
'field' => 'arcadia_id'
|
|
]
|
|
]
|
|
];
|
|
$esMust = array_merge($esMust, $esArcadia);
|
|
//$query->whereNotNull('arcadia_id' );
|
|
}
|
|
|
|
// Filtre etiquette
|
|
if (property_exists($filters, 'etiquette_id')) {
|
|
$tmp = [];
|
|
foreach ($filters->etiquette_id->value as $id) {
|
|
$tmp = array_merge($tmp, [
|
|
[
|
|
'match' => [
|
|
'etiquette_id' => $id
|
|
]
|
|
]
|
|
]);
|
|
}
|
|
$esEtiquette = [
|
|
[
|
|
'bool' => [
|
|
'should' => $tmp
|
|
]
|
|
]
|
|
];
|
|
$esMust = array_merge($esMust, $esEtiquette);
|
|
//$query->whereIn('etiquette_id', $filters->etiquette_id->value);
|
|
}
|
|
|
|
/*
|
|
$query->with('Etiquette')
|
|
->offset($offset)
|
|
->limit($limit)
|
|
->orderBy($orderField, $order);
|
|
$response['data'] = $query->get(); */
|
|
|
|
$query = array(
|
|
'bool' => array(
|
|
'must' => $esMust,
|
|
'must_not' => $esMustNot,
|
|
'should' => $esShould,
|
|
'filter' => $esFilters
|
|
),
|
|
);
|
|
|
|
//echo json_encode($query); exit;
|
|
|
|
$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;
|
|
}
|
|
|
|
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');
|
|
$data = $request->input('data');
|
|
$sujet_id = $request->input('sujet_id');
|
|
$data = explode(",", $data);
|
|
$user_id = Auth::user()->id;
|
|
$libelle = "";
|
|
|
|
if ($dossier_id > 0) {
|
|
$dossier = Dossier::where('id', $dossier_id)->first();
|
|
if ($dossier) {
|
|
$libelle = $dossier->libelle;
|
|
}
|
|
}
|
|
|
|
foreach ($data as $key => $media_id) {
|
|
$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";
|
|
|
|
$media->sujet_id = $sujet_id;
|
|
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 = $mediaUpdate->libelle;
|
|
$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;
|
|
}*/
|
|
|
|
$media->save();
|
|
|
|
// MAJ des tags
|
|
$media->tags()->detach();
|
|
if (isset($mediaUpdate->tags)) {
|
|
foreach ($mediaUpdate->tags as $tag) {
|
|
$oTag = Tag::where('libelle', $tag)->first();
|
|
if (!$oTag) {
|
|
$oTag = new Tag();
|
|
$oTag->libelle = $tag;
|
|
$oTag->save();
|
|
}
|
|
$media->tags()->attach($oTag->id);
|
|
}
|
|
}
|
|
|
|
|
|
// ---- Les sociétés
|
|
$media->societes()->detach();
|
|
$sSocietes = $mediaUpdate->societes;
|
|
if ($sSocietes != "") {
|
|
$societes = explode(",", $sSocietes);
|
|
foreach ($societes as $societeId) {
|
|
$media->societes()->attach($societeId);
|
|
}
|
|
}
|
|
|
|
// ---- Les marques
|
|
$media->marques()->detach();
|
|
$sMarques = $mediaUpdate->marques;
|
|
if ($sMarques != "") {
|
|
$marques = explode(",", $sMarques);
|
|
foreach ($marques as $marqueId) {
|
|
$media->marques()->attach($marqueId);
|
|
}
|
|
}
|
|
|
|
// ---- Les articles
|
|
$media->articles()->detach();
|
|
$sArticles = $mediaUpdate->articles;
|
|
if ($sArticles) {
|
|
$articles = explode(",", $sArticles);
|
|
foreach ($articles as $articleId) {
|
|
$media->articles()->attach($articleId);
|
|
}
|
|
}
|
|
|
|
// ---- 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 on é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("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
|
|
*
|
|
*/
|
|
public function applySticker(Request $request)
|
|
{
|
|
Auth::user()->authorizeRoles([config('roles.const.ADMIN'), config('roles.const.REDACTEUR'), config('roles.const.CONTRIBUTEUR')]);
|
|
$response = array();
|
|
$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 $media_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);
|
|
}
|
|
|
|
/**
|
|
* Sauvegarde une retouche photo
|
|
*
|
|
*/
|
|
public function editPhoto(Request $request)
|
|
{
|
|
Auth::user()->authorizeRoles([config('roles.const.ADMIN'), config('roles.const.REDACTEUR'), config('roles.const.CONTRIBUTEUR')]);
|
|
$response = array();
|
|
$media_id = $request->input('media_id');
|
|
$cropData = $request->input('cropData');
|
|
|
|
$media = Media::where('id', $media_id)->first();
|
|
if ($media) {
|
|
$mediaPath = $media->folder . $media->uid . "." . $media->extension;
|
|
|
|
$im = new \imagick($mediaPath);
|
|
$im->setImageUnits(\imagick::RESOLUTION_PIXELSPERINCH);
|
|
$im->setImageFormat($media->extension);
|
|
list($top, $left, $width, $height) = $cropData;
|
|
|
|
$left = round($left * $media->largeur);
|
|
$top = round($top * $media->hauteur);
|
|
$width = round(min($width * $media->largeur, $media->largeur - $left));
|
|
$height = round(min($height * $media->hauteur, $media->hauteur - $top));
|
|
|
|
$im->setImagePage(0, 0, 0, 0);
|
|
$im->setImageResolution($media->resolution, $media->resolution);
|
|
$im->cropImage($width, $height, $left, $top);
|
|
Storage::put($mediaPath, $im->getImageBlob());
|
|
$im->destroy();
|
|
|
|
$im = new \imagick($mediaPath);
|
|
$imageprops = $im->getImageGeometry();
|
|
$media->largeur = $imageprops['width'];
|
|
$media->hauteur = $imageprops['height'];
|
|
|
|
$resolutions = $im->getImageResolution();
|
|
$media->resolution = round($resolutions['x'] * 2.54, 2);
|
|
if ($media->resolution != 0) {
|
|
$media->largeur_mm = ceil(($media->largeur * 2.54 / $media->resolution) * 10);
|
|
$media->hauteur_mm = ceil(($media->hauteur * 2.54 / $media->resolution) * 10);
|
|
} else {
|
|
$media->largeur_mm = "";
|
|
$media->hauteur_mm = "";
|
|
}
|
|
$media->taille = $this->formatBytes($this->get_file_size($mediaPath));
|
|
$media->save();
|
|
|
|
// ---- Index dans Elasticsearch
|
|
$id = $media->id;
|
|
$media = Media::find($id);
|
|
$media->removeFromIndex();
|
|
$media->addToIndex();
|
|
|
|
$historique = new historique();
|
|
$historique->user_id = Auth::user()->id;;
|
|
$historique->media_id = $media->id;
|
|
$historique->type = "Retouche";
|
|
$historique->description = "Retouche de l'image";
|
|
$historique->save();
|
|
|
|
// Regénération des miniatures de base
|
|
$this->create_sys_thumb($media);
|
|
// ---- Creation des vignettes en lien avec le sujet du média
|
|
$this->create_thumb($media);
|
|
$response = $media;
|
|
|
|
// Lien Arcadia
|
|
ArcadiaLink::update($media);
|
|
}
|
|
|
|
return json_encode($response);
|
|
}
|
|
|
|
public function checkRefKey(Request $request)
|
|
{
|
|
Auth::user()->authorizeRoles([config('roles.const.ADMIN')]);
|
|
$media_id = $request->input('media_id');
|
|
$media = Media::find($media_id);
|
|
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 != 0) {
|
|
$format = Format::withoutGlobalScope('App\Scopes\SystemScope')->where('id', $formatId)->first();
|
|
if (!$format) abort(404, 'Erreur lors du téléchargement');
|
|
} else {
|
|
$format = "";
|
|
}
|
|
|
|
$mediasId = $request->input('medias');
|
|
$medias = explode(",", $mediasId);
|
|
if (sizeof($medias) == 1) {
|
|
$media = Media::where('id', $mediasId)->first();
|
|
if ($media) {
|
|
$fp = $this->get_mediaPath($media, $format);
|
|
if (!file_exists($fp))
|
|
abort(404, 'Fichier inconnu.');
|
|
}
|
|
} 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) {
|
|
$fp = $this->get_mediaPath($media, $format);
|
|
if (!file_exists($fp)) {
|
|
$zip->close();
|
|
unlink($zip_file);
|
|
abort(404, 'Fichier(s) inconnu(s).');
|
|
}
|
|
$zip->addFile($fp, basename($fp));
|
|
}
|
|
}
|
|
$deleteFileAfterSend = true;
|
|
$zip->close();
|
|
$fp = $zip_file;
|
|
}
|
|
return response()->download($fp)->deleteFileAfterSend($deleteFileAfterSend);
|
|
}
|
|
|
|
|
|
/**
|
|
* Renvoie l'historique des actions d'un média
|
|
*
|
|
*/
|
|
public function getMediaHistory(Request $request)
|
|
{
|
|
$response = array();
|
|
if (!$user = Auth::user()) abort(401, 'Cette action n\'est pas autorisée.');
|
|
$media_id = $request->input('media_id');
|
|
|
|
$media = Historique::where('media_id', $media_id)->with('user')->orderBy("created_date", "DESC")->get();
|
|
$response = $media;
|
|
return json_encode($response);
|
|
}
|
|
|
|
/**
|
|
* Ajoute un média sans fichier
|
|
*
|
|
*/
|
|
public function addMedia(Request $request)
|
|
{
|
|
$response = false;
|
|
$sujet_id = $request->input('sujet_id');
|
|
$dossier_id = $request->input('dossier_id');
|
|
if ($dossier_id == 0) $dossier_id = NULL;
|
|
|
|
// On déplace le média par défaut dans le répertoire des médias
|
|
$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);
|
|
|
|
|
|
// on créé un média en bdd
|
|
$media = new Media();
|
|
$media->dossier_id = $dossier_id;
|
|
$media->sujet_id = $sujet_id;
|
|
$media->libelle = "Nouveau média";
|
|
$media->folder = $destinationPath;
|
|
$media->taille = $this->formatBytes($this->get_file_size($destinationPath . $filename));
|
|
if ($user = Auth::user()) {
|
|
$media->created_by = $user->id;
|
|
}
|
|
$media->uid = $uid;
|
|
$media->extension = "jpg";
|
|
$media->typemedia_id = 1;
|
|
$im = new \imagick($destinationPath . $filename);
|
|
$resolutions = $im->getImageResolution();
|
|
$media->resolution = round($resolutions['x'] * 2.54, 2);
|
|
$imageprops = $im->getImageGeometry();
|
|
$media->largeur = $imageprops['width'];
|
|
$media->hauteur = $imageprops['height'];
|
|
|
|
if ($media->resolution != 0) {
|
|
$media->largeur_mm = ceil(($media->largeur * 2.54 / $media->resolution) * 10);
|
|
$media->hauteur_mm = ceil(($media->hauteur * 2.54 / $media->resolution) * 10);
|
|
} else {
|
|
$media->largeur_mm = "";
|
|
$media->hauteur_mm = "";
|
|
}
|
|
|
|
// Par défaut FR
|
|
$media->langue_id = 1;
|
|
// Par défaut propriété de la marque
|
|
$media->copyright_id = 3;
|
|
$media->isDefault = 1;
|
|
$media->save();
|
|
|
|
$id = $media->id;
|
|
$media = Media::find($id);
|
|
|
|
// ---- Index dans Elasticsearch
|
|
$media = Media::where('id', $media->id)->first();
|
|
$media->addToIndex();
|
|
|
|
$response = $media;
|
|
$im->destroy();
|
|
|
|
// ---- Creation des vignettes de base
|
|
$this->create_sys_thumb($media);
|
|
// ---- Creation des vignettes en lien avec le sujet du média
|
|
if (isset($sujet_id)) {
|
|
$this->create_thumb($media);
|
|
}
|
|
|
|
// ---- Historique des actions
|
|
$historique = new historique();
|
|
if ($user = Auth::user()) {
|
|
$historique->user_id = $user->id;
|
|
}
|
|
$historique->media_id = $media->id;
|
|
$historique->type = "Création";
|
|
$historique->description = "Média ajouté";
|
|
$historique->save();
|
|
|
|
return json_encode($response);
|
|
}
|
|
}
|