diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php
index f2a137429..19e201183 100644
--- a/app/Http/Controllers/PageController.php
+++ b/app/Http/Controllers/PageController.php
@@ -135,12 +135,23 @@ class PageController extends Controller
return redirect($page->getUrl());
}
+ /**
+ * Redirect from a special link url which
+ * uses the page id rather than the name.
+ * @param $pageId
+ * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
+ */
public function redirectFromLink($pageId)
{
$page = $this->pageRepo->getById($pageId);
return redirect($page->getUrl());
}
+ /**
+ * Search all available pages, Across all books.
+ * @param Request $request
+ * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
+ */
public function searchAll(Request $request)
{
$searchTerm = $request->get('term');
@@ -150,6 +161,30 @@ class PageController extends Controller
return view('pages/search-results', ['pages' => $pages, 'searchTerm' => $searchTerm]);
}
+ /**
+ * Shows the view which allows pages to be re-ordered and sorted.
+ * @param $bookSlug
+ * @return \Illuminate\View\View
+ */
+ public function sortPages($bookSlug)
+ {
+ $book = $this->bookRepo->getBySlug($bookSlug);
+ $tree = $this->bookRepo->getTree($book);
+ return view('pages/sort', ['book' => $book, 'tree' => $tree]);
+ }
+
+ public function savePageSort($bookSlug, Request $request)
+ {
+ $book = $this->bookRepo->getBySlug($bookSlug);
+ if(!$request->has('sort-tree')) {
+ return redirect($book->getUrl());
+ }
+
+ $sortMap = json_decode($request->get('sort-tree'));
+ $this->pageRepo->applySortMap($sortMap, $book->id);
+ return redirect($book->getUrl());
+ }
+
/**
* Remove the specified resource from storage.
*
diff --git a/app/Http/routes.php b/app/Http/routes.php
index e80f53274..0dd0226a2 100644
--- a/app/Http/routes.php
+++ b/app/Http/routes.php
@@ -24,6 +24,8 @@ Route::group(['prefix' => 'books'], function() {
Route::get('/{bookSlug}/page/create', 'PageController@create');
Route::post('/{bookSlug}/page', 'PageController@store');
+ Route::get('/{bookSlug}/sort', 'PageController@sortPages');
+ Route::put('/{bookSlug}/sort', 'PageController@savePageSort');
Route::get('/{bookSlug}/{pageSlug}', 'PageController@show');
Route::get('/{bookSlug}/{pageSlug}/create', 'PageController@create');
Route::get('/{bookSlug}/{pageSlug}/edit', 'PageController@edit');
diff --git a/app/Page.php b/app/Page.php
index 3d9e6e3b2..819b14024 100644
--- a/app/Page.php
+++ b/app/Page.php
@@ -24,7 +24,7 @@ class Page extends Model
public function children()
{
- return $this->hasMany('Oxbow\Page');
+ return $this->hasMany('Oxbow\Page')->orderBy('priority', 'ASC');
}
public function parent()
diff --git a/app/Repos/BookRepo.php b/app/Repos/BookRepo.php
index 3693fe40a..060b6fc49 100644
--- a/app/Repos/BookRepo.php
+++ b/app/Repos/BookRepo.php
@@ -57,6 +57,7 @@ class BookRepo
{
$tree = $book->toArray();
$tree['pages'] = $this->pageRepo->getTreeByBookId($book->id);
+ $tree['hasChildren'] = count($tree['pages']) > 0;
return $tree;
}
diff --git a/app/Repos/PageRepo.php b/app/Repos/PageRepo.php
index 08da6654b..05fe3f707 100644
--- a/app/Repos/PageRepo.php
+++ b/app/Repos/PageRepo.php
@@ -114,7 +114,24 @@ class PageRepo
*/
private function getTopLevelPages($bookId)
{
- return $this->page->where('book_id', '=', $bookId)->where('page_id', '=', 0)->get();
+ return $this->page->where('book_id', '=', $bookId)->where('page_id', '=', 0)->orderBy('priority')->get();
+ }
+
+ /**
+ * Applies a sort map to all applicable pages.
+ * @param $sortMap
+ * @param $bookId
+ */
+ public function applySortMap($sortMap, $bookId)
+ {
+ foreach($sortMap as $index => $map) {
+ $page = $this->getById($map->id);
+ if($page->book_id === $bookId) {
+ $page->page_id = $map->parent;
+ $page->priority = $index;
+ $page->save();
+ }
+ }
}
}
\ No newline at end of file
diff --git a/resources/assets/sass/styles.scss b/resources/assets/sass/styles.scss
index ef9a69bc3..0fd4f638d 100644
--- a/resources/assets/sass/styles.scss
+++ b/resources/assets/sass/styles.scss
@@ -94,6 +94,9 @@ header .menu {
&:hover {
opacity: 1;
}
+ .buttons a {
+ display: block;
+ }
}
.page-nav-list {
@@ -268,4 +271,44 @@ h1, h2, h3, h4, h5, h6 {
display: block;
}
}
+}
+
+.sortable-page-list, .sortable-page-list ul {
+ list-style: none;
+ //background-color: rgba(0, 0, 0, 0.04);
+}
+.sortable-page-list {
+ margin-left: 0;
+ ul {
+ margin-bottom: 0;
+ margin-top: 0;
+ }
+ li {
+ border-bottom: 1px solid #BBB;
+ border-left: 1px solid #BBB;
+ border-right: 1px solid #BBB;
+ padding: $-xs $-s;
+ }
+ li:first-child {
+ margin-top: $-xs;
+ border-top: 1px solid #BBB;
+ }
+}
+
+// Jquery Sortable Styles
+.dragged {
+ position: absolute;
+ opacity: 0.5;
+ z-index: 2000;
+}
+
+body.dragging, body.dragging * {
+ cursor: move !important;
+}
+
+.sortable-page-list li.placeholder {
+ position: relative;
+}
+.sortable-page-list li.placeholder:before {
+ position: absolute;
}
\ No newline at end of file
diff --git a/resources/views/base.blade.php b/resources/views/base.blade.php
index 59860b037..282b0d35e 100644
--- a/resources/views/base.blade.php
+++ b/resources/views/base.blade.php
@@ -8,6 +8,7 @@
+
+@stop