| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Entities\Models; | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Auth\User; | 
					
						
							| 
									
										
										
										
											2020-11-19 07:38:44 +08:00
										 |  |  | use BookStack\Interfaces\Loggable; | 
					
						
							| 
									
										
										
										
											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
										 |  |  | /** | 
					
						
							|  |  |  |  * @property Model deletable | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-11-19 07:38:44 +08:00
										 |  |  | class Deletion extends Model implements Loggable | 
					
						
							| 
									
										
										
										
											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([ | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 07:38:44 +08:00
										 |  |  |         return "Deletion ({$this->id}) for {$deletable->getType()} ({$deletable->id}) {$deletable->name}"; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 19:12:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a URL for this specific deletion. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getUrl($path): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return url("/settings/recycle-bin/{$this->id}/" . ltrim($path, '/')); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-09-28 06:24:33 +08:00
										 |  |  | } |