]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/PageEditActivity.php
Display warnings when saving draft if another user is editing the page or if the...
[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;
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      * @return bool
26      */
27     public function hasActiveEditing(): bool
28     {
29         $value = $this->activePageEditingQuery(60)->count();
30         return $this->activePageEditingQuery(60)->count() > 0;
31     }
32
33     /**
34      * Get a notification message concerning the editing activity on the page.
35      */
36     public function activeEditingMessage(): string
37     {
38         $pageDraftEdits = $this->activePageEditingQuery(60)->get();
39         $count = $pageDraftEdits->count();
40
41         $userMessage = $count > 1 ? trans('entities.pages_draft_edit_active.start_a', ['count' => $count]) : trans('entities.pages_draft_edit_active.start_b', ['userName' => $pageDraftEdits->first()->createdBy->name]);
42         $timeMessage = trans('entities.pages_draft_edit_active.time_b', ['minCount'=> 60]);
43
44         return trans('entities.pages_draft_edit_active.message', ['start' => $userMessage, 'time' => $timeMessage]);
45     }
46
47     /**
48      * Check if the page has been updated since the draft has been saved.
49      *
50      * @return bool
51      */
52     public function hasPageBeenUpdatedSinceDraftSaved(PageRevision $draft): bool
53     {
54         return $draft->page->updated_at->timestamp >= $draft->updated_at->timestamp;
55     }
56
57     /**
58      * Get the message to show when the user will be editing one of their drafts.
59      *
60      * @param PageRevision $draft
61      *
62      * @return string
63      */
64     public function getEditingActiveDraftMessage(PageRevision $draft): string
65     {
66         $message = trans('entities.pages_editing_draft_notification', ['timeDiff' => $draft->updated_at->diffForHumans()]);
67         if ($draft->page->updated_at->timestamp <= $draft->updated_at->timestamp) {
68             return $message;
69         }
70
71         return $message . "\n" . trans('entities.pages_draft_edited_notification');
72     }
73
74     /**
75      * A query to check for active update drafts on a particular page
76      * within the last given many minutes.
77      */
78     protected function activePageEditingQuery(int $withinMinutes): Builder
79     {
80         $checkTime = Carbon::now()->subMinutes($withinMinutes);
81         $query = PageRevision::query()
82             ->where('type', '=', 'update_draft')
83             ->where('page_id', '=', $this->page->id)
84             ->where('updated_at', '>', $this->page->updated_at)
85             ->where('created_by', '!=', user()->id)
86             ->where('updated_at', '>=', $checkTime)
87             ->with('createdBy');
88
89         return $query;
90     }
91 }