mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-10-26 11:11:56 +00:00
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
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.
51 lines
1.2 KiB
PHP
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;
|
|
}
|
|
}
|