]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/PageEditActivity.php
ExportFormatter: Add book description and check for empty book and chapter descriptio...
[bookstack] / app / Entities / Tools / PageEditActivity.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Entities\Models\Page;
6 use BookStack\Entities\Models\PageRevision;
7 use Carbon\Carbon;
8 use Illuminate\Database\Eloquent\Builder;
9
10 class PageEditActivity
11 {
12     protected Page $page;
13
14     /**
15      * PageEditActivity constructor.
16      */
17     public function __construct(Page $page)
18     {
19         $this->page = $page;
20     }
21
22     /**
23      * Check if there's active editing being performed on this page.
24      */
25     public function hasActiveEditing(): bool
26     {
27         return $this->activePageEditingQuery(60)->count() > 0;
28     }
29
30     /**
31      * Get a notification message concerning the editing activity on the page.
32      */
33     public function activeEditingMessage(): string
34     {
35         $pageDraftEdits = $this->activePageEditingQuery(60)->get();
36         $count = $pageDraftEdits->count();
37
38         $userMessage = trans('entities.pages_draft_edit_active.start_a', ['count' => $count]);
39         if ($count === 1) {
40             /** @var PageRevision $firstDraft */
41             $firstDraft = $pageDraftEdits->first();
42             $userMessage = trans('entities.pages_draft_edit_active.start_b', ['userName' => $firstDraft->createdBy->name ?? '']);
43         }
44
45         $timeMessage = trans('entities.pages_draft_edit_active.time_b', ['minCount' => 60]);
46
47         return trans('entities.pages_draft_edit_active.message', ['start' => $userMessage, 'time' => $timeMessage]);
48     }
49
50     /**
51      * Get any editor clash warning messages to show for the given draft revision.
52      *
53      * @param PageRevision|Page $draft
54      *
55      * @return string[]
56      */
57     public function getWarningMessagesForDraft($draft): array
58     {
59         $warnings = [];
60
61         if ($this->hasActiveEditing()) {
62             $warnings[] = $this->activeEditingMessage();
63         }
64
65         if ($draft instanceof PageRevision && $this->hasPageBeenUpdatedSinceDraftCreated($draft)) {
66             $warnings[] = trans('entities.pages_draft_page_changed_since_creation');
67         }
68
69         return $warnings;
70     }
71
72     /**
73      * Check if the page has been updated since the draft has been saved.
74      */
75     protected function hasPageBeenUpdatedSinceDraftCreated(PageRevision $draft): bool
76     {
77         return $draft->page->updated_at->timestamp > $draft->created_at->timestamp;
78     }
79
80     /**
81      * Get the message to show when the user will be editing one of their drafts.
82      */
83     public function getEditingActiveDraftMessage(PageRevision $draft): string
84     {
85         $message = trans('entities.pages_editing_draft_notification', ['timeDiff' => $draft->updated_at->diffForHumans()]);
86         if ($draft->page->updated_at->timestamp <= $draft->updated_at->timestamp) {
87             return $message;
88         }
89
90         return $message . "\n" . trans('entities.pages_draft_edited_notification');
91     }
92
93     /**
94      * A query to check for active update drafts on a particular page
95      * within the last given many minutes.
96      */
97     protected function activePageEditingQuery(int $withinMinutes): Builder
98     {
99         $checkTime = Carbon::now()->subMinutes($withinMinutes);
100         $query = PageRevision::query()
101             ->where('type', '=', 'update_draft')
102             ->where('page_id', '=', $this->page->id)
103             ->where('updated_at', '>', $this->page->updated_at)
104             ->where('created_by', '!=', user()->id)
105             ->where('updated_at', '>=', $checkTime)
106             ->with('createdBy');
107
108         return $query;
109     }
110 }