| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Anax\Comments\HTMLForm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \Anax\HTMLForm\FormModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use \Anax\DI\DIInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use \Anax\Comments\Comm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Example of FormModel implementation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class UpdateCommForm extends FormModel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * Constructor injects with DI container and the id to update. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @param Anax\DI\DIInterface $di a service container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param integer             $id to update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function __construct(DIInterface $di, $id, $sessid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         parent::__construct($di); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $comm = $this->getCommDetails($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $textfilter = $this->di->get("textfilter"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $toparse = json_decode($comm->comment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $comt = $textfilter->parse($toparse->text, ["yamlfrontmatter", "shortcode", "markdown", "titlefromheader"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $comt = strip_tags($comt->text); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->aForm($id, $sessid, $comm, $comt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * Create the form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     public function aForm($id, $sessid, $comm, $comt) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $this->form->create( | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |                 "id" => __CLASS__, | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |                 "legend" => "Uppdatera ditt konto", | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |                 "sessid" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                     "type"  => "hidden", | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                     "value" => $sessid, | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 "id" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                     "type"  => "hidden", | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                     "value" => $id, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 "parentid" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                     "type"  => "hidden", | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                     "value" => $comm->parentid, | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                 "title" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                     "type" => "text", | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                     "validation" => ["not_empty"], | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                     "value" => $comm->title, | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                 "comment" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                     "type" => "textarea", | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                     "validation" => ["not_empty"], | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                     "value" => $comt, | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |                 "submit" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                     "type" => "submit", | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |                     "value" => "Spara", | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |                     "callback" => [$this, "callbackSubmit"] | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                 "reset" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                     "type"      => "reset", | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Get details on item to load form with. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param integer $id get details on item with id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @return Comm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 94 |  | View Code Duplication |     public function getCommDetails($id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $comm = new Comm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $comm->setDb($this->di->get("db")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $comm->find("id", $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         return $comm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * Callback for submit-button which should return true if it could | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * carry out its work and false if something failed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return boolean true if okey, false if something went wrong. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     public function callbackSubmit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $now = date("Y-m-d H:i:s"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $textfilter = $this->di->get("textfilter"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $userController = $this->di->get("userController"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $userdetails = $userController->getOne($this->form->value("sessid")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $parses = ["yamlfrontmatter", "shortcode", "markdown", "titlefromheader"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $comment = $textfilter->parse($this->form->value("comment"), $parses); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $comment->frontmatter['title'] = $this->form->value("title"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $comment = json_encode($comment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $comm = new Comm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $comm->setDb($this->di->get("db")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $comm->find("id", $this->form->value("id")); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $comm->updated = $now; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $comm->title = $this->form->value("title"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $comm->userid = $this->form->value("sessid"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $comm->comment = $comment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $comm->email = $userdetails["email"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $comm->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $parentid = (int)$this->form->value("parentid"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $back = $parentid > 0 ? "/view-one/" . $parentid : "/view-one/" . $this->form->value("id"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $pagerender = $this->di->get("pageRender"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $pagerender->redirect("comm" . $back); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.