| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PSFS\base\config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PSFS\base\types\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class ConfigForm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package PSFS\base\config | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | class ConfigForm extends Form | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * Constructor por defecto | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * @param string $route | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @param array $required | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @param array $optional | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @param array $data | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @throws \PSFS\base\exception\FormException | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @throws \PSFS\base\exception\RouterException | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public function __construct($route, array $required, array $optional = [], array $data = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         parent::__construct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $this->setAction($route); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         //Añadimos los campos obligatorios | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         foreach ($required as $field) { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             $type = in_array($field, Config::$encrypted) ? 'password' : 'text'; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             $value = isset(Config::$defaults[$field]) ? Config::$defaults[$field] : null; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             $this->add($field, array( | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |                 'label' => t($field), | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |                 'class' => 'col-md-6', | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |                 'required' => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |                 'type' => $type, | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |                 'value' => $value, | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             )); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $this->add(Form::SEPARATOR); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         if (!empty($optional) && !empty($data)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             foreach ($optional as $field) { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |                 if (array_key_exists($field, $data) && strlen($data[$field] ?? '') > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |                     $type = preg_match('/(password|secret)/i', $field) ? 'password' : 'text'; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |                     $this->add($field, array( | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |                         'label' => t($field), | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |                         'class' => 'col-md-6', | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                         'required' => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                         'value' => $data[$field], | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                         'type' => $type, | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                     )); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $extra = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $extraKeys = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         if (!empty($data)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             $extraKeys = array_keys($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             $extra = array_diff($extraKeys, array_merge($required, $optional)); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         if (!empty($extra)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             foreach ($extra as $key => $field) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 if (strlen($data[$field]) > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                     $type = preg_match('/(password|secret)/i', $field) ? 'password' : 'text'; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                     $this->add($extraKeys[$key], array( | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                         'label' => $field, | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                         'class' => 'col-md-6', | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                         'required' => false, | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                         'value' => $data[$field], | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |                         'type' => $type, | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |                     )); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         $this->add(Form::SEPARATOR); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         //Aplicamos estilo al formulario | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         $this->setAttrs(array( | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             'class' => 'form-horizontal', | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         //Hidratamos el formulario | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         $this->setData($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         //Añadimos las acciones del formulario | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         $add = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |             'class' => 'btn-warning md-default', | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             'icon' => 'fa-plus', | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         if (Config::getParam('admin.version', 'v1') === 'v1') { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |             $add['onclick'] = 'javascript:addNewField(document.getElementById("' . $this->getName() . '"));'; | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             $add['ng-click'] = 'addNewField()'; | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $this->addButton('submit', t('Guardar configuración'), 'submit', array( | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             'class' => 'btn-success col-md-offset-2 md-primary', | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             'icon' => 'fa-save', | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         )) | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             ->addButton('add_field', t('Añadir nuevo parámetro'), 'button', $add); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * Nombre del formulario | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     public function getName() | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         return 'config'; | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      * Nombre del título del formulario | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     public function getTitle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  |         return t('Parámetros necesarios para la ejecución de PSFS'); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 115 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 116 |  |  |  |