load('config'); $currentContent = json_decode($model->getAttributes()['content'], true); $originalContent = $model->getOriginal('content'); if (isset($originalContent[$locale])) { $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 \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 */ 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; // Case n°1 : Same values when force delete. if ((is_string($old) === is_string($new)) && $forceDeleteImage) { // We need to remove the actual file, not the old as we do in other conditions. FileHelper::removeFile($model, "content.$key"); } // Case n°2: old = string, new = null → deletion. if (is_string($old) && is_null($new)) { // Remove the original file. FileHelper::removeOldFile($model, "content.$key"); continue; } // Case n°3: old = null, new = string → addition. if (is_null($old) && is_string($new)) { // Nothing to do. continue; } // Case n°4: old = string, new = other string → replacement. if (is_string($old) && is_string($new) && $old !== $new) { FileHelper::removeOldFile($model, "content.$key"); continue; } } //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); }); } /** * Get all value that correspond to a prefix in field input and group them. * * @param array $oldContent * @param array $newContent * @param string $prefix * @return \Illuminate\Support\Collection */ 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; } }