]> BookStack Code Mirror - bookstack/blob - app/Activity/Models/Activity.php
Queries: Update API to align data with previous versions
[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 $entity_type
21  * @property int    $entity_id
22  * @property int    $user_id
23  * @property Carbon $created_at
24  * @property Carbon $updated_at
25  */
26 class Activity extends Model
27 {
28     /**
29      * Get the entity for this activity.
30      */
31     public function entity(): MorphTo
32     {
33         if ($this->entity_type === '') {
34             $this->entity_type = null;
35         }
36
37         return $this->morphTo('entity');
38     }
39
40     /**
41      * Get the user this activity relates to.
42      */
43     public function user(): BelongsTo
44     {
45         return $this->belongsTo(User::class);
46     }
47
48     public function jointPermissions(): HasMany
49     {
50         return $this->hasMany(JointPermission::class, 'entity_id', 'entity_id')
51             ->whereColumn('activities.entity_type', '=', 'joint_permissions.entity_type');
52     }
53
54     /**
55      * Returns text from the language files, Looks up by using the activity key.
56      */
57     public function getText(): string
58     {
59         return trans('activities.' . $this->type);
60     }
61
62     /**
63      * Check if this activity is intended to be for an entity.
64      */
65     public function isForEntity(): bool
66     {
67         return Str::startsWith($this->type, [
68             'page_', 'chapter_', 'book_', 'bookshelf_',
69         ]);
70     }
71
72     /**
73      * Checks if another Activity matches the general information of another.
74      */
75     public function isSimilarTo(self $activityB): bool
76     {
77         return [$this->type, $this->entity_type, $this->entity_id] === [$activityB->type, $activityB->entity_type, $activityB->entity_id];
78     }
79 }