2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Repos;
|
2019-10-05 07:55:01 -04:00
|
|
|
|
2020-11-07 17:37:27 -05:00
|
|
|
use BookStack\Actions\ActivityType;
|
2020-11-21 19:17:45 -05:00
|
|
|
use BookStack\Entities\Models\Book;
|
|
|
|
use BookStack\Entities\Models\Chapter;
|
2020-11-21 18:20:54 -05:00
|
|
|
use BookStack\Entities\Tools\BookContents;
|
|
|
|
use BookStack\Entities\Tools\TrashCan;
|
2019-10-05 07:55:01 -04:00
|
|
|
use BookStack\Exceptions\MoveOperationException;
|
|
|
|
use BookStack\Exceptions\NotFoundException;
|
2020-11-07 17:37:27 -05:00
|
|
|
use BookStack\Facades\Activity;
|
2019-10-05 07:55:01 -04:00
|
|
|
use Exception;
|
|
|
|
|
|
|
|
class ChapterRepo
|
|
|
|
{
|
|
|
|
protected $baseRepo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ChapterRepo constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(BaseRepo $baseRepo)
|
|
|
|
{
|
|
|
|
$this->baseRepo = $baseRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a chapter via the slug.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2019-10-05 07:55:01 -04:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function getBySlug(string $bookSlug, string $chapterSlug): Chapter
|
|
|
|
{
|
|
|
|
$chapter = Chapter::visible()->whereSlugs($bookSlug, $chapterSlug)->first();
|
|
|
|
|
|
|
|
if ($chapter === null) {
|
|
|
|
throw new NotFoundException(trans('errors.chapter_not_found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $chapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new chapter in the system.
|
|
|
|
*/
|
|
|
|
public function create(array $input, Book $parentBook): Chapter
|
|
|
|
{
|
|
|
|
$chapter = new Chapter();
|
|
|
|
$chapter->book_id = $parentBook->id;
|
|
|
|
$chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
|
|
|
|
$this->baseRepo->create($chapter, $input);
|
2020-11-07 18:15:13 -05:00
|
|
|
Activity::addForEntity($chapter, ActivityType::CHAPTER_CREATE);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
return $chapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the given chapter.
|
|
|
|
*/
|
|
|
|
public function update(Chapter $chapter, array $input): Chapter
|
|
|
|
{
|
|
|
|
$this->baseRepo->update($chapter, $input);
|
2020-11-07 18:15:13 -05:00
|
|
|
Activity::addForEntity($chapter, ActivityType::CHAPTER_UPDATE);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
return $chapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a chapter from the system.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2019-10-05 07:55:01 -04:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function destroy(Chapter $chapter)
|
|
|
|
{
|
|
|
|
$trashCan = new TrashCan();
|
2020-09-27 18:24:33 -04:00
|
|
|
$trashCan->softDestroyChapter($chapter);
|
2020-11-07 18:15:13 -05:00
|
|
|
Activity::addForEntity($chapter, ActivityType::CHAPTER_DELETE);
|
2020-11-07 08:58:23 -05:00
|
|
|
$trashCan->autoClearOld();
|
2019-10-05 07:55:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the given chapter into a new parent book.
|
|
|
|
* The $parentIdentifier must be a string of the following format:
|
2021-06-26 11:23:15 -04:00
|
|
|
* 'book:<id>' (book:5).
|
|
|
|
*
|
2019-10-05 07:55:01 -04:00
|
|
|
* @throws MoveOperationException
|
|
|
|
*/
|
|
|
|
public function move(Chapter $chapter, string $parentIdentifier): Book
|
|
|
|
{
|
|
|
|
$stringExploded = explode(':', $parentIdentifier);
|
|
|
|
$entityType = $stringExploded[0];
|
|
|
|
$entityId = intval($stringExploded[1]);
|
|
|
|
|
|
|
|
if ($entityType !== 'book') {
|
|
|
|
throw new MoveOperationException('Chapters can only be moved into books');
|
|
|
|
}
|
|
|
|
|
2020-11-07 17:37:27 -05:00
|
|
|
/** @var Book $parent */
|
2019-10-05 07:55:01 -04:00
|
|
|
$parent = Book::visible()->where('id', '=', $entityId)->first();
|
|
|
|
if ($parent === null) {
|
|
|
|
throw new MoveOperationException('Book to move chapter into not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
$chapter->changeBook($parent->id);
|
|
|
|
$chapter->rebuildPermissions();
|
2020-11-07 18:15:13 -05:00
|
|
|
Activity::addForEntity($chapter, ActivityType::CHAPTER_MOVE);
|
2020-11-07 17:37:27 -05:00
|
|
|
|
2019-10-05 07:55:01 -04:00
|
|
|
return $parent;
|
|
|
|
}
|
|
|
|
}
|