]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/PageApiController.php
Improved shelf book management interface
[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 $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'    => ['integer'],
28             'chapter_id' => ['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
48         return $this->apiListingResponse($pages, [
49             'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',
50             'draft', 'template',
51             'created_at', 'updated_at',
52             'created_by', 'updated_by', 'owned_by',
53         ]);
54     }
55
56     /**
57      * Create a new page in the system.
58      *
59      * The ID of a parent book or chapter is required to indicate
60      * where this page should be located.
61      *
62      * Any HTML content provided should be kept to a single-block depth of plain HTML
63      * elements to remain compatible with the BookStack front-end and editors.
64      * Any images included via base64 data URIs will be extracted and saved as gallery
65      * images against the page during upload.
66      */
67     public function create(Request $request)
68     {
69         $this->validate($request, $this->rules['create']);
70
71         if ($request->has('chapter_id')) {
72             $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
73         } else {
74             $parent = Book::visible()->findOrFail($request->get('book_id'));
75         }
76         $this->checkOwnablePermission('page-create', $parent);
77
78         $draft = $this->pageRepo->getNewDraftPage($parent);
79         $this->pageRepo->publishDraft($draft, $request->only(array_keys($this->rules['create'])));
80
81         return response()->json($draft->forJsonDisplay());
82     }
83
84     /**
85      * View the details of a single page.
86      *
87      * Pages will always have HTML content. They may have markdown content
88      * if the markdown editor was used to last update the page.
89      */
90     public function read(string $id)
91     {
92         $page = $this->pageRepo->getById($id, []);
93
94         return response()->json($page->forJsonDisplay());
95     }
96
97     /**
98      * Update the details of a single page.
99      *
100      * See the 'create' action for details on the provided HTML/Markdown.
101      * Providing a 'book_id' or 'chapter_id' property will essentially move
102      * the page into that parent element if you have permissions to do so.
103      */
104     public function update(Request $request, string $id)
105     {
106         $requestData = $this->validate($request, $this->rules['update']);
107
108         $page = $this->pageRepo->getById($id, []);
109         $this->checkOwnablePermission('page-update', $page);
110
111         $parent = null;
112         if ($request->has('chapter_id')) {
113             $parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
114         } elseif ($request->has('book_id')) {
115             $parent = Book::visible()->findOrFail($request->get('book_id'));
116         }
117
118         if ($parent && !$parent->matches($page->getParent())) {
119             $this->checkOwnablePermission('page-delete', $page);
120
121             try {
122                 $this->pageRepo->move($page, $parent->getType() . ':' . $parent->id);
123             } catch (Exception $exception) {
124                 if ($exception instanceof  PermissionsException) {
125                     $this->showPermissionError();
126                 }
127
128                 return $this->jsonError(trans('errors.selected_book_chapter_not_found'));
129             }
130         }
131
132         $updatedPage = $this->pageRepo->update($page, $requestData);
133
134         return response()->json($updatedPage->forJsonDisplay());
135     }
136
137     /**
138      * Delete a page.
139      * This will typically send the page to the recycle bin.
140      */
141     public function delete(string $id)
142     {
143         $page = $this->pageRepo->getById($id, []);
144         $this->checkOwnablePermission('page-delete', $page);
145
146         $this->pageRepo->destroy($page);
147
148         return response('', 204);
149     }
150 }