ldc-supportweb/app/Models/File.php
Jérémy Munsch 18f104ae80
phpcs:pass
2022-08-30 09:20:32 +02:00

190 lines
5.4 KiB
PHP

<?php
namespace App\Models;
use App\Enums\FileCategory;
use App\Enums\FileType;
use App\Events\FileCreated;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
use Kwaadpepper\CrudPolicies\Enums\CrudType;
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
use Kwaadpepper\Enum\Traits\CastsEnums;
class File extends Model
{
use CastsEnums;
use HasFactory;
use IsCrudModel;
/**
* The attributes that should be cast to Enums.
*
* @var array
*/
protected $enumCasts = [
'category' => FileCategory::class,
'type' => FileType::class
];
/**
* Crud label column
*
* @var string
*/
protected $crudLabelColumn = 'name';
/**
* Crud value column
*
* @var string
*/
protected $crudValueColumn = 'id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['type', 'uri', 'file', 'name', 'category'];
/**
* File
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
self::$editableProperties = [
'name' => [
'label' => trans('Nom'),
'type' => CrudType::string(),
'nullable' => false,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::update(),
CrudAction::create()
]
],
'type' => [
'label' => trans('Type'),
'type' => CrudType::enum(),
'enum' => FileType::class,
'required' => true,
'nullable' => false,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create()
]
],
'category' => [
'label' => trans('Categorie'),
'type' => CrudType::enum(),
'enum' => FileCategory::class,
'required' => true,
'nullable' => false,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::update(),
CrudAction::create()
]
],
'uri' => [
'label' => trans('Lien uri'),
'type' => CrudType::uri(),
'required' => false,
'nullable' => true,
'validate' => [
'required_without:file',
'nullable'
],
'actions' => [
CrudAction::view(),
CrudAction::create()
]
],
'file' => [
'label' => trans('Fichier'),
'type' => CrudType::file(),
'required' => false,
'nullable' => true,
'validate' => [
'required_without:uri',
'nullable',
'max:5120',
//phpcs:ignore Generic.Files.LineLength.TooLong
'mimetypes:application/pdf,text/csv,text/plain,application/csv,text/comma-separated-values,text/anytext,application/octet-stream,application/txt',
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
if (!in_array($value->getClientMimeType(), ['application/vnd.ms-excel', 'application/pdf'])) {
//phpcs:ignore Generic.Files.LineLength.TooLong
$fail(__('Le fichier doit être d\'un mime type application/vnd.ms-excel ou application/pdf'));
}
}
],
'accept' => 'application/pdf,text/csv',
'actions' => [
CrudAction::view(),
CrudAction::create()
]
],
'created_at' => [
'label' => trans('Créé le'),
'type' => CrudType::string(),
'actions' => [
CrudAction::viewAny(),
CrudAction::view()
]
]
];
parent::__construct($attributes);
}
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::created(function (File $file) {
event(new FileCreated($file, \auth()->user()));
});
}
// * RELATIONSHIPS
/**
* MorphTo parent object relation
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function parent()
{
return $this->morphTo();
}
/**
* MorphTo parent object site relation
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function parentSite()
{
return $this->morphTo('parent', 'site');
}
/**
* MorphTo parent object project relation
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function parentProject()
{
return $this->morphTo('parent', 'project');
}
}