]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/PageEditorData.php
Adjusted/improved some color setting wording
[bookstack] / app / Entities / Tools / PageEditorData.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Entities\Models\Page;
6 use BookStack\Entities\Repos\PageRepo;
7 use BookStack\Entities\Tools\Markdown\HtmlToMarkdown;
8 use BookStack\Entities\Tools\Markdown\MarkdownToHtml;
9
10 class PageEditorData
11 {
12     protected Page $page;
13     protected PageRepo $pageRepo;
14     protected string $requestedEditor;
15
16     protected array $viewData;
17     protected array $warnings;
18
19     public function __construct(Page $page, PageRepo $pageRepo, string $requestedEditor)
20     {
21         $this->page = $page;
22         $this->pageRepo = $pageRepo;
23         $this->requestedEditor = $requestedEditor;
24
25         $this->viewData = $this->build();
26     }
27
28     public function getViewData(): array
29     {
30         return $this->viewData;
31     }
32
33     public function getWarnings(): array
34     {
35         return $this->warnings;
36     }
37
38     protected function build(): array
39     {
40         $page = clone $this->page;
41         $isDraft = boolval($this->page->draft);
42         $templates = $this->pageRepo->getTemplates(10);
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->pageRepo->getUserDraft($page);
55         if ($userDraft !== null) {
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         ];
73     }
74
75     protected function updateContentForEditor(Page $page, string $editorType): void
76     {
77         $isHtml = !empty($page->html) && empty($page->markdown);
78
79         // HTML to markdown-clean conversion
80         if ($editorType === 'markdown' && $isHtml && $this->requestedEditor === 'markdown-clean') {
81             $page->markdown = (new HtmlToMarkdown($page->html))->convert();
82         }
83
84         // Markdown to HTML conversion if we don't have HTML
85         if ($editorType === 'wysiwyg' && !$isHtml) {
86             $page->html = (new MarkdownToHtml($page->markdown))->convert();
87         }
88     }
89
90     /**
91      * Get the type of editor to show for editing the given page.
92      * Defaults based upon the current content of the page otherwise will fall back
93      * to system default but will take a requested type (if provided) if permissions allow.
94      */
95     protected function getEditorType(Page $page): string
96     {
97         $editorType = $page->editor ?: self::getSystemDefaultEditor();
98
99         // Use requested editor if valid and if we have permission
100         $requestedType = explode('-', $this->requestedEditor)[0];
101         if (($requestedType === 'markdown' || $requestedType === 'wysiwyg') && userCan('editor-change')) {
102             $editorType = $requestedType;
103         }
104
105         return $editorType;
106     }
107
108     /**
109      * Get the configured system default editor.
110      */
111     public static function getSystemDefaultEditor(): string
112     {
113         return setting('app-editor') === 'markdown' ? 'markdown' : 'wysiwyg';
114     }
115 }