]> BookStack Code Mirror - bookstack/blob - app/Actions/Activity.php
Added force option for update-url command
[bookstack] / app / Actions / Activity.php
1 <?php
2
3 namespace BookStack\Actions;
4
5 use BookStack\Auth\Permissions\JointPermission;
6 use BookStack\Auth\User;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Model;
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  */
23 class Activity extends Model
24 {
25     /**
26      * Get the entity for this activity.
27      */
28     public function entity(): MorphTo
29     {
30         if ($this->entity_type === '') {
31             $this->entity_type = null;
32         }
33
34         return $this->morphTo('entity');
35     }
36
37     /**
38      * Get the user this activity relates to.
39      */
40     public function user(): BelongsTo
41     {
42         return $this->belongsTo(User::class);
43     }
44
45     public function jointPermissions(): HasMany
46     {
47         return $this->hasMany(JointPermission::class, 'entity_id', 'entity_id')
48             ->whereColumn('activities.entity_type', '=', 'joint_permissions.entity_type');
49     }
50
51     /**
52      * Returns text from the language files, Looks up by using the activity key.
53      */
54     public function getText(): string
55     {
56         return trans('activities.' . $this->type);
57     }
58
59     /**
60      * Check if this activity is intended to be for an entity.
61      */
62     public function isForEntity(): bool
63     {
64         return Str::startsWith($this->type, [
65             'page_', 'chapter_', 'book_', 'bookshelf_',
66         ]);
67     }
68
69     /**
70      * Checks if another Activity matches the general information of another.
71      */
72     public function isSimilarTo(self $activityB): bool
73     {
74         return [$this->type, $this->entity_type, $this->entity_id] === [$activityB->type, $activityB->entity_type, $activityB->entity_id];
75     }
76 }