getAttributes()['content'], true); $originalContent = $model->getOriginal('content'); $availableLocales = array_keys(config('laravellocalization.supportedLocales', ['fr' => "Français"])); // On delete it's all the locales. $localesToProcess = ($forceDeleteImage) ? $availableLocales : [$locale]; foreach ($localesToProcess as $lang) { if (isset($originalContent[$lang])) { $currentContentLocal = is_array($currentContent[$lang]) ? $currentContent[$lang] : [DynamicFieldTypes::wysiwyg->name . '_' => $currentContent[$lang]]; $originalContentLocal = is_array($originalContent[$lang]) ? $originalContent[$lang] : [DynamicFieldTypes::wysiwyg->name . '_' => $originalContent[$lang]]; static::removeDynamicImages($model, $originalContentLocal, $currentContentLocal, $forceDeleteImage); static::removeWysiwygImages($originalContentLocal, $currentContentLocal, $forceDeleteImage); } } } /** * 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 . '_'; $oldImages = static::extractImagePaths($oldContent, $prefix); $newImages = static::extractImagePaths($newContent, $prefix); $allPaths = collect(array_keys($oldImages)) ->merge(array_keys($newImages)) ->unique(); foreach ($allPaths as $key) { $old = $oldImages[$key] ?? null; $new = $newImages[$key] ?? null; // Case n°1 : Same values when force delete. if ((is_string($old) === is_string($new)) && $forceDeleteImage) { $path = ToolboxHelper::mbReplace('storage/', '', $new); if (Storage::disk('public')->exists($path)) { Storage::disk('public')->delete($path); } } // 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 } /** * Extract image path for fields with image key prefix. * * @param array $array * @param string $prefix * @param string $path * @return array */ protected static function extractImagePaths( array $array, string $prefix, string $path = '' ): array { $result = []; foreach ($array as $key => $value) { $currentPath = $path === '' ? $key : "$path.$key"; if (Str::startsWith($key, $prefix) && is_string($value)) { $result[$currentPath] = $value; } if (is_array($value)) { $result += static::extractImagePaths($value, $prefix, $currentPath); } } return $result; } /** * Remove imgs from wysiwygs. * * @param array $oldContent * @param array $newContent * @param boolean $forceDeleteImage * @return void */ protected static function removeWysiwygImages(array $oldContent, array $newContent, bool $forceDeleteImage): void { $prefix = DynamicFieldTypes::wysiwyg->name . '_'; static::getDynamicFieldKeys($oldContent, $newContent, $prefix) ->each(function ($key) use ($oldContent, $newContent, $forceDeleteImage) { $old = data_get($oldContent, $key); $new = $forceDeleteImage ? '' : data_get($newContent, $key, ''); if ($old) { FileHelper::removeOldModelFilesFromHtml($new ?? '', $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 { $oldFieldsPath = static::extractImagePaths($oldContent, $prefix); $newFieldsPath = static::extractImagePaths($newContent, $prefix); return collect(array_keys($oldFieldsPath)) ->merge(array_keys($newFieldsPath)) ->unique(); } /** * Get all segments url for dynamic pages. * * @param \App\Models\DynamicPage $page * @param \Illuminate\Database\Eloquent\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 $parent */ $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; } }