| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Models\CategoryModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Models\PostModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\Models\SlugModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Core\Constant; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Core\Container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Core\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Core\Traits\StringFunctions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Category extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     use StringFunctions; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $siteConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $pagination; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $slugModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $postModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $categoryModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function __construct(Container $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->loadModules[] = 'SiteConfig'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->loadModules[] = 'pagination'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         parent::__construct($container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->slugModel = new SlugModel($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->postModel = new PostModel($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->categoryModel = new CategoryModel($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * show all posts in a category | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string $categorySlug the slug passed via url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param string $page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @throws \ErrorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @throws \ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @throws \Twig_Error_Loader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @throws \Twig_Error_Runtime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @throws \Twig_Error_Syntax | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     public function posts(string $categorySlug, string $page="page-1") | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $categoryId = $this->slugModel->getIdFromSlug($categorySlug, "categories", "categories_slug", "idcategories"); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $totalPosts = $this->postModel->totalNumberPostsInCategory($categoryId) ; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $pagination = $this->pagination->getPagination($page, $totalPosts); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $this->data['configs'] = $this->siteConfig->getSiteConfig(); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this->data['navigation'] = $this->categoryModel->getMenu(); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $this->data['posts'] = $this->postModel->getPostsInCategory($categoryId,$pagination["offset"]); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->data['pagination'] = $pagination; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $this->data['categorySlug'] = $categorySlug; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         $this->renderView('Category'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     public function allPosts(string $page="page-1") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $totalPosts = $this->postModel->totalNumberPosts(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $pagination = $this->pagination->getPagination($page, $totalPosts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->data['configs'] = $this->siteConfig->getSiteConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->data['navigation'] = $this->categoryModel->getMenu(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->data['posts'] = $this->postModel->getPosts($pagination["offset"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->data['pagination'] = $pagination; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $this->renderView('Category'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  | } |