| 
									
										
										
										
											2015-07-13 04:31:15 +08:00
										 |  |  | <?php namespace Oxbow\Repos; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Illuminate\Support\Str; | 
					
						
							|  |  |  | use Oxbow\Page; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PageRepo | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     protected $page; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * PageRepo constructor. | 
					
						
							|  |  |  |      * @param $page | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(Page $page) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->page = $page; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-31 05:27:35 +08:00
										 |  |  |     public function idExists($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->page->where('page_id', '=', $id)->count() > 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 04:31:15 +08:00
										 |  |  |     public function getById($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->page->findOrFail($id); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getAll() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->page->all(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-16 05:55:49 +08:00
										 |  |  |     public function getBySlug($slug, $bookId) | 
					
						
							| 
									
										
										
										
											2015-07-13 04:31:15 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-16 05:55:49 +08:00
										 |  |  |         return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first(); | 
					
						
							| 
									
										
										
										
											2015-07-13 04:31:15 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function newFromInput($input) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $page = $this->page->fill($input); | 
					
						
							|  |  |  |         return $page; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function countBySlug($slug, $bookId) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function destroyById($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $page = $this->getById($id); | 
					
						
							|  |  |  |         $page->delete(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 02:53:24 +08:00
										 |  |  |     public function getBySearch($term) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $terms = explode(' ', trim($term)); | 
					
						
							|  |  |  |         $query = $this->page; | 
					
						
							|  |  |  |         foreach($terms as $term) { | 
					
						
							|  |  |  |             $query = $query->where('text', 'like', '%'.$term.'%'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $query->get(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-21 05:05:26 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-07-31 05:27:35 +08:00
										 |  |  |      * Checks if a slug exists within a book already. | 
					
						
							|  |  |  |      * @param $slug | 
					
						
							| 
									
										
										
										
											2015-07-21 05:05:26 +08:00
										 |  |  |      * @param $bookId | 
					
						
							| 
									
										
										
										
											2015-07-31 05:27:35 +08:00
										 |  |  |      * @param bool|false $currentId | 
					
						
							|  |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2015-07-21 05:05:26 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-31 05:27:35 +08:00
										 |  |  |     public function doesSlugExist($slug, $bookId, $currentId = false) | 
					
						
							| 
									
										
										
										
											2015-07-21 05:05:26 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-31 05:27:35 +08:00
										 |  |  |         $query = $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId); | 
					
						
							|  |  |  |         if($currentId) { | 
					
						
							|  |  |  |             $query = $query->where('id', '!=', $currentId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $query->count() > 0; | 
					
						
							| 
									
										
										
										
											2015-07-22 03:13:29 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2015-07-31 05:27:35 +08:00
										 |  |  |      * Gets a suitable slug for the resource | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param $name | 
					
						
							| 
									
										
										
										
											2015-07-22 03:13:29 +08:00
										 |  |  |      * @param $bookId | 
					
						
							| 
									
										
										
										
											2015-07-31 05:27:35 +08:00
										 |  |  |      * @param bool|false $currentId | 
					
						
							|  |  |  |      * @return string | 
					
						
							| 
									
										
										
										
											2015-07-22 03:13:29 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-07-31 05:27:35 +08:00
										 |  |  |     public function findSuitableSlug($name, $bookId, $currentId = false) | 
					
						
							| 
									
										
										
										
											2015-07-22 03:13:29 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-07-31 05:27:35 +08:00
										 |  |  |         $slug = Str::slug($name); | 
					
						
							|  |  |  |         while($this->doesSlugExist($slug, $bookId, $currentId)) { | 
					
						
							|  |  |  |             $slug .= '-' . substr(md5(rand(1, 500)), 0, 3); | 
					
						
							| 
									
										
										
										
											2015-07-22 03:13:29 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-07-31 05:27:35 +08:00
										 |  |  |         return $slug; | 
					
						
							| 
									
										
										
										
											2015-07-21 05:05:26 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-31 05:27:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 04:31:15 +08:00
										 |  |  | } |