| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  | <?php namespace BookStack\Entities; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Auth\User; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Model; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Relations\BelongsTo; | 
					
						
							|  |  |  | use Illuminate\Database\Eloquent\Relations\MorphTo; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-04 01:44:12 +08:00
										 |  |  | class Deletion extends Model | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * The the user that performed the deletion. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-10-04 01:44:12 +08:00
										 |  |  |     public static function createForEntity(Entity $entity): Deletion | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $record = (new self())->forceFill([ | 
					
						
							|  |  |  |             'deleted_by' => user()->id, | 
					
						
							|  |  |  |             'deletable_type' => $entity->getMorphClass(), | 
					
						
							|  |  |  |             'deletable_id' => $entity->id, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         $record->save(); | 
					
						
							|  |  |  |         return $record; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |