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