]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageTemplateController.php
Added a "skip to content" link.
[bookstack] / app / Http / Controllers / PageTemplateController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Entities\Repos\PageRepo;
6 use BookStack\Exceptions\NotFoundException;
7 use Illuminate\Http\Request;
8
9 class PageTemplateController extends Controller
10 {
11     protected $pageRepo;
12
13     /**
14      * PageTemplateController constructor
15      */
16     public function __construct(PageRepo $pageRepo)
17     {
18         $this->pageRepo = $pageRepo;
19     }
20
21     /**
22      * Fetch a list of templates from the system.
23      */
24     public function list(Request $request)
25     {
26         $page = $request->get('page', 1);
27         $search = $request->get('search', '');
28         $templates = $this->pageRepo->getTemplates(10, $page, $search);
29
30         if ($search) {
31             $templates->appends(['search' => $search]);
32         }
33
34         return view('pages.template-manager-list', [
35             'templates' => $templates
36         ]);
37     }
38
39     /**
40      * Get the content of a template.
41      * @throws NotFoundException
42      */
43     public function get(int $templateId)
44     {
45         $page = $this->pageRepo->getById($templateId);
46
47         if (!$page->template) {
48             throw new NotFoundException();
49         }
50
51         return response()->json([
52             'html' => $page->html,
53             'markdown' => $page->markdown,
54         ]);
55     }
56 }