]> BookStack Code Mirror - bookstack/blob - app/Repos/EntityRepo.php
ea2805855452cf95111b8632cca4a4e3c5bca715
[bookstack] / app / Repos / EntityRepo.php
1 <?php namespace BookStack\Repos;
2
3 use BookStack\Book;
4 use BookStack\Chapter;
5 use BookStack\Entity;
6 use BookStack\Page;
7 use BookStack\Services\RestrictionService;
8
9 class EntityRepo
10 {
11
12     /**
13      * @var Book $book
14      */
15     public $book;
16
17     /**
18      * @var Chapter
19      */
20     public $chapter;
21
22     /**
23      * @var Page
24      */
25     public $page;
26
27     /**
28      * @var RestrictionService
29      */
30     protected $restrictionService;
31
32     /**
33      * EntityService constructor.
34      */
35     public function __construct()
36     {
37         $this->book = app(Book::class);
38         $this->chapter = app(Chapter::class);
39         $this->page = app(Page::class);
40         $this->restrictionService = app(RestrictionService::class);
41     }
42
43     /**
44      * Get the latest books added to the system.
45      * @param int $count
46      * @param int $page
47      * @param bool $additionalQuery
48      * @return
49      */
50     public function getRecentlyCreatedBooks($count = 20, $page = 0, $additionalQuery = false)
51     {
52         $query = $this->restrictionService->enforceBookRestrictions($this->book)
53             ->orderBy('created_at', 'desc');
54         if ($additionalQuery !== false && is_callable($additionalQuery)) {
55             $additionalQuery($query);
56         }
57         return $query->skip($page * $count)->take($count)->get();
58     }
59
60     /**
61      * Get the most recently updated books.
62      * @param $count
63      * @param int $page
64      * @return mixed
65      */
66     public function getRecentlyUpdatedBooks($count = 20, $page = 0)
67     {
68         return $this->restrictionService->enforceBookRestrictions($this->book)
69             ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
70     }
71
72     /**
73      * Get the latest pages added to the system.
74      * @param int $count
75      * @param int $page
76      * @param bool $additionalQuery
77      * @return
78      */
79     public function getRecentlyCreatedPages($count = 20, $page = 0, $additionalQuery = false)
80     {
81         $query = $this->restrictionService->enforcePageRestrictions($this->page)
82             ->orderBy('created_at', 'desc');
83         if ($additionalQuery !== false && is_callable($additionalQuery)) {
84             $additionalQuery($query);
85         }
86         return $query->skip($page * $count)->take($count)->get();
87     }
88
89     /**
90      * Get the latest chapters added to the system.
91      * @param int $count
92      * @param int $page
93      * @param bool $additionalQuery
94      * @return
95      */
96     public function getRecentlyCreatedChapters($count = 20, $page = 0, $additionalQuery = false)
97     {
98         $query = $this->restrictionService->enforceChapterRestrictions($this->chapter)
99             ->orderBy('created_at', 'desc');
100         if ($additionalQuery !== false && is_callable($additionalQuery)) {
101             $additionalQuery($query);
102         }
103         return $query->skip($page * $count)->take($count)->get();
104     }
105
106     /**
107      * Get the most recently updated pages.
108      * @param $count
109      * @param int $page
110      * @return mixed
111      */
112     public function getRecentlyUpdatedPages($count = 20, $page = 0)
113     {
114         return $this->restrictionService->enforcePageRestrictions($this->page)
115             ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
116     }
117
118     /**
119      * Updates entity restrictions from a request
120      * @param $request
121      * @param Entity $entity
122      */
123     public function updateRestrictionsFromRequest($request, Entity $entity)
124     {
125         $entity->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
126         $entity->restrictions()->delete();
127         if ($request->has('restrictions')) {
128             foreach ($request->get('restrictions') as $roleId => $restrictions) {
129                 foreach ($restrictions as $action => $value) {
130                     $entity->restrictions()->create([
131                         'role_id' => $roleId,
132                         'action'  => strtolower($action)
133                     ]);
134                 }
135             }
136         }
137         $entity->save();
138     }
139
140     /**
141      * Prepare a string of search terms by turning
142      * it into an array of terms.
143      * Keeps quoted terms together.
144      * @param $termString
145      * @return array
146      */
147     protected function prepareSearchTerms($termString)
148     {
149         preg_match_all('/"(.*?)"/', $termString, $matches);
150         if (count($matches[1]) > 0) {
151             $terms = $matches[1];
152             $termString = trim(preg_replace('/"(.*?)"/', '', $termString));
153         } else {
154             $terms = [];
155         }
156         if (!empty($termString)) $terms = array_merge($terms, explode(' ', $termString));
157         return $terms;
158     }
159
160
161 }