fix: 🐛 update dynamic pages relation/status when parent page is deleted

This commit is contained in:
Alexis Gousseau 2026-02-17 13:02:08 +01:00
parent 7403cabbe9
commit f36d4861da

View file

@ -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<int, \App\Models\DynamicPage> $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();
});
}