]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/PageApiController.php
Merge branch 'master' of git://github.com/Swoy/BookStack into Swoy-master
[bookstack] / app / Http / Controllers / Api / PageApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Repos\PageRepo;
9 use BookStack\Exceptions\PermissionsException;
10 use Exception;
11 use Illuminate\Http\Request;
12
13 class PageApiController extends ApiController
14 {
15     protected $pageRepo;
16
17     protected $rules = [
18         'create' => [
19             'book_id' => 'required_without:chapter_id|integer',
20             'chapter_id' => 'required_without:book_id|integer',
21             'name' => 'required|string|max:255',
22             'html' => 'required_without:markdown|string',
23             'markdown' => 'required_without:html|string',
24             'tags' => 'array',
25         ],
26         'update' => [
27             'book_id' => 'required|integer',
28             'chapter_id' => 'required|integer',
29             'name' => 'string|min:1|max:255',
30             'html' => 'string',
31             'markdown' => 'string',
32             'tags' => 'array',
33         ],
34     ];
35
36     public function __construct(PageRepo $pageRepo)
37     {
38         $this->pageRepo = $pageRepo;
39     }
40
41     /**
42      * Get a listing of pages visible to the user.
43      */
44     public function list()
45     {
46         $pages = Page::visible();
47         return $this->apiListingResponse($pages, [
48             'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',
49             'draft', 'template',
50             'created_at', 'updated_at', 'created_by', 'updated_by',
51         ]);
52     }
53
54     /**
55      * Create a new page in the system.
56      *
57      * The ID of a parent book or chapter is required to indicate
58      * where this page should be located.
59      *
60      * Any HTML content provided should be kept to a single-block depth of plain HTML
61      * elements to remain compatible with the BookStack front-end and editors.
62      */
63     public function create(Request $request)
64     {
65         $this->validate($request, $this->rules['create']);
66
67         if ($request->has('chapter_id')) {
68             $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
69         } else {
70             $parent = Book::visible()->findOrFail($request->get('book_id'));
71         }
72         $this->checkOwnablePermission('page-create', $parent);
73
74         $draft = $this->pageRepo->getNewDraftPage($parent);
75         $this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
76
77         return response()->json($draft->forJsonDisplay());
78     }
79
80     /**
81      * View the details of a single page.
82      *
83      * Pages will always have HTML content. They may have markdown content
84      * if the markdown editor was used to last update the page.
85      */
86     public function read(string $id)
87     {
88         $page = $this->pageRepo->getById($id, []);
89         return response()->json($page->forJsonDisplay());
90     }
91
92     /**
93      * Update the details of a single page.
94      *
95      * See the 'create' action for details on the provided HTML/Markdown.
96      * Providing a 'book_id' or 'chapter_id' property will essentially move
97      * the page into that parent element if you have permissions to do so.
98      */
99     public function update(Request $request, string $id)
100     {
101         $page = $this->pageRepo->getById($id, []);
102         $this->checkOwnablePermission('page-update', $page);
103
104         $parent = null;
105         if ($request->has('chapter_id')) {
106             $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
107         } else if ($request->has('book_id')) {
108             $parent = Book::visible()->findOrFail($request->get('book_id'));
109         }
110
111         if ($parent && !$parent->matches($page->getParent())) {
112             $this->checkOwnablePermission('page-delete', $page);
113             try {
114                 $this->pageRepo->move($page, $parent->getType() . ':' . $parent->id);
115             } catch (Exception $exception) {
116                 if ($exception instanceof  PermissionsException) {
117                     $this->showPermissionError();
118                 }
119
120                 return $this->jsonError(trans('errors.selected_book_chapter_not_found'));
121             }
122         }
123
124         $updatedPage = $this->pageRepo->update($page, $request->all());
125         return response()->json($updatedPage->forJsonDisplay());
126     }
127
128     /**
129      * Delete a page.
130      * This will typically send the page to the recycle bin.
131      */
132     public function delete(string $id)
133     {
134         $page = $this->pageRepo->getById($id, []);
135         $this->checkOwnablePermission('page-delete', $page);
136
137         $this->pageRepo->destroy($page);
138         return response('', 204);
139     }
140 }