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

178 lines
4.8 KiB
PHP
Raw 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-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;
use Spatie\Translatable\HasTranslations;
2023-03-15 13:51:01 +01:00
/**
* @property integer $id
* @property integer $part2_product1_id
* @property integer $part2_product2_id
* @property string $seo_title
* @property string $seo_description
* @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
* @property string $part2_list
* @property string $part3_picture
* @property string $created_at
* @property string $updated_at
* @property Product $product
* @property Product $product
*/
class Home extends Model
{
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
/**
* @var array
*/
protected $fillable = [
'part2_product1_id',
'part2_product2_id',
'seo_title',
'seo_description',
'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.
*
* @var array
*/
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 = [
'seo_title',
'seo_description',
'part1_title',
'part1_subtitle',
'part1_text_1',
'part1_text_2',
'part1_text_3',
'part2_title',
'part2_subtitle',
'part2_text',
'part2_list',
'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);
self::cleanTranslationJsonFields($model, 'part2_list');
2023-03-15 13:51:01 +01:00
});
static::updating(function (self $model) {
self::manageFields($model);
self::cleanTranslationJsonFields($model, 'part2_list');
2023-03-15 13:51:01 +01:00
});
static::updated(function (self $model) {
FileStorageHelper::removeOldFile($model, 'part3_picture');
});
static::deleted(function (self $model) {
FileStorageHelper::removeFile($model, 'part3_picture');
});
}
// * METHODS
/**
* Undocumented function
*
* @param Model $model
* @return void
*/
private static function manageFields(Model $model)
{
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-04-18 10:49:39 +02:00
$model->part1_text_1 = clean($model->part1_text_1);
$model->part1_text_2 = clean($model->part1_text_2);
$model->part1_text_3 = clean($model->part1_text_3);
$model->part2_text = clean($model->part2_text);
$model->part2_list = clean($model->part2_list);
2023-03-15 13:51:01 +01:00
$model->part3_picture = FileStorageHelper::storeFile($model, $model->part3_picture, true);
}
2023-04-11 08:32:59 +00:00
/**
* Remove the original array from json field
* @param Model $model
* @param string $fields The names of the json fields
*/
private static function cleanTranslationJsonFields(Model $model, ...$fields)
2023-04-11 08:32:59 +00:00
{
foreach ($fields as $field) {
$translations = $model->getTranslations($field);
foreach ($translations as $key => $trans) {
if (!in_array($key, config('app.locales'))) {
2023-04-11 08:32:59 +00:00
$model->forgetTranslation($field, $key);
}
}
}
}
2023-03-15 13:51:01 +01:00
// * RELATIONSHIPS
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function product1()
{
return $this->belongsTo('App\Models\Product', 'part2_product1_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function product2()
{
return $this->belongsTo('App\Models\Product', 'part2_product2_id');
}
}