+
+ $draft->fill($data);
+ if (setting('app-editor') !== 'markdown') $draft->markdown = '';
+
+ $draft->save();
+ return $draft;
+ }
+
+ /**
+ * Update a draft page.
+ * @param Page $page
+ * @param array $data
+ * @return Page
+ */
+ public function updateDraftPage(Page $page, $data = [])
+ {
+ $page->fill($data);
+
+ if (isset($data['html'])) {
+ $page->text = strip_tags($data['html']);
+ }
+
+ $page->save();
+ return $page;
+ }
+
+ /**
+ * The base query for getting user update drafts.
+ * @param Page $page
+ * @param $userId
+ * @return mixed
+ */
+ private function userUpdateDraftsQuery(Page $page, $userId)
+ {
+ return $this->pageRevision->where('created_by', '=', $userId)
+ ->where('type', 'update_draft')
+ ->where('page_id', '=', $page->id)
+ ->orderBy('created_at', 'desc');
+ }
+
+ /**
+ * Checks whether a user has a draft version of a particular page or not.
+ * @param Page $page
+ * @param $userId
+ * @return bool
+ */
+ public function hasUserGotPageDraft(Page $page, $userId)
+ {
+ return $this->userUpdateDraftsQuery($page, $userId)->count() > 0;
+ }
+
+ /**
+ * Get the latest updated draft revision for a particular page and user.
+ * @param Page $page
+ * @param $userId
+ * @return mixed
+ */
+ public function getUserPageDraft(Page $page, $userId)
+ {
+ return $this->userUpdateDraftsQuery($page, $userId)->first();
+ }
+
+ /**
+ * Get the notification message that informs the user that they are editing a draft page.
+ * @param PageRevision $draft
+ * @return string
+ */
+ public function getUserPageDraftMessage(PageRevision $draft)
+ {
+ $message = 'You are currently editing a draft that was last saved ' . $draft->updated_at->diffForHumans() . '.';
+ if ($draft->page->updated_at->timestamp > $draft->updated_at->timestamp) {
+ $message .= "\n This page has been updated by since that time. It is recommended that you discard this draft.";
+ }
+ return $message;
+ }
+
+ /**
+ * Check if a page is being actively editing.
+ * Checks for edits since last page updated.
+ * Passing in a minuted range will check for edits
+ * within the last x minutes.
+ * @param Page $page
+ * @param null $minRange
+ * @return bool
+ */
+ public function isPageEditingActive(Page $page, $minRange = null)
+ {
+ $draftSearch = $this->activePageEditingQuery($page, $minRange);
+ return $draftSearch->count() > 0;
+ }
+
+ /**
+ * Get a notification message concerning the editing activity on
+ * a particular page.
+ * @param Page $page
+ * @param null $minRange
+ * @return string
+ */
+ public function getPageEditingActiveMessage(Page $page, $minRange = null)
+ {
+ $pageDraftEdits = $this->activePageEditingQuery($page, $minRange)->get();
+ $userMessage = $pageDraftEdits->count() > 1 ? $pageDraftEdits->count() . ' users have' : $pageDraftEdits->first()->createdBy->name . ' has';
+ $timeMessage = $minRange === null ? 'since the page was last updated' : 'in the last ' . $minRange . ' minutes';
+ $message = '%s started editing this page %s. Take care not to overwrite each other\'s updates!';
+ return sprintf($message, $userMessage, $timeMessage);
+ }
+
+ /**
+ * A query to check for active update drafts on a particular page.
+ * @param Page $page
+ * @param null $minRange
+ * @return mixed
+ */
+ private function activePageEditingQuery(Page $page, $minRange = null)
+ {
+ $query = $this->pageRevision->where('type', '=', 'update_draft')
+ ->where('page_id', '=', $page->id)
+ ->where('updated_at', '>', $page->updated_at)
+ ->where('created_by', '!=', user()->id)
+ ->with('createdBy');
+
+ if ($minRange !== null) {
+ $query = $query->where('updated_at', '>=', Carbon::now()->subMinutes($minRange));
+ }
+
+ return $query;
+ }
+
+ /**
+ * Gets a single revision via it's id.
+ * @param $id
+ * @return PageRevision
+ */
+ public function getRevisionById($id)
+ {
+ return $this->pageRevision->findOrFail($id);
+ }
+
+ /**
+ * Checks if a slug exists within a book already.
+ * @param $slug
+ * @param $bookId
+ * @param bool|false $currentId
+ * @return bool
+ */
+ public function doesSlugExist($slug, $bookId, $currentId = false)
+ {
+ $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
+ if ($currentId) $query = $query->where('id', '!=', $currentId);
+ return $query->count() > 0;
+ }
+
+ /**
+ * Changes the related book for the specified page.
+ * Changes the book id of any relations to the page that store the book id.
+ * @param int $bookId
+ * @param Page $page
+ * @return Page
+ */
+ public function changeBook($bookId, Page $page)
+ {
+ $page->book_id = $bookId;
+ foreach ($page->activity as $activity) {
+ $activity->book_id = $bookId;
+ $activity->save();
+ }
+ $page->slug = $this->findSuitableSlug($page->name, $bookId, $page->id);
+ $page->save();
+ return $page;
+ }
+
+
+ /**
+ * Change the page's parent to the given entity.
+ * @param Page $page
+ * @param Entity $parent
+ */
+ public function changePageParent(Page $page, Entity $parent)
+ {
+ $book = $parent->isA('book') ? $parent : $parent->book;
+ $page->chapter_id = $parent->isA('chapter') ? $parent->id : 0;
+ $page->save();
+ $page = $this->changeBook($book->id, $page);
+ $page->load('book');
+ $this->permissionService->buildJointPermissionsForEntity($book);
+ }
+
+ /**
+ * Gets a suitable slug for the resource
+ * @param string $name
+ * @param int $bookId
+ * @param bool|false $currentId
+ * @return string
+ */
+ public function findSuitableSlug($name, $bookId, $currentId = false)
+ {
+ $slug = Str::slug($name);
+ if ($slug === "") $slug = substr(md5(rand(1, 500)), 0, 5);
+ while ($this->doesSlugExist($slug, $bookId, $currentId)) {
+ $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
+ }
+ return $slug;
+ }
+
+ /**
+ * Destroy a given page along with its dependencies.
+ * @param $page
+ */
+ public function destroy(Page $page)
+ {
+ Activity::removeEntity($page);
+ $page->views()->delete();
+ $page->tags()->delete();
+ $page->revisions()->delete();
+ $page->permissions()->delete();
+ $this->permissionService->deleteJointPermissionsForEntity($page);
+
+ // Delete AttachedFiles
+ $fileService = app(FileService::class);
+ foreach ($page->files as $file) {
+ $fileService->deleteFile($file);
+ }
+
+ $page->delete();
+ }
+
+ /**
+ * Get the latest pages added to the system.
+ * @param $count
+ */
+ public function getRecentlyCreatedPaginated($count = 20)
+ {
+ return $this->pageQuery()->orderBy('created_at', 'desc')->paginate($count);
+ }
+
+ /**
+ * Get the latest pages added to the system.
+ * @param $count
+ */
+ public function getRecentlyUpdatedPaginated($count = 20)
+ {
+ return $this->pageQuery()->orderBy('updated_at', 'desc')->paginate($count);