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,
......
...@@ -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');
63 - if ($additionalQuery !== false && is_callable($additionalQuery)) {
64 - $additionalQuery($query);
65 } 79 }
66 - return $query->skip($page * $count)->take($count)->get(); 80 +
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);
92 + }
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
101 + */
102 + public function exists($type, $id)
103 + {
104 + return $this->entityQuery($type)->where('id', '=', $id)->exists();
105 + }
106 +
107 + /**
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
74 */ 147 */
75 - public function getRecentlyUpdatedBooks($count = 20, $page = 0) 148 + public function getAll($type, $count = 20)
76 { 149 {
77 - return $this->permissionService->enforceBookRestrictions($this->book) 150 + $q = $this->entityQuery($type)->orderBy('name', 'asc');
78 - ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get(); 151 + if ($count !== false) $q = $q->take($count);
152 + return $q->get();
79 } 153 }
80 154
81 /** 155 /**
82 - * Get the latest pages added to the system. 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
......