BookStack/app/Entities/Models/BookChild.php
Dan Brown b866dee0cf
Some checks are pending
analyse-php / build (push) Waiting to run
lint-php / build (push) Waiting to run
test-migrations / build (8.2) (push) Waiting to run
test-migrations / build (8.3) (push) Waiting to run
test-migrations / build (8.4) (push) Waiting to run
test-php / build (8.2) (push) Waiting to run
test-php / build (8.3) (push) Waiting to run
test-php / build (8.4) (push) Waiting to run
Entities: Updated repos to act on refreshed clones
Changes to core entity models are now done on clones to ensure clean
state before save, and those clones are returned back if changes are
needed after that action.
2025-09-24 18:19:16 +01:00

51 lines
1.2 KiB
PHP

<?php
namespace BookStack\Entities\Models;
use BookStack\References\ReferenceUpdater;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* Class BookChild.
*
* @property int $book_id
* @property int $priority
* @property string $book_slug
* @property Book $book
*/
abstract class BookChild extends Entity
{
/**
* Get the book this page sits in.
*/
public function book(): BelongsTo
{
return $this->belongsTo(Book::class)->withTrashed();
}
/**
* Change the book that this entity belongs to.
*/
public function changeBook(int $newBookId): self
{
$altered = $this->clone()->refresh();
$oldUrl = $altered->getUrl();
$altered->book_id = $newBookId;
$altered->refreshSlug();
$altered->save();
if ($oldUrl !== $altered->getUrl()) {
app()->make(ReferenceUpdater::class)->updateEntityReferences($altered, $oldUrl);
}
// Update all child pages if a chapter
if ($altered instanceof Chapter) {
foreach ($altered->pages()->withTrashed()->get() as $page) {
$page->changeBook($newBookId);
}
}
return $altered;
}
}