193 lines
6 KiB
PHP
193 lines
6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudType;
|
|
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
|
|
|
|
class Societe extends Model
|
|
{
|
|
use HasFactory;
|
|
use IsCrudModel;
|
|
|
|
protected $crudLabelColumn = 'name';
|
|
|
|
protected $crudValueColumn = 'id';
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'address',
|
|
'phone',
|
|
'logo',
|
|
'style',
|
|
'colors',
|
|
'pole_id'
|
|
];
|
|
|
|
protected $casts = [
|
|
'colors' => 'array',
|
|
];
|
|
|
|
private $defaultColors = '{"primary":"#1D1646", "secondary": "#F6BE00", "color-background": "#FFF"}';
|
|
|
|
protected $attributes = [
|
|
'style' => '',
|
|
'colors' => '{"primary":"#1D1646", "secondary": "#F6BE00", "color-background": "#FFF"}'
|
|
];
|
|
|
|
public function __construct($attributes = [])
|
|
{
|
|
self::$editableProperties = [
|
|
'name' => [
|
|
'label' => trans('Nom'),
|
|
'type' => CrudType::string(),
|
|
'validate' => [
|
|
'string',
|
|
'min:10',
|
|
'max:255'
|
|
],
|
|
'actions' => [
|
|
CrudAction::viewAny(),
|
|
CrudAction::view(),
|
|
CrudAction::create(),
|
|
CrudAction::update()
|
|
]
|
|
],
|
|
'address' => [
|
|
'label' => trans('Adresse'),
|
|
'type' => CrudType::string(),
|
|
'validate' => [
|
|
'string',
|
|
'min:10',
|
|
'max:255'
|
|
],
|
|
'actions' => [
|
|
CrudAction::view(),
|
|
CrudAction::create(),
|
|
CrudAction::update()
|
|
]
|
|
],
|
|
'phone' => [
|
|
'label' => trans('Téléphone'),
|
|
'type' => CrudType::string(),
|
|
'validate' => [
|
|
'string',
|
|
'min:10',
|
|
'max:255'
|
|
],
|
|
'actions' => [
|
|
CrudAction::view(),
|
|
CrudAction::create(),
|
|
CrudAction::update()
|
|
]
|
|
],
|
|
'logo' => [
|
|
'label' => trans('Logo'),
|
|
'type' => CrudType::image(),
|
|
'validate' => [
|
|
'file',
|
|
'max:2048'
|
|
],
|
|
'actions' => [
|
|
CrudAction::view(),
|
|
CrudAction::create(),
|
|
CrudAction::update()
|
|
]
|
|
],
|
|
'style' => [
|
|
'label' => trans('Style'),
|
|
'type' => CrudType::text(),
|
|
'required' => false,
|
|
'nullable' => true,
|
|
'actions' => [
|
|
CrudAction::view(),
|
|
CrudAction::create(),
|
|
CrudAction::update(),
|
|
]
|
|
],
|
|
'colors' => [
|
|
'label' => trans('Couleurs'),
|
|
'type' => CrudType::json(),
|
|
'default' => $this->defaultColors,
|
|
'mode' => 'form',
|
|
'validate' => [
|
|
'json',
|
|
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
|
function ($attribute, $value, $fail) {
|
|
$pattern = '/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/';
|
|
$a = \json_decode($value, true);
|
|
$mt = [];
|
|
if (
|
|
!isset($a['primary']) or
|
|
!isset($a['secondary']) or
|
|
!isset($a['color-background']) or
|
|
!\preg_match($pattern, $a['primary'], $mt) or
|
|
!\preg_match($pattern, $a['secondary'], $mt) or
|
|
!\preg_match($pattern, $a['color-background'], $mt)
|
|
) {
|
|
$fail(__('colors doit être un object json tel quel, :json', [
|
|
'json' => json_encode([
|
|
'primary' => '#XXXXXX',
|
|
'secondary' => '#XXXXXX',
|
|
'color-background' => '#XXXXXX'
|
|
])
|
|
]));
|
|
}
|
|
}
|
|
],
|
|
'actions' => [
|
|
CrudAction::view(),
|
|
CrudAction::create(),
|
|
CrudAction::update(),
|
|
],
|
|
'setAttribute' => function ($model, $value) {
|
|
$model->setAttribute('colors', json_decode($value, true));
|
|
}
|
|
],
|
|
'pole_id' => [
|
|
'label' => trans('Pole'),
|
|
'type' => CrudType::belongsTo(),
|
|
'belongsTo' => Pole::class,
|
|
'actions' => [
|
|
CrudAction::viewAny(),
|
|
CrudAction::view(),
|
|
CrudAction::create(),
|
|
CrudAction::update()
|
|
]
|
|
]
|
|
];
|
|
parent::__construct($attributes);
|
|
}
|
|
|
|
/**
|
|
* Has one Pole
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function pole()
|
|
{
|
|
return $this->belongsTo(Pole::class);
|
|
}
|
|
|
|
/**
|
|
* Has many Site
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function sites()
|
|
{
|
|
return $this->hasMany(Site::class);
|
|
}
|
|
|
|
public static function saveLogo(UploadedFile $image, string $name)
|
|
{
|
|
return $image->storePubliclyAs(
|
|
'/public/societe',
|
|
sprintf('%s-%s.%s', $name, rand(10000, 99999), $image->extension())
|
|
);
|
|
}
|
|
}
|