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