103 lines
3.9 KiB
PHP
103 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\Enums\DynamicFieldTypes;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Uwil\LaravelCommon\Helpers\FileHelper;
|
|
|
|
class DynamicPageHelper
|
|
{
|
|
/**
|
|
* Remove imgs from wysiwyg OR dynamic wysiwyg OR image input.
|
|
*
|
|
* @param Model $model
|
|
* @param bool $forceDeleteImage In forceDelete event we need to ensure all the images will be destroyed
|
|
* @return void
|
|
*/
|
|
public static function removeImgs(Model $model, $forceDeleteImage = false): void
|
|
{
|
|
$locale = config('app.locale');
|
|
$model = $model->load('config');
|
|
|
|
$currentContent = json_decode($model->getAttributes()['content'], true);
|
|
$originalContent = $model->getOriginal('content');
|
|
|
|
$currentContentLocal = is_array($currentContent[$locale]) ? $currentContent[$locale] : ["text_" => $currentContent[$locale]];
|
|
$originalContentLocal = is_array($originalContent[$locale]) ? $originalContent[$locale] : ["text_" => $originalContent[$locale]];
|
|
static::removeDynamicImages($model, $originalContentLocal, $currentContentLocal, $forceDeleteImage);
|
|
static::removeWysiwygImages($originalContentLocal, $currentContentLocal);
|
|
}
|
|
|
|
/**
|
|
* Remove imgs from image inputs.
|
|
*
|
|
* @param Model $model
|
|
* @param array $oldContent
|
|
* @param array $newContent
|
|
* @param bool $forceDeleteImage In forceDelete event we need to ensure all the images will be destroyed
|
|
* @return void
|
|
*/
|
|
protected static function removeDynamicImages(Model $model, array $oldContent, array $newContent, bool $forceDeleteImage): void
|
|
{
|
|
$prefix = DynamicFieldTypes::image->name . '_';
|
|
$allKeys = static::getDynamicFieldKeys($oldContent, $newContent, $prefix);
|
|
foreach ($allKeys as $key) {
|
|
$old = $oldContent[$key] ?? null;
|
|
$new = $newContent[$key] ?? null;
|
|
// Cas 1 : Valeurs identiques
|
|
if ((is_string($old) === is_string($new)) && $forceDeleteImage) {
|
|
// In this case we need to remove the actual file, not the old as we do in other conditions
|
|
FileHelper::removeFile($model, "content.$key");
|
|
}
|
|
// // Cas 2 : ancienne = string, nouvelle = null → suppression
|
|
if (is_string($old) && is_null($new)) {
|
|
// Action suppression
|
|
FileHelper::removeOldFile($model, "content.$key");
|
|
continue;
|
|
}
|
|
|
|
// // Cas 3 : ancienne = null, nouvelle = string → ajout
|
|
|
|
// // Cas 4 : ancienne = string, nouvelle = autre string → remplacement
|
|
if (is_string($old) && is_string($new) && $old !== $new) {
|
|
FileHelper::removeOldFile($model, "content.$key");
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove imgs from wysiwygs.
|
|
*
|
|
* @param array $oldContent
|
|
* @param array $newContent
|
|
* @return void
|
|
*/
|
|
protected static function removeWysiwygImages(array $oldContent, array $newContent): void
|
|
{
|
|
$prefix = DynamicFieldTypes::text->name . '_';
|
|
|
|
static::getDynamicFieldKeys($oldContent, $newContent, $prefix)
|
|
->each(function ($key) use ($oldContent) {
|
|
$old = $oldContent[$key] ?? null;
|
|
FileHelper::removeOldModelFilesFromHtml('', $old);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Get the field keys of wysiwygs or images field input and group them.
|
|
*
|
|
* @param array $oldContent
|
|
* @param array $newContent
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
protected static function getDynamicFieldKeys(array $oldContent, array $newContent, string $prefix): \Illuminate\Support\Collection
|
|
{
|
|
$filter = fn($value, $key) => Str::startsWith($key, $prefix);
|
|
$old = collect($oldContent)->filter($filter);
|
|
$new = collect($newContent)->filter($filter);
|
|
return $new->keys()->merge($old->keys())->unique();
|
|
}
|
|
}
|