177 lines
4 KiB
PHP
177 lines
4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\MediaStatut;
|
|
use App\Models\User;
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class Dossier extends Model
|
|
{
|
|
/**
|
|
* The relations to eager load on every query.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $with = [
|
|
'sousdossiers'
|
|
];
|
|
|
|
/**
|
|
* The accessors to append to the model's array form.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $appends = [
|
|
'favoris', 'mediacount'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'is_private' => 'bool'
|
|
];
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'libelle', 'description',
|
|
'is_private', 'parent_id'
|
|
];
|
|
|
|
// * GETTERS
|
|
|
|
/**
|
|
* Get user favoris
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function getFavorisAttribute(): bool
|
|
{
|
|
$userId = Auth::user()->id;
|
|
if ($this->users()->where('id', $userId)->first()) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get media linked
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function getMediacountAttribute(): int
|
|
{
|
|
/** @var \App\Model\User $user */
|
|
$user = Auth::user();
|
|
|
|
// ? Est un dossier dépot ou racine ?
|
|
if ($this->parent_id === 1 or $this->id === 1) {
|
|
// * Dossier dépôt
|
|
return $this->mediaList()->count();
|
|
}
|
|
|
|
// * Dossier classic
|
|
// ? User is admin ?
|
|
if ($user->hasRole(config('roles.const.ADMIN'))) {
|
|
// * Tout de tout le monde
|
|
return $this->mediaList()->count();
|
|
}
|
|
|
|
// * Tous les non brouillons ou crées par le user
|
|
return $this->mediaList()
|
|
->where(fn (Builder $q) => $q->where('statut', '!=', MediaStatut::brouillon)
|
|
->orWhere('created_by', $user->id))
|
|
->count();
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* BelongsToMany marque relation
|
|
* The marques that belong to the dossier.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function marques()
|
|
{
|
|
return $this->belongsToMany(Marque::class, 'marque_dossier', 'dossier_id', 'marque_id');
|
|
}
|
|
|
|
/**
|
|
* BelongsTo sujet relation
|
|
* Get the sujet that owns the dossier.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function sujet()
|
|
{
|
|
return $this->belongsTo(Sujet::class, 'sujet_id');
|
|
}
|
|
|
|
/**
|
|
* HasMany media relation
|
|
* Get the dossier for the media.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function mediaList()
|
|
{
|
|
return $this->hasMany(Media::class);
|
|
}
|
|
|
|
/**
|
|
* HasMany dossier relation
|
|
* Get the dossiers for the parent.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function sousdossiers()
|
|
{
|
|
return $this->hasMany(Dossier::class, 'parent_id');
|
|
}
|
|
|
|
/**
|
|
* BelongsTo dossier relation
|
|
* Get the parent that owns the dossiers.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function dossierParent()
|
|
{
|
|
return $this->belongsTo(Dossier::class, 'id', 'parent_id');
|
|
}
|
|
|
|
/**
|
|
* BelongsToMany user relation
|
|
* The users that belong to the dossier.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function users()
|
|
{
|
|
return $this->belongsToMany(User::class);
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Find Dossier by subject id
|
|
*
|
|
* @param integer $sujetId
|
|
* @return Dossier|null
|
|
*/
|
|
public static function getDossierBySujetid(int $sujetId)
|
|
{
|
|
return Dossier::where('sujet_id', $sujetId)->whereNull('parent_id')->get();
|
|
}
|
|
}
|