From f36d4861da7b3f918d07e647c6f47b1cdcfedcf1 Mon Sep 17 00:00:00 2001 From: Alexis Gousseau Date: Tue, 17 Feb 2026 13:02:08 +0100 Subject: [PATCH] fix: :bug: update dynamic pages relation/status when parent page is deleted --- app/Models/DynamicPage.php | 60 ++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/app/Models/DynamicPage.php b/app/Models/DynamicPage.php index 352307dc..08ac7285 100644 --- a/app/Models/DynamicPage.php +++ b/app/Models/DynamicPage.php @@ -21,18 +21,32 @@ use Mcamara\LaravelLocalization\Facades\LaravelLocalization; use Uwil\LaravelActivityLogs\Traits\ActivityLog; /** - * @property string $id_key Unique key identification of the dynamic page. - * @property string $title Title of the dynamic page. - * @property string $slug Slug of the dynamic page. - * @property string $content Content of the dynamic page. - * @property integer|null $config_id Config associated to the dynamic page. - * @property integer|null $page_id Page associated to the dynamic page. - * @property \App\Enums\DynamicPages\PageStatusEnum $status Status of the dynamic page. - * @property boolean $isLocked Locked status of dynamic page. - * @property boolean $isRoot Root status of dynamic page. - * @property-read \Illuminate\Support\Carbon $created_at Date on which the dynamic page was created. - * @property-read \Illuminate\Support\Carbon $updated_at Date on which the dynamic page was updated. - * @property-read \Illuminate\Support\Carbon $deleted_at Date on which the dynamic page was updated. + * @property string $id_key + * Unique key identification of the dynamic page. + * @property string $title + * Title of the dynamic page. + * @property string $slug + * Slug of the dynamic page. + * @property string $content + * Content of the dynamic page. + * @property integer|null $config_id + * Config associated to the dynamic page. + * @property integer|null $page_id + * Page associated to the dynamic page. + * @property \App\Enums\DynamicPages\PageStatusEnum $status + * Status of the dynamic page. + * @property boolean $isLocked + * Locked status of dynamic page. + * @property boolean $isRoot + * Root status of dynamic page. + * @property-read \Illuminate\Support\Carbon $created_at + * Date on which the dynamic page was created. + * @property-read \Illuminate\Support\Carbon $updated_at + * Date on which the dynamic page was updated. + * @property-read \Illuminate\Support\Carbon $deleted_at + * Date on which the dynamic page was updated. + * @property \Illuminate\Database\Eloquent\Collection $childPages + * Associated dynamic pages of the current page. */ class DynamicPage extends Model implements TranslatableModel { @@ -130,9 +144,19 @@ class DynamicPage extends Model implements TranslatableModel ]), ]); } - $model->status = PageStatusEnum::draft; + $model->status = PageStatusEnum::unpublished; $model->isRoot = false; }); + static::deleted(function (self $model) { + DynamicPageHelper::manageImgs($model, true); + + $model->status = PageStatusEnum::unpublished; + $model->saveOrFail(); + + self::setAllChildPageStatus($model, function (DynamicPage $page) { + $page->page_id = null; + }); + }); static::forceDeleted(function (self $model) { DynamicPageHelper::manageImgs($model, true); }); @@ -179,17 +203,21 @@ class DynamicPage extends Model implements TranslatableModel /** * Unpublished all children pages if change status published from draft/unpublished. * - * @param self $model + * @param self $model + * @param callable|null $callback * @return void */ - private static function setAllChildPageStatus(self $model): void + private static function setAllChildPageStatus(self $model, ?callable $callback = null): void { if ( !$model->status->equals(PageStatusEnum::published) && !$model->getOriginal('status')->equals($model->status) ) { - $model->childPages->map(function (DynamicPage $page) use ($model) { + $model->childPages->map(function (DynamicPage $page) use ($model, $callback) { $page->status = $model->status; + if (!is_null($callback)) { + $callback($page); + } $page->saveOrFail(); }); }