]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/PageTemplateController.php
Merge pull request #1573 from miles75/lang-hu
[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      * @param $pageRepo
16      */
17     public function __construct(PageRepo $pageRepo)
18     {
19         $this->pageRepo = $pageRepo;
20         parent::__construct();
21     }
22
23     /**
24      * Fetch a list of templates from the system.
25      * @param Request $request
26      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
27      */
28     public function list(Request $request)
29     {
30         $page = $request->get('page', 1);
31         $search = $request->get('search', '');
32         $templates = $this->pageRepo->getPageTemplates(10, $page, $search);
33
34         if ($search) {
35             $templates->appends(['search' => $search]);
36         }
37
38         return view('pages.template-manager-list', [
39             'templates' => $templates
40         ]);
41     }
42
43     /**
44      * Get the content of a template.
45      * @param $templateId
46      * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
47      * @throws NotFoundException
48      */
49     public function get($templateId)
50     {
51         $page = $this->pageRepo->getById('page', $templateId);
52
53         if (!$page->template) {
54             throw new NotFoundException();
55         }
56
57         return response()->json([
58             'html' => $page->html,
59             'markdown' => $page->markdown,
60         ]);
61     }
62
63 }