+ * Create a new entity from request input.
+ * Used for books and chapters.
+ * @param string $type
+ * @param array $input
+ * @param bool|Book $book
+ * @return Entity
+ */
+ public function createFromInput($type, $input = [], $book = false)
+ {
+ $isChapter = strtolower($type) === 'chapter';
+ $entityModel = $this->getEntity($type)->newInstance($input);
+ $entityModel->slug = $this->findSuitableSlug($type, $entityModel->name, false, $isChapter ? $book->id : false);
+ $entityModel->created_by = user()->id;
+ $entityModel->updated_by = user()->id;
+ $isChapter ? $book->chapters()->save($entityModel) : $entityModel->save();
+
+ if (isset($input['tags'])) {
+ $this->tagRepo->saveTagsToEntity($entityModel, $input['tags']);
+ }
+
+ $this->permissionService->buildJointPermissionsForEntity($entityModel);
+ $this->searchService->indexEntity($entityModel);
+ return $entityModel;
+ }
+
+ /**
+ * Update entity details from request input.
+ * Used for books and chapters
+ * @param string $type
+ * @param Entity $entityModel
+ * @param array $input
+ * @return Entity
+ */
+ public function updateFromInput($type, Entity $entityModel, $input = [])
+ {
+ if ($entityModel->name !== $input['name']) {
+ $entityModel->slug = $this->findSuitableSlug($type, $input['name'], $entityModel->id);
+ }
+ $entityModel->fill($input);
+ $entityModel->updated_by = user()->id;
+ $entityModel->save();
+
+ if (isset($input['tags'])) {
+ $this->tagRepo->saveTagsToEntity($entityModel, $input['tags']);
+ }
+
+ $this->permissionService->buildJointPermissionsForEntity($entityModel);
+ $this->searchService->indexEntity($entityModel);
+ return $entityModel;
+ }
+
+ /**
+ * Change the book that an entity belongs to.
+ * @param string $type
+ * @param integer $newBookId
+ * @param Entity $entity
+ * @param bool $rebuildPermissions
+ * @return Entity
+ */
+ public function changeBook($type, $newBookId, Entity $entity, $rebuildPermissions = false)
+ {
+ $entity->book_id = $newBookId;
+ // Update related activity
+ foreach ($entity->activity as $activity) {
+ $activity->book_id = $newBookId;
+ $activity->save();
+ }
+ $entity->slug = $this->findSuitableSlug($type, $entity->name, $entity->id, $newBookId);
+ $entity->save();
+
+ // Update all child pages if a chapter
+ if (strtolower($type) === 'chapter') {
+ foreach ($entity->pages as $page) {
+ $this->changeBook('page', $newBookId, $page, false);
+ }
+ }
+
+ // Update permissions if applicable
+ if ($rebuildPermissions) {
+ $entity->load('book');
+ $this->permissionService->buildJointPermissionsForEntity($entity->book);
+ }
+
+ return $entity;
+ }
+
+ /**
+ * Alias method to update the book jointPermissions in the PermissionService.
+ * @param Book $book
+ */
+ public function buildJointPermissionsForBook(Book $book)
+ {
+ $this->permissionService->buildJointPermissionsForEntity($book);
+ }
+
+ /**
+ * Format a name as a url slug.
+ * @param $name
+ * @return string
+ */
+ protected function nameToSlug($name)
+ {
+ $slug = preg_replace('/[\+\/\\\?\@\}\{\.\,\=\[\]\#\&\!\*\'\;\:\$\%]/', '', mb_strtolower($name));
+ $slug = preg_replace('/\s{2,}/', ' ', $slug);
+ $slug = str_replace(' ', '-', $slug);
+ if ($slug === "") {
+ $slug = substr(md5(rand(1, 500)), 0, 5);
+ }
+ return $slug;
+ }
+
+ /**
+ * Publish a draft page to make it a normal page.
+ * Sets the slug and updates the content.
+ * @param Page $draftPage
+ * @param array $input
+ * @return Page
+ */
+ public function publishPageDraft(Page $draftPage, array $input)
+ {
+ $draftPage->fill($input);
+
+ // Save page tags if present
+ if (isset($input['tags'])) {
+ $this->tagRepo->saveTagsToEntity($draftPage, $input['tags']);
+ }
+
+ $draftPage->slug = $this->findSuitableSlug('page', $draftPage->name, false, $draftPage->book->id);
+ $draftPage->html = $this->formatHtml($input['html']);
+ $draftPage->text = $this->pageToPlainText($draftPage);
+ $draftPage->draft = false;
+ $draftPage->revision_count = 1;
+
+ $draftPage->save();
+ $this->savePageRevision($draftPage, trans('entities.pages_initial_revision'));
+ $this->searchService->indexEntity($draftPage);
+ return $draftPage;
+ }
+
+ /**
+ * Saves a page revision into the system.
+ * @param Page $page
+ * @param null|string $summary
+ * @return PageRevision
+ */
+ public function savePageRevision(Page $page, $summary = null)
+ {
+ $revision = $this->pageRevision->newInstance($page->toArray());
+ if (setting('app-editor') !== 'markdown') {
+ $revision->markdown = '';
+ }
+ $revision->page_id = $page->id;
+ $revision->slug = $page->slug;
+ $revision->book_slug = $page->book->slug;
+ $revision->created_by = user()->id;
+ $revision->created_at = $page->updated_at;
+ $revision->type = 'version';
+ $revision->summary = $summary;
+ $revision->revision_number = $page->revision_count;
+ $revision->save();
+
+ // Clear old revisions
+ if ($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
+ $this->pageRevision->where('page_id', '=', $page->id)
+ ->orderBy('created_at', 'desc')->skip(50)->take(5)->delete();
+ }
+
+ return $revision;
+ }
+
+ /**
+ * Formats a page's html to be tagged correctly
+ * within the system.
+ * @param string $htmlText
+ * @return string