| 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 |  |  |         //TODO have we receved a $_POST, if yes then probably an error on the create new post | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |         $categoryModel = new CategoryModel($this->container); | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         $tagModel = new TagsModel($this->container); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         $this->data['categories'] = $categoryModel->getCategories(); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         $this->data['tags'] = $tagModel->getTags(); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         $this->renderView('Admin/NewPost'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * Lists all the posts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function list() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->onlyAdmin(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Shows the post to modify and update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param $idPost | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function modify($idPost) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->onlyAdmin(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Create a new post | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @throws \ErrorException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function createNewPost() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         //Security checks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->onlyAdmin(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if (!$this->request->isPost()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $this->alertBox->setAlert('Only post messages allowed', 'error'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $this->response->redirect('admin'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $posts = $this->container->getRequest()->getDataFull(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $userSessionid = $this->container->getSession()->get("user_id"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         //TODO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         //Slug, check if duplicate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         //Tags, check if duplicate before creating new tag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         //Tags, must have created the post and got the id before associating the tags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         //grab author from session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $title = $posts["newPostTitle"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $postImage = $posts["newPostImage"]; //TODO Sanatize the input ? Or will PDO be enough ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $postSlug = $posts["newPostSlug"]; //TODO Check if unique | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $article = $posts["newPostTextArea"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $idCategory = $posts["categorySelector"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $published = $posts["isPublished"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $onFrontpage = $posts["isOnFrontPage"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $idUser = $userSessionid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $slugModel = new SlugModel($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $tagModel = new TagsModel($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $postModel = new PostModel($this->container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         //security and error checks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if (!$slugModel->isUnique($postSlug, "posts", "posts_slug")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             die("SLUG not unique"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $postId = $postModel->newPost($title, $postImage, $idCategory, $article, $idUser, $published, $onFrontpage, $postSlug); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         echo "<p>new post ID : " . $postId . "</p>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if(isset($posts["tags"])){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             foreach ($posts["tags"] as $tag) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 if(isset($tag["id"])){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                     $tagModel->addTagToPost($postId, $tag["id"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $tagModel->addNewTagToPost($postId, $tag["name"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         echo "<pre>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         var_dump($posts); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         die(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         //TODO send a return $_POST with all data to the new post page if errors (also need to check if new page recives post, then add the data back into the forms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         //TODO else redirect to the modify page on OK ? good usability or stay on the new page with blank ? | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  | } | 
            
                        
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.