]> BookStack Code Mirror - bookstack/blob - app/Repos/EntityRepo.php
Merge branch 'master' into nwalke-update_site_color
[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 $count
46      * @param $page
47      */
48     public function getRecentlyCreatedBooks($count = 20, $page = 0)
49     {
50         return $this->restrictionService->enforceBookRestrictions($this->book)
51             ->orderBy('created_at', 'desc')->skip($page * $count)->take($count)->get();
52     }
53
54     /**
55      * Get the most recently updated books.
56      * @param $count
57      * @param int $page
58      * @return mixed
59      */
60     public function getRecentlyUpdatedBooks($count = 20, $page = 0)
61     {
62         return $this->restrictionService->enforceBookRestrictions($this->book)
63             ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
64     }
65
66     /**
67      * Get the latest pages added to the system.
68      * @param $count
69      * @param $page
70      */
71     public function getRecentlyCreatedPages($count = 20, $page = 0)
72     {
73         return $this->restrictionService->enforcePageRestrictions($this->page)
74             ->orderBy('created_at', 'desc')->skip($page * $count)->take($count)->get();
75     }
76
77     /**
78      * Get the most recently updated pages.
79      * @param $count
80      * @param int $page
81      * @return mixed
82      */
83     public function getRecentlyUpdatedPages($count = 20, $page = 0)
84     {
85         return $this->restrictionService->enforcePageRestrictions($this->page)
86             ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
87     }
88
89     /**
90      * Updates entity restrictions from a request
91      * @param $request
92      * @param Entity $entity
93      */
94     public function updateRestrictionsFromRequest($request, Entity $entity)
95     {
96         $entity->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
97         $entity->restrictions()->delete();
98         if ($request->has('restrictions')) {
99             foreach ($request->get('restrictions') as $roleId => $restrictions) {
100                 foreach ($restrictions as $action => $value) {
101                     $entity->restrictions()->create([
102                         'role_id' => $roleId,
103                         'action' => strtolower($action)
104                     ]);
105                 }
106             }
107         }
108         $entity->save();
109     }
110
111     /**
112      * Prepare a string of search terms by turning
113      * it into an array of terms.
114      * Keeps quoted terms together.
115      * @param $termString
116      * @return array
117      */
118     protected function prepareSearchTerms($termString)
119     {
120         preg_match_all('/"(.*?)"/', $termString, $matches);
121         if (count($matches[1]) > 0) {
122             $terms = $matches[1];
123             $termString = trim(preg_replace('/"(.*?)"/', '', $termString));
124         } else {
125             $terms = [];
126         }
127         if (!empty($termString)) $terms = array_merge($terms, explode(' ', $termString));
128         return $terms;
129     }
130
131
132 }