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