1 <?php namespace Oxbow\Repos;
4 use Illuminate\Support\Str;
12 * PageRepo constructor.
15 public function __construct(Page $page)
20 public function getById($id)
22 return $this->page->findOrFail($id);
25 public function getAll()
27 return $this->page->all();
30 public function getBySlug($slug, $bookId)
32 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
35 public function newFromInput($input)
37 $page = $this->page->fill($input);
41 public function countBySlug($slug, $bookId)
43 return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
46 public function destroyById($id)
48 $page = $this->getById($id);
52 public function getBySearch($term)
54 $terms = explode(' ', trim($term));
56 foreach($terms as $term) {
57 $query = $query->where('text', 'like', '%'.$term.'%');
62 public function getBreadCrumbs($page)
66 while($cPage->parent && $cPage->parent->id !== 0) {
67 $cPage = $cPage->parent;
70 return count($tree) > 0 ? array_reverse($tree) : false;
74 * Creates a tree of child pages, Nested by their
77 * @param bool $currentPageId
80 public function getTreeByBookId($bookId, $currentPageId = false)
82 $topLevelPages = $this->getTopLevelPages($bookId);
85 foreach($topLevelPages as $key => $topPage) {
86 $pageTree[$key] = $this->toArrayTree($topPage, $currentPageId);
93 * Creates a page tree array with the supplied page
94 * as the parent of the tree.
96 * @param bool $currentPageId
99 private function toArrayTree($page, $currentPageId = false)
101 $cPage = $page->toSimpleArray();
102 $cPage['current'] = ($currentPageId !== false && $cPage['id'] === $currentPageId);
103 $cPage['pages'] = [];
104 foreach($page->children as $key => $childPage) {
105 $cPage['pages'][$key] = $this->toArrayTree($childPage, $currentPageId);
107 $cPage['hasChildren'] = count($cPage['pages']) > 0;
112 * Gets the pages at the top of the page hierarchy.
115 private function getTopLevelPages($bookId)
117 return $this->page->where('book_id', '=', $bookId)->where('chapter_id', '=', 0)->orderBy('priority')->get();
121 * Applies a sort map to all applicable pages.
125 public function applySortMap($sortMap, $bookId)
127 foreach($sortMap as $index => $map) {
128 $page = $this->getById($map->id);
129 if($page->book_id === $bookId) {
130 $page->page_id = $map->parent;
131 $page->priority = $index;