select.php
| 1 | <?php |
| 2 | |
| 3 | class acf_Select extends acf_Field |
| 4 | { |
| 5 | |
| 6 | /*-------------------------------------------------------------------------------------- |
| 7 | * |
| 8 | * Constructor |
| 9 | * |
| 10 | * @author Elliot Condon |
| 11 | * @since 1.0.0 |
| 12 | * @updated 2.2.0 |
| 13 | * |
| 14 | *-------------------------------------------------------------------------------------*/ |
| 15 | |
| 16 | function __construct($parent) |
| 17 | { |
| 18 | parent::__construct($parent); |
| 19 | |
| 20 | $this->name = 'select'; |
| 21 | $this->title = __("Select",'acf'); |
| 22 | |
| 23 | } |
| 24 | |
| 25 | |
| 26 | |
| 27 | /*-------------------------------------------------------------------------------------- |
| 28 | * |
| 29 | * create_field |
| 30 | * |
| 31 | * @author Elliot Condon |
| 32 | * @since 2.0.5 |
| 33 | * @updated 2.2.0 |
| 34 | * |
| 35 | *-------------------------------------------------------------------------------------*/ |
| 36 | |
| 37 | function create_field($field) |
| 38 | { |
| 39 | // defaults |
| 40 | $field['value'] = isset($field['value']) ? $field['value'] : array(); |
| 41 | $field['multiple'] = isset($field['multiple']) ? $field['multiple'] : false; |
| 42 | $field['allow_null'] = isset($field['allow_null']) ? $field['allow_null'] : false; |
| 43 | $field['choices'] = isset($field['choices']) ? $field['choices'] : array(); |
| 44 | $field['optgroup'] = isset($field['optgroup']) ? $field['optgroup'] : false; |
| 45 | |
| 46 | |
| 47 | // no choices |
| 48 | if(empty($field['choices'])) |
| 49 | { |
| 50 | echo '<p>' . __("No choices to choose from",'acf') . '</p>'; |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | // multiple select |
| 55 | $multiple = ''; |
| 56 | if($field['multiple'] == '1') |
| 57 | { |
| 58 | $multiple = ' multiple="multiple" size="5" '; |
| 59 | $field['name'] .= '[]'; |
| 60 | } |
| 61 | |
| 62 | // html |
| 63 | echo '<select id="' . $field['name'] . '" class="' . $field['class'] . '" name="' . $field['name'] . '" ' . $multiple . ' >'; |
| 64 | |
| 65 | // null |
| 66 | if($field['allow_null'] == '1') |
| 67 | { |
| 68 | echo '<option value="null"> - Select - </option>'; |
| 69 | } |
| 70 | |
| 71 | // loop through values and add them as options |
| 72 | foreach($field['choices'] as $key => $value) |
| 73 | { |
| 74 | if($field['optgroup']) |
| 75 | { |
| 76 | // this select is grouped with optgroup |
| 77 | if($key != '') echo '<optgroup label="'.$key.'">'; |
| 78 | |
| 79 | if($value) |
| 80 | { |
| 81 | foreach($value as $id => $label) |
| 82 | { |
| 83 | $selected = ''; |
| 84 | if(is_array($field['value']) && in_array($id, $field['value'])) |
| 85 | { |
| 86 | // 2. If the value is an array (multiple select), loop through values and check if it is selected |
| 87 | $selected = 'selected="selected"'; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | // 3. this is not a multiple select, just check normaly |
| 92 | if($id == $field['value']) |
| 93 | { |
| 94 | $selected = 'selected="selected"'; |
| 95 | } |
| 96 | } |
| 97 | echo '<option value="'.$id.'" '.$selected.'>'.$label.'</option>'; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if($key != '') echo '</optgroup>'; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | $selected = ''; |
| 106 | if(is_array($field['value']) && in_array($key, $field['value'])) |
| 107 | { |
| 108 | // 2. If the value is an array (multiple select), loop through values and check if it is selected |
| 109 | $selected = 'selected="selected"'; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | // 3. this is not a multiple select, just check normaly |
| 114 | if($key == $field['value']) |
| 115 | { |
| 116 | $selected = 'selected="selected"'; |
| 117 | } |
| 118 | } |
| 119 | echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>'; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | } |
| 125 | |
| 126 | echo '</select>'; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /*-------------------------------------------------------------------------------------- |
| 131 | * |
| 132 | * create_options |
| 133 | * |
| 134 | * @author Elliot Condon |
| 135 | * @since 2.0.6 |
| 136 | * @updated 2.2.0 |
| 137 | * |
| 138 | *-------------------------------------------------------------------------------------*/ |
| 139 | |
| 140 | function create_options($key, $field) |
| 141 | { |
| 142 | // defaults |
| 143 | $field['multiple'] = isset($field['multiple']) ? $field['multiple'] : '0'; |
| 144 | $field['allow_null'] = isset($field['allow_null']) ? $field['allow_null'] : '0'; |
| 145 | $field['default_value'] = isset($field['default_value']) ? $field['default_value'] : ''; |
| 146 | |
| 147 | // implode selects so they work in a textarea |
| 148 | if(isset($field['choices']) && is_array($field['choices'])) |
| 149 | { |
| 150 | foreach($field['choices'] as $choice_key => $choice_val) |
| 151 | { |
| 152 | $field['choices'][$choice_key] = $choice_key.' : '.$choice_val; |
| 153 | } |
| 154 | $field['choices'] = implode("\n", $field['choices']); |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | $field['choices'] = ""; |
| 159 | } |
| 160 | |
| 161 | ?> |
| 162 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 163 | <td class="label"> |
| 164 | <label for=""><?php _e("Choices",'acf'); ?></label> |
| 165 | <p class="description"><?php _e("Enter your choices one per line",'acf'); ?><br /> |
| 166 | <br /> |
| 167 | <?php _e("Red",'acf'); ?><br /> |
| 168 | <?php _e("Blue",'acf'); ?><br /> |
| 169 | <br /> |
| 170 | <?php _e("red : Red",'acf'); ?><br /> |
| 171 | <?php _e("blue : Blue",'acf'); ?><br /> |
| 172 | </p> |
| 173 | </td> |
| 174 | <td> |
| 175 | <textarea rows="5" name="fields[<?php echo $key; ?>][choices]" id=""><?php echo $field['choices']; ?></textarea> |
| 176 | </td> |
| 177 | </tr> |
| 178 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 179 | <td class="label"> |
| 180 | <label><?php _e("Default Value",'acf'); ?></label> |
| 181 | </td> |
| 182 | <td> |
| 183 | <?php |
| 184 | $this->parent->create_field(array( |
| 185 | 'type' => 'text', |
| 186 | 'name' => 'fields['.$key.'][default_value]', |
| 187 | 'value' => $field['default_value'], |
| 188 | )); |
| 189 | ?> |
| 190 | </td> |
| 191 | </tr> |
| 192 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 193 | <td class="label"> |
| 194 | <label><?php _e("Allow Null?",'acf'); ?></label> |
| 195 | </td> |
| 196 | <td> |
| 197 | <?php |
| 198 | $this->parent->create_field(array( |
| 199 | 'type' => 'radio', |
| 200 | 'name' => 'fields['.$key.'][allow_null]', |
| 201 | 'value' => $field['allow_null'], |
| 202 | 'choices' => array( |
| 203 | '1' => __("Yes",'acf'), |
| 204 | '0' => __("No",'acf'), |
| 205 | ), |
| 206 | 'layout' => 'horizontal', |
| 207 | )); |
| 208 | ?> |
| 209 | </td> |
| 210 | </tr> |
| 211 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 212 | <td class="label"> |
| 213 | <label><?php _e("Select multiple values?",'acf'); ?></label> |
| 214 | </td> |
| 215 | <td> |
| 216 | <?php |
| 217 | $this->parent->create_field(array( |
| 218 | 'type' => 'radio', |
| 219 | 'name' => 'fields['.$key.'][multiple]', |
| 220 | 'value' => $field['multiple'], |
| 221 | 'choices' => array( |
| 222 | '1' => __("Yes",'acf'), |
| 223 | '0' => __("No",'acf'), |
| 224 | ), |
| 225 | 'layout' => 'horizontal', |
| 226 | )); |
| 227 | ?> |
| 228 | </td> |
| 229 | </tr> |
| 230 | |
| 231 | <?php |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /*-------------------------------------------------------------------------------------- |
| 236 | * |
| 237 | * pre_save_field |
| 238 | * - called just before saving the field to the database. |
| 239 | * |
| 240 | * @author Elliot Condon |
| 241 | * @since 2.2.0 |
| 242 | * |
| 243 | *-------------------------------------------------------------------------------------*/ |
| 244 | |
| 245 | function pre_save_field($field) |
| 246 | { |
| 247 | // defaults |
| 248 | $field['choices'] = isset($field['choices']) ? $field['choices'] : ''; |
| 249 | |
| 250 | // vars |
| 251 | $new_choices = array(); |
| 252 | |
| 253 | // explode choices from each line |
| 254 | if(strpos($field['choices'], "\n") !== false) |
| 255 | { |
| 256 | // found multiple lines, explode it |
| 257 | $field['choices'] = explode("\n", $field['choices']); |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | // no multiple lines! |
| 262 | $field['choices'] = array($field['choices']); |
| 263 | } |
| 264 | |
| 265 | // key => value |
| 266 | foreach($field['choices'] as $choice) |
| 267 | { |
| 268 | if(strpos($choice, ' : ') !== false) |
| 269 | { |
| 270 | $choice = explode(' : ', $choice); |
| 271 | $new_choices[trim($choice[0])] = trim($choice[1]); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | $new_choices[trim($choice)] = trim($choice); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // update choices |
| 280 | $field['choices'] = $new_choices; |
| 281 | |
| 282 | // return updated field |
| 283 | return $field; |
| 284 | |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /*-------------------------------------------------------------------------------------- |
| 289 | * |
| 290 | * get_value_for_api |
| 291 | * |
| 292 | * @author Elliot Condon |
| 293 | * @since 3.1.2 |
| 294 | * |
| 295 | *-------------------------------------------------------------------------------------*/ |
| 296 | |
| 297 | function get_value_for_api($post_id, $field) |
| 298 | { |
| 299 | $value = parent::get_value($post_id, $field); |
| 300 | |
| 301 | if($value == 'null') |
| 302 | { |
| 303 | $value = false; |
| 304 | } |
| 305 | |
| 306 | return $value; |
| 307 | } |
| 308 | |
| 309 | } |
| 310 | |
| 311 | ?> |