| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | namespace BookStack\Entities\Controllers; | 
					
						
							| 
									
										
										
										
											2020-10-04 01:44:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Activity\ActivityType; | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Deletion; | 
					
						
							| 
									
										
										
										
											2021-06-26 19:12:11 +08:00
										 |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							| 
									
										
										
										
											2022-04-07 04:57:18 +08:00
										 |  |  | use BookStack\Entities\Repos\DeletionRepo; | 
					
						
							| 
									
										
										
										
											2020-11-22 07:20:54 +08:00
										 |  |  | use BookStack\Entities\Tools\TrashCan; | 
					
						
							| 
									
										
										
										
											2023-05-19 03:53:39 +08:00
										 |  |  | use BookStack\Http\Controller; | 
					
						
							| 
									
										
										
										
											2020-10-04 01:44:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class RecycleBinController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-12 23:45:30 +08:00
										 |  |  |     protected string $recycleBinBaseUrl = '/settings/recycle-bin'; | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * On each request to a method of this controller check permissions | 
					
						
							|  |  |  |      * using a middleware closure. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->middleware(function ($request, $next) { | 
					
						
							|  |  |  |             $this->checkPermission('settings-manage'); | 
					
						
							|  |  |  |             $this->checkPermission('restrictions-manage-all'); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |             return $next($request); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-04 01:44:12 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Show the top-level listing for the recycle bin. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function index() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $deletions = Deletion::query()->with(['deletable', 'deleter'])->paginate(10); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 01:03:24 +08:00
										 |  |  |         $this->setPageTitle(trans('settings.recycle_bin')); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |         return view('settings.recycle-bin.index', [ | 
					
						
							| 
									
										
										
										
											2020-10-04 01:44:12 +08:00
										 |  |  |             'deletions' => $deletions, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Show the page to confirm a restore of the deletion of the given id. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function showRestore(string $id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Deletion $deletion */ | 
					
						
							|  |  |  |         $deletion = Deletion::query()->findOrFail($id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-26 19:12:11 +08:00
										 |  |  |         // Walk the parent chain to find any cascading parent deletions
 | 
					
						
							|  |  |  |         $currentDeletable = $deletion->deletable; | 
					
						
							|  |  |  |         $searching = true; | 
					
						
							|  |  |  |         while ($searching && $currentDeletable instanceof Entity) { | 
					
						
							|  |  |  |             $parent = $currentDeletable->getParent(); | 
					
						
							|  |  |  |             if ($parent && $parent->trashed()) { | 
					
						
							|  |  |  |                 $currentDeletable = $parent; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $searching = false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-11-20 22:03:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-26 19:12:11 +08:00
										 |  |  |         /** @var ?Deletion $parentDeletion */ | 
					
						
							|  |  |  |         $parentDeletion = ($currentDeletable === $deletion->deletable) ? null : $currentDeletable->deletions()->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |         return view('settings.recycle-bin.restore', [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'deletion'       => $deletion, | 
					
						
							| 
									
										
										
										
											2021-06-26 19:12:11 +08:00
										 |  |  |             'parentDeletion' => $parentDeletion, | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Restore the element attached to the given deletion. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |      * @throws \Exception | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-04-07 04:57:18 +08:00
										 |  |  |     public function restore(DeletionRepo $deletionRepo, string $id) | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-04-07 04:57:18 +08:00
										 |  |  |         $restoreCount = $deletionRepo->restore((int) $id); | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->showSuccessNotification(trans('settings.recycle_bin_restore_notification', ['count' => $restoreCount])); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |         return redirect($this->recycleBinBaseUrl); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the page to confirm a Permanent deletion of the element attached to the deletion of the given id. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function showDestroy(string $id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Deletion $deletion */ | 
					
						
							|  |  |  |         $deletion = Deletion::query()->findOrFail($id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return view('settings.recycle-bin.destroy', [ | 
					
						
							|  |  |  |             'deletion' => $deletion, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Permanently delete the content associated with the given deletion. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |      * @throws \Exception | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-04-07 04:57:18 +08:00
										 |  |  |     public function destroy(DeletionRepo $deletionRepo, string $id) | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-04-07 04:57:18 +08:00
										 |  |  |         $deleteCount = $deletionRepo->destroy((int) $id); | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount])); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |         return redirect($this->recycleBinBaseUrl); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-04 01:44:12 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Empty out the recycle bin. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |      * @throws \Exception | 
					
						
							| 
									
										
										
										
											2020-10-04 01:44:12 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function empty() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-06 20:54:39 +08:00
										 |  |  |         $deleteCount = (new TrashCan())->empty(); | 
					
						
							| 
									
										
										
										
											2020-10-04 01:53:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-19 07:38:44 +08:00
										 |  |  |         $this->logActivity(ActivityType::RECYCLE_BIN_EMPTY); | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |         $this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount])); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-03 06:47:48 +08:00
										 |  |  |         return redirect($this->recycleBinBaseUrl); | 
					
						
							| 
									
										
										
										
											2020-10-04 01:44:12 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } |