ldc-nature-and-respect/app/Models/Home.php

147 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2023-03-15 13:51:01 +01:00
<?php
namespace App\Models;
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
2023-05-09 09:14:21 +02:00
use App\Traits\Models\HasTranslations;
2023-03-16 09:47:16 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-03-15 13:51:01 +01:00
use Illuminate\Database\Eloquent\Model;
/**
* @property integer $part2_product1_id
* @property integer $part2_product2_id
* @property string $part1_title
2023-03-24 15:39:11 +00:00
* @property string $part1_subtitle
2023-03-15 13:51:01 +01:00
* @property string $part1_text_1
* @property string $part1_text_2
* @property string $part1_text_3
* @property string $part2_title
2023-03-24 15:39:11 +00:00
* @property string $part2_subtitle
2023-03-15 13:51:01 +01:00
* @property string $part2_text
2023-05-09 19:24:09 +02:00
* @property array $part2_list
2023-03-15 13:51:01 +01:00
* @property string $part3_picture
* @property string $created_at
* @property string $updated_at
*/
class Home extends Model
2023-03-15 13:51:01 +01:00
{
2023-03-16 09:47:16 +01:00
use HasFactory;
use HasTranslations;
2023-03-16 09:47:16 +01:00
2023-03-15 13:51:01 +01:00
/**
2024-07-16 18:21:29 +02:00
* The attributes that are mass assignable.
*
* @var array<int, string>
2023-03-15 13:51:01 +01:00
*/
protected $fillable = [
'part2_product1_id',
'part2_product2_id',
'part1_title',
2023-03-24 15:39:11 +00:00
'part1_subtitle',
2023-03-15 13:51:01 +01:00
'part1_text_1',
'part1_text_2',
'part1_text_3',
'part2_title',
2023-03-24 15:39:11 +00:00
'part2_subtitle',
2023-03-15 13:51:01 +01:00
'part2_text',
'part2_list',
'part3_picture',
2023-03-16 15:08:37 +01:00
'part3_picture_alt',
'part3_picture_title',
2023-03-15 13:51:01 +01:00
'created_at',
'updated_at'
];
/**
* The attributes that should be cast.
*
2024-01-29 16:02:03 +01:00
* @var array<string, string>
2023-03-15 13:51:01 +01:00
*/
protected $casts = [
2023-03-16 15:08:37 +01:00
'part2_list' => 'array',
2023-03-15 13:51:01 +01:00
];
/**
* The translatable columns.
2023-04-18 16:05:34 +02:00
*
* @var array
*/
public $translatable = [
'part1_title',
'part1_subtitle',
'part1_text_1',
'part1_text_2',
'part1_text_3',
'part2_title',
'part2_subtitle',
'part2_text',
'part2_list',
2023-05-09 19:24:09 +02:00
'part3_picture',
'part3_picture_alt',
'part3_picture_title',
];
2023-03-15 13:51:01 +01:00
// * EVENTS
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
static::creating(function (self $model) {
self::manageFields($model);
});
static::updating(function (self $model) {
self::manageFields($model);
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'part3_picture');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'part3_picture');
});
}
// * METHODS
/**
* Undocumented function
*
2024-01-29 16:02:03 +01:00
* @param self $model
2023-03-15 13:51:01 +01:00
* @return void
*/
2024-01-29 16:02:03 +01:00
private static function manageFields(self $model)
2023-03-15 13:51:01 +01:00
{
ToolboxHelper::assertNoInternalBoUrls('part1_text_1', $model->part1_text_1);
ToolboxHelper::assertNoInternalBoUrls('part1_text_2', $model->part1_text_2);
ToolboxHelper::assertNoInternalBoUrls('part1_text_3', $model->part1_text_3);
ToolboxHelper::assertNoInternalBoUrls('part2_text', $model->part2_text);
if (is_array($model->part2_list)) {
foreach ($model->part2_list as $item) {
ToolboxHelper::assertNoInternalBoUrls('part2_list', $item['value']);
}
2023-03-16 15:08:37 +01:00
}
2023-03-15 13:51:01 +01:00
$model->part3_picture = FileStorageHelper::storeFile($model, $model->part3_picture, true);
}
// * RELATIONSHIPS
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function product1()
{
2023-05-09 09:14:21 +02:00
return $this->belongsTo(Product::class, 'part2_product1_id');
2023-03-15 13:51:01 +01:00
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function product2()
{
2023-05-09 09:14:21 +02:00
return $this->belongsTo(Product::class, 'part2_product2_id');
2023-03-15 13:51:01 +01:00
}
}