1 <?php namespace BookStack\Services;
3 use Illuminate\Support\Facades\Auth;
4 use BookStack\Activity;
14 * ActivityService constructor.
17 public function __construct(Activity $activity)
19 $this->activity = $activity;
20 $this->user = auth()->user();
24 * Add activity data to database.
25 * @param Entity $entity
30 public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
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;
38 $entity->activity()->save($this->activity);
39 $this->setNotification($activityKey);
43 * Adds a activity history with a message & without binding to a entitiy.
46 * @param bool|false $extra
48 public function addMessage($activityKey, $bookId = 0, $extra = false)
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;
56 $this->activity->save();
57 $this->setNotification($activityKey);
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
68 public function removeEntity(Entity $entity)
70 $activities = $entity->activity;
71 foreach ($activities as $activity) {
72 $activity->extra = $entity->name;
73 $activity->entity_id = 0;
74 $activity->entity_type = null;
81 * Gets the latest activity.
86 public function latest($count = 20, $page = 0)
88 $activityList = $this->activity->orderBy('created_at', 'desc')
89 ->skip($count * $page)->take($count)->get();
90 return $this->filterSimilar($activityList);
94 * Gets the latest activity for an entitiy, Filtering out similar
95 * items to prevent a message activity list.
96 * @param Entity $entity
101 function entityActivity($entity, $count = 20, $page = 0)
103 $activity = $entity->hasMany('BookStack\Activity')->orderBy('created_at', 'desc')
104 ->skip($count * $page)->take($count)->get();
106 return $this->filterSimilar($activity);
110 * Filters out similar activity.
111 * @param Activity[] $activity
114 protected function filterSimilar($activity)
117 $previousItem = false;
118 foreach ($activity as $activityItem) {
119 if ($previousItem === false) {
120 $previousItem = $activityItem;
121 $newActivity[] = $activityItem;
124 if (!$activityItem->isSimilarTo($previousItem)) {
125 $newActivity[] = $activityItem;
127 $previousItem = $activityItem;
133 * Flashes a notification message to the session if an appropriate message is available.
134 * @param $activityKey
136 protected function setNotification($activityKey)
138 $notificationTextKey = 'activities.' . $activityKey . '_notification';
139 if (trans()->has($notificationTextKey)) {
140 $message = trans($notificationTextKey);
141 Session::flash('success', $message);