+ $this->permissionService->buildJointPermissionsForEntity($entity);
+ }
+
+
+
+ /**
+ * 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;
+ }
+
+ /**
+ * Get a new draft page instance.
+ * @param Book $book
+ * @param Chapter|bool $chapter
+ * @return Page
+ */
+ public function getDraftPage(Book $book, $chapter = false)
+ {
+ $page = $this->page->newInstance();
+ $page->name = trans('entities.pages_initial_name');
+ $page->created_by = user()->id;
+ $page->updated_by = user()->id;
+ $page->draft = true;
+
+ if ($chapter) {
+ $page->chapter_id = $chapter->id;
+ }
+
+ $book->pages()->save($page);
+ $page = $this->page->find($page->id);
+ $this->permissionService->buildJointPermissionsForEntity($page);
+ return $page;
+ }
+
+ /**
+ * 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;
+ }
+
+ /**
+ * Create a copy of a page in a new location with a new name.
+ * @param Page $page
+ * @param Entity $newParent
+ * @param string $newName
+ * @return Page
+ */
+ public function copyPage(Page $page, Entity $newParent, $newName = '')
+ {
+ $newBook = $newParent->isA('book') ? $newParent : $newParent->book;
+ $newChapter = $newParent->isA('chapter') ? $newParent : null;
+ $copyPage = $this->getDraftPage($newBook, $newChapter);
+ $pageData = $page->getAttributes();
+
+ // Update name
+ if (!empty($newName)) {
+ $pageData['name'] = $newName;
+ }
+
+ // Copy tags from previous page if set
+ if ($page->tags) {
+ $pageData['tags'] = [];
+ foreach ($page->tags as $tag) {
+ $pageData['tags'][] = ['name' => $tag->name, 'value' => $tag->value];
+ }
+ }
+
+ // Set priority
+ if ($newParent->isA('chapter')) {
+ $pageData['priority'] = $this->getNewChapterPriority($newParent);
+ } else {
+ $pageData['priority'] = $this->getNewBookPriority($newParent);
+ }
+
+ return $this->publishPageDraft($copyPage, $pageData);