]> BookStack Code Mirror - bookstack/blob - app/Repos/ChapterRepo.php
Prevent duplicate slugs on sort
[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         return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
60     }
61
62     /**
63      * Create a new chapter from request input.
64      * @param $input
65      * @return $this
66      */
67     public function newFromInput($input)
68     {
69         return $this->chapter->fill($input);
70     }
71
72     /**
73      * Destroy a chapter and its relations by providing its slug.
74      * @param Chapter $chapter
75      */
76     public function destroy(Chapter $chapter)
77     {
78         if (count($chapter->pages) > 0) {
79             foreach ($chapter->pages as $page) {
80                 $page->chapter_id = 0;
81                 $page->save();
82             }
83         }
84         Activity::removeEntity($chapter);
85         $chapter->views()->delete();
86         $chapter->delete();
87     }
88
89     /**
90      * Check if a chapter's slug exists.
91      * @param            $slug
92      * @param            $bookId
93      * @param bool|false $currentId
94      * @return bool
95      */
96     public function doesSlugExist($slug, $bookId, $currentId = false)
97     {
98         $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
99         if ($currentId) {
100             $query = $query->where('id', '!=', $currentId);
101         }
102         return $query->count() > 0;
103     }
104
105     /**
106      * Finds a suitable slug for the provided name.
107      * Checks database to prevent duplicate slugs.
108      * @param            $name
109      * @param            $bookId
110      * @param bool|false $currentId
111      * @return string
112      */
113     public function findSuitableSlug($name, $bookId, $currentId = false)
114     {
115         $slug = Str::slug($name);
116         while ($this->doesSlugExist($slug, $bookId, $currentId)) {
117             $slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
118         }
119         return $slug;
120     }
121
122     /**
123      * Get chapters by the given search term.
124      * @param       $term
125      * @param array $whereTerms
126      * @return mixed
127      */
128     public function getBySearch($term, $whereTerms = [])
129     {
130         $terms = explode(' ', preg_quote(trim($term)));
131         $chapters = $this->chapter->fullTextSearch(['name', 'description'], $terms, $whereTerms);
132         $words = join('|', $terms);
133         foreach ($chapters as $chapter) {
134             //highlight
135             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $chapter->getExcerpt(100));
136             $chapter->searchSnippet = $result;
137         }
138         return $chapters;
139     }
140
141     /**
142      * Changes the book relation of this chapter.
143      * @param         $bookId
144      * @param Chapter $chapter
145      * @return Chapter
146      */
147     public function changeBook($bookId, Chapter $chapter)
148     {
149         $chapter->book_id = $bookId;
150         foreach ($chapter->activity as $activity) {
151             $activity->book_id = $bookId;
152             $activity->save();
153         }
154         $chapter->slug = $this->findSuitableSlug($chapter->name, $bookId, $chapter->id);
155         $chapter->save();
156         return $chapter;
157     }
158
159 }