1 <?php namespace BookStack\Repos;
7 use BookStack\Services\RestrictionService;
28 * @var RestrictionService
30 protected $restrictionService;
33 * EntityService constructor.
35 public function __construct()
37 $this->book = app(Book::class);
38 $this->chapter = app(Chapter::class);
39 $this->page = app(Page::class);
40 $this->restrictionService = app(RestrictionService::class);
44 * Get the latest books added to the system.
47 * @param bool $additionalQuery
50 public function getRecentlyCreatedBooks($count = 20, $page = 0, $additionalQuery = false)
52 $query = $this->restrictionService->enforceBookRestrictions($this->book)
53 ->orderBy('created_at', 'desc');
54 if ($additionalQuery !== false && is_callable($additionalQuery)) {
55 $additionalQuery($query);
57 return $query->skip($page * $count)->take($count)->get();
61 * Get the most recently updated books.
66 public function getRecentlyUpdatedBooks($count = 20, $page = 0)
68 return $this->restrictionService->enforceBookRestrictions($this->book)
69 ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
73 * Get the latest pages added to the system.
76 * @param bool $additionalQuery
79 public function getRecentlyCreatedPages($count = 20, $page = 0, $additionalQuery = false)
81 $query = $this->restrictionService->enforcePageRestrictions($this->page)
82 ->orderBy('created_at', 'desc');
83 if ($additionalQuery !== false && is_callable($additionalQuery)) {
84 $additionalQuery($query);
86 return $query->skip($page * $count)->take($count)->get();
90 * Get the latest chapters added to the system.
93 * @param bool $additionalQuery
96 public function getRecentlyCreatedChapters($count = 20, $page = 0, $additionalQuery = false)
98 $query = $this->restrictionService->enforceChapterRestrictions($this->chapter)
99 ->orderBy('created_at', 'desc');
100 if ($additionalQuery !== false && is_callable($additionalQuery)) {
101 $additionalQuery($query);
103 return $query->skip($page * $count)->take($count)->get();
107 * Get the most recently updated pages.
112 public function getRecentlyUpdatedPages($count = 20, $page = 0)
114 return $this->restrictionService->enforcePageRestrictions($this->page)
115 ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
119 * Updates entity restrictions from a request
121 * @param Entity $entity
123 public function updateRestrictionsFromRequest($request, Entity $entity)
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)
141 * Prepare a string of search terms by turning
142 * it into an array of terms.
143 * Keeps quoted terms together.
147 protected function prepareSearchTerms($termString)
149 preg_match_all('/"(.*?)"/', $termString, $matches);
150 if (count($matches[1]) > 0) {
151 $terms = $matches[1];
152 $termString = trim(preg_replace('/"(.*?)"/', '', $termString));
156 if (!empty($termString)) $terms = array_merge($terms, explode(' ', $termString));