Dan Brown

Started refactor to merge entity repos

...@@ -5,6 +5,8 @@ class Chapter extends Entity ...@@ -5,6 +5,8 @@ class Chapter extends Entity
5 { 5 {
6 protected $fillable = ['name', 'description', 'priority', 'book_id']; 6 protected $fillable = ['name', 'description', 'priority', 'book_id'];
7 7
8 + protected $with = ['book'];
9 +
8 /** 10 /**
9 * Get the book this chapter is within. 11 * Get the book this chapter is within.
10 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo 12 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
2 2
3 use BookStack\Exceptions\FileUploadException; 3 use BookStack\Exceptions\FileUploadException;
4 use BookStack\Attachment; 4 use BookStack\Attachment;
5 +use BookStack\Repos\EntityRepo;
5 use BookStack\Repos\PageRepo; 6 use BookStack\Repos\PageRepo;
6 use BookStack\Services\AttachmentService; 7 use BookStack\Services\AttachmentService;
7 use Illuminate\Http\Request; 8 use Illuminate\Http\Request;
...@@ -11,6 +12,7 @@ class AttachmentController extends Controller ...@@ -11,6 +12,7 @@ class AttachmentController extends Controller
11 protected $attachmentService; 12 protected $attachmentService;
12 protected $attachment; 13 protected $attachment;
13 protected $pageRepo; 14 protected $pageRepo;
15 + protected $entityRepo;
14 16
15 /** 17 /**
16 * AttachmentController constructor. 18 * AttachmentController constructor.
...@@ -18,11 +20,13 @@ class AttachmentController extends Controller ...@@ -18,11 +20,13 @@ class AttachmentController extends Controller
18 * @param Attachment $attachment 20 * @param Attachment $attachment
19 * @param PageRepo $pageRepo 21 * @param PageRepo $pageRepo
20 */ 22 */
21 - public function __construct(AttachmentService $attachmentService, Attachment $attachment, PageRepo $pageRepo) 23 + public function __construct(AttachmentService $attachmentService, Attachment $attachment, EntityRepo $entityRepo, PageRepo $pageRepo)
22 { 24 {
23 $this->attachmentService = $attachmentService; 25 $this->attachmentService = $attachmentService;
24 $this->attachment = $attachment; 26 $this->attachment = $attachment;
27 + // TODO - Remove this
25 $this->pageRepo = $pageRepo; 28 $this->pageRepo = $pageRepo;
29 + $this->entityRepo = $entityRepo;
26 parent::__construct(); 30 parent::__construct();
27 } 31 }
28 32
...@@ -40,7 +44,7 @@ class AttachmentController extends Controller ...@@ -40,7 +44,7 @@ class AttachmentController extends Controller
40 ]); 44 ]);
41 45
42 $pageId = $request->get('uploaded_to'); 46 $pageId = $request->get('uploaded_to');
43 - $page = $this->pageRepo->getById($pageId, true); 47 + $page = $this->entityRepo->getById('page', $pageId, true);
44 48
45 $this->checkPermission('attachment-create-all'); 49 $this->checkPermission('attachment-create-all');
46 $this->checkOwnablePermission('page-update', $page); 50 $this->checkOwnablePermission('page-update', $page);
...@@ -70,7 +74,7 @@ class AttachmentController extends Controller ...@@ -70,7 +74,7 @@ class AttachmentController extends Controller
70 ]); 74 ]);
71 75
72 $pageId = $request->get('uploaded_to'); 76 $pageId = $request->get('uploaded_to');
73 - $page = $this->pageRepo->getById($pageId, true); 77 + $page = $this->entityRepo->getById('page', $pageId, true);
74 $attachment = $this->attachment->findOrFail($attachmentId); 78 $attachment = $this->attachment->findOrFail($attachmentId);
75 79
76 $this->checkOwnablePermission('page-update', $page); 80 $this->checkOwnablePermission('page-update', $page);
...@@ -106,7 +110,7 @@ class AttachmentController extends Controller ...@@ -106,7 +110,7 @@ class AttachmentController extends Controller
106 ]); 110 ]);
107 111
108 $pageId = $request->get('uploaded_to'); 112 $pageId = $request->get('uploaded_to');
109 - $page = $this->pageRepo->getById($pageId, true); 113 + $page = $this->entityRepo->getById('page', $pageId, true);
110 $attachment = $this->attachment->findOrFail($attachmentId); 114 $attachment = $this->attachment->findOrFail($attachmentId);
111 115
112 $this->checkOwnablePermission('page-update', $page); 116 $this->checkOwnablePermission('page-update', $page);
...@@ -134,7 +138,7 @@ class AttachmentController extends Controller ...@@ -134,7 +138,7 @@ class AttachmentController extends Controller
134 ]); 138 ]);
135 139
136 $pageId = $request->get('uploaded_to'); 140 $pageId = $request->get('uploaded_to');
137 - $page = $this->pageRepo->getById($pageId, true); 141 + $page = $this->entityRepo->getById('page', $pageId, true);
138 142
139 $this->checkPermission('attachment-create-all'); 143 $this->checkPermission('attachment-create-all');
140 $this->checkOwnablePermission('page-update', $page); 144 $this->checkOwnablePermission('page-update', $page);
...@@ -153,7 +157,7 @@ class AttachmentController extends Controller ...@@ -153,7 +157,7 @@ class AttachmentController extends Controller
153 */ 157 */
154 public function listForPage($pageId) 158 public function listForPage($pageId)
155 { 159 {
156 - $page = $this->pageRepo->getById($pageId, true); 160 + $page = $this->entityRepo->getById('page', $pageId, true);
157 $this->checkOwnablePermission('page-view', $page); 161 $this->checkOwnablePermission('page-view', $page);
158 return response()->json($page->attachments); 162 return response()->json($page->attachments);
159 } 163 }
...@@ -170,7 +174,7 @@ class AttachmentController extends Controller ...@@ -170,7 +174,7 @@ class AttachmentController extends Controller
170 'files' => 'required|array', 174 'files' => 'required|array',
171 'files.*.id' => 'required|integer', 175 'files.*.id' => 'required|integer',
172 ]); 176 ]);
173 - $page = $this->pageRepo->getById($pageId); 177 + $page = $this->entityRepo->getById('page', $pageId);
174 $this->checkOwnablePermission('page-update', $page); 178 $this->checkOwnablePermission('page-update', $page);
175 179
176 $attachments = $request->get('files'); 180 $attachments = $request->get('files');
...@@ -186,7 +190,7 @@ class AttachmentController extends Controller ...@@ -186,7 +190,7 @@ class AttachmentController extends Controller
186 public function get($attachmentId) 190 public function get($attachmentId)
187 { 191 {
188 $attachment = $this->attachment->findOrFail($attachmentId); 192 $attachment = $this->attachment->findOrFail($attachmentId);
189 - $page = $this->pageRepo->getById($attachment->uploaded_to); 193 + $page = $this->entityRepo->getById('page', $attachment->uploaded_to);
190 $this->checkOwnablePermission('page-view', $page); 194 $this->checkOwnablePermission('page-view', $page);
191 195
192 if ($attachment->external) { 196 if ($attachment->external) {
......
1 <?php namespace BookStack\Http\Controllers; 1 <?php namespace BookStack\Http\Controllers;
2 2
3 use Activity; 3 use Activity;
4 +use BookStack\Repos\EntityRepo;
4 use BookStack\Repos\UserRepo; 5 use BookStack\Repos\UserRepo;
5 use Illuminate\Http\Request; 6 use Illuminate\Http\Request;
6 use BookStack\Http\Requests; 7 use BookStack\Http\Requests;
...@@ -13,6 +14,7 @@ use Views; ...@@ -13,6 +14,7 @@ use Views;
13 class BookController extends Controller 14 class BookController extends Controller
14 { 15 {
15 16
17 + protected $entityRepo;
16 protected $bookRepo; 18 protected $bookRepo;
17 protected $pageRepo; 19 protected $pageRepo;
18 protected $chapterRepo; 20 protected $chapterRepo;
...@@ -25,8 +27,10 @@ class BookController extends Controller ...@@ -25,8 +27,10 @@ class BookController extends Controller
25 * @param ChapterRepo $chapterRepo 27 * @param ChapterRepo $chapterRepo
26 * @param UserRepo $userRepo 28 * @param UserRepo $userRepo
27 */ 29 */
28 - public function __construct(BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo) 30 + public function __construct(EntityRepo $entityRepo, BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
29 { 31 {
32 + $this->entityRepo = $entityRepo;
33 + // TODO - Remove below
30 $this->bookRepo = $bookRepo; 34 $this->bookRepo = $bookRepo;
31 $this->pageRepo = $pageRepo; 35 $this->pageRepo = $pageRepo;
32 $this->chapterRepo = $chapterRepo; 36 $this->chapterRepo = $chapterRepo;
...@@ -40,9 +44,9 @@ class BookController extends Controller ...@@ -40,9 +44,9 @@ class BookController extends Controller
40 */ 44 */
41 public function index() 45 public function index()
42 { 46 {
43 - $books = $this->bookRepo->getAllPaginated(10); 47 + $books = $this->entityRepo->getAllPaginated('book', 10);
44 - $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false; 48 + $recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
45 - $popular = $this->bookRepo->getPopular(4, 0); 49 + $popular = $this->entityRepo->getPopular('book', 4, 0);
46 $this->setPageTitle('Books'); 50 $this->setPageTitle('Books');
47 return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]); 51 return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
48 } 52 }
...@@ -83,7 +87,7 @@ class BookController extends Controller ...@@ -83,7 +87,7 @@ class BookController extends Controller
83 */ 87 */
84 public function show($slug) 88 public function show($slug)
85 { 89 {
86 - $book = $this->bookRepo->getBySlug($slug); 90 + $book = $this->entityRepo->getBySlug('book', $slug);
87 $this->checkOwnablePermission('book-view', $book); 91 $this->checkOwnablePermission('book-view', $book);
88 $bookChildren = $this->bookRepo->getChildren($book); 92 $bookChildren = $this->bookRepo->getChildren($book);
89 Views::add($book); 93 Views::add($book);
...@@ -98,7 +102,7 @@ class BookController extends Controller ...@@ -98,7 +102,7 @@ class BookController extends Controller
98 */ 102 */
99 public function edit($slug) 103 public function edit($slug)
100 { 104 {
101 - $book = $this->bookRepo->getBySlug($slug); 105 + $book = $this->entityRepo->getBySlug('book', $slug);
102 $this->checkOwnablePermission('book-update', $book); 106 $this->checkOwnablePermission('book-update', $book);
103 $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()])); 107 $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()]));
104 return view('books/edit', ['book' => $book, 'current' => $book]); 108 return view('books/edit', ['book' => $book, 'current' => $book]);
...@@ -112,7 +116,7 @@ class BookController extends Controller ...@@ -112,7 +116,7 @@ class BookController extends Controller
112 */ 116 */
113 public function update(Request $request, $slug) 117 public function update(Request $request, $slug)
114 { 118 {
115 - $book = $this->bookRepo->getBySlug($slug); 119 + $book = $this->entityRepo->getBySlug('book', $slug);
116 $this->checkOwnablePermission('book-update', $book); 120 $this->checkOwnablePermission('book-update', $book);
117 $this->validate($request, [ 121 $this->validate($request, [
118 'name' => 'required|string|max:255', 122 'name' => 'required|string|max:255',
...@@ -130,7 +134,7 @@ class BookController extends Controller ...@@ -130,7 +134,7 @@ class BookController extends Controller
130 */ 134 */
131 public function showDelete($bookSlug) 135 public function showDelete($bookSlug)
132 { 136 {
133 - $book = $this->bookRepo->getBySlug($bookSlug); 137 + $book = $this->entityRepo->getBySlug('book', $bookSlug);
134 $this->checkOwnablePermission('book-delete', $book); 138 $this->checkOwnablePermission('book-delete', $book);
135 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()])); 139 $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()]));
136 return view('books/delete', ['book' => $book, 'current' => $book]); 140 return view('books/delete', ['book' => $book, 'current' => $book]);
...@@ -143,10 +147,10 @@ class BookController extends Controller ...@@ -143,10 +147,10 @@ class BookController extends Controller
143 */ 147 */
144 public function sort($bookSlug) 148 public function sort($bookSlug)
145 { 149 {
146 - $book = $this->bookRepo->getBySlug($bookSlug); 150 + $book = $this->entityRepo->getBySlug('book', $bookSlug);
147 $this->checkOwnablePermission('book-update', $book); 151 $this->checkOwnablePermission('book-update', $book);
148 $bookChildren = $this->bookRepo->getChildren($book, true); 152 $bookChildren = $this->bookRepo->getChildren($book, true);
149 - $books = $this->bookRepo->getAll(false); 153 + $books = $this->entityRepo->getAll('book', false);
150 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()])); 154 $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
151 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]); 155 return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
152 } 156 }
...@@ -159,7 +163,7 @@ class BookController extends Controller ...@@ -159,7 +163,7 @@ class BookController extends Controller
159 */ 163 */
160 public function getSortItem($bookSlug) 164 public function getSortItem($bookSlug)
161 { 165 {
162 - $book = $this->bookRepo->getBySlug($bookSlug); 166 + $book = $this->entityRepo->getBySlug('book', $bookSlug);
163 $bookChildren = $this->bookRepo->getChildren($book); 167 $bookChildren = $this->bookRepo->getChildren($book);
164 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]); 168 return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
165 } 169 }
...@@ -172,7 +176,7 @@ class BookController extends Controller ...@@ -172,7 +176,7 @@ class BookController extends Controller
172 */ 176 */
173 public function saveSort($bookSlug, Request $request) 177 public function saveSort($bookSlug, Request $request)
174 { 178 {
175 - $book = $this->bookRepo->getBySlug($bookSlug); 179 + $book = $this->entityRepo->getBySlug('book', $bookSlug);
176 $this->checkOwnablePermission('book-update', $book); 180 $this->checkOwnablePermission('book-update', $book);
177 181
178 // Return if no map sent 182 // Return if no map sent
...@@ -191,9 +195,9 @@ class BookController extends Controller ...@@ -191,9 +195,9 @@ class BookController extends Controller
191 $priority = $bookChild->sort; 195 $priority = $bookChild->sort;
192 $id = intval($bookChild->id); 196 $id = intval($bookChild->id);
193 $isPage = $bookChild->type == 'page'; 197 $isPage = $bookChild->type == 'page';
194 - $bookId = $this->bookRepo->exists($bookChild->book) ? intval($bookChild->book) : $defaultBookId; 198 + $bookId = $this->entityRepo->exists('book', $bookChild->book) ? intval($bookChild->book) : $defaultBookId;
195 $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter); 199 $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter);
196 - $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id); 200 + $model = $this->entityRepo->getById($isPage?'page':'chapter', $id);
197 201
198 // Update models only if there's a change in parent chain or ordering. 202 // Update models only if there's a change in parent chain or ordering.
199 if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) { 203 if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
...@@ -212,7 +216,7 @@ class BookController extends Controller ...@@ -212,7 +216,7 @@ class BookController extends Controller
212 216
213 // Add activity for books 217 // Add activity for books
214 foreach ($sortedBooks as $bookId) { 218 foreach ($sortedBooks as $bookId) {
215 - $updatedBook = $this->bookRepo->getById($bookId); 219 + $updatedBook = $this->entityRepo->getById('book', $bookId);
216 Activity::add($updatedBook, 'book_sort', $updatedBook->id); 220 Activity::add($updatedBook, 'book_sort', $updatedBook->id);
217 } 221 }
218 222
...@@ -229,7 +233,7 @@ class BookController extends Controller ...@@ -229,7 +233,7 @@ class BookController extends Controller
229 */ 233 */
230 public function destroy($bookSlug) 234 public function destroy($bookSlug)
231 { 235 {
232 - $book = $this->bookRepo->getBySlug($bookSlug); 236 + $book = $this->entityRepo->getBySlug('book', $bookSlug);
233 $this->checkOwnablePermission('book-delete', $book); 237 $this->checkOwnablePermission('book-delete', $book);
234 Activity::addMessage('book_delete', 0, $book->name); 238 Activity::addMessage('book_delete', 0, $book->name);
235 Activity::removeEntity($book); 239 Activity::removeEntity($book);
...@@ -244,7 +248,7 @@ class BookController extends Controller ...@@ -244,7 +248,7 @@ class BookController extends Controller
244 */ 248 */
245 public function showRestrict($bookSlug) 249 public function showRestrict($bookSlug)
246 { 250 {
247 - $book = $this->bookRepo->getBySlug($bookSlug); 251 + $book = $this->entityRepo->getBySlug('book', $bookSlug);
248 $this->checkOwnablePermission('restrictions-manage', $book); 252 $this->checkOwnablePermission('restrictions-manage', $book);
249 $roles = $this->userRepo->getRestrictableRoles(); 253 $roles = $this->userRepo->getRestrictableRoles();
250 return view('books/restrictions', [ 254 return view('books/restrictions', [
...@@ -262,7 +266,7 @@ class BookController extends Controller ...@@ -262,7 +266,7 @@ class BookController extends Controller
262 */ 266 */
263 public function restrict($bookSlug, Request $request) 267 public function restrict($bookSlug, Request $request)
264 { 268 {
265 - $book = $this->bookRepo->getBySlug($bookSlug); 269 + $book = $this->entityRepo->getBySlug('book', $bookSlug);
266 $this->checkOwnablePermission('restrictions-manage', $book); 270 $this->checkOwnablePermission('restrictions-manage', $book);
267 $this->bookRepo->updateEntityPermissionsFromRequest($request, $book); 271 $this->bookRepo->updateEntityPermissionsFromRequest($request, $book);
268 session()->flash('success', trans('entities.books_permissions_updated')); 272 session()->flash('success', trans('entities.books_permissions_updated'));
......
1 <?php namespace BookStack\Http\Controllers; 1 <?php namespace BookStack\Http\Controllers;
2 2
3 use Activity; 3 use Activity;
4 +use BookStack\Repos\EntityRepo;
4 use BookStack\Repos\UserRepo; 5 use BookStack\Repos\UserRepo;
5 use Illuminate\Http\Request; 6 use Illuminate\Http\Request;
6 use BookStack\Repos\BookRepo; 7 use BookStack\Repos\BookRepo;
...@@ -14,15 +15,19 @@ class ChapterController extends Controller ...@@ -14,15 +15,19 @@ class ChapterController extends Controller
14 protected $bookRepo; 15 protected $bookRepo;
15 protected $chapterRepo; 16 protected $chapterRepo;
16 protected $userRepo; 17 protected $userRepo;
18 + protected $entityRepo;
17 19
18 /** 20 /**
19 * ChapterController constructor. 21 * ChapterController constructor.
22 + * @param EntityRepo $entityRepo
20 * @param BookRepo $bookRepo 23 * @param BookRepo $bookRepo
21 * @param ChapterRepo $chapterRepo 24 * @param ChapterRepo $chapterRepo
22 * @param UserRepo $userRepo 25 * @param UserRepo $userRepo
23 */ 26 */
24 - public function __construct(BookRepo $bookRepo, ChapterRepo $chapterRepo, UserRepo $userRepo) 27 + public function __construct(EntityRepo $entityRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
25 { 28 {
29 + $this->entityRepo = $entityRepo;
30 + // TODO - Remove below
26 $this->bookRepo = $bookRepo; 31 $this->bookRepo = $bookRepo;
27 $this->chapterRepo = $chapterRepo; 32 $this->chapterRepo = $chapterRepo;
28 $this->userRepo = $userRepo; 33 $this->userRepo = $userRepo;
...@@ -36,7 +41,7 @@ class ChapterController extends Controller ...@@ -36,7 +41,7 @@ class ChapterController extends Controller
36 */ 41 */
37 public function create($bookSlug) 42 public function create($bookSlug)
38 { 43 {
39 - $book = $this->bookRepo->getBySlug($bookSlug); 44 + $book = $this->entityRepo->getBySlug('book', $bookSlug);
40 $this->checkOwnablePermission('chapter-create', $book); 45 $this->checkOwnablePermission('chapter-create', $book);
41 $this->setPageTitle(trans('entities.chapters_create')); 46 $this->setPageTitle(trans('entities.chapters_create'));
42 return view('chapters/create', ['book' => $book, 'current' => $book]); 47 return view('chapters/create', ['book' => $book, 'current' => $book]);
...@@ -54,7 +59,7 @@ class ChapterController extends Controller ...@@ -54,7 +59,7 @@ class ChapterController extends Controller
54 'name' => 'required|string|max:255' 59 'name' => 'required|string|max:255'
55 ]); 60 ]);
56 61
57 - $book = $this->bookRepo->getBySlug($bookSlug); 62 + $book = $this->entityRepo->getBySlug('book', $bookSlug);
58 $this->checkOwnablePermission('chapter-create', $book); 63 $this->checkOwnablePermission('chapter-create', $book);
59 64
60 $input = $request->all(); 65 $input = $request->all();
...@@ -72,15 +77,14 @@ class ChapterController extends Controller ...@@ -72,15 +77,14 @@ class ChapterController extends Controller
72 */ 77 */
73 public function show($bookSlug, $chapterSlug) 78 public function show($bookSlug, $chapterSlug)
74 { 79 {
75 - $book = $this->bookRepo->getBySlug($bookSlug); 80 + $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
76 - $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
77 $this->checkOwnablePermission('chapter-view', $chapter); 81 $this->checkOwnablePermission('chapter-view', $chapter);
78 - $sidebarTree = $this->bookRepo->getChildren($book); 82 + $sidebarTree = $this->bookRepo->getChildren($chapter->book);
79 Views::add($chapter); 83 Views::add($chapter);
80 $this->setPageTitle($chapter->getShortName()); 84 $this->setPageTitle($chapter->getShortName());
81 $pages = $this->chapterRepo->getChildren($chapter); 85 $pages = $this->chapterRepo->getChildren($chapter);
82 return view('chapters/show', [ 86 return view('chapters/show', [
83 - 'book' => $book, 87 + 'book' => $chapter->book,
84 'chapter' => $chapter, 88 'chapter' => $chapter,
85 'current' => $chapter, 89 'current' => $chapter,
86 'sidebarTree' => $sidebarTree, 90 'sidebarTree' => $sidebarTree,
...@@ -96,11 +100,10 @@ class ChapterController extends Controller ...@@ -96,11 +100,10 @@ class ChapterController extends Controller
96 */ 100 */
97 public function edit($bookSlug, $chapterSlug) 101 public function edit($bookSlug, $chapterSlug)
98 { 102 {
99 - $book = $this->bookRepo->getBySlug($bookSlug); 103 + $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
100 - $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
101 $this->checkOwnablePermission('chapter-update', $chapter); 104 $this->checkOwnablePermission('chapter-update', $chapter);
102 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()])); 105 $this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
103 - return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]); 106 + return view('chapters/edit', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
104 } 107 }
105 108
106 /** 109 /**
...@@ -112,16 +115,15 @@ class ChapterController extends Controller ...@@ -112,16 +115,15 @@ class ChapterController extends Controller
112 */ 115 */
113 public function update(Request $request, $bookSlug, $chapterSlug) 116 public function update(Request $request, $bookSlug, $chapterSlug)
114 { 117 {
115 - $book = $this->bookRepo->getBySlug($bookSlug); 118 + $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
116 - $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
117 $this->checkOwnablePermission('chapter-update', $chapter); 119 $this->checkOwnablePermission('chapter-update', $chapter);
118 if ($chapter->name !== $request->get('name')) { 120 if ($chapter->name !== $request->get('name')) {
119 - $chapter->slug = $this->chapterRepo->findSuitableSlug($request->get('name'), $book->id, $chapter->id); 121 + $chapter->slug = $this->chapterRepo->findSuitableSlug($request->get('name'), $chapter->book->id, $chapter->id);
120 } 122 }
121 $chapter->fill($request->all()); 123 $chapter->fill($request->all());
122 $chapter->updated_by = user()->id; 124 $chapter->updated_by = user()->id;
123 $chapter->save(); 125 $chapter->save();
124 - Activity::add($chapter, 'chapter_update', $book->id); 126 + Activity::add($chapter, 'chapter_update', $chapter->book->id);
125 return redirect($chapter->getUrl()); 127 return redirect($chapter->getUrl());
126 } 128 }
127 129
...@@ -133,11 +135,10 @@ class ChapterController extends Controller ...@@ -133,11 +135,10 @@ class ChapterController extends Controller
133 */ 135 */
134 public function showDelete($bookSlug, $chapterSlug) 136 public function showDelete($bookSlug, $chapterSlug)
135 { 137 {
136 - $book = $this->bookRepo->getBySlug($bookSlug); 138 + $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
137 - $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
138 $this->checkOwnablePermission('chapter-delete', $chapter); 139 $this->checkOwnablePermission('chapter-delete', $chapter);
139 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()])); 140 $this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
140 - return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]); 141 + return view('chapters/delete', ['book' => $chapter->book, 'chapter' => $chapter, 'current' => $chapter]);
141 } 142 }
142 143
143 /** 144 /**
...@@ -148,8 +149,8 @@ class ChapterController extends Controller ...@@ -148,8 +149,8 @@ class ChapterController extends Controller
148 */ 149 */
149 public function destroy($bookSlug, $chapterSlug) 150 public function destroy($bookSlug, $chapterSlug)
150 { 151 {
151 - $book = $this->bookRepo->getBySlug($bookSlug); 152 + $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
152 - $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id); 153 + $book = $chapter->book;
153 $this->checkOwnablePermission('chapter-delete', $chapter); 154 $this->checkOwnablePermission('chapter-delete', $chapter);
154 Activity::addMessage('chapter_delete', $book->id, $chapter->name); 155 Activity::addMessage('chapter_delete', $book->id, $chapter->name);
155 $this->chapterRepo->destroy($chapter); 156 $this->chapterRepo->destroy($chapter);
...@@ -164,13 +165,12 @@ class ChapterController extends Controller ...@@ -164,13 +165,12 @@ class ChapterController extends Controller
164 * @throws \BookStack\Exceptions\NotFoundException 165 * @throws \BookStack\Exceptions\NotFoundException
165 */ 166 */
166 public function showMove($bookSlug, $chapterSlug) { 167 public function showMove($bookSlug, $chapterSlug) {
167 - $book = $this->bookRepo->getBySlug($bookSlug); 168 + $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
168 - $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
169 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()])); 169 $this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
170 $this->checkOwnablePermission('chapter-update', $chapter); 170 $this->checkOwnablePermission('chapter-update', $chapter);
171 return view('chapters/move', [ 171 return view('chapters/move', [
172 'chapter' => $chapter, 172 'chapter' => $chapter,
173 - 'book' => $book 173 + 'book' => $chapter->book
174 ]); 174 ]);
175 } 175 }
176 176
...@@ -183,8 +183,7 @@ class ChapterController extends Controller ...@@ -183,8 +183,7 @@ class ChapterController extends Controller
183 * @throws \BookStack\Exceptions\NotFoundException 183 * @throws \BookStack\Exceptions\NotFoundException
184 */ 184 */
185 public function move($bookSlug, $chapterSlug, Request $request) { 185 public function move($bookSlug, $chapterSlug, Request $request) {
186 - $book = $this->bookRepo->getBySlug($bookSlug); 186 + $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
187 - $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
188 $this->checkOwnablePermission('chapter-update', $chapter); 187 $this->checkOwnablePermission('chapter-update', $chapter);
189 188
190 $entitySelection = $request->get('entity_selection', null); 189 $entitySelection = $request->get('entity_selection', null);
...@@ -199,7 +198,7 @@ class ChapterController extends Controller ...@@ -199,7 +198,7 @@ class ChapterController extends Controller
199 $parent = false; 198 $parent = false;
200 199
201 if ($entityType == 'book') { 200 if ($entityType == 'book') {
202 - $parent = $this->bookRepo->getById($entityId); 201 + $parent = $this->entityRepo->getById('book', $entityId);
203 } 202 }
204 203
205 if ($parent === false || $parent === null) { 204 if ($parent === false || $parent === null) {
...@@ -222,8 +221,7 @@ class ChapterController extends Controller ...@@ -222,8 +221,7 @@ class ChapterController extends Controller
222 */ 221 */
223 public function showRestrict($bookSlug, $chapterSlug) 222 public function showRestrict($bookSlug, $chapterSlug)
224 { 223 {
225 - $book = $this->bookRepo->getBySlug($bookSlug); 224 + $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
226 - $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
227 $this->checkOwnablePermission('restrictions-manage', $chapter); 225 $this->checkOwnablePermission('restrictions-manage', $chapter);
228 $roles = $this->userRepo->getRestrictableRoles(); 226 $roles = $this->userRepo->getRestrictableRoles();
229 return view('chapters/restrictions', [ 227 return view('chapters/restrictions', [
...@@ -241,8 +239,7 @@ class ChapterController extends Controller ...@@ -241,8 +239,7 @@ class ChapterController extends Controller
241 */ 239 */
242 public function restrict($bookSlug, $chapterSlug, Request $request) 240 public function restrict($bookSlug, $chapterSlug, Request $request)
243 { 241 {
244 - $book = $this->bookRepo->getBySlug($bookSlug); 242 + $chapter = $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug);
245 - $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
246 $this->checkOwnablePermission('restrictions-manage', $chapter); 243 $this->checkOwnablePermission('restrictions-manage', $chapter);
247 $this->chapterRepo->updateEntityPermissionsFromRequest($request, $chapter); 244 $this->chapterRepo->updateEntityPermissionsFromRequest($request, $chapter);
248 session()->flash('success', trans('entities.chapters_permissions_success')); 245 session()->flash('success', trans('entities.chapters_permissions_success'));
......
...@@ -5,6 +5,7 @@ namespace BookStack\Http\Controllers; ...@@ -5,6 +5,7 @@ namespace BookStack\Http\Controllers;
5 use Activity; 5 use Activity;
6 use BookStack\Repos\EntityRepo; 6 use BookStack\Repos\EntityRepo;
7 use BookStack\Http\Requests; 7 use BookStack\Http\Requests;
8 +use Illuminate\Http\Response;
8 use Views; 9 use Views;
9 10
10 class HomeController extends Controller 11 class HomeController extends Controller
...@@ -31,9 +32,9 @@ class HomeController extends Controller ...@@ -31,9 +32,9 @@ class HomeController extends Controller
31 $activity = Activity::latest(10); 32 $activity = Activity::latest(10);
32 $draftPages = $this->signedIn ? $this->entityRepo->getUserDraftPages(6) : []; 33 $draftPages = $this->signedIn ? $this->entityRepo->getUserDraftPages(6) : [];
33 $recentFactor = count($draftPages) > 0 ? 0.5 : 1; 34 $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
34 - $recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreatedBooks(10*$recentFactor); 35 + $recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreated('book', 10*$recentFactor);
35 - $recentlyCreatedPages = $this->entityRepo->getRecentlyCreatedPages(5); 36 + $recentlyCreatedPages = $this->entityRepo->getRecentlyCreated('page', 5);
36 - $recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdatedPages(5); 37 + $recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdated('page', 5);
37 return view('home', [ 38 return view('home', [
38 'activity' => $activity, 39 'activity' => $activity,
39 'recents' => $recents, 40 'recents' => $recents,
......
...@@ -2,22 +2,22 @@ ...@@ -2,22 +2,22 @@
2 2
3 use Activity; 3 use Activity;
4 use BookStack\Exceptions\NotFoundException; 4 use BookStack\Exceptions\NotFoundException;
5 +use BookStack\Repos\EntityRepo;
5 use BookStack\Repos\UserRepo; 6 use BookStack\Repos\UserRepo;
6 use BookStack\Services\ExportService; 7 use BookStack\Services\ExportService;
7 use Carbon\Carbon; 8 use Carbon\Carbon;
8 use Illuminate\Http\Request; 9 use Illuminate\Http\Request;
9 -use BookStack\Http\Requests;
10 use BookStack\Repos\BookRepo; 10 use BookStack\Repos\BookRepo;
11 use BookStack\Repos\ChapterRepo; 11 use BookStack\Repos\ChapterRepo;
12 use BookStack\Repos\PageRepo; 12 use BookStack\Repos\PageRepo;
13 use Illuminate\Http\Response; 13 use Illuminate\Http\Response;
14 -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15 use Views; 14 use Views;
16 use GatherContent\Htmldiff\Htmldiff; 15 use GatherContent\Htmldiff\Htmldiff;
17 16
18 class PageController extends Controller 17 class PageController extends Controller
19 { 18 {
20 19
20 + protected $entityRepo;
21 protected $pageRepo; 21 protected $pageRepo;
22 protected $bookRepo; 22 protected $bookRepo;
23 protected $chapterRepo; 23 protected $chapterRepo;
...@@ -32,8 +32,10 @@ class PageController extends Controller ...@@ -32,8 +32,10 @@ class PageController extends Controller
32 * @param ExportService $exportService 32 * @param ExportService $exportService
33 * @param UserRepo $userRepo 33 * @param UserRepo $userRepo
34 */ 34 */
35 - public function __construct(PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, ExportService $exportService, UserRepo $userRepo) 35 + public function __construct(EntityRepo $entityRepo, PageRepo $pageRepo, BookRepo $bookRepo, ChapterRepo $chapterRepo, ExportService $exportService, UserRepo $userRepo)
36 { 36 {
37 + $this->entityRepo = $entityRepo;
38 + // TODO - remove below;
37 $this->pageRepo = $pageRepo; 39 $this->pageRepo = $pageRepo;
38 $this->bookRepo = $bookRepo; 40 $this->bookRepo = $bookRepo;
39 $this->chapterRepo = $chapterRepo; 41 $this->chapterRepo = $chapterRepo;
...@@ -51,8 +53,8 @@ class PageController extends Controller ...@@ -51,8 +53,8 @@ class PageController extends Controller
51 */ 53 */
52 public function create($bookSlug, $chapterSlug = null) 54 public function create($bookSlug, $chapterSlug = null)
53 { 55 {
54 - $book = $this->bookRepo->getBySlug($bookSlug); 56 + $book = $this->entityRepo->getBySlug('book', $bookSlug);
55 - $chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : null; 57 + $chapter = $chapterSlug ? $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug) : null;
56 $parent = $chapter ? $chapter : $book; 58 $parent = $chapter ? $chapter : $book;
57 $this->checkOwnablePermission('page-create', $parent); 59 $this->checkOwnablePermission('page-create', $parent);
58 60
...@@ -81,8 +83,8 @@ class PageController extends Controller ...@@ -81,8 +83,8 @@ class PageController extends Controller
81 'name' => 'required|string|max:255' 83 'name' => 'required|string|max:255'
82 ]); 84 ]);
83 85
84 - $book = $this->bookRepo->getBySlug($bookSlug); 86 + $book = $this->entityRepo->getBySlug('book', $bookSlug);
85 - $chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : null; 87 + $chapter = $chapterSlug ? $this->entityRepo->getBySlug('chapter', $chapterSlug, $bookSlug) : null;
86 $parent = $chapter ? $chapter : $book; 88 $parent = $chapter ? $chapter : $book;
87 $this->checkOwnablePermission('page-create', $parent); 89 $this->checkOwnablePermission('page-create', $parent);
88 90
...@@ -102,15 +104,14 @@ class PageController extends Controller ...@@ -102,15 +104,14 @@ class PageController extends Controller
102 */ 104 */
103 public function editDraft($bookSlug, $pageId) 105 public function editDraft($bookSlug, $pageId)
104 { 106 {
105 - $book = $this->bookRepo->getBySlug($bookSlug); 107 + $draft = $this->entityRepo->getById('page', $pageId, true);
106 - $draft = $this->pageRepo->getById($pageId, true); 108 + $this->checkOwnablePermission('page-create', $draft->book);
107 - $this->checkOwnablePermission('page-create', $book);
108 $this->setPageTitle(trans('entities.pages_edit_draft')); 109 $this->setPageTitle(trans('entities.pages_edit_draft'));
109 110
110 $draftsEnabled = $this->signedIn; 111 $draftsEnabled = $this->signedIn;
111 return view('pages/edit', [ 112 return view('pages/edit', [
112 'page' => $draft, 113 'page' => $draft,
113 - 'book' => $book, 114 + 'book' => $draft->book,
114 'isDraft' => true, 115 'isDraft' => true,
115 'draftsEnabled' => $draftsEnabled 116 'draftsEnabled' => $draftsEnabled
116 ]); 117 ]);
...@@ -130,12 +131,12 @@ class PageController extends Controller ...@@ -130,12 +131,12 @@ class PageController extends Controller
130 ]); 131 ]);
131 132
132 $input = $request->all(); 133 $input = $request->all();
133 - $book = $this->bookRepo->getBySlug($bookSlug); 134 + $book = $this->entityRepo->getBySlug('book', $bookSlug);
134 135
135 - $draftPage = $this->pageRepo->getById($pageId, true); 136 + $draftPage = $this->entityRepo->getById('page', $pageId, true);
136 137
137 $chapterId = intval($draftPage->chapter_id); 138 $chapterId = intval($draftPage->chapter_id);
138 - $parent = $chapterId !== 0 ? $this->chapterRepo->getById($chapterId) : $book; 139 + $parent = $chapterId !== 0 ? $this->entityRepo->getById('chapter', $chapterId) : $book;
139 $this->checkOwnablePermission('page-create', $parent); 140 $this->checkOwnablePermission('page-create', $parent);
140 141
141 if ($parent->isA('chapter')) { 142 if ($parent->isA('chapter')) {
...@@ -152,18 +153,15 @@ class PageController extends Controller ...@@ -152,18 +153,15 @@ class PageController extends Controller
152 153
153 /** 154 /**
154 * Display the specified page. 155 * Display the specified page.
155 - * If the page is not found via the slug the 156 + * If the page is not found via the slug the revisions are searched for a match.
156 - * revisions are searched for a match.
157 * @param string $bookSlug 157 * @param string $bookSlug
158 * @param string $pageSlug 158 * @param string $pageSlug
159 * @return Response 159 * @return Response
160 */ 160 */
161 public function show($bookSlug, $pageSlug) 161 public function show($bookSlug, $pageSlug)
162 { 162 {
163 - $book = $this->bookRepo->getBySlug($bookSlug);
164 -
165 try { 163 try {
166 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id); 164 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
167 } catch (NotFoundException $e) { 165 } catch (NotFoundException $e) {
168 $page = $this->pageRepo->findPageUsingOldSlug($pageSlug, $bookSlug); 166 $page = $this->pageRepo->findPageUsingOldSlug($pageSlug, $bookSlug);
169 if ($page === null) abort(404); 167 if ($page === null) abort(404);
...@@ -172,12 +170,12 @@ class PageController extends Controller ...@@ -172,12 +170,12 @@ class PageController extends Controller
172 170
173 $this->checkOwnablePermission('page-view', $page); 171 $this->checkOwnablePermission('page-view', $page);
174 172
175 - $sidebarTree = $this->bookRepo->getChildren($book); 173 + $sidebarTree = $this->bookRepo->getChildren($page->book);
176 $pageNav = $this->pageRepo->getPageNav($page); 174 $pageNav = $this->pageRepo->getPageNav($page);
177 175
178 Views::add($page); 176 Views::add($page);
179 $this->setPageTitle($page->getShortName()); 177 $this->setPageTitle($page->getShortName());
180 - return view('pages/show', ['page' => $page, 'book' => $book, 178 + return view('pages/show', ['page' => $page, 'book' => $page->book,
181 'current' => $page, 'sidebarTree' => $sidebarTree, 'pageNav' => $pageNav]); 179 'current' => $page, 'sidebarTree' => $sidebarTree, 'pageNav' => $pageNav]);
182 } 180 }
183 181
...@@ -188,7 +186,7 @@ class PageController extends Controller ...@@ -188,7 +186,7 @@ class PageController extends Controller
188 */ 186 */
189 public function getPageAjax($pageId) 187 public function getPageAjax($pageId)
190 { 188 {
191 - $page = $this->pageRepo->getById($pageId); 189 + $page = $this->entityRepo->getById('page', $pageId);
192 return response()->json($page); 190 return response()->json($page);
193 } 191 }
194 192
...@@ -200,8 +198,7 @@ class PageController extends Controller ...@@ -200,8 +198,7 @@ class PageController extends Controller
200 */ 198 */
201 public function edit($bookSlug, $pageSlug) 199 public function edit($bookSlug, $pageSlug)
202 { 200 {
203 - $book = $this->bookRepo->getBySlug($bookSlug); 201 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
204 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
205 $this->checkOwnablePermission('page-update', $page); 202 $this->checkOwnablePermission('page-update', $page);
206 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName'=>$page->getShortName()])); 203 $this->setPageTitle(trans('entities.pages_editing_named', ['pageName'=>$page->getShortName()]));
207 $page->isDraft = false; 204 $page->isDraft = false;
...@@ -227,7 +224,7 @@ class PageController extends Controller ...@@ -227,7 +224,7 @@ class PageController extends Controller
227 $draftsEnabled = $this->signedIn; 224 $draftsEnabled = $this->signedIn;
228 return view('pages/edit', [ 225 return view('pages/edit', [
229 'page' => $page, 226 'page' => $page,
230 - 'book' => $book, 227 + 'book' => $page->book,
231 'current' => $page, 228 'current' => $page,
232 'draftsEnabled' => $draftsEnabled 229 'draftsEnabled' => $draftsEnabled
233 ]); 230 ]);
...@@ -245,11 +242,10 @@ class PageController extends Controller ...@@ -245,11 +242,10 @@ class PageController extends Controller
245 $this->validate($request, [ 242 $this->validate($request, [
246 'name' => 'required|string|max:255' 243 'name' => 'required|string|max:255'
247 ]); 244 ]);
248 - $book = $this->bookRepo->getBySlug($bookSlug); 245 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
249 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
250 $this->checkOwnablePermission('page-update', $page); 246 $this->checkOwnablePermission('page-update', $page);
251 - $this->pageRepo->updatePage($page, $book->id, $request->all()); 247 + $this->pageRepo->updatePage($page, $page->book->id, $request->all());
252 - Activity::add($page, 'page_update', $book->id); 248 + Activity::add($page, 'page_update', $page->book->id);
253 return redirect($page->getUrl()); 249 return redirect($page->getUrl());
254 } 250 }
255 251
...@@ -261,7 +257,7 @@ class PageController extends Controller ...@@ -261,7 +257,7 @@ class PageController extends Controller
261 */ 257 */
262 public function saveDraft(Request $request, $pageId) 258 public function saveDraft(Request $request, $pageId)
263 { 259 {
264 - $page = $this->pageRepo->getById($pageId, true); 260 + $page = $this->entityRepo->getById('page', $pageId, true);
265 $this->checkOwnablePermission('page-update', $page); 261 $this->checkOwnablePermission('page-update', $page);
266 262
267 if (!$this->signedIn) { 263 if (!$this->signedIn) {
...@@ -294,7 +290,7 @@ class PageController extends Controller ...@@ -294,7 +290,7 @@ class PageController extends Controller
294 */ 290 */
295 public function redirectFromLink($pageId) 291 public function redirectFromLink($pageId)
296 { 292 {
297 - $page = $this->pageRepo->getById($pageId); 293 + $page = $this->entityRepo->getById('page', $pageId);
298 return redirect($page->getUrl()); 294 return redirect($page->getUrl());
299 } 295 }
300 296
...@@ -306,11 +302,10 @@ class PageController extends Controller ...@@ -306,11 +302,10 @@ class PageController extends Controller
306 */ 302 */
307 public function showDelete($bookSlug, $pageSlug) 303 public function showDelete($bookSlug, $pageSlug)
308 { 304 {
309 - $book = $this->bookRepo->getBySlug($bookSlug); 305 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
310 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
311 $this->checkOwnablePermission('page-delete', $page); 306 $this->checkOwnablePermission('page-delete', $page);
312 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()])); 307 $this->setPageTitle(trans('entities.pages_delete_named', ['pageName'=>$page->getShortName()]));
313 - return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]); 308 + return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
314 } 309 }
315 310
316 311
...@@ -323,11 +318,10 @@ class PageController extends Controller ...@@ -323,11 +318,10 @@ class PageController extends Controller
323 */ 318 */
324 public function showDeleteDraft($bookSlug, $pageId) 319 public function showDeleteDraft($bookSlug, $pageId)
325 { 320 {
326 - $book = $this->bookRepo->getBySlug($bookSlug); 321 + $page = $this->entityRepo->getById('page', $pageId, true);
327 - $page = $this->pageRepo->getById($pageId, true);
328 $this->checkOwnablePermission('page-update', $page); 322 $this->checkOwnablePermission('page-update', $page);
329 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()])); 323 $this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName'=>$page->getShortName()]));
330 - return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]); 324 + return view('pages/delete', ['book' => $page->book, 'page' => $page, 'current' => $page]);
331 } 325 }
332 326
333 /** 327 /**
...@@ -339,8 +333,8 @@ class PageController extends Controller ...@@ -339,8 +333,8 @@ class PageController extends Controller
339 */ 333 */
340 public function destroy($bookSlug, $pageSlug) 334 public function destroy($bookSlug, $pageSlug)
341 { 335 {
342 - $book = $this->bookRepo->getBySlug($bookSlug); 336 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
343 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id); 337 + $book = $page->book;
344 $this->checkOwnablePermission('page-delete', $page); 338 $this->checkOwnablePermission('page-delete', $page);
345 Activity::addMessage('page_delete', $book->id, $page->name); 339 Activity::addMessage('page_delete', $book->id, $page->name);
346 session()->flash('success', trans('entities.pages_delete_success')); 340 session()->flash('success', trans('entities.pages_delete_success'));
...@@ -357,8 +351,8 @@ class PageController extends Controller ...@@ -357,8 +351,8 @@ class PageController extends Controller
357 */ 351 */
358 public function destroyDraft($bookSlug, $pageId) 352 public function destroyDraft($bookSlug, $pageId)
359 { 353 {
360 - $book = $this->bookRepo->getBySlug($bookSlug); 354 + $page = $this->entityRepo->getById('page', $pageId, true);
361 - $page = $this->pageRepo->getById($pageId, true); 355 + $book = $page->book;
362 $this->checkOwnablePermission('page-update', $page); 356 $this->checkOwnablePermission('page-update', $page);
363 session()->flash('success', trans('entities.pages_delete_draft_success')); 357 session()->flash('success', trans('entities.pages_delete_draft_success'));
364 $this->pageRepo->destroy($page); 358 $this->pageRepo->destroy($page);
...@@ -373,10 +367,9 @@ class PageController extends Controller ...@@ -373,10 +367,9 @@ class PageController extends Controller
373 */ 367 */
374 public function showRevisions($bookSlug, $pageSlug) 368 public function showRevisions($bookSlug, $pageSlug)
375 { 369 {
376 - $book = $this->bookRepo->getBySlug($bookSlug); 370 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
377 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
378 $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()])); 371 $this->setPageTitle(trans('entities.pages_revisions_named', ['pageName'=>$page->getShortName()]));
379 - return view('pages/revisions', ['page' => $page, 'book' => $book, 'current' => $page]); 372 + return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]);
380 } 373 }
381 374
382 /** 375 /**
...@@ -388,8 +381,7 @@ class PageController extends Controller ...@@ -388,8 +381,7 @@ class PageController extends Controller
388 */ 381 */
389 public function showRevision($bookSlug, $pageSlug, $revisionId) 382 public function showRevision($bookSlug, $pageSlug, $revisionId)
390 { 383 {
391 - $book = $this->bookRepo->getBySlug($bookSlug); 384 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
392 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
393 $revision = $this->pageRepo->getRevisionById($revisionId); 385 $revision = $this->pageRepo->getRevisionById($revisionId);
394 386
395 $page->fill($revision->toArray()); 387 $page->fill($revision->toArray());
...@@ -397,7 +389,7 @@ class PageController extends Controller ...@@ -397,7 +389,7 @@ class PageController extends Controller
397 389
398 return view('pages/revision', [ 390 return view('pages/revision', [
399 'page' => $page, 391 'page' => $page,
400 - 'book' => $book, 392 + 'book' => $page->book,
401 ]); 393 ]);
402 } 394 }
403 395
...@@ -410,8 +402,7 @@ class PageController extends Controller ...@@ -410,8 +402,7 @@ class PageController extends Controller
410 */ 402 */
411 public function showRevisionChanges($bookSlug, $pageSlug, $revisionId) 403 public function showRevisionChanges($bookSlug, $pageSlug, $revisionId)
412 { 404 {
413 - $book = $this->bookRepo->getBySlug($bookSlug); 405 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
414 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
415 $revision = $this->pageRepo->getRevisionById($revisionId); 406 $revision = $this->pageRepo->getRevisionById($revisionId);
416 407
417 $prev = $revision->getPrevious(); 408 $prev = $revision->getPrevious();
...@@ -423,7 +414,7 @@ class PageController extends Controller ...@@ -423,7 +414,7 @@ class PageController extends Controller
423 414
424 return view('pages/revision', [ 415 return view('pages/revision', [
425 'page' => $page, 416 'page' => $page,
426 - 'book' => $book, 417 + 'book' => $page->book,
427 'diff' => $diff, 418 'diff' => $diff,
428 ]); 419 ]);
429 } 420 }
...@@ -437,11 +428,10 @@ class PageController extends Controller ...@@ -437,11 +428,10 @@ class PageController extends Controller
437 */ 428 */
438 public function restoreRevision($bookSlug, $pageSlug, $revisionId) 429 public function restoreRevision($bookSlug, $pageSlug, $revisionId)
439 { 430 {
440 - $book = $this->bookRepo->getBySlug($bookSlug); 431 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
441 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
442 $this->checkOwnablePermission('page-update', $page); 432 $this->checkOwnablePermission('page-update', $page);
443 - $page = $this->pageRepo->restoreRevision($page, $book, $revisionId); 433 + $page = $this->pageRepo->restoreRevision($page, $page->book, $revisionId);
444 - Activity::add($page, 'page_restore', $book->id); 434 + Activity::add($page, 'page_restore', $page->book->id);
445 return redirect($page->getUrl()); 435 return redirect($page->getUrl());
446 } 436 }
447 437
...@@ -454,8 +444,7 @@ class PageController extends Controller ...@@ -454,8 +444,7 @@ class PageController extends Controller
454 */ 444 */
455 public function exportPdf($bookSlug, $pageSlug) 445 public function exportPdf($bookSlug, $pageSlug)
456 { 446 {
457 - $book = $this->bookRepo->getBySlug($bookSlug); 447 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
458 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
459 $pdfContent = $this->exportService->pageToPdf($page); 448 $pdfContent = $this->exportService->pageToPdf($page);
460 return response()->make($pdfContent, 200, [ 449 return response()->make($pdfContent, 200, [
461 'Content-Type' => 'application/octet-stream', 450 'Content-Type' => 'application/octet-stream',
...@@ -471,8 +460,7 @@ class PageController extends Controller ...@@ -471,8 +460,7 @@ class PageController extends Controller
471 */ 460 */
472 public function exportHtml($bookSlug, $pageSlug) 461 public function exportHtml($bookSlug, $pageSlug)
473 { 462 {
474 - $book = $this->bookRepo->getBySlug($bookSlug); 463 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
475 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
476 $containedHtml = $this->exportService->pageToContainedHtml($page); 464 $containedHtml = $this->exportService->pageToContainedHtml($page);
477 return response()->make($containedHtml, 200, [ 465 return response()->make($containedHtml, 200, [
478 'Content-Type' => 'application/octet-stream', 466 'Content-Type' => 'application/octet-stream',
...@@ -488,8 +476,7 @@ class PageController extends Controller ...@@ -488,8 +476,7 @@ class PageController extends Controller
488 */ 476 */
489 public function exportPlainText($bookSlug, $pageSlug) 477 public function exportPlainText($bookSlug, $pageSlug)
490 { 478 {
491 - $book = $this->bookRepo->getBySlug($bookSlug); 479 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
492 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
493 $containedHtml = $this->exportService->pageToPlainText($page); 480 $containedHtml = $this->exportService->pageToPlainText($page);
494 return response()->make($containedHtml, 200, [ 481 return response()->make($containedHtml, 200, [
495 'Content-Type' => 'application/octet-stream', 482 'Content-Type' => 'application/octet-stream',
...@@ -531,8 +518,7 @@ class PageController extends Controller ...@@ -531,8 +518,7 @@ class PageController extends Controller
531 */ 518 */
532 public function showRestrict($bookSlug, $pageSlug) 519 public function showRestrict($bookSlug, $pageSlug)
533 { 520 {
534 - $book = $this->bookRepo->getBySlug($bookSlug); 521 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
535 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
536 $this->checkOwnablePermission('restrictions-manage', $page); 522 $this->checkOwnablePermission('restrictions-manage', $page);
537 $roles = $this->userRepo->getRestrictableRoles(); 523 $roles = $this->userRepo->getRestrictableRoles();
538 return view('pages/restrictions', [ 524 return view('pages/restrictions', [
...@@ -550,11 +536,10 @@ class PageController extends Controller ...@@ -550,11 +536,10 @@ class PageController extends Controller
550 */ 536 */
551 public function showMove($bookSlug, $pageSlug) 537 public function showMove($bookSlug, $pageSlug)
552 { 538 {
553 - $book = $this->bookRepo->getBySlug($bookSlug); 539 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
554 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
555 $this->checkOwnablePermission('page-update', $page); 540 $this->checkOwnablePermission('page-update', $page);
556 return view('pages/move', [ 541 return view('pages/move', [
557 - 'book' => $book, 542 + 'book' => $page->book,
558 'page' => $page 543 'page' => $page
559 ]); 544 ]);
560 } 545 }
...@@ -569,8 +554,7 @@ class PageController extends Controller ...@@ -569,8 +554,7 @@ class PageController extends Controller
569 */ 554 */
570 public function move($bookSlug, $pageSlug, Request $request) 555 public function move($bookSlug, $pageSlug, Request $request)
571 { 556 {
572 - $book = $this->bookRepo->getBySlug($bookSlug); 557 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
573 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
574 $this->checkOwnablePermission('page-update', $page); 558 $this->checkOwnablePermission('page-update', $page);
575 559
576 $entitySelection = $request->get('entity_selection', null); 560 $entitySelection = $request->get('entity_selection', null);
...@@ -582,15 +566,10 @@ class PageController extends Controller ...@@ -582,15 +566,10 @@ class PageController extends Controller
582 $entityType = $stringExploded[0]; 566 $entityType = $stringExploded[0];
583 $entityId = intval($stringExploded[1]); 567 $entityId = intval($stringExploded[1]);
584 568
585 - $parent = false;
586 -
587 - if ($entityType == 'chapter') {
588 - $parent = $this->chapterRepo->getById($entityId);
589 - } else if ($entityType == 'book') {
590 - $parent = $this->bookRepo->getById($entityId);
591 - }
592 569
593 - if ($parent === false || $parent === null) { 570 + try {
571 + $parent = $this->entityRepo->getById($entityType, $entityId);
572 + } catch (\Exception $e) {
594 session()->flash(trans('entities.selected_book_chapter_not_found')); 573 session()->flash(trans('entities.selected_book_chapter_not_found'));
595 return redirect()->back(); 574 return redirect()->back();
596 } 575 }
...@@ -611,8 +590,7 @@ class PageController extends Controller ...@@ -611,8 +590,7 @@ class PageController extends Controller
611 */ 590 */
612 public function restrict($bookSlug, $pageSlug, Request $request) 591 public function restrict($bookSlug, $pageSlug, Request $request)
613 { 592 {
614 - $book = $this->bookRepo->getBySlug($bookSlug); 593 + $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
615 - $page = $this->pageRepo->getBySlug($pageSlug, $book->id);
616 $this->checkOwnablePermission('restrictions-manage', $page); 594 $this->checkOwnablePermission('restrictions-manage', $page);
617 $this->pageRepo->updateEntityPermissionsFromRequest($request, $page); 595 $this->pageRepo->updateEntityPermissionsFromRequest($request, $page);
618 session()->flash('success', trans('entities.pages_permissions_success')); 596 session()->flash('success', trans('entities.pages_permissions_success'));
......
...@@ -7,6 +7,8 @@ class Page extends Entity ...@@ -7,6 +7,8 @@ class Page extends Entity
7 7
8 protected $simpleAttributes = ['name', 'id', 'slug']; 8 protected $simpleAttributes = ['name', 'id', 'slug'];
9 9
10 + protected $with = ['book'];
11 +
10 /** 12 /**
11 * Converts this page into a simplified array. 13 * Converts this page into a simplified array.
12 * @return mixed 14 * @return mixed
......
1 <?php namespace BookStack\Repos; 1 <?php namespace BookStack\Repos;
2 2
3 -use Alpha\B;
4 -use BookStack\Exceptions\NotFoundException;
5 -use Illuminate\Database\Eloquent\Collection;
6 -use Illuminate\Support\Str;
7 use BookStack\Book; 3 use BookStack\Book;
8 -use Views;
9 4
10 class BookRepo extends EntityRepo 5 class BookRepo extends EntityRepo
11 { 6 {
...@@ -25,105 +20,6 @@ class BookRepo extends EntityRepo ...@@ -25,105 +20,6 @@ class BookRepo extends EntityRepo
25 } 20 }
26 21
27 /** 22 /**
28 - * Base query for getting books.
29 - * Takes into account any restrictions.
30 - * @return mixed
31 - */
32 - private function bookQuery()
33 - {
34 - return $this->permissionService->enforceBookRestrictions($this->book, 'view');
35 - }
36 -
37 - /**
38 - * Get the book that has the given id.
39 - * @param $id
40 - * @return mixed
41 - */
42 - public function getById($id)
43 - {
44 - return $this->bookQuery()->findOrFail($id);
45 - }
46 -
47 - /**
48 - * Get all books, Limited by count.
49 - * @param int $count
50 - * @return mixed
51 - */
52 - public function getAll($count = 10)
53 - {
54 - $bookQuery = $this->bookQuery()->orderBy('name', 'asc');
55 - if (!$count) return $bookQuery->get();
56 - return $bookQuery->take($count)->get();
57 - }
58 -
59 - /**
60 - * Get all books paginated.
61 - * @param int $count
62 - * @return mixed
63 - */
64 - public function getAllPaginated($count = 10)
65 - {
66 - return $this->bookQuery()
67 - ->orderBy('name', 'asc')->paginate($count);
68 - }
69 -
70 -
71 - /**
72 - * Get the latest books.
73 - * @param int $count
74 - * @return mixed
75 - */
76 - public function getLatest($count = 10)
77 - {
78 - return $this->bookQuery()->orderBy('created_at', 'desc')->take($count)->get();
79 - }
80 -
81 - /**
82 - * Gets the most recently viewed for a user.
83 - * @param int $count
84 - * @param int $page
85 - * @return mixed
86 - */
87 - public function getRecentlyViewed($count = 10, $page = 0)
88 - {
89 - return Views::getUserRecentlyViewed($count, $page, $this->book);
90 - }
91 -
92 - /**
93 - * Gets the most viewed books.
94 - * @param int $count
95 - * @param int $page
96 - * @return mixed
97 - */
98 - public function getPopular($count = 10, $page = 0)
99 - {
100 - return Views::getPopular($count, $page, $this->book);
101 - }
102 -
103 - /**
104 - * Get a book by slug
105 - * @param $slug
106 - * @return mixed
107 - * @throws NotFoundException
108 - */
109 - public function getBySlug($slug)
110 - {
111 - $book = $this->bookQuery()->where('slug', '=', $slug)->first();
112 - if ($book === null) throw new NotFoundException(trans('errors.book_not_found'));
113 - return $book;
114 - }
115 -
116 - /**
117 - * Checks if a book exists.
118 - * @param $id
119 - * @return bool
120 - */
121 - public function exists($id)
122 - {
123 - return $this->bookQuery()->where('id', '=', $id)->exists();
124 - }
125 -
126 - /**
127 * Get a new book instance from request input. 23 * Get a new book instance from request input.
128 * @param array $input 24 * @param array $input
129 * @return Book 25 * @return Book
......
...@@ -22,58 +22,6 @@ class ChapterRepo extends EntityRepo ...@@ -22,58 +22,6 @@ class ChapterRepo extends EntityRepo
22 } 22 }
23 23
24 /** 24 /**
25 - * Base query for getting chapters, Takes permissions into account.
26 - * @return mixed
27 - */
28 - private function chapterQuery()
29 - {
30 - return $this->permissionService->enforceChapterRestrictions($this->chapter, 'view');
31 - }
32 -
33 - /**
34 - * Check if an id exists.
35 - * @param $id
36 - * @return bool
37 - */
38 - public function idExists($id)
39 - {
40 - return $this->chapterQuery()->where('id', '=', $id)->count() > 0;
41 - }
42 -
43 - /**
44 - * Get a chapter by a specific id.
45 - * @param $id
46 - * @return mixed
47 - */
48 - public function getById($id)
49 - {
50 - return $this->chapterQuery()->findOrFail($id);
51 - }
52 -
53 - /**
54 - * Get all chapters.
55 - * @return \Illuminate\Database\Eloquent\Collection|static[]
56 - */
57 - public function getAll()
58 - {
59 - return $this->chapterQuery()->all();
60 - }
61 -
62 - /**
63 - * Get a chapter that has the given slug within the given book.
64 - * @param $slug
65 - * @param $bookId
66 - * @return mixed
67 - * @throws NotFoundException
68 - */
69 - public function getBySlug($slug, $bookId)
70 - {
71 - $chapter = $this->chapterQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
72 - if ($chapter === null) throw new NotFoundException(trans('errors.chapter_not_found'));
73 - return $chapter;
74 - }
75 -
76 - /**
77 * Get the child items for a chapter 25 * Get the child items for a chapter
78 * @param Chapter $chapter 26 * @param Chapter $chapter
79 */ 27 */
......
...@@ -3,11 +3,12 @@ ...@@ -3,11 +3,12 @@
3 use BookStack\Book; 3 use BookStack\Book;
4 use BookStack\Chapter; 4 use BookStack\Chapter;
5 use BookStack\Entity; 5 use BookStack\Entity;
6 +use BookStack\Exceptions\NotFoundException;
6 use BookStack\Page; 7 use BookStack\Page;
7 use BookStack\Services\PermissionService; 8 use BookStack\Services\PermissionService;
8 -use BookStack\User; 9 +use BookStack\Services\ViewService;
10 +use Illuminate\Database\Eloquent\Builder;
9 use Illuminate\Support\Collection; 11 use Illuminate\Support\Collection;
10 -use Illuminate\Support\Facades\Log;
11 12
12 class EntityRepo 13 class EntityRepo
13 { 14 {
...@@ -28,11 +29,22 @@ class EntityRepo ...@@ -28,11 +29,22 @@ class EntityRepo
28 public $page; 29 public $page;
29 30
30 /** 31 /**
32 + * Base entity instances keyed by type
33 + * @var []Entity
34 + */
35 + protected $entities;
36 +
37 + /**
31 * @var PermissionService 38 * @var PermissionService
32 */ 39 */
33 protected $permissionService; 40 protected $permissionService;
34 41
35 /** 42 /**
43 + * @var ViewService
44 + */
45 + protected $viewService;
46 +
47 + /**
36 * Acceptable operators to be used in a query 48 * Acceptable operators to be used in a query
37 * @var array 49 * @var array
38 */ 50 */
...@@ -43,69 +55,144 @@ class EntityRepo ...@@ -43,69 +55,144 @@ class EntityRepo
43 */ 55 */
44 public function __construct() 56 public function __construct()
45 { 57 {
58 + // TODO - Redo this to come via injection
46 $this->book = app(Book::class); 59 $this->book = app(Book::class);
47 $this->chapter = app(Chapter::class); 60 $this->chapter = app(Chapter::class);
48 $this->page = app(Page::class); 61 $this->page = app(Page::class);
62 + $this->entities = [
63 + 'page' => $this->page,
64 + 'chapter' => $this->chapter,
65 + 'book' => $this->book
66 + ];
67 + $this->viewService = app(ViewService::class);
49 $this->permissionService = app(PermissionService::class); 68 $this->permissionService = app(PermissionService::class);
50 } 69 }
51 70
52 /** 71 /**
53 - * Get the latest books added to the system. 72 + * Get an entity instance via type.
54 - * @param int $count 73 + * @param $type
55 - * @param int $page 74 + * @return Entity
56 - * @param bool $additionalQuery
57 - * @return
58 */ 75 */
59 - public function getRecentlyCreatedBooks($count = 20, $page = 0, $additionalQuery = false) 76 + protected function getEntity($type)
60 { 77 {
61 - $query = $this->permissionService->enforceBookRestrictions($this->book) 78 + return $this->entities[strtolower($type)];
62 - ->orderBy('created_at', 'desc'); 79 + }
63 - if ($additionalQuery !== false && is_callable($additionalQuery)) { 80 +
64 - $additionalQuery($query); 81 + /**
82 + * Base query for searching entities via permission system
83 + * @param string $type
84 + * @param bool $allowDrafts
85 + * @return \Illuminate\Database\Query\Builder
86 + */
87 + protected function entityQuery($type, $allowDrafts = false)
88 + {
89 + $q = $this->permissionService->enforceEntityRestrictions($type, $this->getEntity($type), 'view');
90 + if (strtolower($type) === 'page' && !$allowDrafts) {
91 + $q = $q->where('draft', '=', false);
65 } 92 }
66 - return $query->skip($page * $count)->take($count)->get(); 93 + return $q;
67 } 94 }
68 95
69 /** 96 /**
70 - * Get the most recently updated books. 97 + * Check if an entity with the given id exists.
71 - * @param $count 98 + * @param $type
72 - * @param int $page 99 + * @param $id
73 - * @return mixed 100 + * @return bool
74 */ 101 */
75 - public function getRecentlyUpdatedBooks($count = 20, $page = 0) 102 + public function exists($type, $id)
76 { 103 {
77 - return $this->permissionService->enforceBookRestrictions($this->book) 104 + return $this->entityQuery($type)->where('id', '=', $id)->exists();
78 - ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
79 } 105 }
80 106
81 /** 107 /**
82 - * Get the latest pages added to the system. 108 + * Get an entity by ID
109 + * @param string $type
110 + * @param integer $id
111 + * @param bool $allowDrafts
112 + * @return Entity
113 + */
114 + public function getById($type, $id, $allowDrafts = false)
115 + {
116 + return $this->entityQuery($type, $allowDrafts)->findOrFail($id);
117 + }
118 +
119 + /**
120 + * Get an entity by its url slug.
121 + * @param string $type
122 + * @param string $slug
123 + * @param string|bool $bookSlug
124 + * @return Entity
125 + * @throws NotFoundException
126 + */
127 + public function getBySlug($type, $slug, $bookSlug = false)
128 + {
129 + $q = $this->entityQuery($type)->where('slug', '=', $slug);
130 + if (strtolower($type) === 'chapter' || strtolower($type) === 'page') {
131 + $q = $q->where('book_id', '=', function($query) use ($bookSlug) {
132 + $query->select('id')
133 + ->from($this->book->getTable())
134 + ->where('slug', '=', $bookSlug)->limit(1);
135 + });
136 + }
137 + $entity = $q->first();
138 + if ($entity === null) throw new NotFoundException(trans('errors.' . strtolower($type) . '_not_found'));
139 + return $entity;
140 + }
141 +
142 + /**
143 + * Get all entities of a type limited by count unless count if false.
144 + * @param string $type
145 + * @param integer|bool $count
146 + * @return Collection
147 + */
148 + public function getAll($type, $count = 20)
149 + {
150 + $q = $this->entityQuery($type)->orderBy('name', 'asc');
151 + if ($count !== false) $q = $q->take($count);
152 + return $q->get();
153 + }
154 +
155 + /**
156 + * Get all entities in a paginated format
157 + * @param $type
158 + * @param int $count
159 + * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
160 + */
161 + public function getAllPaginated($type, $count = 10)
162 + {
163 + return $this->entityQuery($type)->orderBy('name', 'asc')->paginate($count);
164 + }
165 +
166 + /**
167 + * Get the most recently created entities of the given type.
168 + * @param string $type
83 * @param int $count 169 * @param int $count
84 * @param int $page 170 * @param int $page
85 - * @param bool $additionalQuery 171 + * @param bool|callable $additionalQuery
86 - * @return
87 */ 172 */
88 - public function getRecentlyCreatedPages($count = 20, $page = 0, $additionalQuery = false) 173 + public function getRecentlyCreated($type, $count = 20, $page = 0, $additionalQuery = false)
89 { 174 {
90 - $query = $this->permissionService->enforcePageRestrictions($this->page) 175 + $query = $this->permissionService->enforceEntityRestrictions($type, $this->getEntity($type))
91 - ->orderBy('created_at', 'desc')->where('draft', '=', false); 176 + ->orderBy('created_at', 'desc');
177 + if (strtolower($type) === 'page') $query = $query->where('draft', '=', false);
92 if ($additionalQuery !== false && is_callable($additionalQuery)) { 178 if ($additionalQuery !== false && is_callable($additionalQuery)) {
93 $additionalQuery($query); 179 $additionalQuery($query);
94 } 180 }
95 - return $query->with('book')->skip($page * $count)->take($count)->get(); 181 + return $query->skip($page * $count)->take($count)->get();
96 } 182 }
97 183
98 /** 184 /**
99 - * Get the latest chapters added to the system. 185 + * Get the most recently updated entities of the given type.
186 + * @param string $type
100 * @param int $count 187 * @param int $count
101 * @param int $page 188 * @param int $page
102 - * @param bool $additionalQuery 189 + * @param bool|callable $additionalQuery
103 - * @return
104 */ 190 */
105 - public function getRecentlyCreatedChapters($count = 20, $page = 0, $additionalQuery = false) 191 + public function getRecentlyUpdated($type, $count = 20, $page = 0, $additionalQuery = false)
106 { 192 {
107 - $query = $this->permissionService->enforceChapterRestrictions($this->chapter) 193 + $query = $this->permissionService->enforceEntityRestrictions($type, $this->getEntity($type))
108 - ->orderBy('created_at', 'desc'); 194 + ->orderBy('updated_at', 'desc');
195 + if (strtolower($type) === 'page') $query = $query->where('draft', '=', false);
109 if ($additionalQuery !== false && is_callable($additionalQuery)) { 196 if ($additionalQuery !== false && is_callable($additionalQuery)) {
110 $additionalQuery($query); 197 $additionalQuery($query);
111 } 198 }
...@@ -113,16 +200,29 @@ class EntityRepo ...@@ -113,16 +200,29 @@ class EntityRepo
113 } 200 }
114 201
115 /** 202 /**
116 - * Get the most recently updated pages. 203 + * Get the most recently viewed entities.
117 - * @param $count 204 + * @param string|bool $type
205 + * @param int $count
206 + * @param int $page
207 + * @return mixed
208 + */
209 + public function getRecentlyViewed($type, $count = 10, $page = 0)
210 + {
211 + $filter = is_bool($type) ? false : $this->getEntity($type);
212 + return $this->viewService->getUserRecentlyViewed($count, $page, $filter);
213 + }
214 +
215 + /**
216 + * Get the most popular entities base on all views.
217 + * @param string|bool $type
218 + * @param int $count
118 * @param int $page 219 * @param int $page
119 * @return mixed 220 * @return mixed
120 */ 221 */
121 - public function getRecentlyUpdatedPages($count = 20, $page = 0) 222 + public function getPopular($type, $count = 10, $page = 0)
122 { 223 {
123 - return $this->permissionService->enforcePageRestrictions($this->page) 224 + $filter = is_bool($type) ? false : $this->getEntity($type);
124 - ->where('draft', '=', false) 225 + return $this->viewService->getPopular($count, $page, $filter);
125 - ->orderBy('updated_at', 'desc')->with('book')->skip($page * $count)->take($count)->get();
126 } 226 }
127 227
128 /** 228 /**
......
...@@ -46,31 +46,6 @@ class PageRepo extends EntityRepo ...@@ -46,31 +46,6 @@ class PageRepo extends EntityRepo
46 } 46 }
47 47
48 /** 48 /**
49 - * Get a page via a specific ID.
50 - * @param $id
51 - * @param bool $allowDrafts
52 - * @return Page
53 - */
54 - public function getById($id, $allowDrafts = false)
55 - {
56 - return $this->pageQuery($allowDrafts)->findOrFail($id);
57 - }
58 -
59 - /**
60 - * Get a page identified by the given slug.
61 - * @param $slug
62 - * @param $bookId
63 - * @return Page
64 - * @throws NotFoundException
65 - */
66 - public function getBySlug($slug, $bookId)
67 - {
68 - $page = $this->pageQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
69 - if ($page === null) throw new NotFoundException(trans('errors.page_not_found'));
70 - return $page;
71 - }
72 -
73 - /**
74 * Search through page revisions and retrieve 49 * Search through page revisions and retrieve
75 * the last page in the current book that 50 * the last page in the current book that
76 * has a slug equal to the one given. 51 * has a slug equal to the one given.
......
...@@ -168,13 +168,13 @@ class UserRepo ...@@ -168,13 +168,13 @@ class UserRepo
168 public function getRecentlyCreated(User $user, $count = 20) 168 public function getRecentlyCreated(User $user, $count = 20)
169 { 169 {
170 return [ 170 return [
171 - 'pages' => $this->entityRepo->getRecentlyCreatedPages($count, 0, function ($query) use ($user) { 171 + 'pages' => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
172 $query->where('created_by', '=', $user->id); 172 $query->where('created_by', '=', $user->id);
173 }), 173 }),
174 - 'chapters' => $this->entityRepo->getRecentlyCreatedChapters($count, 0, function ($query) use ($user) { 174 + 'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
175 $query->where('created_by', '=', $user->id); 175 $query->where('created_by', '=', $user->id);
176 }), 176 }),
177 - 'books' => $this->entityRepo->getRecentlyCreatedBooks($count, 0, function ($query) use ($user) { 177 + 'books' => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
178 $query->where('created_by', '=', $user->id); 178 $query->where('created_by', '=', $user->id);
179 }) 179 })
180 ]; 180 ];
......
...@@ -8,8 +8,8 @@ use BookStack\Ownable; ...@@ -8,8 +8,8 @@ use BookStack\Ownable;
8 use BookStack\Page; 8 use BookStack\Page;
9 use BookStack\Role; 9 use BookStack\Role;
10 use BookStack\User; 10 use BookStack\User;
11 +use Illuminate\Database\Eloquent\Builder;
11 use Illuminate\Support\Collection; 12 use Illuminate\Support\Collection;
12 -use Illuminate\Support\Facades\Log;
13 13
14 class PermissionService 14 class PermissionService
15 { 15 {
...@@ -469,17 +469,8 @@ class PermissionService ...@@ -469,17 +469,8 @@ class PermissionService
469 */ 469 */
470 public function enforcePageRestrictions($query, $action = 'view') 470 public function enforcePageRestrictions($query, $action = 'view')
471 { 471 {
472 - // Prevent drafts being visible to others. 472 + // TODO - remove this
473 - $query = $query->where(function ($query) { 473 + return $this->enforceEntityRestrictions('page', $query, $action);
474 - $query->where('draft', '=', false);
475 - if ($this->currentUser()) {
476 - $query->orWhere(function ($query) {
477 - $query->where('draft', '=', true)->where('created_by', '=', $this->currentUser()->id);
478 - });
479 - }
480 - });
481 -
482 - return $this->enforceEntityRestrictions($query, $action);
483 } 474 }
484 475
485 /** 476 /**
...@@ -490,7 +481,8 @@ class PermissionService ...@@ -490,7 +481,8 @@ class PermissionService
490 */ 481 */
491 public function enforceChapterRestrictions($query, $action = 'view') 482 public function enforceChapterRestrictions($query, $action = 'view')
492 { 483 {
493 - return $this->enforceEntityRestrictions($query, $action); 484 + // TODO - remove this
485 + return $this->enforceEntityRestrictions('chapter', $query, $action);
494 } 486 }
495 487
496 /** 488 /**
...@@ -501,21 +493,36 @@ class PermissionService ...@@ -501,21 +493,36 @@ class PermissionService
501 */ 493 */
502 public function enforceBookRestrictions($query, $action = 'view') 494 public function enforceBookRestrictions($query, $action = 'view')
503 { 495 {
504 - return $this->enforceEntityRestrictions($query, $action); 496 + // TODO - remove this
497 + return $this->enforceEntityRestrictions('book', $query, $action);
505 } 498 }
506 499
507 /** 500 /**
508 * Add restrictions for a generic entity 501 * Add restrictions for a generic entity
509 - * @param $query 502 + * @param string $entityType
503 + * @param Builder|Entity $query
510 * @param string $action 504 * @param string $action
511 * @return mixed 505 * @return mixed
512 */ 506 */
513 - public function enforceEntityRestrictions($query, $action = 'view') 507 + public function enforceEntityRestrictions($entityType, $query, $action = 'view')
514 { 508 {
509 + if (strtolower($entityType) === 'page') {
510 + // Prevent drafts being visible to others.
511 + $query = $query->where(function ($query) {
512 + $query->where('draft', '=', false);
513 + if ($this->currentUser()) {
514 + $query->orWhere(function ($query) {
515 + $query->where('draft', '=', true)->where('created_by', '=', $this->currentUser()->id);
516 + });
517 + }
518 + });
519 + }
520 +
515 if ($this->isAdmin()) { 521 if ($this->isAdmin()) {
516 $this->clean(); 522 $this->clean();
517 return $query; 523 return $query;
518 } 524 }
525 +
519 $this->currentAction = $action; 526 $this->currentAction = $action;
520 return $this->entityRestrictionQuery($query); 527 return $this->entityRestrictionQuery($query);
521 } 528 }
......
...@@ -65,9 +65,9 @@ class RestrictionsTest extends TestCase ...@@ -65,9 +65,9 @@ class RestrictionsTest extends TestCase
65 $this->forceVisit($bookUrl) 65 $this->forceVisit($bookUrl)
66 ->see('Book not found'); 66 ->see('Book not found');
67 $this->forceVisit($bookPage->getUrl()) 67 $this->forceVisit($bookPage->getUrl())
68 - ->see('Book not found'); 68 + ->see('Page not found');
69 $this->forceVisit($bookChapter->getUrl()) 69 $this->forceVisit($bookChapter->getUrl())
70 - ->see('Book not found'); 70 + ->see('Chapter not found');
71 71
72 $this->setEntityRestrictions($book, ['view']); 72 $this->setEntityRestrictions($book, ['view']);
73 73
......