Dan Brown

Made new pages in chapters have a better inital priority

...@@ -88,7 +88,6 @@ class PageController extends Controller ...@@ -88,7 +88,6 @@ class PageController extends Controller
88 88
89 $input = $request->all(); 89 $input = $request->all();
90 $book = $this->bookRepo->getBySlug($bookSlug); 90 $book = $this->bookRepo->getBySlug($bookSlug);
91 - $input['priority'] = $this->bookRepo->getNewPriority($book);
92 91
93 $draftPage = $this->pageRepo->getById($pageId, true); 92 $draftPage = $this->pageRepo->getById($pageId, true);
94 93
...@@ -96,6 +95,12 @@ class PageController extends Controller ...@@ -96,6 +95,12 @@ class PageController extends Controller
96 $parent = $chapterId !== 0 ? $this->chapterRepo->getById($chapterId) : $book; 95 $parent = $chapterId !== 0 ? $this->chapterRepo->getById($chapterId) : $book;
97 $this->checkOwnablePermission('page-create', $parent); 96 $this->checkOwnablePermission('page-create', $parent);
98 97
98 + if ($parent->isA('chapter')) {
99 + $input['priority'] = $this->chapterRepo->getNewPriority($parent);
100 + } else {
101 + $input['priority'] = $this->bookRepo->getNewPriority($parent);
102 + }
103 +
99 $page = $this->pageRepo->publishDraft($draftPage, $input); 104 $page = $this->pageRepo->publishDraft($draftPage, $input);
100 105
101 Activity::add($page, 'page_create', $book->id); 106 Activity::add($page, 'page_create', $book->id);
......
...@@ -137,6 +137,18 @@ class ChapterRepo extends EntityRepo ...@@ -137,6 +137,18 @@ class ChapterRepo extends EntityRepo
137 } 137 }
138 138
139 /** 139 /**
140 + * Get a new priority value for a new page to be added
141 + * to the given chapter.
142 + * @param Chapter $chapter
143 + * @return int
144 + */
145 + public function getNewPriority(Chapter $chapter)
146 + {
147 + $lastPage = $chapter->pages->last();
148 + return $lastPage !== null ? $lastPage->priority + 1 : 0;
149 + }
150 +
151 + /**
140 * Get chapters by the given search term. 152 * Get chapters by the given search term.
141 * @param string $term 153 * @param string $term
142 * @param array $whereTerms 154 * @param array $whereTerms
......