| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | namespace BookStack\Entities\Controllers; | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  | use BookStack\Entities\Queries\PageQueries; | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  | use BookStack\Entities\Repos\PageRepo; | 
					
						
							|  |  |  | use BookStack\Exceptions\NotFoundException; | 
					
						
							| 
									
										
										
										
											2023-05-19 03:53:39 +08:00
										 |  |  | use BookStack\Http\Controller; | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PageTemplateController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected PageRepo $pageRepo, | 
					
						
							|  |  |  |         protected PageQueries $pageQueries, | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Fetch a list of templates from the system. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function list(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $page = $request->get('page', 1); | 
					
						
							|  |  |  |         $search = $request->get('search', ''); | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $count = 10; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $query = $this->pageQueries->visibleTemplates() | 
					
						
							|  |  |  |             ->orderBy('name', 'asc') | 
					
						
							|  |  |  |             ->skip(($page - 1) * $count) | 
					
						
							|  |  |  |             ->take($count); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($search) { | 
					
						
							|  |  |  |             $query->where('name', 'like', '%' . $search . '%'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $templates = $query->paginate($count, ['*'], 'page', $page); | 
					
						
							|  |  |  |         $templates->withPath('/templates'); | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($search) { | 
					
						
							|  |  |  |             $templates->appends(['search' => $search]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-22 20:15:58 +08:00
										 |  |  |         return view('pages.parts.template-manager-list', [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'templates' => $templates, | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get the content of a template. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  |      * @throws NotFoundException | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |     public function get(int $templateId) | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-02-06 01:35:49 +08:00
										 |  |  |         $page = $this->pageQueries->findVisibleByIdOrFail($templateId); | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!$page->template) { | 
					
						
							|  |  |  |             throw new NotFoundException(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return response()->json([ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'html'     => $page->html, | 
					
						
							| 
									
										
										
										
											2019-08-12 03:04:43 +08:00
										 |  |  |             'markdown' => $page->markdown, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |