| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Models; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Core\Constant; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Core\Container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Core\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class CategoryModel extends Model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $categoryTbl; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     public function __construct(Container $container) | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         parent::__construct($container); | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         $this->categoryTbl = $this->getTablePrefix("categories"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * get the list of categories and return all the data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @return array the categories | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @throws \ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function getCategories() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         return $this->getResultSet('categories'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * get all the details from the category table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param int $categoryId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @throws \ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function getCategoryDetails(int $categoryId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         return $this->getRowById($categoryId, "categories"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * count the total number of categories | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function countCategories(): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return $this->count('categories'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * get the list of categories with pagination | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param int $offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param int $limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function getCategoryList(int $offset = 0, int $limit = Constant::POSTS_PER_PAGE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $this->list($offset, $limit, 'categories'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Update a specific category | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param int $categoryId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param string $categoryName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param string $categorySlug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function update(int $categoryId, string $categoryName, string $categorySlug):bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $sql = " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             UPDATE $this->categoryTbl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             SET | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |               category_name = :categoryName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |               categories_slug = :categorySlug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             WHERE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |               idcategories = :categoryId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->query($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->bind(":categoryName", $categoryName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->bind(":categorySlug", $categorySlug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->bind(":categoryId", $categoryId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return $this->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Create a new category | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param string $categoryName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param string $categorySlug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function new(string $categoryName, string $categorySlug):bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $sql = " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             INSERT INTO $this->categoryTbl (category_name, categories_slug) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             VALUES (:categoryName, :categorySlug) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $this->query($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->bind(":categoryName", $categoryName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->bind(":categorySlug", $categorySlug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         return $this->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * delete a category by Id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @param int $categoryId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     public function delete(int $categoryId):bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $sql = " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         DELETE FROM $this->categoryTbl  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         WHERE idcategories = :categoryId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         "; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->query($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $this->bind(":categoryId", $categoryId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         return $this->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * return the name of the category from the ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @param int $categoryId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     public function getNameFromId(int $categoryId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $sql = "SELECT category_name from $this->categoryTbl WHERE idcategories = :categoryId"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $this->query($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $this->bind(":categoryId", $categoryId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $this->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return $this->stmt->fetchColumn(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * get the category slug from the ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @param int $categoryId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * @throws \ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     public function getCategorySlugFromId(int $categoryId): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         return $this->getSlugFromId($categoryId, "idcategories", "categories_slug", "categories"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * check if category slug is unique | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @param string $categorySlug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     public function isCategorySlugUnique(string $categorySlug):bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         return $this->isSlugUnique($categorySlug, "categories_slug", "categories"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * Get the category ID from a slug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @param string $categorySlug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     public function getCategoryIdFromSlug(string $categorySlug): int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         return $this->getIdFromSlug($categorySlug, "idcategories", "categories_slug", "categories"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 176 |  |  | } |