]> BookStack Code Mirror - bookstack/blob - app/Repos/PageRepo.php
Improved empty lists. Fixes #10.
[bookstack] / app / Repos / PageRepo.php
1 <?php namespace Oxbow\Repos;
2
3
4 use Illuminate\Support\Facades\Auth;
5 use Illuminate\Support\Str;
6 use Oxbow\Page;
7 use Oxbow\PageRevision;
8
9 class PageRepo
10 {
11     protected $page;
12     protected $pageRevision;
13
14     /**
15      * PageRepo constructor.
16      * @param Page $page
17      * @param PageRevision $pageRevision
18      */
19     public function __construct(Page $page, PageRevision $pageRevision)
20     {
21         $this->page = $page;
22         $this->pageRevision = $pageRevision;
23     }
24
25     public function idExists($id)
26     {
27         return $this->page->where('page_id', '=', $id)->count() > 0;
28     }
29
30     public function getById($id)
31     {
32         return $this->page->findOrFail($id);
33     }
34
35     public function getAll()
36     {
37         return $this->page->all();
38     }
39
40     public function getBySlug($slug, $bookId)
41     {
42         return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
43     }
44
45     public function newFromInput($input)
46     {
47         $page = $this->page->fill($input);
48         return $page;
49     }
50
51     public function countBySlug($slug, $bookId)
52     {
53         return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
54     }
55
56     public function destroyById($id)
57     {
58         $page = $this->getById($id);
59         $page->delete();
60     }
61
62     public function getBySearch($term)
63     {
64         $terms = explode(' ', trim($term));
65         $query = $this->page;
66         foreach($terms as $term) {
67             $query = $query->where('text', 'like', '%'.$term.'%');
68         }
69         return $query->get();
70     }
71
72     /**
73      * Updates a page with any fillable data and saves it into the database.
74      * @param Page $page
75      * @param $book_id
76      * @param $data
77      * @return Page
78      */
79     public function updatePage(Page $page, $book_id, $data)
80     {
81         $page->fill($data);
82         $page->slug = $this->findSuitableSlug($page->name, $book_id, $page->id);
83         $page->text = strip_tags($page->html);
84         $page->updated_by = Auth::user()->id;
85         $page->save();
86         $this->saveRevision($page);
87         return $page;
88     }
89
90     /**
91      * Saves a page revision into the system.
92      * @param Page $page
93      * @return $this
94      */
95     public function saveRevision(Page $page)
96     {
97         $lastRevision = $this->getLastRevision($page);
98         if($lastRevision && ($lastRevision->html === $page->html && $lastRevision->name === $page->name)) {
99             return $page;
100         }
101         $revision = $this->pageRevision->fill($page->toArray());
102         $revision->page_id = $page->id;
103         $revision->created_by = Auth::user()->id;
104         $revision->save();
105         // Clear old revisions
106         if($this->pageRevision->where('page_id', '=', $page->id)->count() > 50) {
107             $this->pageRevision->where('page_id', '=', $page->id)
108                 ->orderBy('created_at', 'desc')->skip(50)->take(5)->delete();
109         }
110         return $revision;
111     }
112
113     /**
114      * Gets the most recent revision for a page.
115      * @param Page $page
116      * @return mixed
117      */
118     public function getLastRevision(Page $page)
119     {
120         return $this->pageRevision->where('page_id', '=', $page->id)
121             ->orderBy('created_at', 'desc')->first();
122     }
123
124     /**
125      * Gets a single revision via it's id.
126      * @param $id
127      * @return mixed
128      */
129     public function getRevisionById($id)
130     {
131         return $this->pageRevision->findOrFail($id);
132     }
133
134     /**
135      * Checks if a slug exists within a book already.
136      * @param $slug
137      * @param $bookId
138      * @param bool|false $currentId
139      * @return bool
140      */
141     public function doesSlugExist($slug, $bookId, $currentId = false)
142     {
143         $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
144         if($currentId) {
145             $query = $query->where('id', '!=', $currentId);
146         }
147         return $query->count() > 0;
148     }
149
150     /**
151      * Gets a suitable slug for the resource
152      *
153      * @param $name
154      * @param $bookId
155      * @param bool|false $currentId
156      * @return string
157      */
158     public function findSuitableSlug($name, $bookId, $currentId = false)
159     {
160         $slug = Str::slug($name);
161         while($this->doesSlugExist($slug, $bookId, $currentId)) {
162             $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
163         }
164         return $slug;
165     }
166
167
168 }