Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EEH_Form_Fields often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EEH_Form_Fields, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class EEH_Form_Fields { |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Generates HTML for the forms used on admin pages |
||
| 48 | * |
||
| 49 | * |
||
| 50 | * @static |
||
| 51 | * @access public |
||
| 52 | * @param array $input_vars - array of input field details |
||
| 53 | * format: |
||
| 54 | * $template_form_fields['field-id'] = array( |
||
| 55 | * 'name' => 'name_attribute', |
||
| 56 | * 'label' => __('Field Label', 'event_espresso'), //or false |
||
| 57 | * 'input' => 'hidden', //field input type can be 'text', 'select', 'textarea', 'hidden', 'checkbox', 'wp_editor' |
||
| 58 | * 'type' => 'int', //what "type" the value is (i.e. string, int etc) |
||
| 59 | * 'required' => false, //boolean for whether the field is required |
||
| 60 | * 'validation' => true, //boolean, whether to validate the field (todo) |
||
| 61 | * 'value' => 'some_value_for_field', //what value is used for field |
||
| 62 | * 'format' => '%d', //what format the value is (%d, %f, or %s) |
||
| 63 | * 'db-col' => 'column_in_db' //used to indicate which column the field corresponds with in the db |
||
| 64 | * 'options' => optiona, optionb || array('value' => 'label', '') //if the input type is "select", this allows you to set the args for the different <option> tags. |
||
| 65 | * 'tabindex' => 1 //this allows you to set the tabindex for the field. |
||
| 66 | * 'append_content' => '' //this allows you to send in html content to append to the field. |
||
| 67 | * ) |
||
| 68 | * @param array $id - used for defining unique identifiers for the form. |
||
| 69 | * @return string |
||
| 70 | * @todo: at some point we can break this down into other static methods to abstract it a bit better. |
||
| 71 | */ |
||
| 72 | static public function get_form_fields( $input_vars = array(), $id = FALSE ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * form_fields_array |
||
| 206 | * This utility function assembles form fields from a given structured array with field information. |
||
| 207 | * //TODO: This is an alternate generator that we may want to use instead. |
||
| 208 | * |
||
| 209 | * @param array $fields structured array of fields to assemble in the following format: |
||
| 210 | * [field_name] => array( |
||
| 211 | * ['label'] => 'label for field', |
||
| 212 | * ['labels'] => array('label_1', 'label_2'); //optional - if the field type is a multi select type of field you can indicated the labels for each option via this index |
||
| 213 | * ['extra_desc'] => 'extra description for the field', //optional |
||
| 214 | * ['type'] => 'textarea'|'text'|'wp_editor'|'checkbox'|'radio'|'hidden'|'select', //defaults to text |
||
| 215 | * ['value'] => 'value that goes in the field', //(if multi then this is an array of values and the 'default' paramater will be used for what is selected) |
||
| 216 | * ['default'] => 'default if the field type is multi (i.e. select or radios or checkboxes)', |
||
| 217 | * ['class'] => 'name-of-class(es)-for-input', |
||
| 218 | * ['classes'] => array('class_1', 'class_2'); //optional - if the field type is a multi select type of field you can indicate the css class for each option via this index. |
||
| 219 | * ['id'] => 'css-id-for-input') //defaults to 'field_name' |
||
| 220 | * ['unique_id'] => 1 //defaults to empty string. This is useful for when the fields generated are going to be used in a loop and you want to make sure that the field identifiers are unique from each other. |
||
| 221 | * ['dimensions'] => array(100,300), //defaults to empty array. This is used by field types such as textarea to indicate cols/rows. |
||
| 222 | * ['tabindex'] => '' //this allows you to set the tabindex for the field. |
||
| 223 | * ['wpeditor_args'] => array() //if the type of field is wpeditor then this can optionally contain an array of arguments for the editor setup. |
||
| 224 | * |
||
| 225 | * @return array an array of inputs for form indexed by field name, and in the following structure: |
||
| 226 | * [field_name] => array( 'label' => '{label_html}', 'field' => '{input_html}' |
||
| 227 | */ |
||
| 228 | static public function get_form_fields_array($fields) { |
||
| 229 | |||
| 230 | $form_fields = array(); |
||
| 231 | $fields = (array) $fields; |
||
| 232 | |||
| 233 | foreach ( $fields as $field_name => $field_atts ) { |
||
| 234 | //defaults: |
||
| 235 | $defaults = array( |
||
| 236 | 'label' => '', |
||
| 237 | 'labels' => '', |
||
| 238 | 'extra_desc' => '', |
||
| 239 | 'type' => 'text', |
||
| 240 | 'value' => '', |
||
| 241 | 'default' => '', |
||
| 242 | 'class' => '', |
||
| 243 | 'classes' => '', |
||
| 244 | 'id' => $field_name, |
||
| 245 | 'unique_id' => '', |
||
| 246 | 'dimensions' => array('10', '5'), |
||
| 247 | 'tabindex' => '', |
||
| 248 | 'wpeditor_args' => array() |
||
| 249 | ); |
||
| 250 | // merge defaults with passed arguments |
||
| 251 | $_fields = wp_parse_args( $field_atts, $defaults); |
||
| 252 | extract( $_fields ); |
||
| 253 | // generate label |
||
| 254 | $label = empty($label) ? '' : '<label for="' . $id . '">' . $label . '</label>'; |
||
| 255 | // generate field name |
||
| 256 | $f_name = !empty($unique_id) ? $field_name . '[' . $unique_id . ']' : $field_name; |
||
| 257 | |||
| 258 | //tabindex |
||
| 259 | $tabindex_str = !empty( $tabindex ) ? ' tabindex="' . $tabindex . '"' : ''; |
||
| 260 | |||
| 261 | //we determine what we're building based on the type |
||
| 262 | switch ( $type ) { |
||
| 263 | |||
| 264 | case 'textarea' : |
||
| 265 | $fld = '<textarea id="' . $id . '" class="' . $class . '" rows="' . $dimensions[1] . '" cols="' . $dimensions[0] . '" name="' . $f_name . '"' . $tabindex_str . '>' . $value . '</textarea>'; |
||
| 266 | $fld .= $extra_desc; |
||
| 267 | break; |
||
| 268 | |||
| 269 | View Code Duplication | case 'checkbox' : |
|
| 270 | $c_input = ''; |
||
| 271 | if ( is_array($value) ) { |
||
| 272 | foreach ( $value as $key => $val ) { |
||
| 273 | $c_id = $field_name . '_' . $value; |
||
| 274 | $c_class = isset($classes[$key]) ? ' class="' . $classes[$key] . '" ' : ''; |
||
| 275 | $c_label = isset($labels[$key]) ? '<label for="' . $c_id . '">' . $labels[$key] . '</label>' : ''; |
||
| 276 | $checked = !empty($default) && $default == $val ? ' checked="checked" ' : ''; |
||
| 277 | $c_input .= '<input name="' . $f_name . '[]" type="checkbox" id="' . $c_id . '"' . $c_class . 'value="' . $val . '"' . $checked . $tabindex_str . ' />' . "\n" . $c_label; |
||
| 278 | } |
||
| 279 | $fld = $c_input; |
||
| 280 | } else { |
||
| 281 | $checked = !empty($default) && $default == $val ? 'checked="checked" ' : ''; |
||
| 282 | $fld = '<input name="'. $f_name . '" type="checkbox" id="' . $id . '" class="' . $class . '" value="' . $value . '"' . $checked . $tabindex_str . ' />' . "\n"; |
||
| 283 | } |
||
| 284 | break; |
||
| 285 | |||
| 286 | View Code Duplication | case 'radio' : |
|
| 287 | $c_input = ''; |
||
| 288 | if ( is_array($value) ) { |
||
| 289 | foreach ( $value as $key => $val ) { |
||
| 290 | $c_id = $field_name . '_' . $value; |
||
| 291 | $c_class = isset($classes[$key]) ? 'class="' . $classes[$key] . '" ' : ''; |
||
| 292 | $c_label = isset($labels[$key]) ? '<label for="' . $c_id . '">' . $labels[$key] . '</label>' : ''; |
||
| 293 | $checked = !empty($default) && $default == $val ? ' checked="checked" ' : ''; |
||
| 294 | $c_input .= '<input name="' . $f_name . '" type="checkbox" id="' . $c_id . '"' . $c_class . 'value="' . $val . '"' . $checked . $tabindex_str . ' />' . "\n" . $c_label; |
||
| 295 | } |
||
| 296 | $fld = $c_input; |
||
| 297 | } else { |
||
| 298 | $checked = !empty($default) && $default == $val ? 'checked="checked" ' : ''; |
||
| 299 | $fld = '<input name="'. $f_name . '" type="checkbox" id="' . $id . '" class="' . $class . '" value="' . $value . '"' . $checked . $tabindex_str . ' />' . "\n"; |
||
| 300 | } |
||
| 301 | break; |
||
| 302 | |||
| 303 | case 'hidden' : |
||
| 304 | $fld = '<input name="' . $f_name . '" type="hidden" id="' . $id . '" class="' . $class . '" value="' . $value . '" />' . "\n"; |
||
| 305 | break; |
||
| 306 | |||
| 307 | case 'select' : |
||
| 308 | $fld = '<select name="' . $f_name . '" class="' . $class . '" id="' . $id . '"' . $tabindex_str . '>' . "\n"; |
||
| 309 | foreach ( $value as $key => $val ) { |
||
| 310 | $checked = !empty($default) && $default == $val ? ' selected="selected"' : ''; |
||
| 311 | $fld .= "\t" . '<option value="' . $val . '"' . $checked . '>' . $labels[$key] . '</option>' . "\n"; |
||
| 312 | } |
||
| 313 | $fld .= '</select>'; |
||
| 314 | break; |
||
| 315 | |||
| 316 | case 'wp_editor' : |
||
| 317 | $editor_settings = array( |
||
| 318 | 'textarea_name' => $f_name, |
||
| 319 | 'textarea_rows' => $dimensions[1], |
||
| 320 | 'editor_class' => $class, |
||
| 321 | 'tabindex' => $tabindex |
||
| 322 | ); |
||
| 323 | $editor_settings = array_merge( $wpeditor_args, $editor_settings ); |
||
| 324 | ob_start(); |
||
| 325 | wp_editor( $value, $id, $editor_settings ); |
||
| 326 | $editor = ob_get_contents(); |
||
| 327 | ob_end_clean(); |
||
| 328 | $fld = $editor; |
||
| 329 | break; |
||
| 330 | |||
| 331 | default : //'text fields' |
||
| 332 | $fld = '<input name="' . $f_name . '" type="text" id="' . $id . '" class="' . $class . '" value="' . $value . '"' . $tabindex_str . ' />' . "\n"; |
||
| 333 | $fld .= $extra_desc; |
||
| 334 | |||
| 335 | } |
||
| 336 | |||
| 337 | $form_fields[ $field_name ] = array( 'label' => $label, 'field' => $fld ); |
||
| 338 | } |
||
| 339 | |||
| 340 | return $form_fields; |
||
| 341 | } |
||
| 342 | |||
| 343 | |||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * espresso admin page select_input |
||
| 350 | * Turns an array into a select fields |
||
| 351 | * |
||
| 352 | * @static |
||
| 353 | * @access public |
||
| 354 | * @param string $name field name |
||
| 355 | * @param array $values option values, numbered array starting at 0, where each value is an array with a key 'text' (meaning text to display' and 'id' (meaning the internal value) |
||
| 356 | * eg: array(1=>array('text'=>'Monday','id'=>1),2=>array('text'=>'Tuesday','id'=>2)...). or as an array of key-value pairs, where the key is to be used for the |
||
| 357 | * select input's name, and the value will be the text shown to the user. Optionally you can also include an additional key of "class" which will add a specific class to the option for that value. |
||
| 358 | * @param string $default default value |
||
| 359 | * @param string $parameters extra paramaters |
||
| 360 | * @param string $class css class |
||
| 361 | * @param boolean $autosize whether to autosize the select or not |
||
| 362 | * @return string html string for the select input |
||
| 363 | */ |
||
| 364 | static public function select_input($name, $values, $default = '', $parameters = '', $class = '', $autosize = true) { |
||
| 365 | //if $values was submitted in the wrong format, convert it over |
||
| 366 | if(!empty($values) && (!array_key_exists(0,$values) || !is_array($values[0]))){ |
||
| 367 | $converted_values=array(); |
||
| 368 | foreach($values as $id=>$text){ |
||
| 369 | $converted_values[]=array('id'=>$id,'text'=>$text); |
||
| 370 | } |
||
| 371 | $values=$converted_values; |
||
| 372 | } |
||
| 373 | //load formatter helper |
||
| 374 | EE_Registry::instance()->load_helper( 'Formatter' ); |
||
| 375 | //EE_Registry::instance()->load_helper( 'Formatter' ); |
||
| 376 | |||
| 377 | $field = '<select id="' . EEH_Formatter::ee_tep_output_string($name) . '" name="' . EEH_Formatter::ee_tep_output_string($name) . '"'; |
||
| 378 | //Debug |
||
| 379 | //EEH_Debug_Tools::printr( $values, '$values <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
||
| 380 | if ( EEH_Formatter::ee_tep_not_null($parameters)) |
||
| 381 | $field .= ' ' . $parameters; |
||
| 382 | if ($autosize) { |
||
| 383 | $size = 'med'; |
||
| 384 | for ($ii = 0, $ni = sizeof($values); $ii < $ni; $ii++) { |
||
| 385 | if ($values[$ii]['text']) { |
||
| 386 | if (strlen($values[$ii]['text']) > 5) |
||
| 387 | $size = 'wide'; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | } else { |
||
| 391 | $size = ''; |
||
| 392 | } |
||
| 393 | |||
| 394 | $field .= ' class="' . $class . ' ' . $size . '">'; |
||
| 395 | |||
| 396 | if (empty($default) && isset($GLOBALS[$name])) |
||
| 397 | $default = stripslashes($GLOBALS[$name]); |
||
| 398 | |||
| 399 | |||
| 400 | for ($i = 0, $n = sizeof($values); $i < $n; $i++) { |
||
| 401 | $field .= '<option value="' . $values[$i]['id'] . '"'; |
||
| 402 | if ($default == $values[$i]['id']) { |
||
| 403 | $field .= ' selected = "selected"'; |
||
| 404 | } |
||
| 405 | if ( isset( $values[$i]['class'] ) ) { |
||
| 406 | $field .= ' class="' . $values[$i]['class'] . '"'; |
||
| 407 | } |
||
| 408 | $field .= '>' . $values[$i]['text'] . '</option>'; |
||
| 409 | } |
||
| 410 | $field .= '</select>'; |
||
| 411 | |||
| 412 | return $field; |
||
| 413 | } |
||
| 414 | |||
| 415 | |||
| 416 | |||
| 417 | |||
| 418 | |||
| 419 | |||
| 420 | /** |
||
| 421 | * generate_question_groups_html |
||
| 422 | * |
||
| 423 | * @param string $question_groups |
||
| 424 | * @return string HTML |
||
| 425 | */ |
||
| 426 | static function generate_question_groups_html( $question_groups = array(), $group_wrapper = 'fieldset' ) { |
||
| 427 | |||
| 428 | $html = ''; |
||
| 429 | $before_question_group_questions = apply_filters( 'FHEE__EEH_Form_Fields__generate_question_groups_html__before_question_group_questions', '' ); |
||
| 430 | $after_question_group_questions = apply_filters( 'FHEE__EEH_Form_Fields__generate_question_groups_html__after_question_group_questions', '' ); |
||
| 431 | |||
| 432 | if ( ! empty( $question_groups )) { |
||
| 433 | //EEH_Debug_Tools::printr( $question_groups, '$question_groups <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
||
| 434 | // loop thru question groups |
||
| 435 | foreach ( $question_groups as $QSG ) { |
||
|
|
|||
| 436 | // check that questions exist |
||
| 437 | if ( ! empty( $QSG['QSG_questions'] )) { |
||
| 438 | // use fieldsets |
||
| 439 | $html .= "\n\t" . '<' . $group_wrapper . ' class="espresso-question-group-wrap" id="' . $QSG['QSG_identifier'] . '">'; |
||
| 440 | // group_name |
||
| 441 | $html .= $QSG['QSG_show_group_name'] ? "\n\t\t" . '<h5 class="espresso-question-group-title-h5 section-title">' . self::prep_answer( $QSG['QSG_name'] ) . '</h5>' : ''; |
||
| 442 | // group_desc |
||
| 443 | $html .= $QSG['QSG_show_group_desc'] && ! empty( $QSG['QSG_desc'] ) ? '<div class="espresso-question-group-desc-pg">' . self::prep_answer( $QSG['QSG_desc'] ) . '</div>' : ''; |
||
| 444 | |||
| 445 | $html .= $before_question_group_questions; |
||
| 446 | // loop thru questions |
||
| 447 | foreach ( $QSG['QSG_questions'] as $question ) { |
||
| 448 | // EEH_Debug_Tools::printr( $question, '$question <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
||
| 449 | $QFI = new EE_Question_Form_Input( |
||
| 450 | $question['qst_obj'], |
||
| 451 | $question['ans_obj'], |
||
| 452 | $question |
||
| 453 | ); |
||
| 454 | $html .= self::generate_form_input( $QFI ); |
||
| 455 | } |
||
| 456 | $html .= $after_question_group_questions; |
||
| 457 | $html .= "\n\t" . '</' . $group_wrapper . '>'; |
||
| 458 | } |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | return $html; |
||
| 463 | |||
| 464 | } |
||
| 465 | |||
| 466 | |||
| 467 | |||
| 468 | /** |
||
| 469 | * generate_question_groups_html |
||
| 470 | * |
||
| 471 | * @param array $question_groups |
||
| 472 | * @param array $q_meta |
||
| 473 | * @param bool $from_admin |
||
| 474 | * @param string $group_wrapper |
||
| 475 | * @return string HTML |
||
| 476 | */ |
||
| 477 | static function generate_question_groups_html2( $question_groups = array(), $q_meta = array(), $from_admin = FALSE, $group_wrapper = 'fieldset' ) { |
||
| 478 | |||
| 479 | $html = ''; |
||
| 480 | $before_question_group_questions = apply_filters( 'FHEE__EEH_Form_Fields__generate_question_groups_html__before_question_group_questions', '' ); |
||
| 481 | $after_question_group_questions = apply_filters( 'FHEE__EEH_Form_Fields__generate_question_groups_html__after_question_group_questions', '' ); |
||
| 482 | |||
| 483 | $default_q_meta = array( |
||
| 484 | 'att_nmbr' => 1, |
||
| 485 | 'ticket_id' => '', |
||
| 486 | 'input_name' => '', |
||
| 487 | 'input_id' => '', |
||
| 488 | 'input_class' => '' |
||
| 489 | ); |
||
| 490 | $q_meta = array_merge( $default_q_meta, $q_meta ); |
||
| 491 | //EEH_Debug_Tools::printr( $q_meta, '$q_meta <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
||
| 492 | |||
| 493 | if ( ! empty( $question_groups )) { |
||
| 494 | // EEH_Debug_Tools::printr( $question_groups, '$question_groups <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
||
| 495 | // loop thru question groups |
||
| 496 | foreach ( $question_groups as $QSG ) { |
||
| 497 | if ( $QSG instanceof EE_Question_Group ) { |
||
| 498 | // check that questions exist |
||
| 499 | |||
| 500 | $where = array( 'QST_deleted' => 0 ); |
||
| 501 | if ( ! $from_admin ) { |
||
| 502 | $where['QST_admin_only'] = 0; |
||
| 503 | } |
||
| 504 | $questions = $QSG->questions( array( $where, 'order_by' => array( 'Question_Group_Question.QGQ_order' => 'ASC' ))); |
||
| 505 | if ( ! empty( $questions )) { |
||
| 506 | // use fieldsets |
||
| 507 | $html .= "\n\t" . '<' . $group_wrapper . ' class="espresso-question-group-wrap" id="' . $QSG->get( 'QSG_identifier' ) . '">'; |
||
| 508 | // group_name |
||
| 509 | if ( $QSG->show_group_name() ) { |
||
| 510 | $html .= "\n\t\t" . '<h5 class="espresso-question-group-title-h5 section-title">' . $QSG->get_pretty( 'QSG_name' ) . '</h5>'; |
||
| 511 | } |
||
| 512 | // group_desc |
||
| 513 | if ( $QSG->show_group_desc() ) { |
||
| 514 | $html .= '<div class="espresso-question-group-desc-pg">' . $QSG->get_pretty( 'QSG_desc' ) . '</div>'; |
||
| 515 | } |
||
| 516 | |||
| 517 | $html .= $before_question_group_questions; |
||
| 518 | // loop thru questions |
||
| 519 | foreach ( $questions as $QST ) { |
||
| 520 | $qstn_id = $QST->is_system_question() ? $QST->system_ID() : $QST->ID(); |
||
| 521 | |||
| 522 | $answer = NULL; |
||
| 523 | |||
| 524 | if ( isset( $_GET['qstn'] ) && isset( $q_meta['input_id'] ) && isset( $q_meta['att_nmbr'] )) { |
||
| 525 | // check for answer in $_GET in case we are reprocessing a form after an error |
||
| 526 | if ( isset( $_GET['qstn'][ $q_meta['input_id'] ][ $qstn_id ] )) { |
||
| 527 | $answer = is_array( $_GET['qstn'][ $q_meta['input_id'] ][ $qstn_id ] ) ? $_GET['qstn'][ $q_meta['input_id'] ][ $qstn_id ] : sanitize_text_field( $_GET['qstn'][ $q_meta['input_id'] ][ $qstn_id ] ); |
||
| 528 | } |
||
| 529 | } else if ( isset( $q_meta['attendee'] ) && $q_meta['attendee'] ) { |
||
| 530 | //attendee data from the session |
||
| 531 | $answer = isset( $q_meta['attendee'][ $qstn_id ] ) ? $q_meta['attendee'][ $qstn_id ] : NULL; |
||
| 532 | } |
||
| 533 | |||
| 534 | |||
| 535 | |||
| 536 | $QFI = new EE_Question_Form_Input( |
||
| 537 | $QST, |
||
| 538 | EE_Answer::new_instance ( array( |
||
| 539 | 'ANS_ID'=> 0, |
||
| 540 | 'QST_ID'=> 0, |
||
| 541 | 'REG_ID'=> 0, |
||
| 542 | 'ANS_value'=> $answer |
||
| 543 | )), |
||
| 544 | $q_meta |
||
| 545 | ); |
||
| 546 | //EEH_Debug_Tools::printr( $QFI, '$QFI <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' ); |
||
| 547 | $html .= self::generate_form_input( $QFI ); |
||
| 548 | } |
||
| 549 | $html .= $after_question_group_questions; |
||
| 550 | $html .= "\n\t" . '</' . $group_wrapper . '>'; |
||
| 551 | } |
||
| 552 | } |
||
| 553 | } |
||
| 554 | } |
||
| 555 | return $html; |
||
| 556 | |||
| 557 | } |
||
| 558 | |||
| 559 | |||
| 560 | |||
| 561 | |||
| 562 | |||
| 563 | |||
| 564 | /** |
||
| 565 | * generate_form_input |
||
| 566 | * |
||
| 567 | * @param EE_Question_Form_Input $QFI |
||
| 568 | * @return string HTML |
||
| 569 | */ |
||
| 570 | static function generate_form_input( EE_Question_Form_Input $QFI ) { |
||
| 571 | if ( isset( $QFI->QST_admin_only) && $QFI->QST_admin_only && ! is_admin() ) { |
||
| 572 | return ''; |
||
| 573 | } |
||
| 574 | |||
| 575 | $QFI = self::_load_system_dropdowns( $QFI ); |
||
| 576 | $QFI = self::_load_specialized_dropdowns( $QFI ); |
||
| 577 | |||
| 578 | //we also need to verify |
||
| 579 | |||
| 580 | $display_text = $QFI->get('QST_display_text'); |
||
| 581 | $input_name = $QFI->get('QST_input_name'); |
||
| 582 | $answer = EE_Registry::instance()->REQ->is_set( $input_name ) ? EE_Registry::instance()->REQ->get( $input_name ) : $QFI->get('ANS_value'); |
||
| 583 | $input_id = $QFI->get('QST_input_id'); |
||
| 584 | $input_class = $QFI->get('QST_input_class'); |
||
| 585 | // $disabled = $QFI->get('QST_disabled') ? ' disabled="disabled"' : ''; |
||
| 586 | $disabled = $QFI->get('QST_disabled') ? TRUE : FALSE; |
||
| 587 | $required_label = apply_filters(' FHEE__EEH_Form_Fields__generate_form_input__required_label', '<em>*</em>' ); |
||
| 588 | $QST_required = $QFI->get('QST_required'); |
||
| 589 | $required = $QST_required ? array( 'label' => $required_label, 'class' => 'required needs-value', 'title' => $QST_required ) : array(); |
||
| 590 | $use_html_entities = $QFI->get_meta( 'htmlentities' ); |
||
| 591 | $required_text = $QFI->get('QST_required_text') != '' ? $QFI->get('QST_required_text') : __( 'This field is required', 'event_espresso' ); |
||
| 592 | $required_text = $QST_required ? "\n\t\t\t" . '<div class="required-text hidden">' . self::prep_answer( $required_text, $use_html_entities ) . '</div>' : ''; |
||
| 593 | $label_class = 'espresso-form-input-lbl'; |
||
| 594 | $QST_options = $QFI->options(true,$answer); |
||
| 595 | $options = is_array( $QST_options ) ? self::prep_answer_options( $QST_options ) : array(); |
||
| 596 | $system_ID = $QFI->get('QST_system'); |
||
| 597 | $label_b4 = $QFI->get_meta( 'label_b4' ); |
||
| 598 | $use_desc_4_label = $QFI->get_meta( 'use_desc_4_label' ); |
||
| 599 | |||
| 600 | |||
| 601 | switch ( $QFI->get('QST_type') ){ |
||
| 602 | |||
| 603 | case 'TEXTAREA' : |
||
| 604 | return EEH_Form_Fields::textarea( $display_text, $answer, $input_name, $input_id, $input_class, array(), $required, $required_text, $label_class, $disabled, $system_ID, $use_html_entities ); |
||
| 605 | break; |
||
| 606 | |||
| 607 | View Code Duplication | case 'DROPDOWN' : |
|
| 608 | return EEH_Form_Fields::select( $display_text, $answer, $options, $input_name, $input_id, $input_class, $required, $required_text, $label_class, $disabled, $system_ID, $use_html_entities, TRUE ); |
||
| 609 | break; |
||
| 610 | |||
| 611 | |||
| 612 | View Code Duplication | case 'RADIO_BTN' : |
|
| 613 | return EEH_Form_Fields::radio( $display_text, $answer, $options, $input_name, $input_id, $input_class, $required, $required_text, $label_class, $disabled, $system_ID, $use_html_entities, $label_b4, $use_desc_4_label ); |
||
| 614 | break; |
||
| 615 | |||
| 616 | View Code Duplication | case 'CHECKBOX' : |
|
| 617 | return EEH_Form_Fields::checkbox( $display_text, $answer, $options, $input_name, $input_id, $input_class, $required, $required_text, $label_class, $disabled, $label_b4, $system_ID, $use_html_entities ); |
||
| 618 | break; |
||
| 619 | |||
| 620 | case 'DATE' : |
||
| 621 | return EEH_Form_Fields::datepicker( $display_text, $answer, $input_name, $input_id, $input_class, $required, $required_text, $label_class, $disabled, $system_ID, $use_html_entities ); |
||
| 622 | break; |
||
| 623 | |||
| 624 | case 'TEXT' : |
||
| 625 | default: |
||
| 626 | return EEH_Form_Fields::text( $display_text, $answer, $input_name, $input_id, $input_class, $required, $required_text, $label_class, $disabled, $system_ID, $use_html_entities ); |
||
| 627 | break; |
||
| 628 | |||
| 629 | } |
||
| 630 | |||
| 631 | |||
| 632 | } |
||
| 633 | |||
| 634 | |||
| 635 | |||
| 636 | |||
| 637 | |||
| 638 | |||
| 639 | /** |
||
| 640 | * generates HTML for a form text input |
||
| 641 | * |
||
| 642 | * @param string $question label content |
||
| 643 | * @param string $answer form input value attribute |
||
| 644 | * @param string $name form input name attribute |
||
| 645 | * @param string $id form input css id attribute |
||
| 646 | * @param string $class form input css class attribute |
||
| 647 | * @param array $required 'label', 'class', and 'msg' - array of values for required "label" content, css required 'class', and required 'msg' attribute |
||
| 648 | * @param string $label_class css class attribute for the label |
||
| 649 | * @param string $disabled disabled="disabled" or null |
||
| 650 | * @return string HTML |
||
| 651 | */ |
||
| 652 | static function text( $question = FALSE, $answer = NULL, $name = FALSE, $id = '', $class = '', $required = FALSE, $required_text = '', $label_class = '', $disabled = FALSE, $system_ID = FALSE, $use_html_entities = TRUE ) { |
||
| 653 | // need these |
||
| 654 | if ( ! $question || ! $name ) { |
||
| 655 | return NULL; |
||
| 656 | } |
||
| 657 | // prep the answer |
||
| 658 | $answer = is_array( $answer ) ? '' : self::prep_answer( $answer, $use_html_entities ); |
||
| 659 | // prep the required array |
||
| 660 | $required = self::prep_required( $required ); |
||
| 661 | // set disabled tag |
||
| 662 | $disabled = $answer === NULL || ! $disabled ? '' : ' disabled="disabled"'; |
||
| 663 | // ya gots ta have style man!!! |
||
| 664 | $txt_class = is_admin() ? 'regular-text' : 'espresso-text-inp'; |
||
| 665 | $class = empty( $class ) ? $txt_class : $class; |
||
| 666 | $class .= ! empty( $system_ID ) ? ' ' . $system_ID : ''; |
||
| 667 | $extra = apply_filters( 'FHEE__EEH_Form_Fields__additional_form_field_attributes', '' ); |
||
| 668 | |||
| 669 | $label_html = $required_text . "\n\t\t\t" . '<label for="' . $name . '" class="' . $label_class . '">' . self::prep_question( $question ) . $required['label'] . '</label><br/>'; |
||
| 670 | // filter label but ensure required text comes before it |
||
| 671 | $label_html = apply_filters( 'FHEE__EEH_Form_Fields__label_html', $label_html, $required_text ); |
||
| 672 | |||
| 673 | $input_html = "\n\t\t\t" . '<input type="text" name="' . $name . '" id="' . $id . '" class="' . $class . ' ' . $required['class'] . '" value="' . esc_attr( $answer ) . '" title="' . esc_attr( $required['msg'] ) . '" ' . $disabled .' ' . $extra . '/>'; |
||
| 674 | |||
| 675 | $input_html = apply_filters( 'FHEE__EEH_Form_Fields__input_html', $input_html, $label_html, $id ); |
||
| 676 | return $label_html . $input_html; |
||
| 677 | |||
| 678 | } |
||
| 679 | |||
| 680 | |||
| 681 | |||
| 682 | |||
| 683 | |||
| 684 | /** |
||
| 685 | * generates HTML for a form textarea |
||
| 686 | * |
||
| 687 | * @param string $question label content |
||
| 688 | * @param string $answer form input value attribute |
||
| 689 | * @param string $name form input name attribute |
||
| 690 | * @param string $id form input css id attribute |
||
| 691 | * @param string $class form input css class attribute |
||
| 692 | * @param array $dimensions array of form input rows and cols attributes : array( 'rows' => 3, 'cols' => 40 ) |
||
| 693 | * @param array $required 'label', 'class', and 'msg' - array of values for required "label" content, css required 'class', and required 'msg' attribute |
||
| 694 | * @param string $label_class css class attribute for the label |
||
| 695 | * @param string $disabled disabled="disabled" or null |
||
| 696 | * @return string HTML |
||
| 697 | */ |
||
| 698 | static function textarea( $question = FALSE, $answer = NULL, $name = FALSE, $id = '', $class = '', $dimensions = FALSE, $required = FALSE, $required_text = '', $label_class = '', $disabled = FALSE, $system_ID = FALSE, $use_html_entities = TRUE ) { |
||
| 699 | // need these |
||
| 700 | if ( ! $question || ! $name ) { |
||
| 701 | return NULL; |
||
| 702 | } |
||
| 703 | // prep the answer |
||
| 704 | $answer = is_array( $answer ) ? '' : self::prep_answer( $answer, $use_html_entities ); |
||
| 705 | // prep the required array |
||
| 706 | $required = self::prep_required( $required ); |
||
| 707 | // make sure $dimensions is an array |
||
| 708 | $dimensions = is_array( $dimensions ) ? $dimensions : array(); |
||
| 709 | // and set some defaults |
||
| 710 | $dimensions = array_merge( array( 'rows' => 3, 'cols' => 40 ), $dimensions ); |
||
| 711 | // set disabled tag |
||
| 712 | $disabled = $answer === NULL || ! $disabled ? '' : ' disabled="disabled"'; |
||
| 713 | // ya gots ta have style man!!! |
||
| 714 | $txt_class = is_admin() ? 'regular-text' : 'espresso-textarea-inp'; |
||
| 715 | $class = empty( $class ) ? $txt_class : $class; |
||
| 716 | $class .= ! empty( $system_ID ) ? ' ' . $system_ID : ''; |
||
| 717 | $extra = apply_filters( 'FHEE__EEH_Form_Fields__additional_form_field_attributes', '' ); |
||
| 718 | |||
| 719 | $label_html = $required_text . "\n\t\t\t" . '<label for="' . $name . '" class="' . $label_class . '">' . self::prep_question( $question ) . $required['label'] . '</label><br/>'; |
||
| 720 | // filter label but ensure required text comes before it |
||
| 721 | $label_html = apply_filters( 'FHEE__EEH_Form_Fields__label_html', $label_html, $required_text ); |
||
| 722 | |||
| 723 | $input_html = "\n\t\t\t" . '<textarea name="' . $name . '" id="' . $id . '" class="' . $class . ' ' . $required['class'] . '" rows="' . $dimensions['rows'] . '" cols="' . $dimensions['cols'] . '" title="' . $required['msg'] . '" ' . $disabled . ' ' . $extra . '>' . $answer . '</textarea>'; |
||
| 724 | |||
| 725 | $input_html = apply_filters( 'FHEE__EEH_Form_Fields__input_html', $input_html, $label_html, $id ); |
||
| 726 | return $label_html . $input_html; |
||
| 727 | |||
| 728 | } |
||
| 729 | |||
| 730 | |||
| 731 | |||
| 732 | |||
| 733 | |||
| 734 | |||
| 735 | /** |
||
| 736 | * generates HTML for a form select input |
||
| 737 | * |
||
| 738 | * @param string $question label content |
||
| 739 | * @param string $answer form input value attribute |
||
| 740 | * @param array $options array of answer options where array key = option value and array value = option display text |
||
| 741 | * @param string $name form input name attribute |
||
| 742 | * @param string $id form input css id attribute |
||
| 743 | * @param string $class form input css class attribute |
||
| 744 | * @param array $required 'label', 'class', and 'msg' - array of values for required "label" content, css required 'class', and required 'msg' attribute |
||
| 745 | * @param string $label_class css class attribute for the label |
||
| 746 | * @param string $disabled disabled="disabled" or null |
||
| 747 | * @return string HTML |
||
| 748 | */ |
||
| 749 | static function select( $question = FALSE, $answer = NULL, $options = FALSE, $name = FALSE, $id = '', $class = '', $required = FALSE, $required_text = '', $label_class = '', $disabled = FALSE, $system_ID = FALSE, $use_html_entities = TRUE, $add_please_select_option = FALSE ) { |
||
| 750 | |||
| 751 | // need these |
||
| 752 | View Code Duplication | if ( ! $question || ! $name || ! $options || empty( $options ) || ! is_array( $options )) { |
|
| 753 | return NULL; |
||
| 754 | } |
||
| 755 | // prep the answer |
||
| 756 | $answer = is_array( $answer ) ? self::prep_answer( array_shift( $answer ), $use_html_entities) : self::prep_answer( $answer, $use_html_entities ); |
||
| 757 | // prep the required array |
||
| 758 | $required = self::prep_required( $required ); |
||
| 759 | // set disabled tag |
||
| 760 | $disabled = $answer === NULL || ! $disabled ? '' : ' disabled="disabled"'; |
||
| 761 | // ya gots ta have style man!!! |
||
| 762 | $txt_class = is_admin() ? 'wide' : 'espresso-select-inp'; |
||
| 763 | $class = empty( $class ) ? $txt_class : $class; |
||
| 764 | $class .= ! empty( $system_ID ) ? ' ' . $system_ID : ''; |
||
| 765 | $extra = apply_filters( 'FHEE__EEH_Form_Fields__additional_form_field_attributes', '' ); |
||
| 766 | |||
| 767 | $label_html = $required_text . "\n\t\t\t" . '<label for="' . $name . '" class="' . $label_class . '">' . self::prep_question( $question ) . $required['label'] . '</label><br/>'; |
||
| 768 | // filter label but ensure required text comes before it |
||
| 769 | $label_html = apply_filters( 'FHEE__EEH_Form_Fields__label_html', $label_html, $required_text ); |
||
| 770 | |||
| 771 | $input_html = "\n\t\t\t" . '<select name="' . $name . '" id="' . $id . '" class="' . $class . ' ' . $required['class'] . '" title="' . esc_attr( $required['msg'] ) . '"' . $disabled . ' ' . $extra . '>'; |
||
| 772 | // recursively count array elements, to determine total number of options |
||
| 773 | $only_option = count( $options, 1 ) == 1 ? TRUE : FALSE; |
||
| 774 | if ( ! $only_option ) { |
||
| 775 | // if there is NO answer set and there are multiple options to choose from, then set the "please select" message as selected |
||
| 776 | $selected = $answer === NULL ? ' selected="selected"' : ''; |
||
| 777 | $input_html .= $add_please_select_option ? "\n\t\t\t\t" . '<option value=""' . $selected . '>' . __(' - please select - ', 'event_espresso') . '</option>' : ''; |
||
| 778 | } |
||
| 779 | foreach ( $options as $key => $value ) { |
||
| 780 | // if value is an array, then create option groups, else create regular ol' options |
||
| 781 | $input_html .= is_array( $value ) ? self::_generate_select_option_group( $key, $value, $answer, $use_html_entities ) : self::_generate_select_option( $value->value(), $value->desc(), $answer, $only_option, $use_html_entities ); |
||
| 782 | } |
||
| 783 | |||
| 784 | $input_html .= "\n\t\t\t" . '</select>'; |
||
| 785 | |||
| 786 | $input_html = apply_filters( 'FHEE__EEH_Form_Fields__select__before_end_wrapper', $input_html, $question, $answer, $name, $id, $class, $system_ID ); |
||
| 787 | |||
| 788 | $input_html = apply_filters( 'FHEE__EEH_Form_Fields__input_html', $input_html, $label_html, $id ); |
||
| 789 | return $label_html . $input_html; |
||
| 790 | |||
| 791 | } |
||
| 792 | |||
| 793 | |||
| 794 | |||
| 795 | /** |
||
| 796 | * _generate_select_option_group |
||
| 797 | * |
||
| 798 | * if $value for a select box is an array, then the key will be used as the optgroup label |
||
| 799 | * and the value array will be looped thru and the elements sent to _generate_select_option |
||
| 800 | * |
||
| 801 | * @param mixed $opt_group |
||
| 802 | * @param mixed $QSOs |
||
| 803 | * @param mixed $answer |
||
| 804 | * @param boolean $use_html_entities |
||
| 805 | * @return string |
||
| 806 | */ |
||
| 807 | private static function _generate_select_option_group( $opt_group, $QSOs, $answer, $use_html_entities = true ){ |
||
| 808 | $html = "\n\t\t\t\t" . '<optgroup label="' . self::prep_option_value( $opt_group ) . '">'; |
||
| 809 | foreach ( $QSOs as $QSO ) { |
||
| 810 | $html .= self::_generate_select_option( $QSO->value(), $QSO->desc(), $answer, false, $use_html_entities ); |
||
| 811 | } |
||
| 812 | $html .= "\n\t\t\t\t" . '</optgroup>'; |
||
| 813 | return $html; |
||
| 814 | } |
||
| 815 | |||
| 816 | |||
| 817 | |||
| 818 | /** |
||
| 819 | * _generate_select_option |
||
| 820 | * @param mixed $key |
||
| 821 | * @param mixed $value |
||
| 822 | * @param mixed $answer |
||
| 823 | * @param int $only_option |
||
| 824 | * @param boolean $use_html_entities |
||
| 825 | * @return string |
||
| 826 | */ |
||
| 827 | private static function _generate_select_option( $key, $value, $answer, $only_option = FALSE, $use_html_entities = true ){ |
||
| 834 | |||
| 835 | |||
| 836 | |||
| 837 | /** |
||
| 838 | * generates HTML for form radio button inputs |
||
| 839 | * |
||
| 840 | * @param bool|string $question label content |
||
| 841 | * @param string $answer form input value attribute |
||
| 842 | * @param array|bool $options array of answer options where array key = option value and array value = option display text |
||
| 843 | * @param bool|string $name form input name attribute |
||
| 844 | * @param string $id form input css id attribute |
||
| 845 | * @param string $class form input css class attribute |
||
| 846 | * @param array|bool $required 'label', 'class', and 'msg' - array of values for required "label" content, css required 'class', and required 'msg' attribute |
||
| 847 | * @param string $required_text |
||
| 848 | * @param string $label_class css class attribute for the label |
||
| 849 | * @param bool|string $disabled disabled="disabled" or null |
||
| 850 | * @param bool $system_ID |
||
| 851 | * @param bool $use_html_entities |
||
| 852 | * @param bool $label_b4 |
||
| 853 | * @param bool $use_desc_4_label |
||
| 854 | * @return string HTML |
||
| 855 | */ |
||
| 856 | static function radio( $question = FALSE, $answer = NULL, $options = FALSE, $name = FALSE, $id = '', $class = '', $required = FALSE, $required_text = '', $label_class = '', $disabled = FALSE, $system_ID = FALSE, $use_html_entities = TRUE, $label_b4 = FALSE, $use_desc_4_label = FALSE ) { |
||
| 857 | // need these |
||
| 858 | View Code Duplication | if ( ! $question || ! $name || ! $options || empty( $options ) || ! is_array( $options )) { |
|
| 859 | return NULL; |
||
| 860 | } |
||
| 861 | // prep the answer |
||
| 862 | $answer = is_array( $answer ) ? '' : self::prep_answer( $answer, $use_html_entities ); |
||
| 863 | // prep the required array |
||
| 864 | $required = self::prep_required( $required ); |
||
| 865 | // set disabled tag |
||
| 866 | $disabled = $answer === NULL || ! $disabled ? '' : ' disabled="disabled"'; |
||
| 867 | // ya gots ta have style man!!! |
||
| 868 | $radio_class = is_admin() ? 'ee-admin-radio-lbl' : $label_class; |
||
| 869 | $class = ! empty( $class ) ? $class : 'espresso-radio-btn-inp'; |
||
| 870 | $extra = apply_filters( 'FHEE__EEH_Form_Fields__additional_form_field_attributes', '' ); |
||
| 871 | |||
| 872 | $label_html = $required_text . "\n\t\t\t" . '<label class="' . $label_class . '">' . self::prep_question( $question ) . $required['label'] . '</label> '; |
||
| 873 | // filter label but ensure required text comes before it |
||
| 874 | $label_html = apply_filters( 'FHEE__EEH_Form_Fields__label_html', $label_html, $required_text ); |
||
| 875 | |||
| 876 | $input_html = "\n\t\t\t" . '<ul id="' . $id . '-ul" class="espresso-radio-btn-options-ul ' . $label_class . ' ' . $class . '-ul">'; |
||
| 877 | |||
| 878 | $class .= ! empty( $system_ID ) ? ' ' . $system_ID : ''; |
||
| 879 | $class .= ! empty( $required['class'] ) ? ' ' . $required['class'] : ''; |
||
| 880 | |||
| 881 | foreach ( $options as $OPT ) { |
||
| 882 | if ( $OPT instanceof EE_Question_Option ) { |
||
| 883 | $value = self::prep_option_value( $OPT->value() ); |
||
| 884 | $label = $use_desc_4_label ? $OPT->desc() : $OPT->value(); |
||
| 885 | $size = $use_desc_4_label ? self::get_label_size_class( $OPT->value() . ' ' . $OPT->desc() ) : self::get_label_size_class( $OPT->value() ); |
||
| 886 | $desc = $OPT->desc();//no self::prep_answer |
||
| 887 | $answer = is_numeric( $value ) && empty( $answer ) ? 0 : $answer; |
||
| 888 | $checked = (string)$value == (string)$answer ? ' checked="checked"' : ''; |
||
| 889 | $opt = '-' . sanitize_key( $value ); |
||
| 890 | |||
| 891 | $input_html .= "\n\t\t\t\t" . '<li' . $size . '>'; |
||
| 892 | $input_html .= "\n\t\t\t\t\t" . '<label class="' . $radio_class . ' espresso-radio-btn-lbl">'; |
||
| 893 | $input_html .= $label_b4 ? "\n\t\t\t\t\t\t" . '<span>' . $label . '</span>' : ''; |
||
| 894 | $input_html .= "\n\t\t\t\t\t\t" . '<input type="radio" name="' . $name . '" id="' . $id . $opt . '" class="' . $class . '" value="' . $value . '" title="' . esc_attr( $required['msg'] ) . '" ' . $disabled . $checked . ' ' . $extra . '/>'; |
||
| 895 | $input_html .= ! $label_b4 ? "\n\t\t\t\t\t\t" . '<span class="espresso-radio-btn-desc">' . $label . '</span>' : ''; |
||
| 896 | $input_html .= "\n\t\t\t\t\t" . '</label>'; |
||
| 897 | $input_html .= $use_desc_4_label ? '' : '<span class="espresso-radio-btn-option-desc small-text grey-text">' . $desc . '</span>'; |
||
| 898 | $input_html .= "\n\t\t\t\t" . '</li>'; |
||
| 899 | } |
||
| 900 | } |
||
| 901 | |||
| 902 | $input_html .= "\n\t\t\t" . '</ul>'; |
||
| 903 | |||
| 904 | $input_html = apply_filters( 'FHEE__EEH_Form_Fields__input_html', $input_html, $label_html, $id ); |
||
| 905 | return $label_html . $input_html; |
||
| 906 | |||
| 907 | } |
||
| 908 | |||
| 909 | |||
| 910 | |||
| 911 | |||
| 912 | |||
| 913 | |||
| 914 | /** |
||
| 915 | * generates HTML for form checkbox inputs |
||
| 916 | * |
||
| 917 | * @param string $question label content |
||
| 918 | * @param string $answer form input value attribute |
||
| 919 | * @param array $options array of options where array key = option value and array value = option display text |
||
| 920 | * @param string $name form input name attribute |
||
| 921 | * @param string $id form input css id attribute |
||
| 922 | * @param string $class form input css class attribute |
||
| 923 | * @param array $required 'label', 'class', and 'msg' - array of values for required "label" content, css required 'class', and required 'msg' attribute |
||
| 924 | * @param string $label_class css class attribute for the label |
||
| 925 | * @param string $disabled disabled="disabled" or null |
||
| 926 | * @return string HTML |
||
| 927 | */ |
||
| 928 | static function checkbox( $question = FALSE, $answer = NULL, $options = FALSE, $name = FALSE, $id = '', $class = '', $required = FALSE, $required_text = '', $label_class = '', $disabled = FALSE, $label_b4 = FALSE, $system_ID = FALSE, $use_html_entities = TRUE ) { |
||
| 929 | // need these |
||
| 930 | View Code Duplication | if ( ! $question || ! $name || ! $options || empty( $options ) || ! is_array( $options )) { |
|
| 931 | return NULL; |
||
| 932 | } |
||
| 933 | $answer = maybe_unserialize( $answer ); |
||
| 934 | |||
| 935 | // prep the answer(s) |
||
| 936 | $answer = is_array( $answer ) ? $answer : array( sanitize_key( $answer ) => $answer ); |
||
| 937 | |||
| 938 | foreach ( $answer as $key => $value ) { |
||
| 939 | $key = self::prep_option_value( $key ); |
||
| 940 | $answer[$key] = self::prep_answer( $value, $use_html_entities ); |
||
| 941 | } |
||
| 942 | |||
| 943 | // prep the required array |
||
| 944 | $required = self::prep_required( $required ); |
||
| 945 | // set disabled tag |
||
| 946 | $disabled = $answer === NULL || ! $disabled ? '' : ' disabled="disabled"'; |
||
| 947 | // ya gots ta have style man!!! |
||
| 948 | $radio_class = is_admin() ? 'ee-admin-radio-lbl' : $label_class; |
||
| 949 | $class = empty( $class ) ? 'espresso-radio-btn-inp' : $class; |
||
| 950 | $extra = apply_filters( 'FHEE__EEH_Form_Fields__additional_form_field_attributes', '' ); |
||
| 951 | |||
| 952 | $label_html = $required_text . "\n\t\t\t" . '<label class="' . $label_class . '">' . self::prep_question( $question ) . $required['label'] . '</label> '; |
||
| 953 | // filter label but ensure required text comes before it |
||
| 954 | $label_html = apply_filters( 'FHEE__EEH_Form_Fields__label_html', $label_html, $required_text ); |
||
| 955 | |||
| 956 | $input_html = "\n\t\t\t" . '<ul id="' . $id . '-ul" class="espresso-checkbox-options-ul ' . $label_class . ' ' . $class . '-ul">'; |
||
| 957 | |||
| 958 | $class .= ! empty( $system_ID ) ? ' ' . $system_ID : ''; |
||
| 959 | $class .= ! empty( $required['class'] ) ? ' ' . $required['class'] : ''; |
||
| 960 | |||
| 961 | foreach ( $options as $OPT ) { |
||
| 962 | $value = $OPT->value();//self::prep_option_value( $OPT->value() ); |
||
| 963 | $size = self::get_label_size_class( $OPT->value() . ' ' . $OPT->desc() ); |
||
| 964 | $text = self::prep_answer( $OPT->value() ); |
||
| 965 | $desc = $OPT->desc() ; |
||
| 966 | $opt = '-' . sanitize_key( $value ); |
||
| 967 | |||
| 968 | $checked = is_array( $answer ) && in_array( $text, $answer ) ? ' checked="checked"' : ''; |
||
| 969 | |||
| 970 | $input_html .= "\n\t\t\t\t" . '<li' . $size . '>'; |
||
| 971 | $input_html .= "\n\t\t\t\t\t" . '<label class="' . $radio_class . ' espresso-checkbox-lbl">'; |
||
| 972 | $input_html .= $label_b4 ? "\n\t\t\t\t\t\t" . '<span>' . $text . '</span>' : ''; |
||
| 973 | $input_html .= "\n\t\t\t\t\t\t" . '<input type="checkbox" name="' . $name . '[' . $OPT->ID() . ']" id="' . $id . $opt . '" class="' . $class . '" value="' . $value . '" title="' . esc_attr( $required['msg'] ) . '" ' . $disabled . $checked . ' ' . $extra . '/>'; |
||
| 974 | $input_html .= ! $label_b4 ? "\n\t\t\t\t\t\t" . '<span>' . $text . '</span>' : ''; |
||
| 975 | $input_html .= "\n\t\t\t\t\t" . '</label>'; |
||
| 976 | if ( ! empty( $desc ) && $desc != $text ) { |
||
| 977 | $input_html .= "\n\t\t\t\t\t" . ' <br/><div class="espresso-checkbox-option-desc small-text grey-text">' . $desc . '</div>'; |
||
| 978 | } |
||
| 979 | $input_html .= "\n\t\t\t\t" . '</li>'; |
||
| 980 | |||
| 981 | } |
||
| 982 | |||
| 983 | $input_html .= "\n\t\t\t" . '</ul>'; |
||
| 984 | |||
| 985 | $input_html = apply_filters( 'FHEE__EEH_Form_Fields__input_html', $input_html, $label_html, $id ); |
||
| 986 | return $label_html . $input_html; |
||
| 987 | |||
| 988 | } |
||
| 989 | |||
| 990 | |||
| 991 | |||
| 992 | |||
| 993 | |||
| 994 | |||
| 995 | /** |
||
| 996 | * generates HTML for a form datepicker input |
||
| 997 | * |
||
| 998 | * @param string $question label content |
||
| 999 | * @param string $answer form input value attribute |
||
| 1000 | * @param string $name form input name attribute |
||
| 1001 | * @param string $id form input css id attribute |
||
| 1002 | * @param string $class form input css class attribute |
||
| 1003 | * @param array $required 'label', 'class', and 'msg' - array of values for required "label" content, css required 'class', and required 'msg' attribute |
||
| 1004 | * @param string $label_class css class attribute for the label |
||
| 1005 | * @param string $disabled disabled="disabled" or null |
||
| 1006 | * @return string HTML |
||
| 1007 | */ |
||
| 1008 | static function datepicker( $question = FALSE, $answer = NULL, $name = FALSE, $id = '', $class = '', $required = FALSE, $required_text = '', $label_class = '', $disabled = FALSE, $system_ID = FALSE, $use_html_entities = TRUE ) { |
||
| 1009 | // need these |
||
| 1010 | if ( ! $question || ! $name ) { |
||
| 1011 | return NULL; |
||
| 1012 | } |
||
| 1013 | // prep the answer |
||
| 1014 | $answer = is_array( $answer ) ? '' : self::prep_answer( $answer, $use_html_entities ); |
||
| 1015 | // prep the required array |
||
| 1016 | $required = self::prep_required( $required ); |
||
| 1017 | // set disabled tag |
||
| 1018 | $disabled = $answer === NULL || ! $disabled ? '' : ' disabled="disabled"'; |
||
| 1019 | // ya gots ta have style man!!! |
||
| 1020 | $txt_class = is_admin() ? 'regular-text' : 'espresso-datepicker-inp'; |
||
| 1021 | $class = empty( $class ) ? $txt_class : $class; |
||
| 1022 | $class .= ! empty( $system_ID ) ? ' ' . $system_ID : ''; |
||
| 1023 | $extra = apply_filters( 'FHEE__EEH_Form_Fields__additional_form_field_attributes', '' ); |
||
| 1024 | |||
| 1025 | $label_html = $required_text . "\n\t\t\t" . '<label for="' . $name . '" class="' . $label_class . '">' . self::prep_question( $question ) . $required['label'] . '</label><br/>'; |
||
| 1026 | // filter label but ensure required text comes before it |
||
| 1027 | $label_html = apply_filters( 'FHEE__EEH_Form_Fields__label_html', $label_html, $required_text ); |
||
| 1028 | |||
| 1029 | $input_html = "\n\t\t\t" . '<input type="text" name="' . $name . '" id="' . $id . '" class="' . $class . ' ' . $required['class'] . ' datepicker" value="' . $answer . '" title="' . esc_attr( $required['msg'] ) . '" ' . $disabled . ' ' . $extra . '/>'; |
||
| 1030 | |||
| 1031 | // enqueue scripts |
||
| 1032 | wp_register_style( 'espresso-ui-theme', EE_GLOBAL_ASSETS_URL . 'css/espresso-ui-theme/jquery-ui-1.10.3.custom.min.css', array(), EVENT_ESPRESSO_VERSION ); |
||
| 1033 | wp_enqueue_style( 'espresso-ui-theme'); |
||
| 1034 | wp_enqueue_script( 'jquery-ui-datepicker' ); |
||
| 1035 | |||
| 1036 | $input_html = apply_filters( 'FHEE__EEH_Form_Fields__input_html', $input_html, $label_html, $id ); |
||
| 1037 | return $label_html . $input_html; |
||
| 1038 | |||
| 1039 | } |
||
| 1040 | |||
| 1041 | |||
| 1042 | |||
| 1043 | /** |
||
| 1044 | * remove_label_keep_required_msg |
||
| 1045 | * this will strip out a form input's label HTML while keeping the required text HTML that MUST be before the label |
||
| 1046 | * @access public |
||
| 1047 | * @return string |
||
| 1048 | */ |
||
| 1049 | public static function remove_label_keep_required_msg( $label_html, $required_text ) { |
||
| 1052 | |||
| 1053 | |||
| 1054 | |||
| 1055 | |||
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Simply return sthe HTML for a hidden input of the given name and value. |
||
| 1059 | * @param string $name |
||
| 1060 | * @param string $value |
||
| 1061 | * @return string HTML |
||
| 1062 | */ |
||
| 1063 | static function hidden_input( $name, $value, $id = '' ){ |
||
| 1067 | |||
| 1068 | |||
| 1069 | |||
| 1070 | |||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * prep_question |
||
| 1074 | * @param string $question |
||
| 1075 | * @return string |
||
| 1076 | */ |
||
| 1077 | static function prep_question( $question ){ |
||
| 1078 | return $question; |
||
| 1079 | // $link = ''; |
||
| 1080 | // // does this label have a help link attached ? |
||
| 1081 | // if ( strpos( $question, '<a ' ) !== FALSE ) { |
||
| 1082 | // $qbits = explode( '<a ', $question ); |
||
| 1083 | // foreach ( $qbits as $qbit ) { |
||
| 1084 | // $link = strpos( $qbit, 'title="' ) !== FALSE ? $qbit : $link; |
||
| 1085 | // $question = strpos( $qbit, 'title="' ) === FALSE ? $qbit : $question; |
||
| 1086 | // } |
||
| 1087 | // $link = '<a ' . $link; |
||
| 1088 | // } |
||
| 1089 | // return htmlspecialchars( trim( stripslashes( str_replace( ''', "'", $question ))), ENT_QUOTES, 'UTF-8' ) . ' ' . $link; |
||
| 1090 | } |
||
| 1091 | |||
| 1092 | |||
| 1093 | |||
| 1094 | |||
| 1095 | /** |
||
| 1096 | * prep_answer |
||
| 1097 | * @param mixed $answer |
||
| 1098 | * @return string |
||
| 1099 | */ |
||
| 1100 | static function prep_answer( $answer, $use_html_entities = TRUE ){ |
||
| 1108 | |||
| 1109 | |||
| 1110 | |||
| 1111 | /** |
||
| 1112 | * prep_answer_options |
||
| 1113 | * @param array $QSOs array of EE_Question_Option objects |
||
| 1114 | * @return array |
||
| 1115 | */ |
||
| 1116 | public static function prep_answer_options( $QSOs = array() ){ |
||
| 1117 | $prepped_answer_options = array(); |
||
| 1118 | if ( is_array( $QSOs ) && ! empty( $QSOs )) { |
||
| 1119 | foreach( $QSOs as $key => $QSO ) { |
||
| 1120 | if ( ! $QSO instanceof EE_Question_Option ) { |
||
| 1121 | $QSO = EE_Question_Option::new_instance( array( |
||
| 1122 | 'QSO_value' => is_array( $QSO ) && isset( $QSO['id'] ) ? (string)$QSO['id'] : (string)$key, |
||
| 1123 | 'QSO_desc' => is_array( $QSO ) && isset( $QSO['text'] ) ? (string)$QSO['text'] : (string)$QSO |
||
| 1124 | )); |
||
| 1125 | } |
||
| 1126 | if ( $QSO->opt_group() ) { |
||
| 1127 | $prepped_answer_options[ $QSO->opt_group() ][] = $QSO; |
||
| 1128 | } else { |
||
| 1129 | $prepped_answer_options[] = $QSO; |
||
| 1130 | } |
||
| 1131 | } |
||
| 1132 | } |
||
| 1133 | // d( $prepped_answer_options ); |
||
| 1134 | return $prepped_answer_options; |
||
| 1135 | } |
||
| 1136 | |||
| 1137 | |||
| 1138 | /** |
||
| 1139 | * prep_option_value |
||
| 1140 | * @param string $option_value |
||
| 1141 | * @return string |
||
| 1142 | */ |
||
| 1143 | static function prep_option_value( $option_value ){ |
||
| 1146 | |||
| 1147 | |||
| 1148 | |||
| 1149 | |||
| 1150 | /** |
||
| 1151 | * prep_required |
||
| 1152 | * @param string|array $required |
||
| 1153 | * @return array |
||
| 1154 | */ |
||
| 1155 | static function prep_required( $required = array() ){ |
||
| 1156 | // make sure required is an array |
||
| 1157 | $required = is_array( $required ) ? $required : array(); |
||
| 1158 | // and set some defaults |
||
| 1159 | $required = array_merge( array( 'label' => '', 'class' => '', 'msg' => '' ), $required ); |
||
| 1160 | return $required; |
||
| 1162 | |||
| 1163 | |||
| 1164 | |||
| 1165 | /** |
||
| 1166 | * get_label_size_class |
||
| 1167 | * @param string $value |
||
| 1168 | * @return string |
||
| 1169 | */ |
||
| 1170 | static function get_label_size_class( $value = FALSE ){ |
||
| 1198 | |||
| 1199 | |||
| 1200 | |||
| 1201 | |||
| 1202 | /** |
||
| 1203 | * _load_system_dropdowns |
||
| 1204 | * @param array $QFI |
||
| 1205 | * @return array |
||
| 1206 | */ |
||
| 1207 | private static function _load_system_dropdowns( $QFI ){ |
||
| 1225 | |||
| 1226 | |||
| 1227 | |||
| 1228 | /** |
||
| 1229 | * This preps dropdowns that are specialized. |
||
| 1230 | * |
||
| 1231 | * @since 4.6.0 |
||
| 1232 | * |
||
| 1233 | * @param EE_Question_Form_Input $QFI |
||
| 1234 | * |
||
| 1235 | * @return EE_Question_Form_Input |
||
| 1236 | */ |
||
| 1237 | protected static function _load_specialized_dropdowns( $QFI ) { |
||
| 1248 | |||
| 1249 | |||
| 1250 | |||
| 1251 | /** |
||
| 1252 | * generate_state_dropdown |
||
| 1253 | * @param array $QST |
||
| 1254 | * @param bool $get_all |
||
| 1255 | * @return array |
||
| 1256 | */ |
||
| 1257 | public static function generate_state_dropdown( $QST, $get_all = FALSE ){ |
||
| 1279 | |||
| 1280 | |||
| 1281 | |||
| 1282 | /** |
||
| 1283 | * generate_country_dropdown |
||
| 1284 | * @param $QST |
||
| 1285 | * @param bool $get_all |
||
| 1286 | * @internal param array $question |
||
| 1287 | * @return array |
||
| 1288 | */ |
||
| 1289 | public static function generate_country_dropdown( $QST, $get_all = FALSE ){ |
||
| 1308 | |||
| 1309 | |||
| 1310 | |||
| 1311 | |||
| 1312 | |||
| 1313 | /** |
||
| 1314 | * generates options for a month dropdown selector with numbers from 01 to 12 |
||
| 1315 | * @return array() |
||
| 1316 | */ |
||
| 1317 | public static function two_digit_months_dropdown_options() { |
||
| 1325 | |||
| 1326 | |||
| 1327 | |||
| 1328 | |||
| 1329 | |||
| 1330 | /** |
||
| 1331 | * generates a year dropdown selector with numbers for the next ten years |
||
| 1332 | * @return object |
||
| 1333 | */ |
||
| 1334 | public static function next_decade_two_digit_year_dropdown_options() { |
||
| 1344 | |||
| 1345 | |||
| 1346 | |||
| 1347 | |||
| 1348 | |||
| 1349 | /** |
||
| 1350 | * generates a month/year dropdown selector for all registrations matching the given criteria. Typically used for list table filter. |
||
| 1351 | * @param string $cur_date any currently selected date can be entered here. |
||
| 1352 | * @param string $status Registration status |
||
| 1353 | * @param integer $evt_category Event Category ID if the Event Category filter is selected |
||
| 1354 | * @return string html |
||
| 1355 | */ |
||
| 1356 | public static function generate_registration_months_dropdown( $cur_date = '', $status = '', $evt_category = 0 ) { |
||
| 1386 | |||
| 1387 | |||
| 1388 | |||
| 1389 | /** |
||
| 1390 | * generates a month/year dropdown selector for all events matching the given criteria |
||
| 1391 | * Typically used for list table filter |
||
| 1392 | * @param string $cur_date any currently selected date can be entered here. |
||
| 1393 | * @param string $status "view" (i.e. all, today, month, draft) |
||
| 1394 | * @param int $evt_category category event belongs to |
||
| 1395 | * @param string $evt_active_status "upcoming", "expired", "active", or "inactive" |
||
| 1396 | * @return string html |
||
| 1397 | */ |
||
| 1398 | public static function generate_event_months_dropdown( $cur_date = '', $status = NULL, $evt_category = NULL, $evt_active_status = NULL ) { |
||
| 1399 | //determine what post_status our condition will have for the query. |
||
| 1400 | switch ( $status ) { |
||
| 1401 | case 'month' : |
||
| 1402 | case 'today' : |
||
| 1403 | case NULL : |
||
| 1404 | case 'all' : |
||
| 1405 | $where['Event.status'] = array( 'NOT IN', array('trash') ); |
||
| 1406 | break; |
||
| 1407 | |||
| 1408 | case 'draft' : |
||
| 1409 | $where['Event.status'] = array( 'IN', array('draft', 'auto-draft') ); |
||
| 1410 | |||
| 1411 | default : |
||
| 1412 | $where['Event.status'] = $status; |
||
| 1413 | } |
||
| 1414 | |||
| 1415 | //categories? |
||
| 1416 | |||
| 1417 | |||
| 1418 | if ( !empty ( $evt_category ) ) { |
||
| 1419 | $where['Event.Term_Taxonomy.taxonomy'] = 'espresso_event_categories'; |
||
| 1420 | $where['Event.Term_Taxonomy.term_id'] = $evt_category; |
||
| 1421 | } |
||
| 1422 | |||
| 1423 | |||
| 1424 | // $where['DTT_is_primary'] = 1; |
||
| 1425 | |||
| 1426 | $DTTS = EE_Registry::instance()->load_model('Datetime')->get_dtt_months_and_years($where, $evt_active_status ); |
||
| 1427 | |||
| 1428 | //let's setup vals for select input helper |
||
| 1429 | $options = array( |
||
| 1430 | 0 => array( |
||
| 1431 | 'text' => __('Select a Month/Year', 'event_espresso'), |
||
| 1432 | 'id' => "" |
||
| 1433 | ) |
||
| 1434 | ); |
||
| 1435 | |||
| 1436 | |||
| 1437 | |||
| 1438 | //translate month and date |
||
| 1439 | global $wp_locale; |
||
| 1440 | |||
| 1441 | foreach ( $DTTS as $DTT ) { |
||
| 1442 | $localized_date = $wp_locale->get_month( $DTT->dtt_month_num ) . ' ' . $DTT->dtt_year; |
||
| 1443 | $id = $DTT->dtt_month . ' ' . $DTT->dtt_year; |
||
| 1444 | $options[] = array( |
||
| 1445 | 'text' => $localized_date, |
||
| 1446 | 'id' => $id |
||
| 1447 | ); |
||
| 1448 | } |
||
| 1449 | |||
| 1450 | |||
| 1451 | return self::select_input( 'month_range', $options, $cur_date, '', 'wide' ); |
||
| 1452 | } |
||
| 1453 | |||
| 1454 | |||
| 1455 | |||
| 1456 | /** |
||
| 1457 | * generates the dropdown selector for event categories |
||
| 1458 | * typically used as a filter on list tables. |
||
| 1459 | * @param integer $current_cat currently selected category |
||
| 1460 | * @return string html for dropdown |
||
| 1461 | */ |
||
| 1462 | public static function generate_event_category_dropdown( $current_cat = -1 ) { |
||
| 1481 | |||
| 1482 | |||
| 1483 | |||
| 1484 | /** |
||
| 1485 | * generate a submit button with or without it's own microform |
||
| 1486 | * this is the only way to create buttons that are compatible across all themes |
||
| 1487 | * |
||
| 1488 | * @access public |
||
| 1489 | * @param string $url - the form action |
||
| 1490 | * @param string $ID - some kind of unique ID, appended with "-sbmt" for the input and "-frm" for the form |
||
| 1491 | * @param string $class - css classes (separated by spaces if more than one) |
||
| 1492 | * @param string $text - what appears on the button |
||
| 1493 | * @param string $nonce_action - if using nonces |
||
| 1494 | * @param bool|string $input_only - whether to print form header and footer. TRUE returns the input without the form |
||
| 1495 | * @param string $extra_attributes - any extra attributes that need to be attached to the form input |
||
| 1496 | * @return void |
||
| 1497 | */ |
||
| 1498 | public static function submit_button( $url = '', $ID = '', $class = '', $text = '', $nonce_action = '', $input_only = FALSE, $extra_attributes = '' ) { |
||
| 1515 | |||
| 1516 | |||
| 1517 | |||
| 1518 | }//end class EEH_Form_Fields |
||
| 1519 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.