1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-08-09 10:22:51 +03:00

Started work on hierachy conversion actions

- Updates book/shelf cover image handling for easier cloning/handling.
- Adds core logic for promoting books/chapters up a level.
- Enables usage of book/shelf cover image via API.

Related to #1087
This commit is contained in:
Dan Brown
2022-06-13 17:20:21 +01:00
parent 0a05119aa5
commit d676e1e824
10 changed files with 166 additions and 72 deletions

View File

@@ -91,6 +91,7 @@ class BookRepo
{
$book = new Book();
$this->baseRepo->create($book, $input);
$this->baseRepo->updateCoverImage($book, $input['image']);
Activity::add(ActivityType::BOOK_CREATE, $book);
return $book;
@@ -102,6 +103,11 @@ class BookRepo
public function update(Book $book, array $input): Book
{
$this->baseRepo->update($book, $input);
if (isset($input['image'])) {
$this->baseRepo->updateCoverImage($book, $input['image'], $input['image'] === null);
}
Activity::add(ActivityType::BOOK_UPDATE, $book);
return $book;

View File

@@ -89,6 +89,7 @@ class BookshelfRepo
{
$shelf = new Bookshelf();
$this->baseRepo->create($shelf, $input);
$this->baseRepo->updateCoverImage($shelf, $input['image']);
$this->updateBooks($shelf, $bookIds);
Activity::add(ActivityType::BOOKSHELF_CREATE, $shelf);
@@ -106,14 +107,17 @@ class BookshelfRepo
$this->updateBooks($shelf, $bookIds);
}
if (isset($input['image'])) {
$this->baseRepo->updateCoverImage($shelf, $input['image'], $input['image'] === null);
}
Activity::add(ActivityType::BOOKSHELF_UPDATE, $shelf);
return $shelf;
}
/**
* Update which books are assigned to this shelf by
* syncing the given book ids.
* Update which books are assigned to this shelf by syncing the given book ids.
* Function ensures the books are visible to the current user and existing.
*/
protected function updateBooks(Bookshelf $shelf, array $bookIds)
@@ -132,17 +136,6 @@ class BookshelfRepo
$shelf->books()->sync($syncData);
}
/**
* Update the given shelf cover image, or clear it.
*
* @throws ImageUploadException
* @throws Exception
*/
public function updateCoverImage(Bookshelf $shelf, ?UploadedFile $coverImage, bool $removeImage = false)
{
$this->baseRepo->updateCoverImage($shelf, $coverImage, $removeImage);
}
/**
* Copy down the permissions of the given shelf to all child books.
*/

View File

@@ -50,11 +50,8 @@ class Cloner
public function clonePage(Page $original, Entity $parent, string $newName): Page
{
$copyPage = $this->pageRepo->getNewDraftPage($parent);
$pageData = $original->getAttributes();
// Update name & tags
$pageData = $this->entityToInputData($original);
$pageData['name'] = $newName;
$pageData['tags'] = $this->entityTagsToInputArray($original);
return $this->pageRepo->publishDraft($copyPage, $pageData);
}
@@ -65,9 +62,8 @@ class Cloner
*/
public function cloneChapter(Chapter $original, Book $parent, string $newName): Chapter
{
$chapterDetails = $original->getAttributes();
$chapterDetails = $this->entityToInputData($original);
$chapterDetails['name'] = $newName;
$chapterDetails['tags'] = $this->entityTagsToInputArray($original);
$copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
@@ -87,9 +83,8 @@ class Cloner
*/
public function cloneBook(Book $original, string $newName): Book
{
$bookDetails = $original->getAttributes();
$bookDetails = $this->entityToInputData($original);
$bookDetails['name'] = $newName;
$bookDetails['tags'] = $this->entityTagsToInputArray($original);
$copyBook = $this->bookRepo->create($bookDetails);
@@ -104,16 +99,26 @@ class Cloner
}
}
if ($original->cover) {
try {
$tmpImgFile = tmpfile();
$uploadedFile = $this->imageToUploadedFile($original->cover, $tmpImgFile);
$this->bookRepo->updateCoverImage($copyBook, $uploadedFile, false);
} catch (\Exception $exception) {
}
return $copyBook;
}
/**
* Convert an entity to a raw data array of input data.
* @return array<string, mixed>
*/
public function entityToInputData(Entity $entity): array
{
$inputData = $entity->getAttributes();
$inputData['tags'] = $this->entityTagsToInputArray($entity);
// Add a cover to the data if existing on the original entity
if ($entity->cover instanceof Image) {
$tmpImgFile = tmpfile();
$uploadedFile = $this->imageToUploadedFile($entity->cover, $tmpImgFile);
$inputData['image'] = $uploadedFile;
}
return $copyBook;
return $inputData;
}
/**

View File

@@ -0,0 +1,73 @@
<?php
namespace BookStack\Entities\Tools;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\BookRepo;
use BookStack\Entities\Repos\BookshelfRepo;
class HierarchyTransformer
{
protected BookRepo $bookRepo;
protected BookshelfRepo $shelfRepo;
protected Cloner $cloner;
protected TrashCan $trashCan;
// TODO - Test setting book cover image from API
// Ensure we can update without resetting image accidentally
// Ensure api docs correct.
// TODO - As above but for shelves.
public function transformChapterToBook(Chapter $chapter): Book
{
// TODO - Check permissions before call
// Permissions: edit-chapter, delete-chapter, create-book
$inputData = $this->cloner->entityToInputData($chapter);
$book = $this->bookRepo->create($inputData);
// TODO - Copy permissions
/** @var Page $page */
foreach ($chapter->pages as $page) {
$page->chapter_id = 0;
$page->changeBook($book->id);
}
$this->trashCan->destroyEntity($chapter);
// TODO - Log activity for change
return $book;
}
public function transformBookToShelf(Book $book): Bookshelf
{
// TODO - Check permissions before call
// Permissions: edit-book, delete-book, create-shelf
$inputData = $this->cloner->entityToInputData($book);
$shelf = $this->shelfRepo->create($inputData, []);
// TODO - Copy permissions?
$shelfBookSyncData = [];
/** @var Chapter $chapter */
foreach ($book->chapters as $index => $chapter) {
$newBook = $this->transformChapterToBook($chapter);
$shelfBookSyncData[$newBook->id] = ['order' => $index];
}
$shelf->books()->sync($shelfBookSyncData);
if ($book->directPages->count() > 0) {
$book->name .= ' ' . trans('entities.pages');
} else {
$this->trashCan->destroyEntity($book);
}
// TODO - Log activity for change
return $shelf;
}
}

View File

@@ -344,7 +344,7 @@ class TrashCan
*
* @throws Exception
*/
protected function destroyEntity(Entity $entity): int
public function destroyEntity(Entity $entity): int
{
if ($entity instanceof Page) {
return $this->destroyPage($entity);