| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Controllers\Admin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Models\CategoryModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use App\Models\PostModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\Models\SlugModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\Models\TagsModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Core\AdminController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Post extends AdminController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * page for new post | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function new() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->onlyAdmin(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $categoryModel = new CategoryModel($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $tagModel = new TagsModel($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->data['categories'] = $categoryModel->getCategories(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->data['tags'] = $tagModel->getTags(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->renderView('Admin/NewPost'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Lists all the posts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->onlyAdmin(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Shows the post to modify and update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @throws \ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @throws \Twig_Error_Loader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @throws \Twig_Error_Runtime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @throws \Twig_Error_Syntax | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @throws \ErrorException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     public function modify(string $slug):void | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->onlyAdmin(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $categoryModel = new CategoryModel($this->container); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         $tagModel = new TagsModel($this->container); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $postModel = new PostModel($this->container); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $slugModel = new SlugModel($this->container); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $postId = $slugModel->getIdFromSlug($slug,"posts","posts_slug", "idposts"); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $this->data['post'] = $postModel->getSinglePost($postId); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $this->data['postTags'] = $tagModel->getTagsOnPost($postId); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $this->data['categories'] = $categoryModel->getCategories(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this->data['tags'] = $tagModel->getTags(); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $this->renderView('Admin/ModifyPost'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Create a new post | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @throws \ErrorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function createNewPost() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         //Security checks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->onlyAdmin(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if (!$this->request->isPost()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $this->alertBox->setAlert('Only post messages allowed', 'error'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $this->response->redirect('admin'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $posts = $this->container->getRequest()->getDataFull(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $userSessionid = $this->container->getSession()->get("user_id"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $title = trim($posts["newPostTitle"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $postImage = $posts["newPostImage"]; //TODO Sanatize the input ? Or will PDO be enough ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $postSlug = trim($posts["newPostSlug"]); //TODO Check if unique | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $article = $posts["newPostTextArea"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $idCategory = $posts["categorySelector"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $published = $posts["isPublished"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $onFrontpage = $posts["isOnFrontPage"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $idUser = $userSessionid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $slugModel = new SlugModel($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $tagModel = new TagsModel($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $postModel = new PostModel($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         //security and error checks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $error = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if ($title == "") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $error = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $this->alertBox->setAlert("empty title not allowed", "error"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if ($postSlug == "") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $error = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $this->alertBox->setAlert("empty slug not allowed", "error"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if (!$slugModel->isUnique($postSlug, "posts", "posts_slug")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $error = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $this->alertBox->setAlert("Slug not unique", "error"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if ($error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $this->container->getResponse()->redirect("admin/post/new"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $postId = $postModel->newPost($title, $postImage, $idCategory, $article, $idUser, $published, $onFrontpage, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $postSlug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         if (isset($posts["tags"])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             foreach ($posts["tags"] as $tag) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                 if (isset($tag["id"])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                     $tagModel->addTagToPost($postId, $tag["id"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 $tagModel->addNewTagToPost($postId, $tag["name"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $this->alertBox->setAlert("Post " . $title . " Created"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $this->container->getResponse()->redirect("admin/post/modify/" . $postSlug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * update a post | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     public function modifyPost() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         //Security checks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $this->onlyAdmin(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         if (!$this->request->isPost()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $this->alertBox->setAlert('Only post messages allowed', 'error'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             $this->response->redirect('admin'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $posts = $this->container->getRequest()->getDataFull(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         /*TODO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         update the post | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         add and delete tags -> for that compare list of tags already set (added and missing tags ...) ?? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         echo "<pre>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         var_dump($posts); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         die(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 151 |  |  | } |