Delete Book
+This will delete the book with the name '{{$book->name}}', All pages and chapters will be removed.
+Are you sure you want to delete this book?
+ + +diff --git a/app/Http/Controllers/BookController.php b/app/Http/Controllers/BookController.php index fcb8c7752..269e240a4 100644 --- a/app/Http/Controllers/BookController.php +++ b/app/Http/Controllers/BookController.php @@ -78,8 +78,7 @@ class BookController extends Controller public function show($slug) { $book = $this->bookRepo->getBySlug($slug); - $pageTree = $this->pageRepo->getTreeByBookId($book->id); - return view('books/show', ['book' => $book, 'pageTree' => $pageTree]); + return view('books/show', ['book' => $book]); } /** @@ -118,15 +117,26 @@ class BookController extends Controller return redirect($book->getUrl()); } + /** + * Shows the page to confirm deletion + * @param $bookSlug + * @return \Illuminate\View\View + */ + public function showDelete($bookSlug) + { + $book = $this->bookRepo->getBySlug($bookSlug); + return view('books/delete', ['book' => $book]); + } + /** * Remove the specified book from storage. * - * @param int $id + * @param $bookSlug * @return Response */ - public function destroy($id) + public function destroy($bookSlug) { - $this->bookRepo->destroyById($id); + $this->bookRepo->destroyBySlug($bookSlug); return redirect('/books'); } } diff --git a/app/Http/Controllers/ChapterController.php b/app/Http/Controllers/ChapterController.php index e7d1f1799..42c511d95 100644 --- a/app/Http/Controllers/ChapterController.php +++ b/app/Http/Controllers/ChapterController.php @@ -25,20 +25,10 @@ class ChapterController extends Controller $this->bookRepo = $bookRepo; $this->chapterRepo = $chapterRepo; } - + /** - * Display a listing of the resource. - * - * @return Response - */ - public function index() - { - // - } - - /** - * Show the form for creating a new resource. + * Show the form for creating a new chapter. * * @param $bookSlug * @return Response @@ -50,7 +40,7 @@ class ChapterController extends Controller } /** - * Store a newly created resource in storage. + * Store a newly created chapter in storage. * * @param $bookSlug * @param Request $request @@ -65,52 +55,88 @@ class ChapterController extends Controller $book = $this->bookRepo->getBySlug($bookSlug); $chapter = $this->chapterRepo->newFromInput($request->all()); $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id); + $chapter->priority = $this->bookRepo->getNewPriority($book); $book->chapters()->save($chapter); return redirect($book->getUrl()); } /** - * Display the specified resource. + * Display the specified chapter. * - * @param int $id + * @param $bookSlug + * @param $chapterSlug * @return Response */ - public function show($id) + public function show($bookSlug, $chapterSlug) { - // + $book = $this->bookRepo->getBySlug($bookSlug); + $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id); + return view('chapters/show', ['book' => $book, 'chapter' => $chapter]); } /** - * Show the form for editing the specified resource. + * Show the form for editing the specified chapter. * - * @param int $id + * @param $bookSlug + * @param $chapterSlug * @return Response */ - public function edit($id) + public function edit($bookSlug, $chapterSlug) { - // + $book = $this->bookRepo->getBySlug($bookSlug); + $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id); + return view('chapters/edit', ['book' => $book, 'chapter' => $chapter]); } /** - * Update the specified resource in storage. + * Update the specified chapter in storage. * - * @param Request $request - * @param int $id + * @param Request $request + * @param $bookSlug + * @param $chapterSlug * @return Response */ - public function update(Request $request, $id) + public function update(Request $request, $bookSlug, $chapterSlug) { - // + $book = $this->bookRepo->getBySlug($bookSlug); + $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id); + $chapter->fill($request->all()); + $chapter->slug = $this->chapterRepo->findSuitableSlug($chapter->name, $book->id, $chapter->id); + $chapter->save(); + return redirect($chapter->getUrl()); } /** - * Remove the specified resource from storage. + * Shows the page to confirm deletion of this chapter. + * @param $bookSlug + * @param $chapterSlug + * @return \Illuminate\View\View + */ + public function showDelete($bookSlug, $chapterSlug) + { + $book = $this->bookRepo->getBySlug($bookSlug); + $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id); + return view('chapters/delete', ['book' => $book, 'chapter' => $chapter]); + } + + /** + * Remove the specified chapter from storage. * - * @param int $id + * @param $bookSlug + * @param $chapterSlug * @return Response */ - public function destroy($id) + public function destroy($bookSlug, $chapterSlug) { - // + $book = $this->bookRepo->getBySlug($bookSlug); + $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id); + if(count($chapter->pages) > 0) { + foreach($chapter->pages as $page) { + $page->chapter_id = 0; + $page->save(); + } + } + $chapter->delete(); + return redirect($book->getUrl()); } } diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php index c0ab01a09..25009f768 100644 --- a/app/Http/Controllers/PageController.php +++ b/app/Http/Controllers/PageController.php @@ -63,7 +63,6 @@ class PageController extends Controller $this->validate($request, [ 'name' => 'required|string|max:255', 'html' => 'required|string', - 'priority' => 'integer', 'parent' => 'integer|exists:pages,id' ]); $book = $this->bookRepo->getBySlug($bookSlug); @@ -72,7 +71,8 @@ class PageController extends Controller while($this->pageRepo->countBySlug($slug, $book->id) > 0) { $slug .= '1'; } - $page->slug =$slug; + $page->slug = $slug; + $page->priority = $this->bookRepo->getNewPriority($book); if($request->has('parent')) { $page->page_id = $request->get('parent'); @@ -96,9 +96,8 @@ class PageController extends Controller $book = $this->bookRepo->getBySlug($bookSlug); $page = $this->pageRepo->getBySlug($pageSlug, $book->id); $breadCrumbs = $this->pageRepo->getBreadCrumbs($page); - $sidebarBookTree = $this->bookRepo->getTree($book, $page->id); //dd($sidebarBookTree); - return view('pages/show', ['page' => $page, 'breadCrumbs' => $breadCrumbs, 'book' => $book, 'sidebarBookTree' => $sidebarBookTree]); + return view('pages/show', ['page' => $page, 'breadCrumbs' => $breadCrumbs, 'book' => $book]); } /** @@ -187,14 +186,26 @@ class PageController extends Controller return redirect($book->getUrl()); } + public function showDelete($bookSlug, $pageSlug) + { + $book = $this->bookRepo->getBySlug($bookSlug); + $page = $this->pageRepo->getBySlug($pageSlug, $book->id); + return view('pages/delete', ['book' => $book, 'page' => $page]); + } + /** * Remove the specified resource from storage. * - * @param int $id + * @param $bookSlug + * @param $pageSlug * @return Response + * @internal param int $id */ - public function destroy($id) + public function destroy($bookSlug, $pageSlug) { - // + $book = $this->bookRepo->getBySlug($bookSlug); + $page = $this->pageRepo->getBySlug($pageSlug, $book->id); + $page->delete(); + return redirect($book->getUrl()); } } diff --git a/app/Http/routes.php b/app/Http/routes.php index 8204599f5..06b802ae2 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -19,8 +19,9 @@ Route::group(['prefix' => 'books'], function() { Route::post('/', 'BookController@store'); Route::get('/{slug}/edit', 'BookController@edit'); Route::put('/{slug}', 'BookController@update'); - Route::delete('/{id}/destroy', 'BookController@destroy'); + Route::delete('/{id}', 'BookController@destroy'); Route::get('/{slug}', 'BookController@show'); + Route::get('/{slug}/delete', 'BookController@showDelete'); Route::get('/{bookSlug}/page/create', 'PageController@create'); Route::post('/{bookSlug}/page', 'PageController@store'); @@ -29,10 +30,17 @@ Route::group(['prefix' => 'books'], function() { Route::get('/{bookSlug}/page/{pageSlug}', 'PageController@show'); Route::get('/{bookSlug}/page/{pageSlug}/create', 'PageController@create'); Route::get('/{bookSlug}/page/{pageSlug}/edit', 'PageController@edit'); + Route::get('/{bookSlug}/page/{pageSlug}/delete', 'PageController@showDelete'); Route::put('/{bookSlug}/page/{pageSlug}', 'PageController@update'); + Route::delete('/{bookSlug}/page/{pageSlug}', 'PageController@destroy'); Route::get('/{bookSlug}/chapter/create', 'ChapterController@create'); Route::post('/{bookSlug}/chapter/create', 'ChapterController@store'); + Route::get('/{bookSlug}/chapter/{chapterSlug}', 'ChapterController@show'); + Route::put('/{bookSlug}/chapter/{chapterSlug}', 'ChapterController@update'); + Route::get('/{bookSlug}/chapter/{chapterSlug}/edit', 'ChapterController@edit'); + Route::get('/{bookSlug}/chapter/{chapterSlug}/delete', 'ChapterController@showDelete'); + Route::delete('/{bookSlug}/chapter/{chapterSlug}', 'ChapterController@destroy'); }); diff --git a/app/Page.php b/app/Page.php index 53cfc9ada..449cb15c4 100644 --- a/app/Page.php +++ b/app/Page.php @@ -22,11 +22,6 @@ class Page extends Model return $this->belongsTo('Oxbow\Book'); } - public function children() - { - return $this->hasMany('Oxbow\Page')->orderBy('priority', 'ASC'); - } - public function parent() { return $this->belongsTo('Oxbow\Page', 'page_id'); diff --git a/app/Repos/BookRepo.php b/app/Repos/BookRepo.php index c43f449b0..b98ed9aba 100644 --- a/app/Repos/BookRepo.php +++ b/app/Repos/BookRepo.php @@ -44,21 +44,19 @@ class BookRepo return $this->book->where('slug', '=', $slug)->count(); } - public function destroyById($id) + public function destroyBySlug($bookSlug) { - $book = $this->getById($id); - foreach($book->pages as $page) { - $this->pageRepo->destroyById($page->id); + $book = $this->getBySlug($bookSlug); + foreach($book->children() as $child) { + $child->delete(); } $book->delete(); } - public function getTree($book, $currentPageId = false) + public function getNewPriority($book) { - $tree = $book->toArray(); - $tree['pages'] = $this->pageRepo->getTreeByBookId($book->id, $currentPageId); - $tree['hasChildren'] = count($tree['pages']) > 0; - return $tree; + $lastElem = $book->children()->pop(); + return $lastElem ? $lastElem->priority + 1 : 0; } } \ No newline at end of file diff --git a/app/Repos/ChapterRepo.php b/app/Repos/ChapterRepo.php index 7cf1db2ce..60d9b24a9 100644 --- a/app/Repos/ChapterRepo.php +++ b/app/Repos/ChapterRepo.php @@ -53,10 +53,10 @@ class ChapterRepo return $query->count() > 0; } - public function findSuitableSlug($name, $bookId) + public function findSuitableSlug($name, $bookId, $currentId = false) { $slug = Str::slug($name); - while($this->doesSlugExist($slug, $bookId)) { + while($this->doesSlugExist($slug, $bookId, $currentId)) { $slug .= '-' . substr(md5(rand(1, 500)), 0, 3); } return $slug; diff --git a/app/Repos/PageRepo.php b/app/Repos/PageRepo.php index 897c9ff77..7ce10498d 100644 --- a/app/Repos/PageRepo.php +++ b/app/Repos/PageRepo.php @@ -70,44 +70,6 @@ class PageRepo return count($tree) > 0 ? array_reverse($tree) : false; } - /** - * Creates a tree of child pages, Nested by their - * set parent pages. - * @param $bookId - * @param bool $currentPageId - * @return array - */ - public function getTreeByBookId($bookId, $currentPageId = false) - { - $topLevelPages = $this->getTopLevelPages($bookId); - $pageTree = []; - - foreach($topLevelPages as $key => $topPage) { - $pageTree[$key] = $this->toArrayTree($topPage, $currentPageId); - } - - return $pageTree; - } - - /** - * Creates a page tree array with the supplied page - * as the parent of the tree. - * @param $page - * @param bool $currentPageId - * @return mixed - */ - private function toArrayTree($page, $currentPageId = false) - { - $cPage = $page->toSimpleArray(); - $cPage['current'] = ($currentPageId !== false && $cPage['id'] === $currentPageId); - $cPage['pages'] = []; - foreach($page->children as $key => $childPage) { - $cPage['pages'][$key] = $this->toArrayTree($childPage, $currentPageId); - } - $cPage['hasChildren'] = count($cPage['pages']) > 0; - return $cPage; - } - /** * Gets the pages at the top of the page hierarchy. * @param $bookId diff --git a/resources/assets/sass/_tinymce.scss b/resources/assets/sass/_tinymce.scss index 10a3c0175..8a19f24ea 100644 --- a/resources/assets/sass/_tinymce.scss +++ b/resources/assets/sass/_tinymce.scss @@ -21,10 +21,21 @@ box-shadow: 0 0 4px 2px rgba(0, 0, 0, 0.08); } + + .edit-area { height: 100%; } .page-style.editor { height: 100%; +} + +.mce-tinymce { + .mce-panel { + background-color: #FFF; + } + .mce-btn { + background-color: #FFF; + } } \ No newline at end of file diff --git a/resources/views/books/delete.blade.php b/resources/views/books/delete.blade.php new file mode 100644 index 000000000..30965071c --- /dev/null +++ b/resources/views/books/delete.blade.php @@ -0,0 +1,22 @@ +@extends('base') + +@section('content') + +
This will delete the book with the name '{{$book->name}}', All pages and chapters will be removed.
+Are you sure you want to delete this book?
+ + +This will delete this book and all it's pages.
- @include('form/delete-button', ['url' => '/books/' . $book->id . '/destroy', 'text' => 'Delete']) -This will delete the chapter with the name '{{$chapter->name}}', All pages will be removed + and added directly to the book.
+Are you sure you want to delete this chapter?
+ + +{{ $chapter->description }}
+ @if(count($chapter->pages) > 0) +Are you sure you want to delete this page?
+ + +