@@ -14,459 +14,459 @@ |
||
| 14 | 14 | class EE_Model_Form_Section extends EE_Form_Section_Proper |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var EEM_Base |
|
| 19 | - */ |
|
| 20 | - protected $_model = null; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var EE_Base_Class |
|
| 24 | - */ |
|
| 25 | - protected $_model_object = null; |
|
| 26 | - |
|
| 27 | - |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param array $options_array keys: { |
|
| 31 | - * @type EEM_Base $model |
|
| 32 | - * @type EE_Base_Class $model_object |
|
| 33 | - * @type array $subsection_args array keys should be subsection names (that either do or will exist), and |
|
| 34 | - * values are the arrays as you would pass them to that subsection |
|
| 35 | - * } |
|
| 36 | - * @throws EE_Error |
|
| 37 | - */ |
|
| 38 | - public function __construct($options_array = array()) |
|
| 39 | - { |
|
| 40 | - if (isset($options_array['model']) && $options_array['model'] instanceof EEM_Base) { |
|
| 41 | - $this->_model = $options_array['model']; |
|
| 42 | - } |
|
| 43 | - if (! $this->_model || ! $this->_model instanceof EEM_Base) { |
|
| 44 | - throw new EE_Error(sprintf(__( |
|
| 45 | - "Model Form Sections must first specify the _model property to be a subclass of EEM_Base", |
|
| 46 | - "event_espresso" |
|
| 47 | - ))); |
|
| 48 | - } |
|
| 49 | - if (isset($options_array['subsection_args'])) { |
|
| 50 | - $subsection_args = $options_array['subsection_args']; |
|
| 51 | - } else { |
|
| 52 | - $subsection_args = array(); |
|
| 53 | - } |
|
| 54 | - // gather fields and relations to convert to inputs |
|
| 55 | - // but if they're just going to exclude a field anyways, don't bother converting it to an input |
|
| 56 | - $exclude = $this->_subsections; |
|
| 57 | - if (isset($options_array['exclude'])) { |
|
| 58 | - $exclude = array_merge($exclude, array_flip($options_array['exclude'])); |
|
| 59 | - } |
|
| 60 | - $model_fields = array_diff_key($this->_model->field_settings(), $exclude); |
|
| 61 | - $model_relations = array_diff_key($this->_model->relation_settings(), $exclude); |
|
| 62 | - // convert fields and relations to inputs |
|
| 63 | - $this->_subsections = array_merge( |
|
| 64 | - $this->_convert_model_fields_to_inputs($model_fields), |
|
| 65 | - $this->_convert_model_relations_to_inputs($model_relations, $subsection_args), |
|
| 66 | - $this->_subsections |
|
| 67 | - ); |
|
| 68 | - parent::__construct($options_array); |
|
| 69 | - if (isset($options_array['model_object']) && $options_array['model_object'] instanceof EE_Base_Class) { |
|
| 70 | - $this->populate_model_obj($options_array['model_object']); |
|
| 71 | - } |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * For now, just makes inputs for only HABTM relations |
|
| 78 | - * |
|
| 79 | - * @param EE_Model_Relation_Base[] $relations |
|
| 80 | - * @param array $subsection_args keys should be existing or soon-to-be-existing input names, and |
|
| 81 | - * their values are { |
|
| 82 | - * @type array { |
|
| 83 | - * @type EE_Base_Class[] $model_objects if the subsection is an EE_Select_Multi_Model_Input |
|
| 84 | - * } |
|
| 85 | - * } |
|
| 86 | - * @return array |
|
| 87 | - */ |
|
| 88 | - protected function _convert_model_relations_to_inputs($relations, $subsection_args = array()) |
|
| 89 | - { |
|
| 90 | - $inputs = array(); |
|
| 91 | - foreach ($relations as $relation_name => $relation_obj) { |
|
| 92 | - $input_constructor_args = array( |
|
| 93 | - array_merge( |
|
| 94 | - array( |
|
| 95 | - 'required' => $relation_obj instanceof EE_Belongs_To_Relation, |
|
| 96 | - 'html_label_text' => $relation_obj instanceof EE_Belongs_To_Relation |
|
| 97 | - ? $relation_obj->get_other_model()->item_name(1) |
|
| 98 | - : $relation_obj->get_other_model() |
|
| 99 | - ->item_name(2), |
|
| 100 | - ), |
|
| 101 | - $subsection_args |
|
| 102 | - ), |
|
| 103 | - ); |
|
| 104 | - $input = null; |
|
| 105 | - switch (get_class($relation_obj)) { |
|
| 106 | - case 'EE_HABTM_Relation': |
|
| 107 | - if (isset($subsection_args[ $relation_name ]) |
|
| 108 | - && isset($subsection_args[ $relation_name ]['model_objects']) |
|
| 109 | - ) { |
|
| 110 | - $model_objects = $subsection_args[ $relation_name ]['model_objects']; |
|
| 111 | - } else { |
|
| 112 | - $model_objects = $relation_obj->get_other_model()->get_all(); |
|
| 113 | - } |
|
| 114 | - $input = new EE_Select_Multi_Model_Input($model_objects, $input_constructor_args); |
|
| 115 | - break; |
|
| 116 | - default: |
|
| 117 | - } |
|
| 118 | - if ($input) { |
|
| 119 | - $inputs[ $relation_name ] = $input; |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - return $inputs; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Changes model fields into form section inputs |
|
| 129 | - * |
|
| 130 | - * @param EE_Model_Field_Base[] $model_fields keys are the model's name |
|
| 131 | - * @throws EE_Error |
|
| 132 | - * @return EE_Form_Input_Base[] |
|
| 133 | - */ |
|
| 134 | - protected function _convert_model_fields_to_inputs($model_fields = array()) |
|
| 135 | - { |
|
| 136 | - $inputs = array(); |
|
| 137 | - foreach ($model_fields as $field_name => $model_field) { |
|
| 138 | - if ($model_field instanceof EE_Model_Field_Base) { |
|
| 139 | - $input_constructor_args = array( |
|
| 140 | - array( |
|
| 141 | - 'required' => ! $model_field->is_nullable() |
|
| 142 | - && $model_field->get_default_value() |
|
| 143 | - === null, |
|
| 144 | - 'html_label_text' => $model_field->get_nicename(), |
|
| 145 | - 'default' => $model_field->get_default_value(), |
|
| 146 | - ), |
|
| 147 | - ); |
|
| 148 | - switch (get_class($model_field)) { |
|
| 149 | - case 'EE_All_Caps_Text_Field': |
|
| 150 | - case 'EE_Any_Foreign_Model_Name_Field': |
|
| 151 | - $input_class = 'EE_Text_Input'; |
|
| 152 | - break; |
|
| 153 | - case 'EE_Boolean_Field': |
|
| 154 | - $input_class = 'EE_Yes_No_Input'; |
|
| 155 | - break; |
|
| 156 | - case 'EE_Datetime_Field': |
|
| 157 | - throw new EE_Error(sprintf(__( |
|
| 158 | - "Model field '%s' does not yet have a known conversion to form input", |
|
| 159 | - "event_espresso" |
|
| 160 | - ), get_class($model_field))); |
|
| 161 | - break; |
|
| 162 | - case 'EE_Email_Field': |
|
| 163 | - $input_class = 'EE_Email_Input'; |
|
| 164 | - break; |
|
| 165 | - case 'EE_Enum_Integer_Field': |
|
| 166 | - throw new EE_Error(sprintf(__( |
|
| 167 | - "Model field '%s' does not yet have a known conversion to form input", |
|
| 168 | - "event_espresso" |
|
| 169 | - ), get_class($model_field))); |
|
| 170 | - break; |
|
| 171 | - case 'EE_Enum_Text_Field': |
|
| 172 | - throw new EE_Error(sprintf(__( |
|
| 173 | - "Model field '%s' does not yet have a known conversion to form input", |
|
| 174 | - "event_espresso" |
|
| 175 | - ), get_class($model_field))); |
|
| 176 | - break; |
|
| 177 | - case 'EE_Float_Field': |
|
| 178 | - $input_class = 'EE_Float_Input'; |
|
| 179 | - break; |
|
| 180 | - case 'EE_Foreign_Key_Int_Field': |
|
| 181 | - case 'EE_Foreign_Key_String_Field': |
|
| 182 | - case 'EE_WP_User_Field': |
|
| 183 | - $models_pointed_to = $model_field instanceof EE_Field_With_Model_Name |
|
| 184 | - ? $model_field->get_model_class_names_pointed_to() : array(); |
|
| 185 | - if (true || is_array($models_pointed_to) && count($models_pointed_to) > 1) { |
|
| 186 | - $input_class = 'EE_Text_Input'; |
|
| 187 | - } else { |
|
| 188 | - // so its just one model |
|
| 189 | - $model_name = is_array($models_pointed_to) ? reset($models_pointed_to) : $models_pointed_to; |
|
| 190 | - $model = EE_Registry::instance()->load_model($model_name); |
|
| 191 | - $model_names = $model->get_all_names(array('limit' => 10)); |
|
| 192 | - if ($model_field->is_nullable()) { |
|
| 193 | - array_unshift($model_names, __("Please Select", 'event_espresso')); |
|
| 194 | - } |
|
| 195 | - $input_constructor_args[1] = $input_constructor_args[0]; |
|
| 196 | - $input_constructor_args[0] = $model_names; |
|
| 197 | - $input_class = 'EE_Select_Input'; |
|
| 198 | - } |
|
| 199 | - break; |
|
| 200 | - case 'EE_Full_HTML_Field': |
|
| 201 | - $input_class = 'EE_Text_Area_Input'; |
|
| 202 | - $input_constructor_args[0]['validation_strategies'] = array(new EE_Full_HTML_Validation_Strategy()); |
|
| 203 | - break; |
|
| 204 | - case 'EE_Infinite_Integer': |
|
| 205 | - throw new EE_Error(sprintf(__( |
|
| 206 | - "Model field '%s' does not yet have a known conversion to form input", |
|
| 207 | - "event_espresso" |
|
| 208 | - ), get_class($model_field))); |
|
| 209 | - break; |
|
| 210 | - case 'EE_Integer_Field': |
|
| 211 | - $input_class = 'EE_Text_Input'; |
|
| 212 | - break; |
|
| 213 | - case 'EE_Maybe_Serialized_Text_Field': |
|
| 214 | - $input_class = 'EE_Text_Area_Input'; |
|
| 215 | - break; |
|
| 216 | - case 'EE_Money_Field': |
|
| 217 | - throw new EE_Error(sprintf(__( |
|
| 218 | - "Model field '%s' does not yet have a known conversion to form input", |
|
| 219 | - "event_espresso" |
|
| 220 | - ), get_class($model_field))); |
|
| 221 | - break; |
|
| 222 | - case 'EE_Post_Content_Field': |
|
| 223 | - $input_class = 'EE_Text_Area_Input'; |
|
| 224 | - $input_constructor_args[0]['validation_strategies'] = array(new EE_Full_HTML_Validation_Strategy()); |
|
| 225 | - break; |
|
| 226 | - case 'EE_Plain_Text_Field': |
|
| 227 | - $input_class = 'EE_Text_Input'; |
|
| 228 | - break; |
|
| 229 | - case 'EE_Primary_Key_Int_Field': |
|
| 230 | - $input_class = 'EE_Hidden_Input'; |
|
| 231 | - $input_constructor_args[0]['normalization_strategy'] = new EE_Int_Normalization(); |
|
| 232 | - break; |
|
| 233 | - case 'EE_Primary_Key_String_Field': |
|
| 234 | - $input_class = 'EE_Hidden_Input'; |
|
| 235 | - break; |
|
| 236 | - case 'EE_Serialized_Text_Field': |
|
| 237 | - $input_class = 'EE_Text_Area_Input'; |
|
| 238 | - break; |
|
| 239 | - case 'EE_Simple_HTML_Field': |
|
| 240 | - $input_class = 'EE_Text_Area_Input'; |
|
| 241 | - $input_constructor_args[0]['validation_strategies'] = array(new EE_Simple_HTML_Validation_Strategy()); |
|
| 242 | - break; |
|
| 243 | - case 'EE_Slug_Field': |
|
| 244 | - $input_class = 'EE_Text_Input'; |
|
| 245 | - break; |
|
| 246 | - case 'EE_Trashed_Flag_Field': |
|
| 247 | - $input_class = 'EE_Yes_No_Input'; |
|
| 248 | - break; |
|
| 249 | - case 'EE_WP_Post_Status_Field': |
|
| 250 | - throw new EE_Error(sprintf(__( |
|
| 251 | - "Model field '%s' does not yet have a known conversion to form input", |
|
| 252 | - "event_espresso" |
|
| 253 | - ), get_class($model_field))); |
|
| 254 | - break; |
|
| 255 | - case 'EE_WP_Post_Type_Field': |
|
| 256 | - throw new EE_Error(sprintf(__( |
|
| 257 | - "Model field '%s' does not yet have a known conversion to form input", |
|
| 258 | - "event_espresso" |
|
| 259 | - ), get_class($model_field))); |
|
| 260 | - break; |
|
| 261 | - default: |
|
| 262 | - throw new EE_Error(sprintf(__( |
|
| 263 | - "Model field of type '%s' does not convert to any known Form Input. Please add a case to EE_Model_Form_section's _convert_model_fields_to_inputs switch statement", |
|
| 264 | - "event_espresso" |
|
| 265 | - ), get_class($model_field))); |
|
| 266 | - } |
|
| 267 | - $reflection = new ReflectionClass($input_class); |
|
| 268 | - $input = $reflection->newInstanceArgs($input_constructor_args); |
|
| 269 | - $inputs[ $field_name ] = $input; |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - return $inputs; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Mostly the same as populate_defaults , except takes a model object as input, not an array, |
|
| 279 | - * and also sets the form's _model_object |
|
| 280 | - * |
|
| 281 | - * @param EE_Base_Class $model_obj |
|
| 282 | - * @return void |
|
| 283 | - */ |
|
| 284 | - public function populate_model_obj($model_obj) |
|
| 285 | - { |
|
| 286 | - $model_obj = $this->_model->ensure_is_obj($model_obj); |
|
| 287 | - $this->_model_object = $model_obj; |
|
| 288 | - $defaults = $model_obj->model_field_array(); |
|
| 289 | - foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
| 290 | - $subsection = $this->get_subsection($relation_name, false); |
|
| 291 | - if ($subsection instanceof EE_Form_Input_Base) { |
|
| 292 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 293 | - // then we only expect there to be one |
|
| 294 | - $related_item = $this->_model_object->get_first_related($relation_name); |
|
| 295 | - $defaults[ $relation_name ] = $related_item->ID(); |
|
| 296 | - } else { |
|
| 297 | - $related_items = $this->_model_object->get_many_related($relation_name); |
|
| 298 | - $ids = array(); |
|
| 299 | - foreach ($related_items as $related_item) { |
|
| 300 | - $ids[] = $related_item->ID(); |
|
| 301 | - } |
|
| 302 | - $defaults[ $relation_name ] = $ids; |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - } |
|
| 306 | - $defaults = apply_filters( |
|
| 307 | - 'FHEE__EE_Model_Form_Section__populate_model_obj', |
|
| 308 | - $defaults, |
|
| 309 | - $this |
|
| 310 | - ); |
|
| 311 | - $this->populate_defaults($defaults); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Gets all the input values that correspond to model fields. Keys are the input/field names, |
|
| 318 | - * values are their normalized values |
|
| 319 | - * |
|
| 320 | - * @return array |
|
| 321 | - */ |
|
| 322 | - public function inputs_values_corresponding_to_model_fields() |
|
| 323 | - { |
|
| 324 | - return array_intersect_key($this->input_values(), $this->_model->field_settings()); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * After we've normalized the data as normal, set the corresponding model object |
|
| 331 | - * on the form. |
|
| 332 | - * |
|
| 333 | - * @param array $req_data should usually be $_REQUEST (the default). |
|
| 334 | - * @return void |
|
| 335 | - */ |
|
| 336 | - public function _normalize($req_data) |
|
| 337 | - { |
|
| 338 | - parent::_normalize($req_data); |
|
| 339 | - // create or set the model object, if it isn't already |
|
| 340 | - if (! $this->_model_object) { |
|
| 341 | - // check to see if the form indicates a PK, in which case we want to only retrieve it and update it |
|
| 342 | - $pk_name = $this->_model->primary_key_name(); |
|
| 343 | - $model_obj = $this->_model->get_one_by_ID($this->get_input_value($pk_name)); |
|
| 344 | - if ($model_obj) { |
|
| 345 | - $this->_model_object = $model_obj; |
|
| 346 | - } else { |
|
| 347 | - $this->_model_object = EE_Registry::instance()->load_class($this->_model->get_this_model_name()); |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * After this form has been initialized and is verified to be valid, |
|
| 356 | - * either creates a model object from its data and saves it, or updates |
|
| 357 | - * the model object its data represents |
|
| 358 | - * |
|
| 359 | - * @throws EE_Error |
|
| 360 | - * @return int, 1 on a successful update, the ID of |
|
| 361 | - * the new entry on insert; 0 on failure |
|
| 362 | - */ |
|
| 363 | - public function save() |
|
| 364 | - { |
|
| 365 | - if (! $this->_model_object) { |
|
| 366 | - throw new EE_Error(sprintf(__( |
|
| 367 | - "Cannot save the model form's model object (model is '%s') because there is no model object set. You must either set it, or call receive_form_submission where it is set automatically", |
|
| 368 | - "event_espresso" |
|
| 369 | - ), get_class($this->_model))); |
|
| 370 | - } |
|
| 371 | - // ok so the model object is set. Just set it with the submitted form data |
|
| 372 | - foreach ($this->inputs_values_corresponding_to_model_fields() as $field_name => $field_value) { |
|
| 373 | - // only set the non-primary key |
|
| 374 | - if ($field_name != $this->_model->primary_key_name()) { |
|
| 375 | - $this->_model_object->set($field_name, $field_value); |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - $success = $this->_model_object->save(); |
|
| 379 | - foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
| 380 | - if (isset($this->_subsections[ $relation_name ])) { |
|
| 381 | - $success = $this->_save_related_info($relation_name); |
|
| 382 | - } |
|
| 383 | - } |
|
| 384 | - do_action('AHEE__EE_Model_Form_Section__save__done', $this, $success); |
|
| 385 | - return $success; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Automatically finds the related model info from the form, if present, and |
|
| 392 | - * save the relations indicated |
|
| 393 | - * |
|
| 394 | - * @type string $relation_name |
|
| 395 | - * @return bool |
|
| 396 | - * @throws EE_Error |
|
| 397 | - */ |
|
| 398 | - protected function _save_related_info($relation_name) |
|
| 399 | - { |
|
| 400 | - $relation_obj = $this->_model->related_settings_for($relation_name); |
|
| 401 | - if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 402 | - // there is just a foreign key on this model pointing to that one |
|
| 403 | - $this->_model_object->_add_relation_to($this->get_input_value($relation_name), $relation_name); |
|
| 404 | - } elseif ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 405 | - // then we want to consider all of its currently-related things. |
|
| 406 | - // if they're in this list, keep them |
|
| 407 | - // if they're not in this list, remove them |
|
| 408 | - // and lastly add all the new items |
|
| 409 | - throw new EE_Error(sprintf(__( |
|
| 410 | - 'Automatic saving of related info across a "has many" relation is not yet supported', |
|
| 411 | - "event_espresso" |
|
| 412 | - ))); |
|
| 413 | - } elseif ($relation_obj instanceof EE_HABTM_Relation) { |
|
| 414 | - // delete everything NOT in this list |
|
| 415 | - $normalized_input_value = $this->get_input_value($relation_name); |
|
| 416 | - if ($normalized_input_value && is_array($normalized_input_value)) { |
|
| 417 | - $where_query_params = array( |
|
| 418 | - $relation_obj->get_other_model()->primary_key_name() => array('NOT_IN', $normalized_input_value), |
|
| 419 | - ); |
|
| 420 | - } else { |
|
| 421 | - $where_query_params = array(); |
|
| 422 | - } |
|
| 423 | - $this->_model_object->_remove_relations($relation_name, $where_query_params); |
|
| 424 | - foreach ($normalized_input_value as $id) { |
|
| 425 | - $this->_model_object->_add_relation_to($id, $relation_name); |
|
| 426 | - } |
|
| 427 | - } |
|
| 428 | - return true; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * Gets the model of this model form |
|
| 435 | - * |
|
| 436 | - * @return EEM_Base |
|
| 437 | - */ |
|
| 438 | - public function get_model() |
|
| 439 | - { |
|
| 440 | - return $this->_model; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Gets the model object for this model form, which was either set |
|
| 447 | - * upon construction (using the $options_array arg 'model_object'), by using |
|
| 448 | - * set_model_object($model_obj), or implicitly |
|
| 449 | - * when receive_form_submission($req_data) was called. |
|
| 450 | - * |
|
| 451 | - * @return EE_Base_Class |
|
| 452 | - */ |
|
| 453 | - public function get_model_object() |
|
| 454 | - { |
|
| 455 | - return $this->_model_object; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - |
|
| 459 | - |
|
| 460 | - /** |
|
| 461 | - * gets teh default name of this form section if none is specified |
|
| 462 | - * |
|
| 463 | - * @return string |
|
| 464 | - */ |
|
| 465 | - protected function _set_default_name_if_empty() |
|
| 466 | - { |
|
| 467 | - if (! $this->_name) { |
|
| 468 | - $default_name = str_replace("EEM_", "", get_class($this->_model)) . "_Model_Form"; |
|
| 469 | - $this->_name = $default_name; |
|
| 470 | - } |
|
| 471 | - } |
|
| 17 | + /** |
|
| 18 | + * @var EEM_Base |
|
| 19 | + */ |
|
| 20 | + protected $_model = null; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var EE_Base_Class |
|
| 24 | + */ |
|
| 25 | + protected $_model_object = null; |
|
| 26 | + |
|
| 27 | + |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param array $options_array keys: { |
|
| 31 | + * @type EEM_Base $model |
|
| 32 | + * @type EE_Base_Class $model_object |
|
| 33 | + * @type array $subsection_args array keys should be subsection names (that either do or will exist), and |
|
| 34 | + * values are the arrays as you would pass them to that subsection |
|
| 35 | + * } |
|
| 36 | + * @throws EE_Error |
|
| 37 | + */ |
|
| 38 | + public function __construct($options_array = array()) |
|
| 39 | + { |
|
| 40 | + if (isset($options_array['model']) && $options_array['model'] instanceof EEM_Base) { |
|
| 41 | + $this->_model = $options_array['model']; |
|
| 42 | + } |
|
| 43 | + if (! $this->_model || ! $this->_model instanceof EEM_Base) { |
|
| 44 | + throw new EE_Error(sprintf(__( |
|
| 45 | + "Model Form Sections must first specify the _model property to be a subclass of EEM_Base", |
|
| 46 | + "event_espresso" |
|
| 47 | + ))); |
|
| 48 | + } |
|
| 49 | + if (isset($options_array['subsection_args'])) { |
|
| 50 | + $subsection_args = $options_array['subsection_args']; |
|
| 51 | + } else { |
|
| 52 | + $subsection_args = array(); |
|
| 53 | + } |
|
| 54 | + // gather fields and relations to convert to inputs |
|
| 55 | + // but if they're just going to exclude a field anyways, don't bother converting it to an input |
|
| 56 | + $exclude = $this->_subsections; |
|
| 57 | + if (isset($options_array['exclude'])) { |
|
| 58 | + $exclude = array_merge($exclude, array_flip($options_array['exclude'])); |
|
| 59 | + } |
|
| 60 | + $model_fields = array_diff_key($this->_model->field_settings(), $exclude); |
|
| 61 | + $model_relations = array_diff_key($this->_model->relation_settings(), $exclude); |
|
| 62 | + // convert fields and relations to inputs |
|
| 63 | + $this->_subsections = array_merge( |
|
| 64 | + $this->_convert_model_fields_to_inputs($model_fields), |
|
| 65 | + $this->_convert_model_relations_to_inputs($model_relations, $subsection_args), |
|
| 66 | + $this->_subsections |
|
| 67 | + ); |
|
| 68 | + parent::__construct($options_array); |
|
| 69 | + if (isset($options_array['model_object']) && $options_array['model_object'] instanceof EE_Base_Class) { |
|
| 70 | + $this->populate_model_obj($options_array['model_object']); |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * For now, just makes inputs for only HABTM relations |
|
| 78 | + * |
|
| 79 | + * @param EE_Model_Relation_Base[] $relations |
|
| 80 | + * @param array $subsection_args keys should be existing or soon-to-be-existing input names, and |
|
| 81 | + * their values are { |
|
| 82 | + * @type array { |
|
| 83 | + * @type EE_Base_Class[] $model_objects if the subsection is an EE_Select_Multi_Model_Input |
|
| 84 | + * } |
|
| 85 | + * } |
|
| 86 | + * @return array |
|
| 87 | + */ |
|
| 88 | + protected function _convert_model_relations_to_inputs($relations, $subsection_args = array()) |
|
| 89 | + { |
|
| 90 | + $inputs = array(); |
|
| 91 | + foreach ($relations as $relation_name => $relation_obj) { |
|
| 92 | + $input_constructor_args = array( |
|
| 93 | + array_merge( |
|
| 94 | + array( |
|
| 95 | + 'required' => $relation_obj instanceof EE_Belongs_To_Relation, |
|
| 96 | + 'html_label_text' => $relation_obj instanceof EE_Belongs_To_Relation |
|
| 97 | + ? $relation_obj->get_other_model()->item_name(1) |
|
| 98 | + : $relation_obj->get_other_model() |
|
| 99 | + ->item_name(2), |
|
| 100 | + ), |
|
| 101 | + $subsection_args |
|
| 102 | + ), |
|
| 103 | + ); |
|
| 104 | + $input = null; |
|
| 105 | + switch (get_class($relation_obj)) { |
|
| 106 | + case 'EE_HABTM_Relation': |
|
| 107 | + if (isset($subsection_args[ $relation_name ]) |
|
| 108 | + && isset($subsection_args[ $relation_name ]['model_objects']) |
|
| 109 | + ) { |
|
| 110 | + $model_objects = $subsection_args[ $relation_name ]['model_objects']; |
|
| 111 | + } else { |
|
| 112 | + $model_objects = $relation_obj->get_other_model()->get_all(); |
|
| 113 | + } |
|
| 114 | + $input = new EE_Select_Multi_Model_Input($model_objects, $input_constructor_args); |
|
| 115 | + break; |
|
| 116 | + default: |
|
| 117 | + } |
|
| 118 | + if ($input) { |
|
| 119 | + $inputs[ $relation_name ] = $input; |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + return $inputs; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Changes model fields into form section inputs |
|
| 129 | + * |
|
| 130 | + * @param EE_Model_Field_Base[] $model_fields keys are the model's name |
|
| 131 | + * @throws EE_Error |
|
| 132 | + * @return EE_Form_Input_Base[] |
|
| 133 | + */ |
|
| 134 | + protected function _convert_model_fields_to_inputs($model_fields = array()) |
|
| 135 | + { |
|
| 136 | + $inputs = array(); |
|
| 137 | + foreach ($model_fields as $field_name => $model_field) { |
|
| 138 | + if ($model_field instanceof EE_Model_Field_Base) { |
|
| 139 | + $input_constructor_args = array( |
|
| 140 | + array( |
|
| 141 | + 'required' => ! $model_field->is_nullable() |
|
| 142 | + && $model_field->get_default_value() |
|
| 143 | + === null, |
|
| 144 | + 'html_label_text' => $model_field->get_nicename(), |
|
| 145 | + 'default' => $model_field->get_default_value(), |
|
| 146 | + ), |
|
| 147 | + ); |
|
| 148 | + switch (get_class($model_field)) { |
|
| 149 | + case 'EE_All_Caps_Text_Field': |
|
| 150 | + case 'EE_Any_Foreign_Model_Name_Field': |
|
| 151 | + $input_class = 'EE_Text_Input'; |
|
| 152 | + break; |
|
| 153 | + case 'EE_Boolean_Field': |
|
| 154 | + $input_class = 'EE_Yes_No_Input'; |
|
| 155 | + break; |
|
| 156 | + case 'EE_Datetime_Field': |
|
| 157 | + throw new EE_Error(sprintf(__( |
|
| 158 | + "Model field '%s' does not yet have a known conversion to form input", |
|
| 159 | + "event_espresso" |
|
| 160 | + ), get_class($model_field))); |
|
| 161 | + break; |
|
| 162 | + case 'EE_Email_Field': |
|
| 163 | + $input_class = 'EE_Email_Input'; |
|
| 164 | + break; |
|
| 165 | + case 'EE_Enum_Integer_Field': |
|
| 166 | + throw new EE_Error(sprintf(__( |
|
| 167 | + "Model field '%s' does not yet have a known conversion to form input", |
|
| 168 | + "event_espresso" |
|
| 169 | + ), get_class($model_field))); |
|
| 170 | + break; |
|
| 171 | + case 'EE_Enum_Text_Field': |
|
| 172 | + throw new EE_Error(sprintf(__( |
|
| 173 | + "Model field '%s' does not yet have a known conversion to form input", |
|
| 174 | + "event_espresso" |
|
| 175 | + ), get_class($model_field))); |
|
| 176 | + break; |
|
| 177 | + case 'EE_Float_Field': |
|
| 178 | + $input_class = 'EE_Float_Input'; |
|
| 179 | + break; |
|
| 180 | + case 'EE_Foreign_Key_Int_Field': |
|
| 181 | + case 'EE_Foreign_Key_String_Field': |
|
| 182 | + case 'EE_WP_User_Field': |
|
| 183 | + $models_pointed_to = $model_field instanceof EE_Field_With_Model_Name |
|
| 184 | + ? $model_field->get_model_class_names_pointed_to() : array(); |
|
| 185 | + if (true || is_array($models_pointed_to) && count($models_pointed_to) > 1) { |
|
| 186 | + $input_class = 'EE_Text_Input'; |
|
| 187 | + } else { |
|
| 188 | + // so its just one model |
|
| 189 | + $model_name = is_array($models_pointed_to) ? reset($models_pointed_to) : $models_pointed_to; |
|
| 190 | + $model = EE_Registry::instance()->load_model($model_name); |
|
| 191 | + $model_names = $model->get_all_names(array('limit' => 10)); |
|
| 192 | + if ($model_field->is_nullable()) { |
|
| 193 | + array_unshift($model_names, __("Please Select", 'event_espresso')); |
|
| 194 | + } |
|
| 195 | + $input_constructor_args[1] = $input_constructor_args[0]; |
|
| 196 | + $input_constructor_args[0] = $model_names; |
|
| 197 | + $input_class = 'EE_Select_Input'; |
|
| 198 | + } |
|
| 199 | + break; |
|
| 200 | + case 'EE_Full_HTML_Field': |
|
| 201 | + $input_class = 'EE_Text_Area_Input'; |
|
| 202 | + $input_constructor_args[0]['validation_strategies'] = array(new EE_Full_HTML_Validation_Strategy()); |
|
| 203 | + break; |
|
| 204 | + case 'EE_Infinite_Integer': |
|
| 205 | + throw new EE_Error(sprintf(__( |
|
| 206 | + "Model field '%s' does not yet have a known conversion to form input", |
|
| 207 | + "event_espresso" |
|
| 208 | + ), get_class($model_field))); |
|
| 209 | + break; |
|
| 210 | + case 'EE_Integer_Field': |
|
| 211 | + $input_class = 'EE_Text_Input'; |
|
| 212 | + break; |
|
| 213 | + case 'EE_Maybe_Serialized_Text_Field': |
|
| 214 | + $input_class = 'EE_Text_Area_Input'; |
|
| 215 | + break; |
|
| 216 | + case 'EE_Money_Field': |
|
| 217 | + throw new EE_Error(sprintf(__( |
|
| 218 | + "Model field '%s' does not yet have a known conversion to form input", |
|
| 219 | + "event_espresso" |
|
| 220 | + ), get_class($model_field))); |
|
| 221 | + break; |
|
| 222 | + case 'EE_Post_Content_Field': |
|
| 223 | + $input_class = 'EE_Text_Area_Input'; |
|
| 224 | + $input_constructor_args[0]['validation_strategies'] = array(new EE_Full_HTML_Validation_Strategy()); |
|
| 225 | + break; |
|
| 226 | + case 'EE_Plain_Text_Field': |
|
| 227 | + $input_class = 'EE_Text_Input'; |
|
| 228 | + break; |
|
| 229 | + case 'EE_Primary_Key_Int_Field': |
|
| 230 | + $input_class = 'EE_Hidden_Input'; |
|
| 231 | + $input_constructor_args[0]['normalization_strategy'] = new EE_Int_Normalization(); |
|
| 232 | + break; |
|
| 233 | + case 'EE_Primary_Key_String_Field': |
|
| 234 | + $input_class = 'EE_Hidden_Input'; |
|
| 235 | + break; |
|
| 236 | + case 'EE_Serialized_Text_Field': |
|
| 237 | + $input_class = 'EE_Text_Area_Input'; |
|
| 238 | + break; |
|
| 239 | + case 'EE_Simple_HTML_Field': |
|
| 240 | + $input_class = 'EE_Text_Area_Input'; |
|
| 241 | + $input_constructor_args[0]['validation_strategies'] = array(new EE_Simple_HTML_Validation_Strategy()); |
|
| 242 | + break; |
|
| 243 | + case 'EE_Slug_Field': |
|
| 244 | + $input_class = 'EE_Text_Input'; |
|
| 245 | + break; |
|
| 246 | + case 'EE_Trashed_Flag_Field': |
|
| 247 | + $input_class = 'EE_Yes_No_Input'; |
|
| 248 | + break; |
|
| 249 | + case 'EE_WP_Post_Status_Field': |
|
| 250 | + throw new EE_Error(sprintf(__( |
|
| 251 | + "Model field '%s' does not yet have a known conversion to form input", |
|
| 252 | + "event_espresso" |
|
| 253 | + ), get_class($model_field))); |
|
| 254 | + break; |
|
| 255 | + case 'EE_WP_Post_Type_Field': |
|
| 256 | + throw new EE_Error(sprintf(__( |
|
| 257 | + "Model field '%s' does not yet have a known conversion to form input", |
|
| 258 | + "event_espresso" |
|
| 259 | + ), get_class($model_field))); |
|
| 260 | + break; |
|
| 261 | + default: |
|
| 262 | + throw new EE_Error(sprintf(__( |
|
| 263 | + "Model field of type '%s' does not convert to any known Form Input. Please add a case to EE_Model_Form_section's _convert_model_fields_to_inputs switch statement", |
|
| 264 | + "event_espresso" |
|
| 265 | + ), get_class($model_field))); |
|
| 266 | + } |
|
| 267 | + $reflection = new ReflectionClass($input_class); |
|
| 268 | + $input = $reflection->newInstanceArgs($input_constructor_args); |
|
| 269 | + $inputs[ $field_name ] = $input; |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + return $inputs; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Mostly the same as populate_defaults , except takes a model object as input, not an array, |
|
| 279 | + * and also sets the form's _model_object |
|
| 280 | + * |
|
| 281 | + * @param EE_Base_Class $model_obj |
|
| 282 | + * @return void |
|
| 283 | + */ |
|
| 284 | + public function populate_model_obj($model_obj) |
|
| 285 | + { |
|
| 286 | + $model_obj = $this->_model->ensure_is_obj($model_obj); |
|
| 287 | + $this->_model_object = $model_obj; |
|
| 288 | + $defaults = $model_obj->model_field_array(); |
|
| 289 | + foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
| 290 | + $subsection = $this->get_subsection($relation_name, false); |
|
| 291 | + if ($subsection instanceof EE_Form_Input_Base) { |
|
| 292 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 293 | + // then we only expect there to be one |
|
| 294 | + $related_item = $this->_model_object->get_first_related($relation_name); |
|
| 295 | + $defaults[ $relation_name ] = $related_item->ID(); |
|
| 296 | + } else { |
|
| 297 | + $related_items = $this->_model_object->get_many_related($relation_name); |
|
| 298 | + $ids = array(); |
|
| 299 | + foreach ($related_items as $related_item) { |
|
| 300 | + $ids[] = $related_item->ID(); |
|
| 301 | + } |
|
| 302 | + $defaults[ $relation_name ] = $ids; |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + } |
|
| 306 | + $defaults = apply_filters( |
|
| 307 | + 'FHEE__EE_Model_Form_Section__populate_model_obj', |
|
| 308 | + $defaults, |
|
| 309 | + $this |
|
| 310 | + ); |
|
| 311 | + $this->populate_defaults($defaults); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Gets all the input values that correspond to model fields. Keys are the input/field names, |
|
| 318 | + * values are their normalized values |
|
| 319 | + * |
|
| 320 | + * @return array |
|
| 321 | + */ |
|
| 322 | + public function inputs_values_corresponding_to_model_fields() |
|
| 323 | + { |
|
| 324 | + return array_intersect_key($this->input_values(), $this->_model->field_settings()); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * After we've normalized the data as normal, set the corresponding model object |
|
| 331 | + * on the form. |
|
| 332 | + * |
|
| 333 | + * @param array $req_data should usually be $_REQUEST (the default). |
|
| 334 | + * @return void |
|
| 335 | + */ |
|
| 336 | + public function _normalize($req_data) |
|
| 337 | + { |
|
| 338 | + parent::_normalize($req_data); |
|
| 339 | + // create or set the model object, if it isn't already |
|
| 340 | + if (! $this->_model_object) { |
|
| 341 | + // check to see if the form indicates a PK, in which case we want to only retrieve it and update it |
|
| 342 | + $pk_name = $this->_model->primary_key_name(); |
|
| 343 | + $model_obj = $this->_model->get_one_by_ID($this->get_input_value($pk_name)); |
|
| 344 | + if ($model_obj) { |
|
| 345 | + $this->_model_object = $model_obj; |
|
| 346 | + } else { |
|
| 347 | + $this->_model_object = EE_Registry::instance()->load_class($this->_model->get_this_model_name()); |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * After this form has been initialized and is verified to be valid, |
|
| 356 | + * either creates a model object from its data and saves it, or updates |
|
| 357 | + * the model object its data represents |
|
| 358 | + * |
|
| 359 | + * @throws EE_Error |
|
| 360 | + * @return int, 1 on a successful update, the ID of |
|
| 361 | + * the new entry on insert; 0 on failure |
|
| 362 | + */ |
|
| 363 | + public function save() |
|
| 364 | + { |
|
| 365 | + if (! $this->_model_object) { |
|
| 366 | + throw new EE_Error(sprintf(__( |
|
| 367 | + "Cannot save the model form's model object (model is '%s') because there is no model object set. You must either set it, or call receive_form_submission where it is set automatically", |
|
| 368 | + "event_espresso" |
|
| 369 | + ), get_class($this->_model))); |
|
| 370 | + } |
|
| 371 | + // ok so the model object is set. Just set it with the submitted form data |
|
| 372 | + foreach ($this->inputs_values_corresponding_to_model_fields() as $field_name => $field_value) { |
|
| 373 | + // only set the non-primary key |
|
| 374 | + if ($field_name != $this->_model->primary_key_name()) { |
|
| 375 | + $this->_model_object->set($field_name, $field_value); |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + $success = $this->_model_object->save(); |
|
| 379 | + foreach ($this->_model->relation_settings() as $relation_name => $relation_obj) { |
|
| 380 | + if (isset($this->_subsections[ $relation_name ])) { |
|
| 381 | + $success = $this->_save_related_info($relation_name); |
|
| 382 | + } |
|
| 383 | + } |
|
| 384 | + do_action('AHEE__EE_Model_Form_Section__save__done', $this, $success); |
|
| 385 | + return $success; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Automatically finds the related model info from the form, if present, and |
|
| 392 | + * save the relations indicated |
|
| 393 | + * |
|
| 394 | + * @type string $relation_name |
|
| 395 | + * @return bool |
|
| 396 | + * @throws EE_Error |
|
| 397 | + */ |
|
| 398 | + protected function _save_related_info($relation_name) |
|
| 399 | + { |
|
| 400 | + $relation_obj = $this->_model->related_settings_for($relation_name); |
|
| 401 | + if ($relation_obj instanceof EE_Belongs_To_Relation) { |
|
| 402 | + // there is just a foreign key on this model pointing to that one |
|
| 403 | + $this->_model_object->_add_relation_to($this->get_input_value($relation_name), $relation_name); |
|
| 404 | + } elseif ($relation_obj instanceof EE_Has_Many_Relation) { |
|
| 405 | + // then we want to consider all of its currently-related things. |
|
| 406 | + // if they're in this list, keep them |
|
| 407 | + // if they're not in this list, remove them |
|
| 408 | + // and lastly add all the new items |
|
| 409 | + throw new EE_Error(sprintf(__( |
|
| 410 | + 'Automatic saving of related info across a "has many" relation is not yet supported', |
|
| 411 | + "event_espresso" |
|
| 412 | + ))); |
|
| 413 | + } elseif ($relation_obj instanceof EE_HABTM_Relation) { |
|
| 414 | + // delete everything NOT in this list |
|
| 415 | + $normalized_input_value = $this->get_input_value($relation_name); |
|
| 416 | + if ($normalized_input_value && is_array($normalized_input_value)) { |
|
| 417 | + $where_query_params = array( |
|
| 418 | + $relation_obj->get_other_model()->primary_key_name() => array('NOT_IN', $normalized_input_value), |
|
| 419 | + ); |
|
| 420 | + } else { |
|
| 421 | + $where_query_params = array(); |
|
| 422 | + } |
|
| 423 | + $this->_model_object->_remove_relations($relation_name, $where_query_params); |
|
| 424 | + foreach ($normalized_input_value as $id) { |
|
| 425 | + $this->_model_object->_add_relation_to($id, $relation_name); |
|
| 426 | + } |
|
| 427 | + } |
|
| 428 | + return true; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * Gets the model of this model form |
|
| 435 | + * |
|
| 436 | + * @return EEM_Base |
|
| 437 | + */ |
|
| 438 | + public function get_model() |
|
| 439 | + { |
|
| 440 | + return $this->_model; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Gets the model object for this model form, which was either set |
|
| 447 | + * upon construction (using the $options_array arg 'model_object'), by using |
|
| 448 | + * set_model_object($model_obj), or implicitly |
|
| 449 | + * when receive_form_submission($req_data) was called. |
|
| 450 | + * |
|
| 451 | + * @return EE_Base_Class |
|
| 452 | + */ |
|
| 453 | + public function get_model_object() |
|
| 454 | + { |
|
| 455 | + return $this->_model_object; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + |
|
| 459 | + |
|
| 460 | + /** |
|
| 461 | + * gets teh default name of this form section if none is specified |
|
| 462 | + * |
|
| 463 | + * @return string |
|
| 464 | + */ |
|
| 465 | + protected function _set_default_name_if_empty() |
|
| 466 | + { |
|
| 467 | + if (! $this->_name) { |
|
| 468 | + $default_name = str_replace("EEM_", "", get_class($this->_model)) . "_Model_Form"; |
|
| 469 | + $this->_name = $default_name; |
|
| 470 | + } |
|
| 471 | + } |
|
| 472 | 472 | } |