]> BookStack Code Mirror - bookstack/blob - app/Entities/Managers/PageEditActivity.php
added api functionality to handle book Shelves
[bookstack] / app / Entities / Managers / PageEditActivity.php
1 <?php namespace BookStack\Entities\Managers;
2
3 use BookStack\Entities\Page;
4 use BookStack\Entities\PageRevision;
5 use Carbon\Carbon;
6 use Illuminate\Database\Eloquent\Builder;
7
8 class PageEditActivity
9 {
10
11     protected $page;
12
13     /**
14      * PageEditActivity constructor.
15      */
16     public function __construct(Page $page)
17     {
18         $this->page = $page;
19     }
20
21     /**
22      * Check if there's active editing being performed on this page.
23      * @return bool
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 = $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]);
39         $timeMessage = trans('entities.pages_draft_edit_active.time_b', ['minCount'=> 60]);
40         return trans('entities.pages_draft_edit_active.message', ['start' => $userMessage, 'time' => $timeMessage]);
41     }
42
43     /**
44      * Get the message to show when the user will be editing one of their drafts.
45      * @param PageRevision $draft
46      * @return string
47      */
48     public function getEditingActiveDraftMessage(PageRevision $draft): string
49     {
50         $message = trans('entities.pages_editing_draft_notification', ['timeDiff' => $draft->updated_at->diffForHumans()]);
51         if ($draft->page->updated_at->timestamp <= $draft->updated_at->timestamp) {
52             return $message;
53         }
54         return $message . "\n" . trans('entities.pages_draft_edited_notification');
55     }
56
57     /**
58      * A query to check for active update drafts on a particular page
59      * within the last given many minutes.
60      */
61     protected function activePageEditingQuery(int $withinMinutes): Builder
62     {
63         $checkTime = Carbon::now()->subMinutes($withinMinutes);
64         $query = PageRevision::query()
65             ->where('type', '=', 'update_draft')
66             ->where('page_id', '=', $this->page->id)
67             ->where('updated_at', '>', $this->page->updated_at)
68             ->where('created_by', '!=', user()->id)
69             ->where('updated_at', '>=', $checkTime)
70             ->with('createdBy');
71
72         return $query;
73     }
74 }