1 <?php
2
3 4 5 6 7 8 9 10 11 12
13 class Fieldmanager_Select extends Fieldmanager_Options {
14
15 16 17 18
19 public $field_class = 'select';
20
21 22 23 24
25 public $type_ahead = False;
26
27 28 29 30
31 public $first_empty = False;
32
33 34 35 36
37 public $multiple = false;
38
39 40 41 42 43
44 public function __construct( $label = '', $options = array() ) {
45
46 $this->attributes = array(
47 'size' => '1'
48 );
49
50
51 fm_add_script( 'fm_select_js', 'js/fieldmanager-select.js', array(), '1.0.1', false, 'fm_select', array( 'nonce' => wp_create_nonce( 'fm_search_terms_nonce' ) ) );
52
53 parent::__construct( $label, $options );
54
55
56
57
58 if ( array_key_exists( 'multiple', $this->attributes ) ) {
59 $this->multiple = true;
60 } elseif ( $this->multiple ) {
61 $this->attributes['multiple'] = 'multiple';
62 }
63
64
65 if ( $this->type_ahead ) {
66 fm_add_script( 'chosen', 'js/chosen/chosen.jquery.js' );
67 fm_add_style( 'chosen_css', 'js/chosen/chosen.css' );
68 }
69
70 }
71
72 73 74 75 76
77 public function form_element( $value = array() ) {
78
79 $select_classes = array( 'fm-element' );
80
81
82 $do_multiple = '';
83 if ( $this->multiple ) {
84 $do_multiple = "[]";
85 }
86
87
88 if ( $this->type_ahead ) {
89 $select_classes[] = 'chzn-select';
90 if ( !isset( $GLOBALS['fm_chosen_initialized'] ) ) {
91 add_action( 'admin_footer', array( $this, 'chosen_init' ) );
92 $GLOBALS['fm_chosen_initialized'] = true;
93 }
94
95 if ( $this->grouped ) {
96 $select_classes[] = "fm-options-grouped";
97 } else {
98 $select_classes[] = "fm-options";
99 }
100 }
101
102 $opts = '';
103 if ( $this->is_repeatable() || $this->first_empty ) {
104 $opts .= '<option value=""> </option>';
105 }
106 $opts .= $this->form_data_elements( $value );
107
108 return sprintf(
109 '<select class="%s" name="%s" id="%s" %s>%s</select>',
110 esc_attr( implode( " ", $select_classes ) ),
111 esc_attr( $this->get_form_name( $do_multiple ) ),
112 esc_attr( $this->get_element_id() ),
113 $this->get_element_attributes(),
114 $opts
115 );
116 }
117
118 119 120 121 122 123
124 public function form_data_element( $data_row, $value = array() ) {
125
126
127
128 $option_selected = $this->option_selected( $data_row['value'], $value, "selected" );
129
130 return sprintf(
131 '<option value="%s" %s>%s</option>',
132 esc_attr( $data_row['value'] ),
133 $option_selected,
134 esc_html( $data_row['name'] )
135 );
136
137 }
138
139 140 141 142 143
144 public function form_data_start_group( $label ) {
145 return sprintf(
146 '<optgroup label="%s">',
147 esc_attr( $label )
148 );
149 }
150
151 152 153 154
155 public function form_data_end_group() {
156 return '</optgroup>';
157 }
158
159 160 161 162
163 public function chosen_init() {
164 ?>
165 <script type="text/javascript">
166 jQuery(function($){
167 $('.fm-wrapper').on("fm_added_element fm_collapsible_toggle fm_activate_tab",".fm-item",function(){
168 $(".chzn-select:visible",this).chosen({allow_single_deselect:true})
169 });
170 $(".chzn-select:visible").chosen({allow_single_deselect:true});
171 });
172 </script>
173 <?php
174 }
175 }