]> BookStack Code Mirror - bookstack/blob - app/Repos/PageRepo.php
897c9ff7707a4c9026465e2f3bff868d8a8d7b92
[bookstack] / app / Repos / PageRepo.php
1 <?php namespace Oxbow\Repos;
2
3
4 use Illuminate\Support\Str;
5 use Oxbow\Page;
6
7 class PageRepo
8 {
9     protected $page;
10
11     /**
12      * PageRepo constructor.
13      * @param $page
14      */
15     public function __construct(Page $page)
16     {
17         $this->page = $page;
18     }
19
20     public function getById($id)
21     {
22         return $this->page->findOrFail($id);
23     }
24
25     public function getAll()
26     {
27         return $this->page->all();
28     }
29
30     public function getBySlug($slug, $bookId)
31     {
32         return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
33     }
34
35     public function newFromInput($input)
36     {
37         $page = $this->page->fill($input);
38         return $page;
39     }
40
41     public function countBySlug($slug, $bookId)
42     {
43         return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
44     }
45
46     public function destroyById($id)
47     {
48         $page = $this->getById($id);
49         $page->delete();
50     }
51
52     public function getBySearch($term)
53     {
54         $terms = explode(' ', trim($term));
55         $query = $this->page;
56         foreach($terms as $term) {
57             $query = $query->where('text', 'like', '%'.$term.'%');
58         }
59         return $query->get();
60     }
61
62     public function getBreadCrumbs($page)
63     {
64         $tree = [];
65         $cPage = $page;
66         while($cPage->parent && $cPage->parent->id !== 0) {
67             $cPage = $cPage->parent;
68             $tree[] = $cPage;
69         }
70         return count($tree) > 0 ? array_reverse($tree) : false;
71     }
72
73     /**
74      * Creates a tree of child pages, Nested by their
75      * set parent pages.
76      * @param $bookId
77      * @param bool $currentPageId
78      * @return array
79      */
80     public function getTreeByBookId($bookId, $currentPageId = false)
81     {
82         $topLevelPages = $this->getTopLevelPages($bookId);
83         $pageTree = [];
84
85         foreach($topLevelPages as $key => $topPage) {
86             $pageTree[$key] = $this->toArrayTree($topPage, $currentPageId);
87         }
88
89         return $pageTree;
90     }
91
92     /**
93      * Creates a page tree array with the supplied page
94      * as the parent of the tree.
95      * @param $page
96      * @param bool $currentPageId
97      * @return mixed
98      */
99     private function toArrayTree($page, $currentPageId = false)
100     {
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);
106         }
107         $cPage['hasChildren'] = count($cPage['pages']) > 0;
108         return $cPage;
109     }
110
111     /**
112      * Gets the pages at the top of the page hierarchy.
113      * @param $bookId
114      */
115     private function getTopLevelPages($bookId)
116     {
117         return $this->page->where('book_id', '=', $bookId)->where('chapter_id', '=', 0)->orderBy('priority')->get();
118     }
119
120     /**
121      * Applies a sort map to all applicable pages.
122      * @param $sortMap
123      * @param $bookId
124      */
125     public function applySortMap($sortMap, $bookId)
126     {
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;
132                 $page->save();
133             }
134         }
135     }
136
137 }