]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/PageTemplateController.php
Pages: Redirect user to view if they can't edit
[bookstack] / app / Entities / Controllers / PageTemplateController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Entities\Queries\PageQueries;
6 use BookStack\Entities\Repos\PageRepo;
7 use BookStack\Exceptions\NotFoundException;
8 use BookStack\Http\Controller;
9 use Illuminate\Http\Request;
10
11 class PageTemplateController extends Controller
12 {
13     public function __construct(
14         protected PageRepo $pageRepo,
15         protected PageQueries $pageQueries,
16     ) {
17     }
18
19     /**
20      * Fetch a list of templates from the system.
21      */
22     public function list(Request $request)
23     {
24         $page = $request->get('page', 1);
25         $search = $request->get('search', '');
26         $count = 10;
27
28         $query = $this->pageQueries->visibleTemplates()
29             ->orderBy('name', 'asc')
30             ->skip(($page - 1) * $count)
31             ->take($count);
32
33         if ($search) {
34             $query->where('name', 'like', '%' . $search . '%');
35         }
36
37         $templates = $query->paginate($count, ['*'], 'page', $page);
38         $templates->withPath('/templates');
39
40         if ($search) {
41             $templates->appends(['search' => $search]);
42         }
43
44         return view('pages.parts.template-manager-list', [
45             'templates' => $templates,
46         ]);
47     }
48
49     /**
50      * Get the content of a template.
51      *
52      * @throws NotFoundException
53      */
54     public function get(int $templateId)
55     {
56         $page = $this->pageQueries->findVisibleByIdOrFail($templateId);
57
58         if (!$page->template) {
59             throw new NotFoundException();
60         }
61
62         return response()->json([
63             'html'     => $page->html,
64             'markdown' => $page->markdown,
65         ]);
66     }
67 }