| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Alvo\Comment\HTMLForm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \Anax\HTMLForm\FormModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use \Anax\DI\DIInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use \Alvo\Comment\Comment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Example of FormModel implementation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class AddCommentForm extends FormModel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * Constructor injects with DI container. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @param Anax\DI\DIInterface $di a service container | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 2 |  |     public function __construct(DIInterface $di) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 2 |  |         parent::__construct($di); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 2 |  |         $this->form->create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 2 |  |                 "id" => __CLASS__, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 "class" => "mb-2", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 "wrapper-element" => "div", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 "use_fieldset" => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 2 |  |                 "heading" => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                     "type" => "text", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                     "class" => "form-control", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                     "validation" => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                         "not_empty" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 "text" => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                     "type" => "textarea", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     "class" => "form-control", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     "validation" => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                         "not_empty" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 "tags" => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                     "label" => "Space separated tags", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                     "type" => "text", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     "class" => "form-control", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     "validation" => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                         "not_empty" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 "submit" => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |                     "type" => "submit", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |                     "value" => "Add post", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |                     "callback" => [$this, "callbackSubmit"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |                     "class" => "btn btn-success" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Callback for submit-button which should return true if it could | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * carry out its work and false if something failed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return boolean true if okey, false if something went wrong. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 1 |  |     public function callbackSubmit() | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         // Get values from the submitted form | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 1 |  |         $heading = $this->form->value("heading"); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 1 |  |         $text = $this->form->value("text"); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 1 |  |         $tags = $this->form->value("tags"); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 1 |  |         $user = $this->di->get("user")->getUser(); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 1 |  |         if (!$user) { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 1 |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         $userId = $user->id; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         $comment = new Comment(); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         $comment->setDb($this->di->get("db")); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         $comment->heading = $heading; | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $comment->text = $text; | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         $comment->tags = $tags; | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         $comment->userId = $userId; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         $comment->created = date("Y-m-d H:i:s"); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $comment->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         $this->form->addOutput("Comment was added"); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  |