* @return Page
* @throws \BookStack\Exceptions\NotFoundException
*/
- public function getPageBySlug(string $pageSlug, string $bookSlug)
+ public function getBySlug(string $pageSlug, string $bookSlug)
{
- return $this->getBySlug('page', $pageSlug, $bookSlug);
+ return $this->getEntityBySlug('page', $pageSlug, $bookSlug);
}
/**
$oldHtml = $page->html;
$oldName = $page->name;
- // Prevent slug being updated if no name change
- if ($page->name !== $input['name']) {
- $page->slug = $this->findSuitableSlug('page', $input['name'], $page->id, $book_id);
- }
-
// Save page tags if present
if (isset($input['tags'])) {
$this->tagRepo->saveTagsToEntity($page, $input['tags']);
}
+ if (isset($input['template']) && userCan('templates-manage')) {
+ $page->template = ($input['template'] === 'true');
+ }
+
// Update with new details
$userId = user()->id;
$page->fill($input);
$page->html = $this->formatHtml($input['html']);
$page->text = $this->pageToPlainText($page);
+ $page->updated_by = $userId;
+ $page->revision_count++;
+
if (setting('app-editor') !== 'markdown') {
$page->markdown = '';
}
- $page->updated_by = $userId;
- $page->revision_count++;
+
+ if ($page->isDirty('name')) {
+ $page->refreshSlug();
+ }
+
$page->save();
// Remove all update drafts for this user & page.
$this->userUpdatePageDraftsQuery($page, $userId)->delete();
// Save a revision after updating
- if ($oldHtml !== $input['html'] || $oldName !== $input['name'] || $input['summary'] !== null) {
- $this->savePageRevision($page, $input['summary']);
+ $summary = $input['summary'] ?? null;
+ if ($oldHtml !== $input['html'] || $oldName !== $input['name'] || $summary !== null) {
+ $this->savePageRevision($page, $summary);
}
$this->searchService->indexEntity($page);
// Create an unique id for the element
// Uses the content as a basis to ensure output is the same every time
// the same content is passed through.
- $contentId = 'bkmrk-' . substr(strtolower(preg_replace('/\s+/', '-', trim($element->nodeValue))), 0, 20);
+ $contentId = 'bkmrk-' . mb_substr(strtolower(preg_replace('/\s+/', '-', trim($element->nodeValue))), 0, 20);
$newId = urlencode($contentId);
$loopIndex = 0;
}
$book->pages()->save($page);
- $page = $this->entityProvider->page->find($page->id);
- $this->permissionService->buildJointPermissionsForEntity($page);
+ $page->refresh()->rebuildPermissions();
return $page;
}
$this->tagRepo->saveTagsToEntity($draftPage, $input['tags']);
}
- $draftPage->slug = $this->findSuitableSlug('page', $draftPage->name, false, $draftPage->book->id);
+ if (isset($input['template']) && userCan('templates-manage')) {
+ $draftPage->template = ($input['template'] === 'true');
+ }
+
$draftPage->html = $this->formatHtml($input['html']);
$draftPage->text = $this->pageToPlainText($draftPage);
$draftPage->draft = false;
$draftPage->revision_count = 1;
-
+ $draftPage->refreshSlug();
$draftPage->save();
$this->savePageRevision($draftPage, trans('entities.pages_initial_revision'));
$this->searchService->indexEntity($draftPage);
return [];
}
- $tree = collect([]);
- foreach ($headers as $header) {
- $text = $header->nodeValue;
- $tree->push([
+ $tree = collect($headers)->map(function ($header) {
+ $text = trim(str_replace("\xc2\xa0", '', $header->nodeValue));
+ $text = mb_substr($text, 0, 100);
+
+ return [
'nodeName' => strtolower($header->nodeName),
'level' => intval(str_replace('h', '', $header->nodeName)),
'link' => '#' . $header->getAttribute('id'),
- 'text' => strlen($text) > 30 ? substr($text, 0, 27) . '...' : $text
- ]);
- }
+ 'text' => $text,
+ ];
+ })->filter(function ($header) {
+ return mb_strlen($header['text']) > 0;
+ });
+
+ // Shift headers if only smaller headers have been used
+ $levelChange = ($tree->pluck('level')->min() - 1);
+ $tree = $tree->map(function ($header) use ($levelChange) {
+ $header['level'] -= ($levelChange);
+ return $header;
+ });
- // Normalise headers if only smaller headers have been used
- if (count($tree) > 0) {
- $minLevel = $tree->pluck('level')->min();
- $tree = $tree->map(function ($header) use ($minLevel) {
- $header['level'] -= ($minLevel - 2);
- return $header;
- });
- }
return $tree->toArray();
}
{
$page->revision_count++;
$this->savePageRevision($page);
+
$revision = $page->revisions()->where('id', '=', $revisionId)->first();
$page->fill($revision->toArray());
- $page->slug = $this->findSuitableSlug('page', $page->name, $page->id, $book->id);
$page->text = $this->pageToPlainText($page);
$page->updated_by = user()->id;
+ $page->refreshSlug();
$page->save();
+
$this->searchService->indexEntity($page);
return $page;
}
* Change the page's parent to the given entity.
* @param Page $page
* @param Entity $parent
- * @throws \Throwable
*/
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();
+
if ($page->book->id !== $book->id) {
- $page = $this->changeBook('page', $book->id, $page);
+ $page = $this->changeBook($page, $book->id);
}
+
$page->load('book');
- $this->permissionService->buildJointPermissionsForEntity($book);
+ $book->rebuildPermissions();
}
/**
return $this->publishPageDraft($copyPage, $pageData);
}
+
+ /**
+ * Get pages that have been marked as templates.
+ * @param int $count
+ * @param int $page
+ * @param string $search
+ * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
+ */
+ public function getPageTemplates(int $count = 10, int $page = 1, string $search = '')
+ {
+ $query = $this->entityQuery('page')
+ ->where('template', '=', true)
+ ->orderBy('name', 'asc')
+ ->skip(($page - 1) * $count)
+ ->take($count);
+
+ if ($search) {
+ $query->where('name', 'like', '%' . $search . '%');
+ }
+
+ $paginator = $query->paginate($count, ['*'], 'page', $page);
+ $paginator->withPath('/templates');
+
+ return $paginator;
+ }
}