| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Text Field Class |
| 5 |
*/ |
| 6 |
class WeForms_Form_Field_Dropdown extends WeForms_Field_Contract { |
| 7 |
|
| 8 |
function __construct() { |
| 9 |
$this->name = __( 'Dropdown', 'weforms' ); |
| 10 |
$this->input_type = 'dropdown_field'; |
| 11 |
$this->icon = 'caret-square-o-down'; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Render the text field |
| 16 |
* |
| 17 |
* @param array $field_settings |
| 18 |
* @param integer $form_id |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function render( $field_settings, $form_id ) { |
| 23 |
$selected = isset( $field_settings['selected'] ) ? $field_settings['selected'] : ''; |
| 24 |
$name = $field_settings['name']; |
| 25 |
?> |
| 26 |
<li <?php $this->print_list_attributes( $field_settings ); ?>> |
| 27 |
<?php $this->print_label( $field_settings, $form_id ); ?> |
| 28 |
|
| 29 |
<div class="wpuf-fields"> |
| 30 |
<select |
| 31 |
class="<?php echo 'wpuf_'. $field_settings['name'] .'_'. $form_id; ?>" |
| 32 |
id="<?php echo $field_settings['name'] . '_' . $form_id; ?>" |
| 33 |
name="<?php echo $name; ?>" |
| 34 |
data-required="<?php echo $field_settings['required'] ?>" |
| 35 |
data-type="select"> |
| 36 |
|
| 37 |
<?php if ( !empty( $field_settings['first'] ) ) { ?> |
| 38 |
<option value=""><?php echo $field_settings['first']; ?></option> |
| 39 |
<?php } ?> |
| 40 |
|
| 41 |
<?php |
| 42 |
if ( $field_settings['options'] && count( $field_settings['options'] ) > 0 ) { |
| 43 |
foreach ($field_settings['options'] as $value => $option) { |
| 44 |
$current_select = selected( $selected, $value, false ); |
| 45 |
?> |
| 46 |
<option value="<?php echo esc_attr( $value ); ?>"<?php echo $current_select; ?>><?php echo $option; ?></option> |
| 47 |
<?php |
| 48 |
} |
| 49 |
} |
| 50 |
?> |
| 51 |
</select> |
| 52 |
<?php $this->help_text( $field_settings ); ?> |
| 53 |
</div> |
| 54 |
|
| 55 |
</li> |
| 56 |
<?php |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Get field options setting |
| 61 |
* |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
public function get_options_settings() { |
| 65 |
$default_options = $this->get_default_option_settings(); |
| 66 |
$dropdown_options = array( |
| 67 |
$this->get_default_option_dropdown_settings(), |
| 68 |
|
| 69 |
array( |
| 70 |
'name' => 'first', |
| 71 |
'title' => __( 'Select Text', 'weforms' ), |
| 72 |
'type' => 'text', |
| 73 |
'section' => 'basic', |
| 74 |
'priority' => 13, |
| 75 |
'help_text' => __( "First element of the select dropdown. Leave this empty if you don't want to show this field", 'weforms' ), |
| 76 |
), |
| 77 |
); |
| 78 |
|
| 79 |
return array_merge( $default_options, $dropdown_options ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get the field props |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function get_field_props() { |
| 88 |
$defaults = $this->default_attributes(); |
| 89 |
$props = array( |
| 90 |
'selected' => '', |
| 91 |
'options' => array( 'Option' => __( 'Option', 'weforms' ) ), |
| 92 |
'first' => __( '— Select —', 'weforms' ), |
| 93 |
); |
| 94 |
|
| 95 |
return array_merge( $defaults, $props ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Prepare entry |
| 100 |
* |
| 101 |
* @param $field |
| 102 |
* |
| 103 |
* @return mixed |
| 104 |
*/ |
| 105 |
public function prepare_entry( $field ) { |
| 106 |
|
| 107 |
$val = $_POST[$field['name']]; |
| 108 |
return isset( $field['options'][$val] ) ? $field['options'][$val] : ''; |
| 109 |
} |
| 110 |
} |