]> BookStack Code Mirror - bookstack/blob - app/Services/ActivityService.php
Updated views for permissions and added notifications. Fixes #2 and #7
[bookstack] / app / Services / ActivityService.php
1 <?php namespace Oxbow\Services;
2
3 use Illuminate\Support\Facades\Auth;
4 use Oxbow\Activity;
5 use Oxbow\Entity;
6 use Session;
7
8 class ActivityService
9 {
10     protected $activity;
11     protected $user;
12
13     /**
14      * ActivityService constructor.
15      * @param $activity
16      */
17     public function __construct(Activity $activity)
18     {
19         $this->activity = $activity;
20         $this->user = Auth::user();
21     }
22
23     /**
24      * Add activity data to database.
25      * @param Entity $entity
26      * @param $activityKey
27      * @param int $bookId
28      * @param bool $extra
29      */
30     public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
31     {
32         $this->activity->user_id = $this->user->id;
33         $this->activity->book_id = $bookId;
34         $this->activity->key = strtolower($activityKey);
35         if($extra !== false) {
36             $this->activity->extra = $extra;
37         }
38         $entity->activity()->save($this->activity);
39         $this->setNotification($activityKey);
40     }
41
42     /**
43      * Adds a activity history with a message & without binding to a entitiy.
44      * @param $activityKey
45      * @param int $bookId
46      * @param bool|false $extra
47      */
48     public function addMessage($activityKey, $bookId = 0, $extra = false)
49     {
50         $this->activity->user_id = $this->user->id;
51         $this->activity->book_id = $bookId;
52         $this->activity->key = strtolower($activityKey);
53         if($extra !== false) {
54             $this->activity->extra = $extra;
55         }
56         $this->activity->save();
57         $this->setNotification($activityKey);
58     }
59
60
61     /**
62      * Removes the entity attachment from each of its activities
63      * and instead uses the 'extra' field with the entities name.
64      * Used when an entity is deleted.
65      * @param Entity $entity
66      * @return mixed
67      */
68     public function removeEntity(Entity $entity)
69     {
70         $activities = $entity->activity;
71         foreach($activities as $activity) {
72             $activity->extra = $entity->name;
73             $activity->entity_id = 0;
74             $activity->entity_type = null;
75             $activity->save();
76         }
77         return $activities;
78     }
79
80     /**
81      * Gets the latest activity.
82      * @param int $count
83      * @param int $page
84      */
85     public function latest($count = 20, $page = 0)
86     {
87         return $this->activity->orderBy('created_at', 'desc')
88             ->skip($count*$page)->take($count)->get();
89     }
90
91     /**
92      * Flashes a notification message to the session if an appropriate message is available.
93      * @param $activityKey
94      */
95     protected function setNotification($activityKey)
96     {
97         $notificationTextKey = 'activities.' . $activityKey . '_notification';
98         if (trans()->has($notificationTextKey)) {
99             $message = trans($notificationTextKey);
100             Session::flash('success', $message);
101         }
102     }
103
104 }