1 <?php namespace BookStack\Entities\Tools;
3 use BookStack\Entities\Models\Page;
4 use BookStack\Entities\Models\PageRevision;
6 use Illuminate\Database\Eloquent\Builder;
14 * PageEditActivity constructor.
16 public function __construct(Page $page)
22 * Check if there's active editing being performed on this page.
25 public function hasActiveEditing(): bool
27 return $this->activePageEditingQuery(60)->count() > 0;
31 * Get a notification message concerning the editing activity on the page.
33 public function activeEditingMessage(): string
35 $pageDraftEdits = $this->activePageEditingQuery(60)->get();
36 $count = $pageDraftEdits->count();
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]);
44 * Get the message to show when the user will be editing one of their drafts.
45 * @param PageRevision $draft
48 public function getEditingActiveDraftMessage(PageRevision $draft): string
50 $message = trans('entities.pages_editing_draft_notification', ['timeDiff' => $draft->updated_at->diffForHumans()]);
51 if ($draft->page->updated_at->timestamp <= $draft->updated_at->timestamp) {
54 return $message . "\n" . trans('entities.pages_draft_edited_notification');
58 * A query to check for active update drafts on a particular page
59 * within the last given many minutes.
61 protected function activePageEditingQuery(int $withinMinutes): Builder
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)