| 1 |
<?php |
| 2 |
|
| 3 |
class acf_Select |
| 4 |
{ |
| 5 |
var $name; |
| 6 |
var $title; |
| 7 |
var $parent; |
| 8 |
|
| 9 |
function acf_Select($parent) |
| 10 |
{ |
| 11 |
$this->name = 'select'; |
| 12 |
$this->title = __("Select",'acf'); |
| 13 |
$this->parent = $parent; |
| 14 |
} |
| 15 |
|
| 16 |
function html($field) |
| 17 |
{ |
| 18 |
if(isset($field->options['multiple']) && $field->options['multiple'] == '1') |
| 19 |
{ |
| 20 |
$name_extra = '[]'; |
| 21 |
if(count($field->options['choices']) <= 1) |
| 22 |
{ |
| 23 |
$name_extra = ''; |
| 24 |
} |
| 25 |
echo '<select id="'.$field->input_name.'" class="'.$field->input_class.'" name="'.$field->input_name.$name_extra.'" multiple="multiple" size="5" >'; |
| 26 |
} |
| 27 |
else |
| 28 |
{ |
| 29 |
echo '<select id="'.$field->input_name.'" class="'.$field->input_class.'" name="'.$field->input_name.'" >'; |
| 30 |
// add top option |
| 31 |
//echo '<option value="null">- Select Option -</option>'; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
// loop through values and add them as options |
| 36 |
foreach($field->options['choices'] as $key => $value) |
| 37 |
{ |
| 38 |
$selected = ''; |
| 39 |
if(is_array($field->value)) |
| 40 |
{ |
| 41 |
// 2. If the value is an array (multiple select), loop through values and check if it is selected |
| 42 |
if(in_array($key, $field->value)) |
| 43 |
{ |
| 44 |
$selected = 'selected="selected"'; |
| 45 |
} |
| 46 |
} |
| 47 |
else |
| 48 |
{ |
| 49 |
// 3. this is not a multiple select, just check normaly |
| 50 |
if($key == $field->value) |
| 51 |
{ |
| 52 |
$selected = 'selected="selected"'; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>'; |
| 58 |
} |
| 59 |
|
| 60 |
echo '</select>'; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
function options_html($key, $options) |
| 65 |
{ |
| 66 |
// implode selects so they work in a textarea |
| 67 |
if(isset($options['choices']) && is_array($options['choices'])) |
| 68 |
{ |
| 69 |
foreach($options['choices'] as $choice_key => $choice_val) |
| 70 |
{ |
| 71 |
$options['choices'][$choice_key] = $choice_key.' : '.$choice_val; |
| 72 |
} |
| 73 |
$options['choices'] = implode("\n", $options['choices']); |
| 74 |
} |
| 75 |
else |
| 76 |
{ |
| 77 |
$options['choices'] = ""; |
| 78 |
} |
| 79 |
|
| 80 |
if(!isset($options['multiple'])) |
| 81 |
{ |
| 82 |
$options['multiple'] = '0'; |
| 83 |
} |
| 84 |
|
| 85 |
?> |
| 86 |
|
| 87 |
<tr class="field_option field_option_select"> |
| 88 |
<td class="label"> |
| 89 |
<label for=""><?php _e("Choices",'acf'); ?></label> |
| 90 |
<p class="description"><?php _e("Enter your choices one per line<br /> |
| 91 |
<br /> |
| 92 |
Red<br /> |
| 93 |
Blue<br /> |
| 94 |
<br /> |
| 95 |
or<br /> |
| 96 |
<br /> |
| 97 |
red : Red<br /> |
| 98 |
blue : Blue",'acf'); ?></p> |
| 99 |
</td> |
| 100 |
<td> |
| 101 |
<textarea rows="5" name="acf[fields][<?php echo $key; ?>][options][choices]" id=""><?php echo $options['choices']; ?></textarea> |
| 102 |
<p class="description"></p> |
| 103 |
</td> |
| 104 |
</tr> |
| 105 |
<tr class="field_option field_option_select"> |
| 106 |
<td class="label"> |
| 107 |
<label><?php _e("Select multiple values?",'acf'); ?></label> |
| 108 |
</td> |
| 109 |
<td> |
| 110 |
<?php |
| 111 |
$temp_field = new stdClass(); |
| 112 |
$temp_field->type = 'true_false'; |
| 113 |
$temp_field->input_name = 'acf[fields]['.$key.'][options][multiple]'; |
| 114 |
$temp_field->input_class = ''; |
| 115 |
$temp_field->value = $options['multiple']; |
| 116 |
$temp_field->options = array('message' => ''); |
| 117 |
$this->parent->create_field($temp_field); |
| 118 |
?> |
| 119 |
</td> |
| 120 |
</tr> |
| 121 |
|
| 122 |
<?php |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/*--------------------------------------------------------------------------------------------- |
| 127 |
* Format Options |
| 128 |
* - this is called from save_field.php, this function formats the options into a savable format |
| 129 |
* |
| 130 |
* @author Elliot Condon |
| 131 |
* @since 1.1 |
| 132 |
* |
| 133 |
---------------------------------------------------------------------------------------------*/ |
| 134 |
function format_options($options) |
| 135 |
{ |
| 136 |
// if no choices, dont do anything |
| 137 |
if($options['choices'] == '') |
| 138 |
{ |
| 139 |
return $options; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
// explode choices from each line |
| 144 |
if(strpos($options['choices'], "\n") !== false) |
| 145 |
{ |
| 146 |
// found multiple lines, explode it |
| 147 |
$choices = explode("\n", $options['choices']); |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
// no multiple lines! |
| 152 |
$choices = array($options['choices']); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
$new_choices = array(); |
| 158 |
foreach($choices as $choice) |
| 159 |
{ |
| 160 |
if(strpos($choice, ' : ') !== false) |
| 161 |
{ |
| 162 |
|
| 163 |
$choice = explode(' : ', $choice); |
| 164 |
$new_choices[trim($choice[0])] = trim($choice[1]); |
| 165 |
} |
| 166 |
else |
| 167 |
{ |
| 168 |
$new_choices[trim($choice)] = trim($choice); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
// return array containing all choices |
| 174 |
$options['choices'] = $new_choices; |
| 175 |
|
| 176 |
return $options; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
/*--------------------------------------------------------------------------------------------- |
| 181 |
* Format Value |
| 182 |
* - this is called from api.php |
| 183 |
* |
| 184 |
* @author Elliot Condon |
| 185 |
* @since 1.1 |
| 186 |
* |
| 187 |
---------------------------------------------------------------------------------------------*/ |
| 188 |
function format_value_for_api($value) |
| 189 |
{ |
| 190 |
return $this->format_value_for_input($value); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/*--------------------------------------------------------------------------------------------- |
| 195 |
* Format Value for input |
| 196 |
* - this is called from api.php |
| 197 |
* |
| 198 |
* @author Elliot Condon |
| 199 |
* @since 1.1 |
| 200 |
* |
| 201 |
---------------------------------------------------------------------------------------------*/ |
| 202 |
function format_value_for_input($value) |
| 203 |
{ |
| 204 |
$is_array = @unserialize($value); |
| 205 |
|
| 206 |
if($is_array) |
| 207 |
{ |
| 208 |
return unserialize($value); |
| 209 |
} |
| 210 |
else |
| 211 |
{ |
| 212 |
return $value; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
?> |