| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Controllers\Ajax; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Models\CommentModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Core\AjaxController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Core\Container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class Comment extends AjaxController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private $commentModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     public function __construct(Container $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         parent::__construct($container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $this->commentModel = new CommentModel($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Update the approved status of a comment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @throws \Core\JsonException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public function modifyApproved() | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         $this->onlyAdmin(); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $this->onlyPost(); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         $state = (bool)($this->request->getData("state") === 'true'); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $commentId = (int)$this->request->getData("commentId"); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $result = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $result["success"] = $this->commentModel->setApproved(!$state, $commentId); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $result["state"] = !$state; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $result["commentId"] = $commentId; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         echo json_encode($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function loadComments() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $commentOffset = (int)$this->request->getData("commentOffset"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $postId = (int)$this->request->getData("postId"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $result = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $result["success"] = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $data = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $data["comments"] = $this->commentModel->getCommentsListOnPost($postId, $commentOffset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         if ($data["comments"] !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $result["success"] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $twig = $this->container->getTemplate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $html = $twig->render('Includes/LoadComments.twig', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $result["html"] = $html; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $result["commentOffset"] = $commentOffset + count(/** @scrutinizer ignore-type */$data["comments"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             //Scrutinizer thinks this can be of type true. which is impossible since PDO returns and array (empty or not) or false on error, we have checked for false. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         echo json_encode($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 61 |  |  | } |