]> BookStack Code Mirror - bookstack/blob - app/Activity/Models/Activity.php
API: Added audit log list endpoint
[bookstack] / app / Activity / Models / Activity.php
1 <?php
2
3 namespace BookStack\Activity\Models;
4
5 use BookStack\App\Model;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Permissions\Models\JointPermission;
8 use BookStack\Users\Models\User;
9 use Illuminate\Database\Eloquent\Relations\BelongsTo;
10 use Illuminate\Database\Eloquent\Relations\HasMany;
11 use Illuminate\Database\Eloquent\Relations\MorphTo;
12 use Illuminate\Support\Carbon;
13 use Illuminate\Support\Str;
14
15 /**
16  * @property string $type
17  * @property User   $user
18  * @property Entity $entity
19  * @property string $detail
20  * @property string $loggable_type
21  * @property int    $loggable_id
22  * @property int    $user_id
23  * @property Carbon $created_at
24  */
25 class Activity extends Model
26 {
27     /**
28      * Get the loggable model related to this activity.
29      * Currently only used for entities (previously entity_[id/type] columns).
30      * Could be used for others but will need an audit of uses where assumed
31      * to be entities.
32      */
33     public function loggable(): MorphTo
34     {
35         return $this->morphTo('loggable');
36     }
37
38     /**
39      * Get the user this activity relates to.
40      */
41     public function user(): BelongsTo
42     {
43         return $this->belongsTo(User::class);
44     }
45
46     public function jointPermissions(): HasMany
47     {
48         return $this->hasMany(JointPermission::class, 'entity_id', 'loggable_id')
49             ->whereColumn('activities.loggable_type', '=', 'joint_permissions.entity_type');
50     }
51
52     /**
53      * Returns text from the language files, Looks up by using the activity key.
54      */
55     public function getText(): string
56     {
57         return trans('activities.' . $this->type);
58     }
59
60     /**
61      * Check if this activity is intended to be for an entity.
62      */
63     public function isForEntity(): bool
64     {
65         return Str::startsWith($this->type, [
66             'page_', 'chapter_', 'book_', 'bookshelf_',
67         ]);
68     }
69
70     /**
71      * Checks if another Activity matches the general information of another.
72      */
73     public function isSimilarTo(self $activityB): bool
74     {
75         return [$this->type, $this->loggable_type, $this->loggable_id] === [$activityB->type, $activityB->loggable_type, $activityB->loggable_id];
76     }
77 }