ldc-dam/app/Models/Dossier.php
Jérémy Munsch 9767e62c72
Update NPM deps and webpack.mix.js
composer require ldc/web-status-checker-laravel remove Old static status checker

Fix ImportMedia

WIP commands OK

many fixes

Fix and try to optimize folder component

Fix front icons + actions based on icons

Favico

front fix alert

Fix Media upload on depot

fix folder component buttons

Full front fix with perf due tu vuejs

Download medias

Navigate within medias superfix

Move media + loader

isLocked boolean and remove media fix

Folder scroll fix

arcadia link color red

Fix panier show external

Tons of fixes back as front

REwrited all
2022-07-22 17:00:48 +02:00

170 lines
3.8 KiB
PHP

<?php
namespace App\Models;
use App\Models\User;
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();
// * dossier classic
if ($this->parent_id != 1 && $this->id != 1) {
if ($user->hasRole(config('roles.const.ADMIN'))) {
return $this->mediaList()->count();
} else {
return $this->mediaList()
->where('statut', '<>', config('services.statut.brouillon'))
->orWhere('created_by', $user->id)->count();
}
} else {
// * dossier dépôt
return $this->mediaList()->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, 'dossier_id', 'id');
}
/**
* 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();
}
}