1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* Class EE_Select_Display_Strategy |
5
|
|
|
* |
6
|
|
|
* displays either simple arrays as selected, or if a 2d array is provided, separates them into optgroups |
7
|
|
|
* |
8
|
|
|
* @package Event Espresso |
9
|
|
|
* @subpackage core |
10
|
|
|
* @author Mike Nelson |
11
|
|
|
* @since $VID:$ |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
class EE_Select_Display_Strategy extends EE_Display_Strategy_Base{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
* @throws EE_Error |
19
|
|
|
* @return string of html to display the field |
20
|
|
|
*/ |
21
|
|
|
function display(){ |
22
|
|
|
if( ! $this->_input instanceof EE_Form_Input_With_Options_Base){ |
23
|
|
|
throw new EE_Error( sprintf( __( 'Cannot use Select Display Strategy with an input that doesn\'t have options', 'event_espresso' ))); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$html = EEH_HTML::nl( 0, 'select' ); |
27
|
|
|
$html .= '<select'; |
28
|
|
|
$html .= ' id="' . $this->_input->html_id() . '"'; |
29
|
|
|
$html .= ' name="' . $this->_input->html_name() . '"'; |
30
|
|
|
$class = $this->_input->required() ? $this->_input->required_css_class() . ' ' . $this->_input->html_class() : $this->_input->html_class(); |
31
|
|
|
$html .= ' class="' . $class . '"'; |
32
|
|
|
// add html5 required |
33
|
|
|
$html .= $this->_input->required() ? ' required' : ''; |
34
|
|
|
$html .= ' style="' . $this->_input->html_style() . '"'; |
35
|
|
|
$html .= ' ' . $this->_input->other_html_attributes(); |
36
|
|
|
$html .= '>'; |
37
|
|
|
|
38
|
|
|
if ( EEH_Array::is_multi_dimensional_array( $this->_input->options() )) { |
39
|
|
|
EEH_HTML::indent( 1, 'optgroup' ); |
40
|
|
|
foreach( $this->_input->options() as $opt_group_label => $opt_group ){ |
41
|
|
|
if ( ! empty($opt_group_label)) { |
42
|
|
|
$html .= EEH_HTML::nl(0, 'optgroup') . '<optgroup label="' . esc_attr($opt_group_label) . '">'; |
43
|
|
|
} |
44
|
|
|
EEH_HTML::indent( 1, 'option' ); |
45
|
|
|
$html .= $this->_display_options( $opt_group ); |
46
|
|
|
EEH_HTML::indent( -1, 'option' ); |
47
|
|
|
if ( ! empty($opt_group_label)) { |
48
|
|
|
$html .= EEH_HTML::nl( 0, 'optgroup' ) . '</optgroup>'; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
EEH_HTML::indent( -1, 'optgroup' ); |
52
|
|
|
} else { |
53
|
|
|
$html.=$this->_display_options( $this->_input->options() ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$html.= EEH_HTML::nl( 0, 'select' ) . '</select>'; |
57
|
|
|
return $html; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Displays a flat list of options as option tags |
64
|
|
|
* @param array $options |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
protected function _display_options($options){ |
68
|
|
|
$html = ''; |
69
|
|
|
EEH_HTML::indent( 1, 'option' ); |
70
|
|
|
foreach( $options as $value => $display_text ){ |
71
|
|
|
$unnormalized_value = $this->_input->get_normalization_strategy()->unnormalize_one( $value ); |
72
|
|
|
$selected = $this->_check_if_option_selected( $unnormalized_value ) ? ' selected="selected"' : ''; |
|
|
|
|
73
|
|
|
$html.= EEH_HTML::nl( 0, 'option' ) . '<option value="' . esc_attr( $unnormalized_value ) . '"' . $selected . '>' . $display_text . '</option>'; |
74
|
|
|
} |
75
|
|
|
EEH_HTML::indent( -1, 'option' ); |
76
|
|
|
return $html; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Checks if that value is the one selected |
83
|
|
|
* @param string|int $value unnormalized value option (string) |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function _check_if_option_selected( $value ){ |
87
|
|
|
return $this->_input->raw_value() == $value ? TRUE : FALSE; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
|
92
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.