]> BookStack Code Mirror - bookstack/blob - app/Repos/ChapterRepo.php
Accounted for non-existant entities
[bookstack] / app / Repos / ChapterRepo.php
1 <?php namespace BookStack\Repos;
2
3
4 use Activity;
5 use Illuminate\Support\Str;
6 use BookStack\Chapter;
7
8 class ChapterRepo
9 {
10
11     protected $chapter;
12
13     /**
14      * ChapterRepo constructor.
15      * @param $chapter
16      */
17     public function __construct(Chapter $chapter)
18     {
19         $this->chapter = $chapter;
20     }
21
22     /**
23      * Check if an id exists.
24      * @param $id
25      * @return bool
26      */
27     public function idExists($id)
28     {
29         return $this->chapter->where('id', '=', $id)->count() > 0;
30     }
31
32     /**
33      * Get a chapter by a specific id.
34      * @param $id
35      * @return mixed
36      */
37     public function getById($id)
38     {
39         return $this->chapter->findOrFail($id);
40     }
41
42     /**
43      * Get all chapters.
44      * @return \Illuminate\Database\Eloquent\Collection|static[]
45      */
46     public function getAll()
47     {
48         return $this->chapter->all();
49     }
50
51     /**
52      * Get a chapter that has the given slug within the given book.
53      * @param $slug
54      * @param $bookId
55      * @return mixed
56      */
57     public function getBySlug($slug, $bookId)
58     {
59         $chapter = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
60         if ($chapter === null) abort(404);
61         return $chapter;
62     }
63
64     /**
65      * Create a new chapter from request input.
66      * @param $input
67      * @return $this
68      */
69     public function newFromInput($input)
70     {
71         return $this->chapter->fill($input);
72     }
73
74     /**
75      * Destroy a chapter and its relations by providing its slug.
76      * @param Chapter $chapter
77      */
78     public function destroy(Chapter $chapter)
79     {
80         if (count($chapter->pages) > 0) {
81             foreach ($chapter->pages as $page) {
82                 $page->chapter_id = 0;
83                 $page->save();
84             }
85         }
86         Activity::removeEntity($chapter);
87         $chapter->views()->delete();
88         $chapter->delete();
89     }
90
91     /**
92      * Check if a chapter's slug exists.
93      * @param            $slug
94      * @param            $bookId
95      * @param bool|false $currentId
96      * @return bool
97      */
98     public function doesSlugExist($slug, $bookId, $currentId = false)
99     {
100         $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
101         if ($currentId) {
102             $query = $query->where('id', '!=', $currentId);
103         }
104         return $query->count() > 0;
105     }
106
107     /**
108      * Finds a suitable slug for the provided name.
109      * Checks database to prevent duplicate slugs.
110      * @param            $name
111      * @param            $bookId
112      * @param bool|false $currentId
113      * @return string
114      */
115     public function findSuitableSlug($name, $bookId, $currentId = false)
116     {
117         $slug = Str::slug($name);
118         while ($this->doesSlugExist($slug, $bookId, $currentId)) {
119             $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
120         }
121         return $slug;
122     }
123
124     /**
125      * Get chapters by the given search term.
126      * @param       $term
127      * @param array $whereTerms
128      * @return mixed
129      */
130     public function getBySearch($term, $whereTerms = [])
131     {
132         $terms = explode(' ', preg_quote(trim($term)));
133         $chapters = $this->chapter->fullTextSearch(['name', 'description'], $terms, $whereTerms);
134         $words = join('|', $terms);
135         foreach ($chapters as $chapter) {
136             //highlight
137             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $chapter->getExcerpt(100));
138             $chapter->searchSnippet = $result;
139         }
140         return $chapters;
141     }
142
143     /**
144      * Changes the book relation of this chapter.
145      * @param         $bookId
146      * @param Chapter $chapter
147      * @return Chapter
148      */
149     public function changeBook($bookId, Chapter $chapter)
150     {
151         $chapter->book_id = $bookId;
152         foreach ($chapter->activity as $activity) {
153             $activity->book_id = $bookId;
154             $activity->save();
155         }
156         $chapter->slug = $this->findSuitableSlug($chapter->name, $bookId, $chapter->id);
157         $chapter->save();
158         return $chapter;
159     }
160
161 }