]> BookStack Code Mirror - bookstack/blob - app/Repos/PageRepo.php
Fix for getting book items
[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 idExists($id)
21     {
22         return $this->page->where('page_id', '=', $id)->count() > 0;
23     }
24
25     public function getById($id)
26     {
27         return $this->page->findOrFail($id);
28     }
29
30     public function getAll()
31     {
32         return $this->page->all();
33     }
34
35     public function getBySlug($slug, $bookId)
36     {
37         return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
38     }
39
40     public function newFromInput($input)
41     {
42         $page = $this->page->fill($input);
43         return $page;
44     }
45
46     public function countBySlug($slug, $bookId)
47     {
48         return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
49     }
50
51     public function destroyById($id)
52     {
53         $page = $this->getById($id);
54         $page->delete();
55     }
56
57     public function getBySearch($term)
58     {
59         $terms = explode(' ', trim($term));
60         $query = $this->page;
61         foreach($terms as $term) {
62             $query = $query->where('text', 'like', '%'.$term.'%');
63         }
64         return $query->get();
65     }
66
67     /**
68      * Checks if a slug exists within a book already.
69      * @param $slug
70      * @param $bookId
71      * @param bool|false $currentId
72      * @return bool
73      */
74     public function doesSlugExist($slug, $bookId, $currentId = false)
75     {
76         $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId);
77         if($currentId) {
78             $query = $query->where('id', '!=', $currentId);
79         }
80         return $query->count() > 0;
81     }
82
83     /**
84      * Gets a suitable slug for the resource
85      *
86      * @param $name
87      * @param $bookId
88      * @param bool|false $currentId
89      * @return string
90      */
91     public function findSuitableSlug($name, $bookId, $currentId = false)
92     {
93         $slug = Str::slug($name);
94         while($this->doesSlugExist($slug, $bookId, $currentId)) {
95             $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
96         }
97         return $slug;
98     }
99
100
101 }