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