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