pages() public method

Get all pages within this book.
public pages ( ) : Illuminate\Database\Eloquent\Relations\HasMany
return Illuminate\Database\Eloquent\Relations\HasMany
示例#1
0
 /**
  * Get all child objects of a book.
  * Returns a sorted collection of Pages and Chapters.
  * Loads the bookslug onto child elements to prevent access database access for getting the slug.
  * @param Book $book
  * @return mixed
  */
 public function getChildren(Book $book)
 {
     $pages = $book->pages()->where('chapter_id', '=', 0)->get();
     $chapters = $book->chapters()->with('pages')->get();
     $children = $pages->merge($chapters);
     $bookSlug = $book->slug;
     $children->each(function ($child) use($bookSlug) {
         $child->setAttribute('bookSlug', $bookSlug);
         if ($child->isA('chapter')) {
             $child->pages->each(function ($page) use($bookSlug) {
                 $page->setAttribute('bookSlug', $bookSlug);
             });
         }
     });
     return $children->sortBy('priority');
 }
示例#2
0
 /**
  * Save a new page into the system.
  * Input validation must be done beforehand.
  * @param array $input
  * @param Book  $book
  * @param int   $chapterId
  * @return Page
  */
 public function saveNew(array $input, Book $book, $chapterId = null)
 {
     $page = $this->newFromInput($input);
     $page->slug = $this->findSuitableSlug($page->name, $book->id);
     if ($chapterId) {
         $page->chapter_id = $chapterId;
     }
     $page->html = $this->formatHtml($input['html']);
     $page->text = strip_tags($page->html);
     $page->created_by = auth()->user()->id;
     $page->updated_by = auth()->user()->id;
     $book->pages()->save($page);
     return $page;
 }
示例#3
0
 /**
  * Get all child objects of a book.
  * Returns a sorted collection of Pages and Chapters.
  * Loads the book slug onto child elements to prevent access database access for getting the slug.
  * @param Book $book
  * @param bool $filterDrafts
  * @return mixed
  */
 public function getChildren(Book $book, $filterDrafts = false)
 {
     $pageQuery = $book->pages()->where('chapter_id', '=', 0);
     $pageQuery = $this->permissionService->enforcePageRestrictions($pageQuery, 'view');
     if ($filterDrafts) {
         $pageQuery = $pageQuery->where('draft', '=', false);
     }
     $pages = $pageQuery->get();
     $chapterQuery = $book->chapters()->with(['pages' => function ($query) use($filterDrafts) {
         $this->permissionService->enforcePageRestrictions($query, 'view');
         if ($filterDrafts) {
             $query->where('draft', '=', false);
         }
     }]);
     $chapterQuery = $this->permissionService->enforceChapterRestrictions($chapterQuery, 'view');
     $chapters = $chapterQuery->get();
     $children = $pages->values();
     foreach ($chapters as $chapter) {
         $children->push($chapter);
     }
     $bookSlug = $book->slug;
     $children->each(function ($child) use($bookSlug) {
         $child->setAttribute('bookSlug', $bookSlug);
         if ($child->isA('chapter')) {
             $child->pages->each(function ($page) use($bookSlug) {
                 $page->setAttribute('bookSlug', $bookSlug);
             });
             $child->pages = $child->pages->sortBy(function ($child, $key) {
                 $score = $child->priority;
                 if ($child->draft) {
                     $score -= 100;
                 }
                 return $score;
             });
         }
     });
     // Sort items with drafts first then by priority.
     return $children->sortBy(function ($child, $key) {
         $score = $child->priority;
         if ($child->isA('page') && $child->draft) {
             $score -= 100;
         }
         return $score;
     });
 }
示例#4
0
 /**
  * Get a new draft page instance.
  * @param Book $book
  * @param Chapter|bool $chapter
  * @return static
  */
 public function getDraftPage(Book $book, $chapter = false)
 {
     $page = $this->page->newInstance();
     $page->name = 'New Page';
     $page->created_by = auth()->user()->id;
     $page->updated_by = auth()->user()->id;
     $page->draft = true;
     if ($chapter) {
         $page->chapter_id = $chapter->id;
     }
     $book->pages()->save($page);
     $this->permissionService->buildJointPermissionsForEntity($page);
     return $page;
 }