| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Entities\Models; | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Activity\Models\Loggable; | 
					
						
							|  |  |  | use BookStack\Users\Models\User; | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Model; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Relations\BelongsTo; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Relations\MorphTo; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-26 19:12:11 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2022-04-26 01:42:31 +08:00
										 |  |  |  * @property int       $id | 
					
						
							|  |  |  |  * @property int       $deleted_by | 
					
						
							|  |  |  |  * @property string    $deletable_type | 
					
						
							|  |  |  |  * @property int       $deletable_id | 
					
						
							| 
									
										
										
										
											2021-11-20 22:03:56 +08:00
										 |  |  |  * @property Deletable $deletable | 
					
						
							| 
									
										
										
										
											2021-06-26 19:12:11 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-11-19 07:38:44 +08:00
										 |  |  | class Deletion extends Model implements Loggable | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-26 00:54:59 +08:00
										 |  |  |     protected $hidden = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the related deletable record. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function deletable(): MorphTo | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-04 01:44:12 +08:00
										 |  |  |         return $this->morphTo('deletable')->withTrashed(); | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-11-06 08:32:01 +08:00
										 |  |  |      * Get the user that performed the deletion. | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-10-04 01:44:12 +08:00
										 |  |  |     public function deleter(): BelongsTo | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-04 01:44:12 +08:00
										 |  |  |         return $this->belongsTo(User::class, 'deleted_by'); | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Create a new deletion record for the provided entity. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-10-27 05:04:18 +08:00
										 |  |  |     public static function createForEntity(Entity $entity): self | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $record = (new self())->forceFill([ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'deleted_by'     => user()->id, | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |             'deletable_type' => $entity->getMorphClass(), | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'deletable_id'   => $entity->id, | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |         $record->save(); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |         return $record; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 07:38:44 +08:00
										 |  |  |     public function logDescriptor(): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $deletable = $this->deletable()->first(); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 08:32:01 +08:00
										 |  |  |         if ($deletable instanceof Entity) { | 
					
						
							|  |  |  |             return "Deletion ({$this->id}) for {$deletable->getType()} ({$deletable->id}) {$deletable->name}"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return "Deletion ({$this->id})"; | 
					
						
							| 
									
										
										
										
											2020-11-19 07:38:44 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 19:12:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a URL for this specific deletion. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-01-11 01:46:17 +08:00
										 |  |  |     public function getUrl(string $path = 'restore'): string | 
					
						
							| 
									
										
										
										
											2021-06-26 19:12:11 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         return url("/settings/recycle-bin/{$this->id}/" . ltrim($path, '/')); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  | } |