-<?php namespace BookStack\Entities\Repos;
+<?php
+
+namespace BookStack\Entities\Repos;
use BookStack\Actions\ActivityType;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity;
+use BookStack\Entities\Models\Page;
+use BookStack\Entities\Models\PageRevision;
use BookStack\Entities\Tools\BookContents;
use BookStack\Entities\Tools\PageContent;
use BookStack\Entities\Tools\TrashCan;
-use BookStack\Entities\Models\Page;
-use BookStack\Entities\Models\PageRevision;
use BookStack\Exceptions\MoveOperationException;
use BookStack\Exceptions\NotFoundException;
use BookStack\Exceptions\PermissionsException;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
-use Illuminate\Support\Collection;
class PageRepo
{
-
protected $baseRepo;
/**
/**
* Get a page by ID.
+ *
* @throws NotFoundException
*/
public function getById(int $id, array $relations = ['book']): Page
/**
* Get a page its book and own slug.
+ *
* @throws NotFoundException
*/
public function getBySlug(string $bookSlug, string $pageSlug): Page
->orderBy('created_at', 'desc')
->with('page')
->first();
+
return $revision ? $revision->page : null;
}
public function getUserDraft(Page $page): ?PageRevision
{
$revision = $this->getUserDraftQuery($page)->first();
+
return $revision;
}
public function getNewDraftPage(Entity $parent)
{
$page = (new Page())->forceFill([
- 'name' => trans('entities.pages_initial_name'),
+ 'name' => trans('entities.pages_initial_name'),
'created_by' => user()->id,
- 'owned_by' => user()->id,
+ 'owned_by' => user()->id,
'updated_by' => user()->id,
- 'draft' => true,
+ 'draft' => true,
]);
if ($parent instanceof Chapter) {
$page->save();
$page->refresh()->rebuildPermissions();
+
return $page;
}
$draft->refresh();
Activity::addForEntity($draft, ActivityType::PAGE_CREATE);
+
return $draft;
}
$this->getUserDraftQuery($page)->delete();
// Save a revision after updating
- $summary = trim($input['summary'] ?? "");
+ $summary = trim($input['summary'] ?? '');
$htmlChanged = isset($input['html']) && $input['html'] !== $oldHtml;
$nameChanged = isset($input['name']) && $input['name'] !== $oldName;
$markdownChanged = isset($input['markdown']) && $input['markdown'] !== $oldMarkdown;
}
Activity::addForEntity($page, ActivityType::PAGE_UPDATE);
+
return $page;
}
$pageContent = new PageContent($page);
if (!empty($input['markdown'] ?? '')) {
$pageContent->setNewMarkdown($input['markdown']);
- } else {
- $pageContent->setNewHTML($input['html'] ?? '');
+ } elseif (isset($input['html'])) {
+ $pageContent->setNewHTML($input['html']);
}
}
$revision->save();
$this->deleteOldRevisions($page);
+
return $revision;
}
}
$page->fill($input);
$page->save();
+
return $page;
}
}
$draft->save();
+
return $draft;
}
/**
* Destroy a page from the system.
+ *
* @throws Exception
*/
public function destroy(Page $page)
} else {
$content->setNewHTML($revision->html);
}
-
+
$page->updated_by = user()->id;
$page->refreshSlug();
$page->save();
$this->savePageRevision($page, $summary);
Activity::addForEntity($page, ActivityType::PAGE_RESTORE);
+
return $page;
}
/**
* Move the given page into a new parent book or chapter.
* The $parentIdentifier must be a string of the following format:
- * 'book:<id>' (book:5)
+ * 'book:<id>' (book:5).
+ *
* @throws MoveOperationException
* @throws PermissionsException
*/
$page->rebuildPermissions();
Activity::addForEntity($page, ActivityType::PAGE_MOVE);
+
return $parent;
}
/**
* Copy an existing page in the system.
* Optionally providing a new parent via string identifier and a new name.
+ *
* @throws MoveOperationException
* @throws PermissionsException
*/
/**
* Find a page parent entity via a identifier string in the format:
* {type}:{id}
- * Example: (book:5)
+ * Example: (book:5).
+ *
* @throws MoveOperationException
*/
protected function findParentByIdentifier(string $identifier): ?Entity
}
$parentClass = $entityType === 'book' ? Book::class : Chapter::class;
+
return $parentClass::visible()->where('id', '=', $entityId)->first();
}
$draft->book_slug = $page->book->slug;
$draft->created_by = user()->id;
$draft->type = 'update_draft';
+
return $draft;
}
}
/**
- * Get a new priority for a page
+ * Get a new priority for a page.
*/
protected function getNewPriority(Page $page): int
{
$parent = $page->getParent();
if ($parent instanceof Chapter) {
$lastPage = $parent->pages('desc')->first();
+
return $lastPage ? $lastPage->priority + 1 : 0;
}