1 <?php namespace BookStack\Repos;
7 use BookStack\Services\PermissionService;
29 * @var PermissionService
31 protected $permissionService;
34 * EntityService constructor.
36 public function __construct()
38 $this->book = app(Book::class);
39 $this->chapter = app(Chapter::class);
40 $this->page = app(Page::class);
41 $this->permissionService = app(PermissionService::class);
45 * Get the latest books added to the system.
48 * @param bool $additionalQuery
51 public function getRecentlyCreatedBooks($count = 20, $page = 0, $additionalQuery = false)
53 $query = $this->permissionService->enforceBookRestrictions($this->book)
54 ->orderBy('created_at', 'desc');
55 if ($additionalQuery !== false && is_callable($additionalQuery)) {
56 $additionalQuery($query);
58 return $query->skip($page * $count)->take($count)->get();
62 * Get the most recently updated books.
67 public function getRecentlyUpdatedBooks($count = 20, $page = 0)
69 return $this->permissionService->enforceBookRestrictions($this->book)
70 ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
74 * Get the latest pages added to the system.
77 * @param bool $additionalQuery
80 public function getRecentlyCreatedPages($count = 20, $page = 0, $additionalQuery = false)
82 $query = $this->permissionService->enforcePageRestrictions($this->page)
83 ->orderBy('created_at', 'desc')->where('draft', '=', false);
84 if ($additionalQuery !== false && is_callable($additionalQuery)) {
85 $additionalQuery($query);
87 return $query->with('book')->skip($page * $count)->take($count)->get();
91 * Get the latest chapters added to the system.
94 * @param bool $additionalQuery
97 public function getRecentlyCreatedChapters($count = 20, $page = 0, $additionalQuery = false)
99 $query = $this->permissionService->enforceChapterRestrictions($this->chapter)
100 ->orderBy('created_at', 'desc');
101 if ($additionalQuery !== false && is_callable($additionalQuery)) {
102 $additionalQuery($query);
104 return $query->skip($page * $count)->take($count)->get();
108 * Get the most recently updated pages.
113 public function getRecentlyUpdatedPages($count = 20, $page = 0)
115 return $this->permissionService->enforcePageRestrictions($this->page)
116 ->where('draft', '=', false)
117 ->orderBy('updated_at', 'desc')->with('book')->skip($page * $count)->take($count)->get();
121 * Get draft pages owned by the current user.
125 public function getUserDraftPages($count = 20, $page = 0)
127 $user = auth()->user();
128 return $this->page->where('draft', '=', true)
129 ->where('created_by', '=', $user->id)
130 ->orderBy('updated_at', 'desc')
131 ->skip($count * $page)->take($count)->get();
135 * Updates entity restrictions from a request
137 * @param Entity $entity
139 public function updateEntityPermissionsFromRequest($request, Entity $entity)
141 $entity->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
142 $entity->permissions()->delete();
143 if ($request->has('restrictions')) {
144 foreach ($request->get('restrictions') as $roleId => $restrictions) {
145 foreach ($restrictions as $action => $value) {
146 $entity->permissions()->create([
147 'role_id' => $roleId,
148 'action' => strtolower($action)
154 $this->permissionService->buildJointPermissionsForEntity($entity);
158 * Prepare a string of search terms by turning
159 * it into an array of terms.
160 * Keeps quoted terms together.
164 protected function prepareSearchTerms($termString)
166 preg_match_all('/"(.*?)"/', $termString, $matches);
167 if (count($matches[1]) > 0) {
168 $terms = $matches[1];
169 $termString = trim(preg_replace('/"(.*?)"/', '', $termString));
173 if (!empty($termString)) $terms = array_merge($terms, explode(' ', $termString));