| 
									
										
										
										
											2015-08-17 01:59:23 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | namespace BookStack\Activity\Models; | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\App\Model; | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Permissions\Models\JointPermission; | 
					
						
							|  |  |  | use BookStack\Users\Models\User; | 
					
						
							| 
									
										
										
										
											2020-11-08 08:03:19 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Relations\BelongsTo; | 
					
						
							| 
									
										
										
										
											2023-01-24 22:55:34 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Relations\HasMany; | 
					
						
							| 
									
										
										
										
											2021-03-08 06:24:05 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Relations\MorphTo; | 
					
						
							| 
									
										
										
										
											2023-12-10 22:58:05 +08:00
										 |  |  | use Illuminate\Support\Carbon; | 
					
						
							| 
									
										
										
										
											2020-11-21 03:33:11 +08:00
										 |  |  | use Illuminate\Support\Str; | 
					
						
							| 
									
										
										
										
											2015-08-17 01:59:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2020-11-08 08:03:19 +08:00
										 |  |  |  * @property string $type | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |  * @property User   $user | 
					
						
							| 
									
										
										
										
											2024-05-05 22:44:58 +08:00
										 |  |  |  * @property Entity $loggable | 
					
						
							| 
									
										
										
										
											2020-11-08 08:03:19 +08:00
										 |  |  |  * @property string $detail | 
					
						
							| 
									
										
										
										
											2024-05-04 23:28:18 +08:00
										 |  |  |  * @property string $loggable_type | 
					
						
							|  |  |  |  * @property int    $loggable_id | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |  * @property int    $user_id | 
					
						
							| 
									
										
										
										
											2023-06-18 01:18:17 +08:00
										 |  |  |  * @property Carbon $created_at | 
					
						
							| 
									
										
										
										
											2015-08-17 01:59:23 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | class Activity extends Model | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-30 18:47:58 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2024-05-04 23:28:18 +08:00
										 |  |  |      * Get the loggable model related to this activity. | 
					
						
							|  |  |  |      * Currently only used for entities (previously entity_[id/type] columns). | 
					
						
							|  |  |  |      * Could be used for others but will need an audit of uses where assumed | 
					
						
							|  |  |  |      * to be entities. | 
					
						
							| 
									
										
										
										
											2015-08-30 18:47:58 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2024-05-04 23:28:18 +08:00
										 |  |  |     public function loggable(): MorphTo | 
					
						
							| 
									
										
										
										
											2015-08-17 01:59:23 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-05-04 23:28:18 +08:00
										 |  |  |         return $this->morphTo('loggable'); | 
					
						
							| 
									
										
										
										
											2015-08-17 01:59:23 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-30 18:47:58 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the user this activity relates to. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-11-08 08:03:19 +08:00
										 |  |  |     public function user(): BelongsTo | 
					
						
							| 
									
										
										
										
											2015-08-17 01:59:23 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |         return $this->belongsTo(User::class); | 
					
						
							| 
									
										
										
										
											2015-08-17 01:59:23 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-24 22:55:34 +08:00
										 |  |  |     public function jointPermissions(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-05-04 23:28:18 +08:00
										 |  |  |         return $this->hasMany(JointPermission::class, 'entity_id', 'loggable_id') | 
					
						
							|  |  |  |             ->whereColumn('activities.loggable_type', '=', 'joint_permissions.entity_type'); | 
					
						
							| 
									
										
										
										
											2023-01-24 22:55:34 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-17 01:59:23 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2020-11-08 08:03:19 +08:00
										 |  |  |      * Returns text from the language files, Looks up by using the activity key. | 
					
						
							| 
									
										
										
										
											2015-08-17 01:59:23 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-11-08 08:03:19 +08:00
										 |  |  |     public function getText(): string | 
					
						
							| 
									
										
										
										
											2015-08-17 01:59:23 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-08 08:03:19 +08:00
										 |  |  |         return trans('activities.' . $this->type); | 
					
						
							| 
									
										
										
										
											2015-08-17 01:59:23 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 03:33:11 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check if this activity is intended to be for an entity. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function isForEntity(): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return Str::startsWith($this->type, [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'page_', 'chapter_', 'book_', 'bookshelf_', | 
					
						
							| 
									
										
										
										
											2020-11-21 03:33:11 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-30 18:47:58 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Checks if another Activity matches the general information of another. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-10-27 05:04:18 +08:00
										 |  |  |     public function isSimilarTo(self $activityB): bool | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-05-04 23:28:18 +08:00
										 |  |  |         return [$this->type, $this->loggable_type, $this->loggable_id] === [$activityB->type, $activityB->loggable_type, $activityB->loggable_id]; | 
					
						
							| 
									
										
										
										
											2015-08-30 18:47:58 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-17 01:59:23 +08:00
										 |  |  | } |