]> BookStack Code Mirror - bookstack/blob - app/Repos/PageRepo.php
7ce10498da3d0961f33a076d9ce9b24cf877caf9
[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      * Gets the pages at the top of the page hierarchy.
75      * @param $bookId
76      */
77     private function getTopLevelPages($bookId)
78     {
79         return $this->page->where('book_id', '=', $bookId)->where('chapter_id', '=', 0)->orderBy('priority')->get();
80     }
81
82     /**
83      * Applies a sort map to all applicable pages.
84      * @param $sortMap
85      * @param $bookId
86      */
87     public function applySortMap($sortMap, $bookId)
88     {
89         foreach($sortMap as $index => $map) {
90             $page = $this->getById($map->id);
91             if($page->book_id === $bookId) {
92                 $page->page_id = $map->parent;
93                 $page->priority = $index;
94                 $page->save();
95             }
96         }
97     }
98
99 }