Skip to content

Commit

Permalink
Made new pages in chapters have a better inital priority
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Apr 7, 2016
1 parent cbff2c6 commit afc3583
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,19 @@ public function store(Request $request, $bookSlug, $pageId)

$input = $request->all();
$book = $this->bookRepo->getBySlug($bookSlug);
$input['priority'] = $this->bookRepo->getNewPriority($book);

$draftPage = $this->pageRepo->getById($pageId, true);

$chapterId = $draftPage->chapter_id;
$parent = $chapterId !== 0 ? $this->chapterRepo->getById($chapterId) : $book;
$this->checkOwnablePermission('page-create', $parent);

if ($parent->isA('chapter')) {
$input['priority'] = $this->chapterRepo->getNewPriority($parent);
} else {
$input['priority'] = $this->bookRepo->getNewPriority($parent);
}

$page = $this->pageRepo->publishDraft($draftPage, $input);

Activity::add($page, 'page_create', $book->id);
Expand Down
12 changes: 12 additions & 0 deletions app/Repos/ChapterRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ public function findSuitableSlug($name, $bookId, $currentId = false)
return $slug;
}

/**
* Get a new priority value for a new page to be added
* to the given chapter.
* @param Chapter $chapter
* @return int
*/
public function getNewPriority(Chapter $chapter)
{
$lastPage = $chapter->pages->last();
return $lastPage !== null ? $lastPage->priority + 1 : 0;
}

/**
* Get chapters by the given search term.
* @param string $term
Expand Down

0 comments on commit afc3583

Please sign in to comment.