| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | namespace BookStack\Entities\Controllers; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Activity\ActivityType; | 
					
						
							| 
									
										
										
										
											2024-02-05 01:35:16 +08:00
										 |  |  | use BookStack\Entities\Queries\BookQueries; | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | use BookStack\Entities\Tools\BookContents; | 
					
						
							| 
									
										
										
										
											2022-01-05 01:31:57 +08:00
										 |  |  | use BookStack\Entities\Tools\BookSortMap; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use BookStack\Facades\Activity; | 
					
						
							| 
									
										
										
										
											2023-05-19 03:53:39 +08:00
										 |  |  | use BookStack\Http\Controller; | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class BookSortController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-02-05 01:35:16 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected BookQueries $queries, | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Shows the view which allows pages to be re-ordered and sorted. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function show(string $bookSlug) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $book = $this->queries->findVisibleBySlugOrFail($bookSlug); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $this->checkOwnablePermission('book-update', $book); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $bookChildren = (new BookContents($book))->getTree(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-18 08:25:20 +08:00
										 |  |  |         $this->setPageTitle(trans('entities.books_sort_named', ['bookName' => $book->getShortName()])); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         return view('books.sort', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Shows the sort box for a single book. | 
					
						
							|  |  |  |      * Used via AJAX when loading in extra books to a sort. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function showItem(string $bookSlug) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $book = $this->queries->findVisibleBySlugOrFail($bookSlug); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $bookChildren = (new BookContents($book))->getTree(); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-22 20:15:58 +08:00
										 |  |  |         return view('books.parts.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Sorts a book using a given mapping array. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function update(Request $request, string $bookSlug) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $book = $this->queries->findVisibleBySlugOrFail($bookSlug); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $this->checkOwnablePermission('book-update', $book); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Return if no map sent
 | 
					
						
							|  |  |  |         if (!$request->filled('sort-tree')) { | 
					
						
							|  |  |  |             return redirect($book->getUrl()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-05 01:31:57 +08:00
										 |  |  |         $sortMap = BookSortMap::fromJson($request->get('sort-tree')); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $bookContents = new BookContents($book); | 
					
						
							| 
									
										
										
										
											2022-01-05 05:09:34 +08:00
										 |  |  |         $booksInvolved = $bookContents->sortUsingMap($sortMap); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Rebuild permissions and add activity for involved books.
 | 
					
						
							| 
									
										
										
										
											2022-01-05 05:09:34 +08:00
										 |  |  |         foreach ($booksInvolved as $bookInvolved) { | 
					
						
							|  |  |  |             Activity::add(ActivityType::BOOK_SORT, $bookInvolved); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return redirect($book->getUrl()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |