| 
									
										
										
										
											2015-09-11 02:31:09 +08:00
										 |  |  | <?php namespace BookStack\Repos; | 
					
						
							| 
									
										
										
										
											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-07-31 05:27:35 +08:00
										 |  |  |     public function idExists($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->chapter->where('id', '=', $id)->count() > 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  |     public function getById($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->chapter->findOrFail($id); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getAll() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->chapter->all(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getBySlug($slug, $bookId) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function newFromInput($input) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->chapter->fill($input); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function destroyById($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $page = $this->getById($id); | 
					
						
							|  |  |  |         $page->delete(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function doesSlugExist($slug, $bookId, $currentId = false) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId); | 
					
						
							|  |  |  |         if($currentId) { | 
					
						
							|  |  |  |             $query = $query->where('id', '!=', $currentId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $query->count() > 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-07-29 03:57:13 +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-09-01 22:35:11 +08:00
										 |  |  |     public function getBySearch($term, $whereTerms = []) | 
					
						
							| 
									
										
										
										
											2015-09-01 03:11:44 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $terms = explode(' ', preg_quote(trim($term))); | 
					
						
							| 
									
										
										
										
											2015-09-01 22:35:11 +08:00
										 |  |  |         $chapters = $this->chapter->fullTextSearch(['name', 'description'], $terms, $whereTerms); | 
					
						
							| 
									
										
										
										
											2015-09-01 03:11:44 +08:00
										 |  |  |         $words = join('|', $terms); | 
					
						
							|  |  |  |         foreach ($chapters as $chapter) { | 
					
						
							|  |  |  |             //highlight
 | 
					
						
							|  |  |  |             $result = preg_replace('#' . $words . '#iu', "<span class=\"highlight\">\$0</span>", $chapter->getExcerpt(100)); | 
					
						
							|  |  |  |             $chapter->searchSnippet = $result; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $chapters; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-06 21:35:53 +08:00
										 |  |  |     public function setBookId($bookId, Chapter $chapter) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $chapter->book_id = $bookId; | 
					
						
							|  |  |  |         foreach($chapter->activity as $activity) { | 
					
						
							|  |  |  |             $activity->book_id = $bookId; | 
					
						
							|  |  |  |             $activity->save(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $chapter->save(); | 
					
						
							|  |  |  |         return $chapter; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 03:17:08 +08:00
										 |  |  | } |