| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace midcom\datamanager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use midcom_error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Form\FormBuilderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\OptionsResolver\Options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\OptionsResolver\OptionsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Validator\Constraints\NotBlank; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use midcom; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use midcom_core_context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use midcom\datamanager\extension\compat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Experimental schema class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class schema | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $defaults = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         'operations' => array('save' => '', 'cancel' => '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $config = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $name = 'default'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 5 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function __construct(array $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 5 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 5 |  |         $this->config = array_merge($this->defaults, $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 5 |  |         $this->complete_fields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function set_name($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |         $this->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function get_name() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return $this->name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param FormBuilderInterface $builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return \Symfony\Component\Form\Form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function build_form(FormBuilderInterface $builder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         foreach ($this->config['fields'] as $name => $config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $options = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 'label' => $config['title'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                 'widget_config' => $config['widget_config'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 'type_config' => $config['type_config'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 'required' => $config['required'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 'constraints' => $config['required'] ? array(new NotBlank()) : null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 'dm2_type' => $config['type'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 'start_fieldset' => $config['start_fieldset'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 'end_fieldset' => $config['end_fieldset'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 'index_method' => $config['index_method'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             // Symfony < 2.8 compat | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             if (compat::is_legacy()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 $options['read_only'] = $config['readonly']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 $options['attr']['readonly'] = $config['readonly']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |             $builder->add($name, compat::get_type_name($config['widget']), $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |         $builder->add('form_toolbar', compat::get_type_name('toolbar'), array('operations' => $this->config['operations'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return $builder->getForm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 5 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function get_fields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 5 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return $this->config['fields']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @return \midcom_services_i18n_l10n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function get_l10n() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |         // Populate the l10n_schema member | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         if (array_key_exists('l10n_db', $this->config)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $l10n_name = $this->config['l10n_db']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $l10n_name = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         if (!midcom::get()->componentloader->is_installed($l10n_name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |             $l10n_name = 'midcom'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return midcom::get()->i18n->get_l10n($l10n_name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 5 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     private function complete_fields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 5 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 4 |  |         foreach ($this->config['fields'] as $name => &$config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 5 |  |             $config = $this->resolve_field_options($config, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 5 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 | 4 |  |  | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |     private function resolve_field_options(array $config, $name) | 
            
                                                        
            
                                    
            
            
                | 128 | 4 |  |     { | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |         $resolver = new OptionsResolver(); | 
            
                                                        
            
                                    
            
            
                | 130 | 4 |  |  | 
            
                                                        
            
                                    
            
            
                | 131 | 4 |  |         $resolver->setDefaults(array( | 
            
                                                        
            
                                    
            
            
                | 132 | 4 |  |             'title' => '', | 
            
                                                        
            
                                    
            
            
                | 133 | 4 |  |             'type' => null, | 
            
                                                        
            
                                    
            
            
                | 134 | 4 |  |             'type_config' => array(), | 
            
                                                        
            
                                    
            
            
                | 135 | 4 |  |             'widget' => null, | 
            
                                                        
            
                                    
            
            
                | 136 | 4 |  |             'widget_config' => array(), | 
            
                                                        
            
                                    
            
            
                | 137 | 4 |  |             'required' => false, | 
            
                                                        
            
                                    
            
            
                | 138 | 4 |  |             'readonly' => false, | 
            
                                                        
            
                                    
            
            
                | 139 | 4 |  |             'default' => null, | 
            
                                                        
            
                                    
            
            
                | 140 | 4 |  |             'storage' => '__UNSET__', | 
            
                                                        
            
                                    
            
            
                | 141 | 4 |  |             'index_method' => 'auto', | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |             'start_fieldset' => null, | 
            
                                                        
            
                                    
            
            
                | 143 | 4 |  |             'end_fieldset' => null, | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |             'validation' => array() | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |         )); | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 147 | 4 |  |         $normalize_widget = function (Options $options, $value) { | 
            
                                                        
            
                                    
            
            
                | 148 | 1 |  |             if (   $value == 'images' | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |                 || $value == 'downloads') { | 
            
                                                        
            
                                    
            
            
                | 150 | 3 |  |                 return 'subform'; | 
            
                                                        
            
                                    
            
            
                | 151 | 4 |  |             } | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |             return $value; | 
            
                                                        
            
                                    
            
            
                | 153 | 4 |  |         }; | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 155 | 4 |  |         $normalize_storage = function (Options $options, $value) use ($name) { | 
            
                                                        
            
                                    
            
            
                | 156 | 4 |  |             $default = array( | 
            
                                                        
            
                                    
            
            
                | 157 |  |  |                 'location' => 'parameter', | 
            
                                                        
            
                                    
            
            
                | 158 | 4 |  |                 'domain' => 'midcom.helper.datamanager2', | 
            
                                                        
            
                                    
            
            
                | 159 | 4 |  |                 'name' => $name | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |             ); | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |             if ($value === '__UNSET__') { | 
            
                                                        
            
                                    
            
            
                | 162 | 4 |  |                 return $default; | 
            
                                                        
            
                                    
            
            
                | 163 | 1 |  |             } | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |             if ($value === null) { | 
            
                                                        
            
                                    
            
            
                | 165 | 3 |  |                 return null; | 
            
                                                        
            
                                    
            
            
                | 166 | 1 |  |             } | 
            
                                                        
            
                                    
            
            
                | 167 |  |  |             if (is_string($value)) { | 
            
                                                        
            
                                    
            
            
                | 168 |  |  |                 if ($value === 'metadata') { | 
            
                                                        
            
                                    
            
            
                | 169 | 1 |  |                     return array('location' => $value, 'name' => $name); | 
            
                                                        
            
                                    
            
            
                | 170 | 1 |  |                 } | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |                 if ($value === 'parameter') { | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |                     return $default; | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 174 | 2 |  |                 return array('location' => $value); | 
            
                                                        
            
                                    
            
            
                | 175 | 2 |  |             } | 
            
                                                        
            
                                    
            
            
                | 176 | 2 |  |             if (strtolower($value['location']) === 'parameter') { | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |                 $value['location'] = strtolower($value['location']); | 
            
                                                        
            
                                    
            
            
                | 178 |  |  |                 if (!array_key_exists('domain', $value)) { | 
            
                                                        
            
                                    
            
            
                | 179 | 2 |  |                     $value['domain'] = 'midcom.helper.datamanager2'; | 
            
                                                        
            
                                    
            
            
                | 180 | 2 |  |                 } | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |             if (strtolower($value['location']) === 'configuration') { | 
            
                                                        
            
                                    
            
            
                | 183 | 2 |  |                 $value['location'] = 'parameter'; | 
            
                                                        
            
                                    
            
            
                | 184 | 4 |  |             } | 
            
                                                        
            
                                    
            
            
                | 185 |  |  |             return $value; | 
            
                                                        
            
                                    
            
            
                | 186 | 4 |  |         }; | 
            
                                                        
            
                                    
            
            
                | 187 | 4 |  |  | 
            
                                                        
            
                                    
            
            
                | 188 |  |  |         $normalize_validation = function (Options $options, $config) { | 
            
                                                        
            
                                    
            
            
                | 189 | 4 |  |             $validation = array(); | 
            
                                                        
            
                                    
            
            
                | 190 |  |  |             if (array_key_exists('validation',(array) $config)) { | 
            
                                                        
            
                                    
            
            
                | 191 |  |  |                 $validation = (array) $config['validation']; | 
            
                                                        
            
                                    
            
            
                | 192 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 194 |  |  |             foreach ($validation as $key => $rule) { | 
            
                                                        
            
                                    
            
            
                | 195 |  |  |                 if (!is_array($rule)) { | 
            
                                                        
            
                                    
            
            
                | 196 |  |  |                     $rule = array('type' => $rule); | 
            
                                                        
            
                                    
            
            
                | 197 |  |  |                 } elseif (!array_key_exists('type', $rule)) { | 
            
                                                        
            
                                    
            
            
                | 198 |  |  |                     throw new midcom_error("Missing validation rule type for rule {$key} on field {$config['name']}, this is a required option."); | 
            
                                                        
            
                                    
            
            
                | 199 |  |  |                 } elseif (   $rule['type'] == 'compare' | 
            
                                                        
            
                                    
            
            
                | 200 |  |  |                           && !array_key_exists('compare_with', $rule)) { | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |                     throw new midcom_error("Missing compare_with option for compare type rule {$key} on field {$config['name']}, this is a required option."); | 
            
                                                        
            
                                    
            
            
                | 202 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 203 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 204 |  |  |                 $defaults = array( | 
            
                                                        
            
                                    
            
            
                | 205 |  |  |                     'message' => "validation failed: {$rule['type']}", | 
            
                                                        
            
                                    
            
            
                | 206 |  |  |                     'format' => '' | 
            
                                                        
            
                                    
            
            
                | 207 |  |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 209 |  |  |                 $validation[$key] = array_merge($defaults, $rule); | 
            
                                                        
            
                                    
            
            
                | 210 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 211 |  |  |             return $validation; | 
            
                                                        
            
                                    
            
            
                | 212 |  |  |         }; | 
            
                                                        
            
                                    
            
            
                | 213 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 214 |  |  |         $resolver->setNormalizer('storage', $normalize_storage); | 
            
                                                        
            
                                    
            
            
                | 215 |  |  |         $resolver->setNormalizer('widget', $normalize_widget); | 
            
                                                        
            
                                    
            
            
                | 216 |  |  |         $resolver->setNormalizer('validation', $normalize_validation); | 
            
                                                        
            
                                    
            
            
                | 217 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 218 |  |  |         return $resolver->resolve($config); | 
            
                                                        
            
                                    
            
            
                | 219 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 220 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 221 |  |  |  |