ldc-groupe/app/Helpers/DynamicPageHelper.php

163 lines
5.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Helpers;
use App\Enums\DynamicFieldTypes;
use App\Enums\DynamicPages\PageStatusEnum;
use App\Models\DynamicPage;
use Illuminate\Database\Eloquent\Collection;
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.
*
2025-10-29 10:29:57 +01:00
* @param \Illuminate\Database\Eloquent\Model $model
* @param boolean $forceDeleteImage
* In forceDelete event we need to ensure all the images will be destroyed
* @return void
*/
2025-10-29 10:29:57 +01:00
public static function removeImgs(Model $model, bool $forceDeleteImage = false): void
{
$locale = config('app.locale');
2025-10-29 10:29:57 +01:00
$model = $model->load('config');
$currentContent = json_decode($model->getAttributes()['content'], true);
$originalContent = $model->getOriginal('content');
if (isset($originalContent[$locale])) {
2025-10-29 10:29:57 +01:00
$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.
*
2025-10-29 10:29:57 +01:00
* @param \Illuminate\Database\Eloquent\Model $model
* @param array $oldContent
* @param array $newContent
* @param boolean $forceDeleteImage
* In forceDelete event we need to ensure all the images will be destroyed
* @return void
*/
2025-10-29 10:29:57 +01:00
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;
2025-10-29 10:29:57 +01:00
// Case n°1 : Same values when force delete.
if ((is_string($old) === is_string($new)) && $forceDeleteImage) {
2025-10-29 10:29:57 +01:00
// We need to remove the actual file, not the old as we do in other conditions.
FileHelper::removeFile($model, "content.$key");
}
2025-10-29 10:29:57 +01:00
// Case n°2: old = string, new = null → deletion.
if (is_string($old) && is_null($new)) {
2025-10-29 10:29:57 +01:00
// Remove the original file.
FileHelper::removeOldFile($model, "content.$key");
continue;
}
2025-10-29 10:29:57 +01:00
// Case n°3: old = null, new = string → addition.
if (is_null($old) && is_string($new)) {
// Nothing to do.
continue;
}
2025-10-29 10:29:57 +01:00
// Case n°4: old = string, new = other string → replacement.
if (is_string($old) && is_string($new) && $old !== $new) {
FileHelper::removeOldFile($model, "content.$key");
continue;
}
2025-10-29 10:29:57 +01:00
} //end foreach
}
/**
* Remove imgs from wysiwygs.
*
* @param array $oldContent
* @param array $newContent
* @return void
*/
protected static function removeWysiwygImages(array $oldContent, array $newContent): void
{
$prefix = DynamicFieldTypes::wysiwyg->name . '_';
static::getDynamicFieldKeys($oldContent, $newContent, $prefix)
->each(function ($key) use ($oldContent) {
$old = $oldContent[$key] ?? null;
FileHelper::removeOldModelFilesFromHtml('', $old);
});
}
/**
2025-10-29 10:29:57 +01:00
* Get all value that correspond to a prefix in field input and group them.
*
2025-10-29 10:29:57 +01:00
* @param array $oldContent
* @param array $newContent
* @param string $prefix
* @return \Illuminate\Support\Collection
*/
2025-10-29 10:29:57 +01:00
protected static function getDynamicFieldKeys(
array $oldContent,
array $newContent,
string $prefix
): \Illuminate\Support\Collection {
$filter = fn($key, $prefix) => Str::startsWith($key, $prefix);
$old = collect($oldContent)->filter($filter);
$new = collect($newContent)->filter($filter);
return $new->keys()->merge($old->keys())->unique();
}
/**
* Get all segments url for dynamic pages
*
* @param DynamicPage $page
* @param Collection $pages
* @param string $locale
* @return array|null
*/
public static function getAllDynamicPageSegments(DynamicPage $page, Collection $pages, string $locale): ?array
{
$segments = [];
$parentId = $page->page_id ?? ($page->parentPage->id ?? null);
// Browse all relationships.
while ($parentId) {
/** @var \App\Models\DynamicPage|null $parentPage */
$parent = $pages->find($parentId);
if (! $parent || !$parent->status->equals(PageStatusEnum::published)) {
return null;
}
$parentSlug = $parent->getTranslation('slug', $locale);
if (! $parentSlug) {
return null;
}
array_unshift($segments, $parentSlug);
$parentId = $parent->page_id;
}
return $segments;
}
}