]> BookStack Code Mirror - bookstack/blob - app/Services/ActivityService.php
Added activity history to to all entities. Fixes #12
[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
7 class ActivityService
8 {
9     protected $activity;
10     protected $user;
11
12     /**
13      * ActivityService constructor.
14      * @param $activity
15      */
16     public function __construct(Activity $activity)
17     {
18         $this->activity = $activity;
19         $this->user = Auth::user();
20     }
21
22
23     /**
24      * Add activity data to database.
25      * @para Entity $entity
26      * @param $activityKey
27      * @param int $bookId
28      */
29     public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
30     {
31         $this->activity->user_id = $this->user->id;
32         $this->activity->book_id = $bookId;
33         $this->activity->key = strtolower($activityKey);
34         if($extra !== false) {
35             $this->activity->extra = $extra;
36         }
37         $entity->activity()->save($this->activity);
38     }
39
40     /**
41      * Adds a activity history with a message & without binding to a entitiy.
42      * @param $activityKey
43      * @param int $bookId
44      * @param bool|false $extra
45      */
46     public function addMessage($activityKey, $bookId = 0, $extra = false)
47     {
48         $this->activity->user_id = $this->user->id;
49         $this->activity->book_id = $bookId;
50         $this->activity->key = strtolower($activityKey);
51         if($extra !== false) {
52             $this->activity->extra = $extra;
53         }
54         $this->activity->save();
55     }
56
57 }