]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/PageEditorData.php
Aligned page edit controller method data usage
[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
8 class PageEditorData
9 {
10     protected Page $page;
11     protected PageRepo $pageRepo;
12
13     protected array $viewData;
14     protected array $warnings;
15
16     public function __construct(Page $page, PageRepo $pageRepo)
17     {
18         $this->page = $page;
19         $this->pageRepo = $pageRepo;
20         $this->viewData = $this->build();
21     }
22
23     public function getViewData(): array
24     {
25         return $this->viewData;
26     }
27
28     public function getWarnings(): array
29     {
30         return $this->warnings;
31     }
32
33     protected function build(): array
34     {
35         $page = clone $this->page;
36         $isDraft = boolval($this->page->draft);
37         $templates = $this->pageRepo->getTemplates(10);
38         $draftsEnabled = auth()->check();
39
40         $isDraftRevision = false;
41         $this->warnings = [];
42         $editActivity = new PageEditActivity($page);
43
44         if ($editActivity->hasActiveEditing()) {
45             $this->warnings[] = $editActivity->activeEditingMessage();
46         }
47
48         // Check for a current draft version for this user
49         $userDraft = $this->pageRepo->getUserDraft($page);
50         if ($userDraft !== null) {
51             $page->forceFill($userDraft->only(['name', 'html', 'markdown']));
52             $isDraftRevision = true;
53             $this->warnings[] = $editActivity->getEditingActiveDraftMessage($userDraft);
54         }
55
56         return [
57             'page'            => $page,
58             'book'            => $page->book,
59             'isDraft'         => $isDraft,
60             'isDraftRevision' => $isDraftRevision,
61             'draftsEnabled'   => $draftsEnabled,
62             'templates'       => $templates,
63             'editor'          =>  setting('app-editor') === 'wysiwyg' ? 'wysiwyg' : 'markdown',
64         ];
65     }
66
67 }