| 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<br /> |
| 166 |
<br /> |
| 167 |
Red<br /> |
| 168 |
Blue<br /> |
| 169 |
<br /> |
| 170 |
or<br /> |
| 171 |
<br /> |
| 172 |
red : Red<br /> |
| 173 |
blue : Blue",'acf'); ?></p> |
| 174 |
</td> |
| 175 |
<td> |
| 176 |
<textarea rows="5" name="fields[<?php echo $key; ?>][choices]" id=""><?php echo $field['choices']; ?></textarea> |
| 177 |
</td> |
| 178 |
</tr> |
| 179 |
<tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 180 |
<td class="label"> |
| 181 |
<label><?php _e("Default Value",'acf'); ?></label> |
| 182 |
</td> |
| 183 |
<td> |
| 184 |
<?php |
| 185 |
$this->parent->create_field(array( |
| 186 |
'type' => 'text', |
| 187 |
'name' => 'fields['.$key.'][default_value]', |
| 188 |
'value' => $field['default_value'], |
| 189 |
)); |
| 190 |
?> |
| 191 |
</td> |
| 192 |
</tr> |
| 193 |
<tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 194 |
<td class="label"> |
| 195 |
<label><?php _e("Allow Null?",'acf'); ?></label> |
| 196 |
</td> |
| 197 |
<td> |
| 198 |
<?php |
| 199 |
$this->parent->create_field(array( |
| 200 |
'type' => 'radio', |
| 201 |
'name' => 'fields['.$key.'][allow_null]', |
| 202 |
'value' => $field['allow_null'], |
| 203 |
'choices' => array( |
| 204 |
'1' => 'Yes', |
| 205 |
'0' => 'No', |
| 206 |
), |
| 207 |
'layout' => 'horizontal', |
| 208 |
)); |
| 209 |
?> |
| 210 |
</td> |
| 211 |
</tr> |
| 212 |
<tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 213 |
<td class="label"> |
| 214 |
<label><?php _e("Select multiple values?",'acf'); ?></label> |
| 215 |
</td> |
| 216 |
<td> |
| 217 |
<?php |
| 218 |
$this->parent->create_field(array( |
| 219 |
'type' => 'radio', |
| 220 |
'name' => 'fields['.$key.'][multiple]', |
| 221 |
'value' => $field['multiple'], |
| 222 |
'choices' => array( |
| 223 |
'1' => 'Yes', |
| 224 |
'0' => 'No', |
| 225 |
), |
| 226 |
'layout' => 'horizontal', |
| 227 |
)); |
| 228 |
?> |
| 229 |
</td> |
| 230 |
</tr> |
| 231 |
|
| 232 |
<?php |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
/*-------------------------------------------------------------------------------------- |
| 237 |
* |
| 238 |
* pre_save_field |
| 239 |
* - called just before saving the field to the database. |
| 240 |
* |
| 241 |
* @author Elliot Condon |
| 242 |
* @since 2.2.0 |
| 243 |
* |
| 244 |
*-------------------------------------------------------------------------------------*/ |
| 245 |
|
| 246 |
function pre_save_field($field) |
| 247 |
{ |
| 248 |
// defaults |
| 249 |
$field['choices'] = isset($field['choices']) ? $field['choices'] : ''; |
| 250 |
|
| 251 |
// vars |
| 252 |
$new_choices = array(); |
| 253 |
|
| 254 |
// explode choices from each line |
| 255 |
if(strpos($field['choices'], "\n") !== false) |
| 256 |
{ |
| 257 |
// found multiple lines, explode it |
| 258 |
$field['choices'] = explode("\n", $field['choices']); |
| 259 |
} |
| 260 |
else |
| 261 |
{ |
| 262 |
// no multiple lines! |
| 263 |
$field['choices'] = array($field['choices']); |
| 264 |
} |
| 265 |
|
| 266 |
// key => value |
| 267 |
foreach($field['choices'] as $choice) |
| 268 |
{ |
| 269 |
if(strpos($choice, ' : ') !== false) |
| 270 |
{ |
| 271 |
$choice = explode(' : ', $choice); |
| 272 |
$new_choices[trim($choice[0])] = trim($choice[1]); |
| 273 |
} |
| 274 |
else |
| 275 |
{ |
| 276 |
$new_choices[trim($choice)] = trim($choice); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
// update choices |
| 281 |
$field['choices'] = $new_choices; |
| 282 |
|
| 283 |
// return updated field |
| 284 |
return $field; |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
/*-------------------------------------------------------------------------------------- |
| 290 |
* |
| 291 |
* get_value_for_api |
| 292 |
* |
| 293 |
* @author Elliot Condon |
| 294 |
* @since 3.1.2 |
| 295 |
* |
| 296 |
*-------------------------------------------------------------------------------------*/ |
| 297 |
|
| 298 |
function get_value_for_api($post_id, $field) |
| 299 |
{ |
| 300 |
$value = parent::get_value($post_id, $field); |
| 301 |
|
| 302 |
if($value == 'null') |
| 303 |
{ |
| 304 |
$value = false; |
| 305 |
} |
| 306 |
|
| 307 |
return $value; |
| 308 |
} |
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
?> |