| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Catcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Drone\Dom\Element\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | Use Drone\Mvc\AbstractionModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Drone\Mvc\AbstractionController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Drone\Mvc\Layout; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Drone\Validator\FormValidator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Drone\Util\ArrayDimension; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | class Module extends AbstractionModule | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | 	public function init(AbstractionController $c) | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         $config = $this->getUserConfig(); | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         $_config = ArrayDimension::toUnidimensional($config, "_"); | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         $this->setTranslator($c); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         # config constraints | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         $components = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             "attributes" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |                 "project_name" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |                     "required"  => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |                     "type"      => "text", | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |                     "minlength" => 2, | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |                     "maxlength" => 60 | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $options = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             "project" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |                 "label"      => "project -> name" | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $form = new Form($components); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $form->fill($_config); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $validator = new FormValidator($form, $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $validator->validate(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $data["validator"] = $validator; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         try | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             if (!$validator->isValid()) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                 $data["messages"] = $validator->getMessages(); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 throw new \Exception("Module config errros in user.config!", 300); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         catch (\Exception $e) | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $file = str_replace('\\', '', __CLASS__); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             $storage = new \Drone\Exception\Storage("cache/$file.json"); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             # stores the error code | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             if (($errorCode = $storage->store($e)) === false) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                 $errors = $storage->getErrors(); | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 # if error storing is not possible, handle it (internal app error) | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                 //$this->handleErrors($errors, __METHOD__); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             $data["code"]    = $errorCode; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             $data["message"] = $e->getMessage(); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             $config = include 'config/application.config.php'; | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             $data["dev_mode"] = $config["environment"]["dev_mode"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             # stops current controller execution | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             $c->stopExecution(false); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             # loads error view | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             $layoutManager = new Layout(); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             $layoutManager->setBasePath($c->getBasePath()); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             $layoutManager->setView($this, "validation"); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |             $layoutManager->setParams($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             # for AJAX requests! | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             if ($c->isXmlHttpRequest()) | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |                 $layoutManager->content(); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             else | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                 $layoutManager->fromTemplate($this, 'blank'); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     private function setTranslator(AbstractionController $c) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $config = include('config/application.config.php'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $locale = $config["environment"]["locale"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $i18nTranslator = \Zend\I18n\Translator\Translator::factory( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 'locale'  => "$locale", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 'translation_files' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                     [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                         "type" => 'phparray', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                         "filename" => __DIR__ . "/lang/$locale.php" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |         $c->translator = new \Zend\Mvc\I18n\Translator($i18nTranslator); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                                                                
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	public function getUserConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	{ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  | 		return include __DIR__ . "/config/user.config.php"; | 
            
                                                        
            
                                    
            
            
                | 118 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 119 |  |  | } |