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.
48 public function getRecentlyCreatedBooks($count = 20, $page = 0)
50 return $this->restrictionService->enforceBookRestrictions($this->book)
51 ->orderBy('created_at', 'desc')->skip($page * $count)->take($count)->get();
55 * Get the most recently updated books.
60 public function getRecentlyUpdatedBooks($count = 20, $page = 0)
62 return $this->restrictionService->enforceBookRestrictions($this->book)
63 ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
67 * Get the latest pages added to the system.
71 public function getRecentlyCreatedPages($count = 20, $page = 0)
73 return $this->restrictionService->enforcePageRestrictions($this->page)
74 ->orderBy('created_at', 'desc')->skip($page * $count)->take($count)->get();
78 * Get the most recently updated pages.
83 public function getRecentlyUpdatedPages($count = 20, $page = 0)
85 return $this->restrictionService->enforcePageRestrictions($this->page)
86 ->orderBy('updated_at', 'desc')->skip($page * $count)->take($count)->get();
90 * Updates entity restrictions from a request
92 * @param Entity $entity
94 public function updateRestrictionsFromRequest($request, Entity $entity)
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)
112 * Prepare a string of search terms by turning
113 * it into an array of terms.
114 * Keeps quoted terms together.
118 protected function prepareSearchTerms($termString)
120 preg_match_all('/"(.*?)"/', $termString, $matches);
121 if (count($matches[1]) > 0) {
122 $terms = $matches[1];
123 $termString = trim(preg_replace('/"(.*?)"/', '', $termString));
127 if (!empty($termString)) $terms = array_merge($terms, explode(' ', $termString));