X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/8933179017bd082d887c1cd694db9acf14c30587..refs/pull/234/head:/app/Http/Controllers/BookController.php diff --git a/app/Http/Controllers/BookController.php b/app/Http/Controllers/BookController.php index 91c965145..80a6c24b3 100644 --- a/app/Http/Controllers/BookController.php +++ b/app/Http/Controllers/BookController.php @@ -3,11 +3,11 @@ use Activity; use BookStack\Repos\UserRepo; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; use BookStack\Http\Requests; use BookStack\Repos\BookRepo; use BookStack\Repos\ChapterRepo; use BookStack\Repos\PageRepo; +use Illuminate\Http\Response; use Views; class BookController extends Controller @@ -54,7 +54,7 @@ class BookController extends Controller public function create() { $this->checkPermission('book-create-all'); - $this->setPageTitle('Create New Book'); + $this->setPageTitle(trans('entities.books_create')); return view('books/create'); } @@ -71,11 +71,7 @@ class BookController extends Controller 'name' => 'required|string|max:255', 'description' => 'string|max:1000' ]); - $book = $this->bookRepo->newFromInput($request->all()); - $book->slug = $this->bookRepo->findSuitableSlug($book->name); - $book->created_by = Auth::user()->id; - $book->updated_by = Auth::user()->id; - $book->save(); + $book = $this->bookRepo->createFromInput($request->all()); Activity::add($book, 'book_create', $book->id); return redirect($book->getUrl()); } @@ -88,6 +84,7 @@ class BookController extends Controller public function show($slug) { $book = $this->bookRepo->getBySlug($slug); + $this->checkOwnablePermission('book-view', $book); $bookChildren = $this->bookRepo->getChildren($book); Views::add($book); $this->setPageTitle($book->getShortName()); @@ -103,7 +100,7 @@ class BookController extends Controller { $book = $this->bookRepo->getBySlug($slug); $this->checkOwnablePermission('book-update', $book); - $this->setPageTitle('Edit Book ' . $book->getShortName()); + $this->setPageTitle(trans('entities.books_edit_named',['bookName'=>$book->getShortName()])); return view('books/edit', ['book' => $book, 'current' => $book]); } @@ -121,10 +118,7 @@ class BookController extends Controller 'name' => 'required|string|max:255', 'description' => 'string|max:1000' ]); - $book->fill($request->all()); - $book->slug = $this->bookRepo->findSuitableSlug($book->name, $book->id); - $book->updated_by = Auth::user()->id; - $book->save(); + $book = $this->bookRepo->updateFromInput($book, $request->all()); Activity::add($book, 'book_update', $book->id); return redirect($book->getUrl()); } @@ -138,7 +132,7 @@ class BookController extends Controller { $book = $this->bookRepo->getBySlug($bookSlug); $this->checkOwnablePermission('book-delete', $book); - $this->setPageTitle('Delete Book ' . $book->getShortName()); + $this->setPageTitle(trans('entities.books_delete_named', ['bookName'=>$book->getShortName()])); return view('books/delete', ['book' => $book, 'current' => $book]); } @@ -153,7 +147,7 @@ class BookController extends Controller $this->checkOwnablePermission('book-update', $book); $bookChildren = $this->bookRepo->getChildren($book, true); $books = $this->bookRepo->getAll(false); - $this->setPageTitle('Sort Book ' . $book->getShortName()); + $this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()])); return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]); } @@ -186,21 +180,31 @@ class BookController extends Controller return redirect($book->getUrl()); } - $sortedBooks = []; // Sort pages and chapters + $sortedBooks = []; + $updatedModels = collect(); $sortMap = json_decode($request->get('sort-tree')); $defaultBookId = $book->id; - foreach ($sortMap as $index => $bookChild) { - $id = $bookChild->id; + + // Loop through contents of provided map and update entities accordingly + foreach ($sortMap as $bookChild) { + $priority = $bookChild->sort; + $id = intval($bookChild->id); $isPage = $bookChild->type == 'page'; - $bookId = $this->bookRepo->exists($bookChild->book) ? $bookChild->book : $defaultBookId; + $bookId = $this->bookRepo->exists($bookChild->book) ? intval($bookChild->book) : $defaultBookId; + $chapterId = ($isPage && $bookChild->parentChapter === false) ? 0 : intval($bookChild->parentChapter); $model = $isPage ? $this->pageRepo->getById($id) : $this->chapterRepo->getById($id); - $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model); - $model->priority = $index; - if ($isPage) { - $model->chapter_id = ($bookChild->parentChapter === false) ? 0 : $bookChild->parentChapter; + + // Update models only if there's a change in parent chain or ordering. + if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) { + $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model); + $model->priority = $priority; + if ($isPage) $model->chapter_id = $chapterId; + $model->save(); + $updatedModels->push($model); } - $model->save(); + + // Store involved books to be sorted later if (!in_array($bookId, $sortedBooks)) { $sortedBooks[] = $bookId; } @@ -212,6 +216,9 @@ class BookController extends Controller Activity::add($updatedBook, 'book_sort', $updatedBook->id); } + // Update permissions on changed models + $this->bookRepo->buildJointPermissions($updatedModels); + return redirect($book->getUrl()); } @@ -226,7 +233,7 @@ class BookController extends Controller $this->checkOwnablePermission('book-delete', $book); Activity::addMessage('book_delete', 0, $book->name); Activity::removeEntity($book); - $this->bookRepo->destroyBySlug($bookSlug); + $this->bookRepo->destroy($book); return redirect('/books'); } @@ -257,8 +264,8 @@ class BookController extends Controller { $book = $this->bookRepo->getBySlug($bookSlug); $this->checkOwnablePermission('restrictions-manage', $book); - $this->bookRepo->updateRestrictionsFromRequest($request, $book); - session()->flash('success', 'Book Restrictions Updated'); + $this->bookRepo->updateEntityPermissionsFromRequest($request, $book); + session()->flash('success', trans('entities.books_permissions_updated')); return redirect($book->getUrl()); } }