ldc-dam/app/Models/Dossier.php

178 lines
4 KiB
PHP
Raw Normal View History

2019-05-16 17:20:30 +02:00
<?php
namespace App\Models;
2019-05-16 17:20:30 +02:00
2022-07-26 13:20:21 +02:00
use App\Enums\MediaStatut;
2022-04-19 14:49:09 +02:00
use App\Models\User;
2023-12-14 15:47:30 +01:00
use Illuminate\Contracts\Database\Eloquent\Builder;
2019-05-16 17:20:30 +02:00
use Illuminate\Database\Eloquent\Model;
2019-05-22 17:10:16 +02:00
use Illuminate\Support\Facades\Auth;
2019-05-16 17:20:30 +02:00
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();
2023-12-14 15:47:30 +01:00
// ? 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()
2023-12-14 15:47:30 +01:00
->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
2019-05-17 11:35:41 +02:00
* Get the sujet that owns the dossier.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
2019-05-17 11:35:41 +02:00
*/
public function sujet()
{
return $this->belongsTo(Sujet::class, 'sujet_id');
2019-05-17 11:35:41 +02:00
}
/**
* HasMany media relation
* Get the dossier for the media.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function mediaList()
{
2023-12-14 15:47:30 +01:00
return $this->hasMany(Media::class);
}
2019-05-22 17:10:16 +02:00
/**
* HasMany dossier relation
2019-05-17 11:35:41 +02:00
* Get the dossiers for the parent.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
2019-05-17 11:35:41 +02:00
*/
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');
2019-05-17 11:35:41 +02:00
}
/**
* 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();
}
2019-05-16 17:20:30 +02:00
}