<?php
class acf_field_select extends acf_field
{
function __construct()
{
$this->name = 'select';
$this->label = __("Select", 'acf');
$this->category = __("Choice", 'acf');
$this->defaults = array(
'multiple' => 0,
'allow_null' => 0,
'choices' => array(),
'default_value' => ''
);
parent::__construct();
add_filter('acf/update_field/type=checkbox', array($this, 'update_field'), 5, 2);
add_filter('acf/update_field/type=radio', array($this, 'update_field'), 5, 2);
}
function create_field( $field )
{
$optgroup = false;
if( is_array($field['choices']) )
{
foreach( $field['choices'] as $k => $v )
{
if( is_array($v) )
{
$optgroup = true;
}
}
}
if( !is_array($field['value']) )
{
if( strpos($field['value'], "\n") !== false )
{
$field['value'] = explode("\n", $field['value']);
}
else
{
$field['value'] = array( $field['value'] );
}
}
$field['value'] = array_map('trim', $field['value']);
$multiple = '';
if( $field['multiple'] )
{
echo '<input type="hidden" name="' . $field['name'] . '" />';
$multiple = ' multiple="multiple" size="5" ';
$field['name'] .= '[]';
}
echo '<select id="' . $field['id'] . '" class="' . $field['class'] . '" name="' . $field['name'] . '" ' . $multiple . ' >';
if( $field['allow_null'] )
{
echo '<option value="null">- ' . __("Select", 'acf') . ' -</option>';
}
if( is_array($field['choices']) )
{
foreach( $field['choices'] as $key => $value )
{
if( $optgroup )
{
if($key != '') echo '<optgroup label="'.$key.'">';
if( is_array($value) )
{
foreach($value as $id => $label)
{
$selected = in_array($id, $field['value']) ? 'selected="selected"' : '';
echo '<option value="'.$id.'" '.$selected.'>'.$label.'</option>';
}
}
if($key != '') echo '</optgroup>';
}
else
{
$selected = in_array($key, $field['value']) ? 'selected="selected"' : '';
echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';
}
}
}
echo '</select>';
}
function create_options( $field )
{
$key = $field['name'];
if( is_array($field['choices']) )
{
foreach( $field['choices'] as $k => $v )
{
$field['choices'][ $k ] = $k . ' : ' . $v;
}
$field['choices'] = implode("\n", $field['choices']);
}
?>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label for=""><?php _e("Choices", 'acf'); ?></label>
<p><?php _e("Enter each choice on a new line.", 'acf'); ?></p>
<p><?php _e("For more control, you may specify both a value and label like this:", 'acf'); ?></p>
<p><?php _e("red : Red", 'acf'); ?><br /><?php _e("blue : Blue", 'acf'); ?></p>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'textarea',
'class' => 'textarea field_option-choices',
'name' => 'fields['.$key.'][choices]',
'value' => $field['choices'],
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Default Value", 'acf'); ?></label>
<p class="description"><?php _e("Enter each default value on a new line", 'acf'); ?></p>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'textarea',
'name' => 'fields['.$key.'][default_value]',
'value' => $field['default_value'],
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Allow Null?", 'acf'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'radio',
'name' => 'fields['.$key.'][allow_null]',
'value' => $field['allow_null'],
'choices' => array(
1 => __("Yes", 'acf'),
0 => __("No", 'acf'),
),
'layout' => 'horizontal',
));
?>
</td>
</tr>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Select multiple values?", 'acf'); ?></label>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'radio',
'name' => 'fields['.$key.'][multiple]',
'value' => $field['multiple'],
'choices' => array(
1 => __("Yes", 'acf'),
0 => __("No", 'acf'),
),
'layout' => 'horizontal',
));
?>
</td>
</tr>
<?php
}
function format_value_for_api( $value, $post_id, $field )
{
if( $value == 'null' )
{
$value = false;
}
return $value;
}
function update_field( $field, $post_id )
{
if( is_array( $field['choices'] ))
{
return $field;
}
$new_choices = array();
if( $field['choices'] )
{
$field['choices'] = stripslashes_deep($field['choices']);
if(strpos($field['choices'], "\n") !== false)
{
$field['choices'] = explode("\n", $field['choices']);
}
else
{
$field['choices'] = array($field['choices']);
}
foreach($field['choices'] as $choice)
{
if(strpos($choice, ' : ') !== false)
{
$choice = explode(' : ', $choice);
$new_choices[ trim($choice[0]) ] = trim($choice[1]);
}
else
{
$new_choices[ trim($choice) ] = trim($choice);
}
}
}
$field['choices'] = $new_choices;
return $field;
}
}
new acf_field_select();
?>