]> BookStack Code Mirror - bookstack/blobdiff - app/Repos/ChapterRepo.php
Merge branch 'custom_role_system'
[bookstack] / app / Repos / ChapterRepo.php
index b423ee8ba9d0062994a5cf0691b83e23d65bf535..6868bbf89eb4806caa9d8c2f8ab6cdf905076f3a 100644 (file)
-<?php namespace Oxbow\Repos;
+<?php namespace BookStack\Repos;
 
 
+use Activity;
+use BookStack\Exceptions\NotFoundException;
+use BookStack\Services\RestrictionService;
 use Illuminate\Support\Str;
-use Oxbow\Chapter;
+use BookStack\Chapter;
 
 class ChapterRepo
 {
 
     protected $chapter;
+    protected $restrictionService;
 
     /**
      * ChapterRepo constructor.
-     * @param $chapter
+     * @param Chapter $chapter
+     * @param RestrictionService $restrictionService
      */
-    public function __construct(Chapter $chapter)
+    public function __construct(Chapter $chapter, RestrictionService $restrictionService)
     {
         $this->chapter = $chapter;
+        $this->restrictionService = $restrictionService;
+    }
+
+    /**
+     * Base query for getting chapters, Takes restrictions into account.
+     * @return mixed
+     */
+    private function chapterQuery()
+    {
+        return $this->restrictionService->enforceChapterRestrictions($this->chapter, 'view');
     }
 
+    /**
+     * Check if an id exists.
+     * @param $id
+     * @return bool
+     */
     public function idExists($id)
     {
-        return $this->chapter->where('id', '=', $id)->count() > 0;
+        return $this->chapterQuery()->where('id', '=', $id)->count() > 0;
     }
 
+    /**
+     * Get a chapter by a specific id.
+     * @param $id
+     * @return mixed
+     */
     public function getById($id)
     {
-        return $this->chapter->findOrFail($id);
+        return $this->chapterQuery()->findOrFail($id);
     }
 
+    /**
+     * Get all chapters.
+     * @return \Illuminate\Database\Eloquent\Collection|static[]
+     */
     public function getAll()
     {
-        return $this->chapter->all();
+        return $this->chapterQuery()->all();
     }
 
+    /**
+     * Get a chapter that has the given slug within the given book.
+     * @param $slug
+     * @param $bookId
+     * @return mixed
+     * @throws NotFoundException
+     */
     public function getBySlug($slug, $bookId)
     {
-        return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
+        $chapter = $this->chapterQuery()->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
+        if ($chapter === null) throw new NotFoundException('Chapter not found');
+        return $chapter;
     }
 
+    /**
+     * Get the child items for a chapter
+     * @param Chapter $chapter
+     */
+    public function getChildren(Chapter $chapter)
+    {
+        return $this->restrictionService->enforcePageRestrictions($chapter->pages())->get();
+    }
+
+    /**
+     * Create a new chapter from request input.
+     * @param $input
+     * @return $this
+     */
     public function newFromInput($input)
     {
         return $this->chapter->fill($input);
     }
 
-    public function destroyById($id)
+    /**
+     * Destroy a chapter and its relations by providing its slug.
+     * @param Chapter $chapter
+     */
+    public function destroy(Chapter $chapter)
     {
-        $page = $this->getById($id);
-        $page->delete();
+        if (count($chapter->pages) > 0) {
+            foreach ($chapter->pages as $page) {
+                $page->chapter_id = 0;
+                $page->save();
+            }
+        }
+        Activity::removeEntity($chapter);
+        $chapter->views()->delete();
+        $chapter->restrictions()->delete();
+        $chapter->delete();
     }
 
+    /**
+     * Check if a chapter's slug exists.
+     * @param            $slug
+     * @param            $bookId
+     * @param bool|false $currentId
+     * @return bool
+     */
     public function doesSlugExist($slug, $bookId, $currentId = false)
     {
         $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
-        if($currentId) {
+        if ($currentId) {
             $query = $query->where('id', '!=', $currentId);
         }
         return $query->count() > 0;
     }
 
+    /**
+     * Finds a suitable slug for the provided name.
+     * Checks database to prevent duplicate slugs.
+     * @param            $name
+     * @param            $bookId
+     * @param bool|false $currentId
+     * @return string
+     */
     public function findSuitableSlug($name, $bookId, $currentId = false)
     {
         $slug = Str::slug($name);
-        while($this->doesSlugExist($slug, $bookId, $currentId)) {
+        while ($this->doesSlugExist($slug, $bookId, $currentId)) {
             $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
         }
         return $slug;
     }
 
-    public function getBySearch($term, $whereTerms = [])
+    /**
+     * Get chapters by the given search term.
+     * @param       $term
+     * @param array $whereTerms
+     * @param int $count
+     * @param array $paginationAppends
+     * @return mixed
+     */
+    public function getBySearch($term, $whereTerms = [], $count = 20, $paginationAppends = [])
     {
-        $terms = explode(' ', preg_quote(trim($term)));
-        $chapters = $this->chapter->fullTextSearch(['name', 'description'], $terms, $whereTerms);
-        $words = join('|', $terms);
+        preg_match_all('/"(.*?)"/', $term, $matches);
+        if (count($matches[1]) > 0) {
+            $terms = $matches[1];
+            $term = trim(preg_replace('/"(.*?)"/', '', $term));
+        } else {
+            $terms = [];
+        }
+        if (!empty($term)) {
+            $terms = array_merge($terms, explode(' ', $term));
+        }
+        $chapters = $this->restrictionService->enforceChapterRestrictions($this->chapter->fullTextSearchQuery(['name', 'description'], $terms, $whereTerms))
+            ->paginate($count)->appends($paginationAppends);
+        $words = join('|', explode(' ', preg_quote(trim($term), '/')));
         foreach ($chapters as $chapter) {
             //highlight
             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $chapter->getExcerpt(100));
@@ -80,4 +177,45 @@ class ChapterRepo
         return $chapters;
     }
 
+    /**
+     * Changes the book relation of this chapter.
+     * @param         $bookId
+     * @param Chapter $chapter
+     * @return Chapter
+     */
+    public function changeBook($bookId, Chapter $chapter)
+    {
+        $chapter->book_id = $bookId;
+        foreach ($chapter->activity as $activity) {
+            $activity->book_id = $bookId;
+            $activity->save();
+        }
+        $chapter->slug = $this->findSuitableSlug($chapter->name, $bookId, $chapter->id);
+        $chapter->save();
+        return $chapter;
+    }
+
+    /**
+     * Updates pages restrictions from a request
+     * @param $request
+     * @param $chapter
+     */
+    public function updateRestrictionsFromRequest($request, $chapter)
+    {
+        // TODO - extract into shared repo
+        $chapter->restricted = $request->has('restricted') && $request->get('restricted') === 'true';
+        $chapter->restrictions()->delete();
+        if ($request->has('restrictions')) {
+            foreach($request->get('restrictions') as $roleId => $restrictions) {
+                foreach ($restrictions as $action => $value) {
+                    $chapter->restrictions()->create([
+                        'role_id' => $roleId,
+                        'action'  => strtolower($action)
+                    ]);
+                }
+            }
+        }
+        $chapter->save();
+    }
+
 }
\ No newline at end of file