ldc-volena/app/Models/Section.php
2024-03-19 10:37:33 +01:00

275 lines
7.7 KiB
PHP

<?php
namespace App\Models;
use App\Enums\InputType;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
use App\Traits\Models\ActivityLog;
use App\Traits\Models\MiniCrudModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
/**
* @property \App\Enums\Sections\PageSectionEnum $section Slider that contains the slide.
* @property string $h2_title Title.
* @property string $text WYSIWYG - Content.
* @property string $data Minicrud content.
* @property-read \Carbon\Carbon $created_at Created date.
* @property-read \Carbon\Carbon $updated_at Updated date.
*
* @property-read \App\Models\StaticPage $page StaticPage Belongs to relationship.
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
*/
class Section extends Model
{
use ActivityLog;
use HasFactory;
use MiniCrudModel;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'section',
'h2_title',
'text',
'data',
'page_id',
'id',
'created_at',
'updated_at'
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'section' => \App\Enums\Sections\PageSectionEnum::class,
'data' => 'object',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function () {
throw new \RuntimeException("Creating a new section is not authorized");
});
static::updating(function (self $model) {
// * ASSERTIONS
ToolboxHelper::assertNoInternalBoUrls('text', $model->text);
// * TRANSFORMATIONS
$model->text = FileStorageHelper::storeModelFilesFromHtml($model, $model->text);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldModelFilesFromHtml(
$model->text,
ToolboxHelper::getOriginal($model, 'text')
);
});
static::deleting(function () {
throw new \RuntimeException('Deleting a section is not authorized');
});
}
// * METHODS.
// * EMPTY BLOCS.
/**
* Empty bloc for picture.
*
* @param array $data
* @param string $img
* @param string $imgAlt
* @param string $imgTitle
* @param integer $width
* @param integer $height
* @param boolean $required
* @param string $name
* @return array
*/
public static function emptyPictureBloc(
array $data,
string $img = '',
string $imgAlt = '',
string $imgTitle = '',
int $width = 100,
int $height = 100,
bool $required = false,
string $name = 'image',
) {
$data[sprintf('bloc_%s', Str::of($name)->snake()->ascii()->remove(['\'', '-']))] = [
'label' => $name,
'dimensions' => ['width' => $width, 'height' => $height],
'inputType' => InputType::image->value,
'alt' => $imgAlt,
'title' => $imgTitle,
'value' => $img,
'validate' => $required ? 'required' : 'nullable',
'required' => $required
];
return $data;
}
/**
* Empty bloc for link associated to the picture.
*
* @param array $data
* @param string $url
* @param boolean $required
* @return array
*/
public static function emptyPictureLinkBloc(
array $data,
string $url = '',
bool $required = false,
) {
$data["bloc_image_link"] = [
'label' => "URL à ouvrir",
'inputType' => InputType::url->value,
'value' => $url,
'validate' => 'url|' . ($required ? 'required' : 'nullable'),
'required' => $required
];
$data["bloc_image_link_new_tab"] = [
'label' => "Ouvrir dans un nouvel onglet",
'inputType' => InputType::boolean->value,
];
return $data;
}
/**
* Empty bloc for key numbers.
*
* @param array $data
* @param string $name
* @param integer $value
* @param boolean $required
*
* @return array
*/
public static function emptyKeyNumberBloc(
array $data,
string $name,
int $value = 0,
bool $required = false,
) {
$snakeCaseName = Str::of($name)->snake()->ascii()->remove(['\'', '-']);
$data['bloc_key_' . $snakeCaseName] = [
'label' => "Chiffre clé {$name}",
'inputType' => InputType::number->value,
'value' => $value,
'validate' => $required ? 'required' : 'nullable',
'required' => $required
];
return $data;
}
/**
* Empty bloc for the button boolean.
*
* @param array $data
* @param string $name
* @return array
*/
public static function emptyButtonBooleanBloc(
array $data,
string $name
) {
$snakeCaseName = Str::of($name)->snake()->ascii()->remove(['\'', '-']);
$data["bloc_button_{$snakeCaseName}"] = [
'label' => "Afficher le bouton {$name} ?",
'inputType' => InputType::boolean->value,
'value' => false
];
return $data;
}
/**
* Empty bloc for the h3 title.
*
* @param array $data
* @param string $name
* @param string $value
* @param boolean $required
* @return array
*/
public static function emptyStringBloc(
array $data,
string $name,
string $value = '',
bool $required = false
) {
$snakeCaseName = Str::of($name)->snake()->ascii()->remove(['\'', '-']);
$data["bloc_string_{$snakeCaseName}"] = [
'label' => $name,
'inputType' => InputType::string->value,
'validate' => 'max:255|' . ($required ? 'required' : 'nullable'),
'value' => $value,
'required' => $required
];
return $data;
}
/**
* Empty bloc for the WYSIWYG content.
*
* @param array $data
* @param string $name
* @param string $value
* @param \App\Enums\CkEditorConfigName $config
* @param boolean $required
* @return array
*/
public static function emptyWysiwygBloc(
array $data,
string $name,
string $value = '',
\App\Enums\CkEditorConfigName $config = \App\Enums\CkEditorConfigName::base,
bool $required = false
) {
$snakeCaseName = Str::of($name)->snake()->ascii()->remove(['\'', '-']);
$data["bloc_{$snakeCaseName}_content"] = [
'label' => "Contenu de la section {$name}",
'inputType' => InputType::text->value,
'validate' => $required ? 'required' : 'nullable',
'value' => $value,
'required' => $required,
'config' => $config->name
];
return $data;
}
// * RELATIONS
/**
* Get the page that owns the section.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function page(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(StaticPage::class);
}
}