]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/PageEditorData.php
Queries: Extracted PageRepo queries to own class
[bookstack] / app / Entities / Tools / PageEditorData.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Activity\Tools\CommentTree;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Queries\EntityQueries;
8 use BookStack\Entities\Tools\Markdown\HtmlToMarkdown;
9 use BookStack\Entities\Tools\Markdown\MarkdownToHtml;
10
11 class PageEditorData
12 {
13     protected array $viewData;
14     protected array $warnings;
15
16     public function __construct(
17         protected Page $page,
18         protected EntityQueries $queries,
19         protected string $requestedEditor
20     ) {
21         $this->viewData = $this->build();
22     }
23
24     public function getViewData(): array
25     {
26         return $this->viewData;
27     }
28
29     public function getWarnings(): array
30     {
31         return $this->warnings;
32     }
33
34     protected function build(): array
35     {
36         $page = clone $this->page;
37         $isDraft = boolval($this->page->draft);
38         $templates = $this->queries->pages->visibleTemplates()
39             ->orderBy('name', 'asc')
40             ->take(10)
41             ->get();
42
43         $draftsEnabled = auth()->check();
44
45         $isDraftRevision = false;
46         $this->warnings = [];
47         $editActivity = new PageEditActivity($page);
48
49         if ($editActivity->hasActiveEditing()) {
50             $this->warnings[] = $editActivity->activeEditingMessage();
51         }
52
53         // Check for a current draft version for this user
54         $userDraft = $this->queries->revisions->findLatestCurrentUserDraftsForPageId($page->id)->first();
55         if (!is_null($userDraft)) {
56             $page->forceFill($userDraft->only(['name', 'html', 'markdown']));
57             $isDraftRevision = true;
58             $this->warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
59         }
60
61         $editorType = $this->getEditorType($page);
62         $this->updateContentForEditor($page, $editorType);
63
64         return [
65             'page'            => $page,
66             'book'            => $page->book,
67             'isDraft'         => $isDraft,
68             'isDraftRevision' => $isDraftRevision,
69             'draftsEnabled'   => $draftsEnabled,
70             'templates'       => $templates,
71             'editor'          => $editorType,
72             'comments'        => new CommentTree($page),
73         ];
74     }
75
76     protected function updateContentForEditor(Page $page, string $editorType): void
77     {
78         $isHtml = !empty($page->html) && empty($page->markdown);
79
80         // HTML to markdown-clean conversion
81         if ($editorType === 'markdown' && $isHtml && $this->requestedEditor === 'markdown-clean') {
82             $page->markdown = (new HtmlToMarkdown($page->html))->convert();
83         }
84
85         // Markdown to HTML conversion if we don't have HTML
86         if ($editorType === 'wysiwyg' && !$isHtml) {
87             $page->html = (new MarkdownToHtml($page->markdown))->convert();
88         }
89     }
90
91     /**
92      * Get the type of editor to show for editing the given page.
93      * Defaults based upon the current content of the page otherwise will fall back
94      * to system default but will take a requested type (if provided) if permissions allow.
95      */
96     protected function getEditorType(Page $page): string
97     {
98         $editorType = $page->editor ?: self::getSystemDefaultEditor();
99
100         // Use requested editor if valid and if we have permission
101         $requestedType = explode('-', $this->requestedEditor)[0];
102         if (($requestedType === 'markdown' || $requestedType === 'wysiwyg') && userCan('editor-change')) {
103             $editorType = $requestedType;
104         }
105
106         return $editorType;
107     }
108
109     /**
110      * Get the configured system default editor.
111      */
112     public static function getSystemDefaultEditor(): string
113     {
114         return setting('app-editor') === 'markdown' ? 'markdown' : 'wysiwyg';
115     }
116 }