| 
									
										
										
										
											2015-09-11 02:31:09 +08:00
										 |  |  | <?php namespace BookStack\Repos; | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  | use Activity; | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  | use Illuminate\Support\Str; | 
					
						
							| 
									
										
										
										
											2015-09-11 02:31:09 +08:00
										 |  |  | use BookStack\Chapter; | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class ChapterRepo | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $chapter; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * ChapterRepo constructor. | 
					
						
							|  |  |  |      * @param $chapter | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(Chapter $chapter) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->chapter = $chapter; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check if an id exists. | 
					
						
							|  |  |  |      * @param $id | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-31 05:27:35 +08:00
										 |  |  |     public function idExists($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->chapter->where('id', '=', $id)->count() > 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get a chapter by a specific id. | 
					
						
							|  |  |  |      * @param $id | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  |     public function getById($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->chapter->findOrFail($id); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get all chapters. | 
					
						
							|  |  |  |      * @return \Illuminate\Database\Eloquent\Collection|static[] | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  |     public function getAll() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->chapter->all(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get a chapter that has the given slug within the given book. | 
					
						
							|  |  |  |      * @param $slug | 
					
						
							|  |  |  |      * @param $bookId | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  |     public function getBySlug($slug, $bookId) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-29 01:19:23 +08:00
										 |  |  |         $chapter = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first(); | 
					
						
							|  |  |  |         if ($chapter === null) abort(404); | 
					
						
							|  |  |  |         return $chapter; | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Create a new chapter from request input. | 
					
						
							|  |  |  |      * @param $input | 
					
						
							|  |  |  |      * @return $this | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  |     public function newFromInput($input) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->chapter->fill($input); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Destroy a chapter and its relations by providing its slug. | 
					
						
							|  |  |  |      * @param Chapter $chapter | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function destroy(Chapter $chapter) | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  |         if (count($chapter->pages) > 0) { | 
					
						
							|  |  |  |             foreach ($chapter->pages as $page) { | 
					
						
							|  |  |  |                 $page->chapter_id = 0; | 
					
						
							|  |  |  |                 $page->save(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Activity::removeEntity($chapter); | 
					
						
							|  |  |  |         $chapter->views()->delete(); | 
					
						
							|  |  |  |         $chapter->delete(); | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check if a chapter's slug exists. | 
					
						
							|  |  |  |      * @param            $slug | 
					
						
							|  |  |  |      * @param            $bookId | 
					
						
							|  |  |  |      * @param bool|false $currentId | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  |     public function doesSlugExist($slug, $bookId, $currentId = false) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId); | 
					
						
							| 
									
										
										
										
											2015-11-22 02:11:46 +08:00
										 |  |  |         if ($currentId) { | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  |             $query = $query->where('id', '!=', $currentId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $query->count() > 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Finds a suitable slug for the provided name. | 
					
						
							|  |  |  |      * Checks database to prevent duplicate slugs. | 
					
						
							|  |  |  |      * @param            $name | 
					
						
							|  |  |  |      * @param            $bookId | 
					
						
							|  |  |  |      * @param bool|false $currentId | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-29 03:57:13 +08:00
										 |  |  |     public function findSuitableSlug($name, $bookId, $currentId = false) | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $slug = Str::slug($name); | 
					
						
							| 
									
										
										
										
											2015-11-22 02:11:46 +08:00
										 |  |  |         while ($this->doesSlugExist($slug, $bookId, $currentId)) { | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  |             $slug .= '-' . substr(md5(rand(1, 500)), 0, 3); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $slug; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get chapters by the given search term. | 
					
						
							|  |  |  |      * @param       $term | 
					
						
							|  |  |  |      * @param array $whereTerms | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-09-01 22:35:11 +08:00
										 |  |  |     public function getBySearch($term, $whereTerms = []) | 
					
						
							| 
									
										
										
										
											2015-09-01 03:11:44 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-29 23:37:13 +08:00
										 |  |  |         $terms = explode(' ', $term); | 
					
						
							| 
									
										
										
										
											2015-09-01 22:35:11 +08:00
										 |  |  |         $chapters = $this->chapter->fullTextSearch(['name', 'description'], $terms, $whereTerms); | 
					
						
							| 
									
										
										
										
											2015-12-29 23:37:13 +08:00
										 |  |  |         $words = join('|', explode(' ', preg_quote(trim($term), '/'))); | 
					
						
							| 
									
										
										
										
											2015-09-01 03:11:44 +08:00
										 |  |  |         foreach ($chapters as $chapter) { | 
					
						
							|  |  |  |             //highlight
 | 
					
						
							|  |  |  |             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $chapter->getExcerpt(100)); | 
					
						
							|  |  |  |             $chapter->searchSnippet = $result; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $chapters; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-11-22 02:11:46 +08:00
										 |  |  |      * Changes the book relation of this chapter. | 
					
						
							| 
									
										
										
										
											2015-11-22 02:05:03 +08:00
										 |  |  |      * @param         $bookId | 
					
						
							|  |  |  |      * @param Chapter $chapter | 
					
						
							|  |  |  |      * @return Chapter | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-11-22 02:11:46 +08:00
										 |  |  |     public function changeBook($bookId, Chapter $chapter) | 
					
						
							| 
									
										
										
										
											2015-09-06 21:35:53 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $chapter->book_id = $bookId; | 
					
						
							| 
									
										
										
										
											2015-11-22 02:11:46 +08:00
										 |  |  |         foreach ($chapter->activity as $activity) { | 
					
						
							| 
									
										
										
										
											2015-09-06 21:35:53 +08:00
										 |  |  |             $activity->book_id = $bookId; | 
					
						
							|  |  |  |             $activity->save(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-11-22 02:11:46 +08:00
										 |  |  |         $chapter->slug = $this->findSuitableSlug($chapter->name, $bookId, $chapter->id); | 
					
						
							| 
									
										
										
										
											2015-09-06 21:35:53 +08:00
										 |  |  |         $chapter->save(); | 
					
						
							|  |  |  |         return $chapter; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  | } |