class-evf-field-address.php
1 year ago
class-evf-field-ai.php
1 year ago
class-evf-field-captcha.php
1 month ago
class-evf-field-checkbox.php
3 months ago
class-evf-field-color.php
1 month ago
class-evf-field-country.php
3 months ago
class-evf-field-credit-card.php
1 month ago
class-evf-field-date-time.php
1 month ago
class-evf-field-divider.php
2 years ago
class-evf-field-email.php
1 year ago
class-evf-field-file-upload.php
1 year ago
class-evf-field-first-name.php
1 year ago
class-evf-field-hcaptcha.php
4 months ago
class-evf-field-hidden.php
1 year ago
class-evf-field-html.php
9 months ago
class-evf-field-image-upload.php
1 year ago
class-evf-field-last-name.php
1 year ago
class-evf-field-likert.php
1 year ago
class-evf-field-lookup.php
1 month ago
class-evf-field-number.php
1 year ago
class-evf-field-password.php
1 month ago
class-evf-field-payment-authorize-net.php
1 month ago
class-evf-field-payment-checkbox.php
1 month ago
class-evf-field-payment-coupon.php
1 month ago
class-evf-field-payment-gateway-selector.php
1 month ago
class-evf-field-payment-quantity.php
1 month ago
class-evf-field-payment-radio.php
1 month ago
class-evf-field-payment-single.php
1 month ago
class-evf-field-payment-square.php
1 year ago
class-evf-field-payment-subscription-plan.php
1 month ago
class-evf-field-payment-subtotal.php
1 month ago
class-evf-field-payment-total.php
1 month ago
class-evf-field-phone.php
1 year ago
class-evf-field-privacy-policy.php
3 months ago
class-evf-field-private-note.php
1 year ago
class-evf-field-progress.php
1 month ago
class-evf-field-radio.php
3 months ago
class-evf-field-range-slider.php
1 month ago
class-evf-field-rating.php
3 months ago
class-evf-field-recaptcha.php
4 months ago
class-evf-field-repeater.php
1 month ago
class-evf-field-reset.php
1 month ago
class-evf-field-scale-rating.php
1 year ago
class-evf-field-select.php
1 year ago
class-evf-field-signature.php
1 month ago
class-evf-field-text.php
1 year ago
class-evf-field-textarea.php
1 year ago
class-evf-field-title.php
2 years ago
class-evf-field-turnstile.php
4 months ago
class-evf-field-url.php
1 year ago
class-evf-field-wysiwyg.php
3 months ago
class-evf-field-yes-no.php
1 year ago
payment-amount-helpers.php
1 month ago
class-evf-field-likert.php
744 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Likert field. |
| 4 | * |
| 5 | * @package EverestForms\Fields |
| 6 | * @since 1.0.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Field_Likert Class. |
| 13 | */ |
| 14 | class EVF_Field_Likert extends EVF_Form_Fields { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->name = esc_html__( 'Likert', 'everest-forms' ); |
| 21 | $this->type = 'likert'; |
| 22 | $this->icon = 'evf-icon evf-icon-likert'; |
| 23 | $this->order = 20; |
| 24 | $this->group = 'survey'; |
| 25 | $this->defaults = array( |
| 26 | 'likert_rows' => array( |
| 27 | 1 => esc_html__( 'Question #1', 'everest-forms' ), |
| 28 | 2 => esc_html__( 'Question #2', 'everest-forms' ), |
| 29 | 3 => esc_html__( 'Question #3', 'everest-forms' ), |
| 30 | ), |
| 31 | 'likert_columns' => array( |
| 32 | 1 => esc_html__( 'Not Satisfied', 'everest-forms' ), |
| 33 | 2 => esc_html__( 'Somewhat Satisfied', 'everest-forms' ), |
| 34 | 3 => esc_html__( 'Satisfied', 'everest-forms' ), |
| 35 | 4 => esc_html__( 'Very Satisfied', 'everest-forms' ), |
| 36 | ), |
| 37 | 'drop_down_choices' => array( |
| 38 | 1 => array( |
| 39 | 'label' => esc_html__( 'Option 1', 'everest-forms' ), |
| 40 | 'default' => '', |
| 41 | ), |
| 42 | 2 => array( |
| 43 | 'label' => esc_html__( 'Option 2', 'everest-forms' ), |
| 44 | 'default' => '', |
| 45 | ), |
| 46 | ), |
| 47 | ); |
| 48 | $this->settings = array( |
| 49 | 'basic-options' => array( |
| 50 | 'field_options' => array( |
| 51 | 'label', |
| 52 | 'input_type', |
| 53 | 'drop_down_choices', |
| 54 | 'likert_rows', |
| 55 | 'likert_columns', |
| 56 | 'description', |
| 57 | 'required', |
| 58 | 'required_field_message_setting', |
| 59 | 'required_field_message', |
| 60 | ), |
| 61 | ), |
| 62 | 'advanced-options' => array( |
| 63 | 'field_options' => array( |
| 64 | 'meta', |
| 65 | 'label_hide', |
| 66 | 'css', |
| 67 | ), |
| 68 | ), |
| 69 | ); |
| 70 | |
| 71 | parent::__construct(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Hook in tabs. |
| 76 | */ |
| 77 | public function init_hooks() { |
| 78 | add_filter( 'everest_forms_field_properties_' . $this->type, array( $this, 'field_properties' ), 5, 3 ); |
| 79 | add_filter( 'everest_forms_entry_save_fields', array( $this, 'save_field' ), 10, 3 ); |
| 80 | add_filter( 'everest_forms_field_new_default', array( $this, 'add_default_likert_rows' ) ); |
| 81 | add_filter( 'everest_forms_format_csv_field_data', array( $this, 'format_csv_data' ), 10, 6 ); |
| 82 | add_filter( 'everest_forms_field_exporter_' . $this->type, array( $this, 'field_exporter' ) ); |
| 83 | add_filter( 'everest_forms_entries_field_editable', array( $this, 'field_editable' ), 10, 2 ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Input Type field option. |
| 88 | * |
| 89 | * @param array $field Field settings. |
| 90 | */ |
| 91 | public function input_type( $field ) { |
| 92 | $lbl = $this->field_element( |
| 93 | 'label', |
| 94 | $field, |
| 95 | array( |
| 96 | 'slug' => 'input_type', |
| 97 | 'value' => esc_html__( 'Input Type', 'everest-forms' ), |
| 98 | 'tooltip' => esc_html__( 'Select an input method.', 'everest-forms' ), |
| 99 | ), |
| 100 | false |
| 101 | ); |
| 102 | $fld = $this->field_element( |
| 103 | 'select', |
| 104 | $field, |
| 105 | array( |
| 106 | 'slug' => 'input_type', |
| 107 | 'value' => ! empty( $field['input_type'] ) ? esc_attr( $field['input_type'] ) : 'radio', |
| 108 | 'options' => array( |
| 109 | 'radio' => __( 'Radio Button', 'everest-forms' ), |
| 110 | 'checkbox' => __( 'Checkbox', 'everest-forms' ), |
| 111 | 'select' => __( 'Drop Down', 'everest-forms' ), |
| 112 | ), |
| 113 | ), |
| 114 | false |
| 115 | ); |
| 116 | $this->field_element( |
| 117 | 'row', |
| 118 | $field, |
| 119 | array( |
| 120 | 'slug' => 'input_type', |
| 121 | 'content' => $lbl . $fld, |
| 122 | ) |
| 123 | ); |
| 124 | |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Rows field option. |
| 129 | * |
| 130 | * @param array $field Field settings. |
| 131 | */ |
| 132 | public function likert_rows( $field ) { |
| 133 | $values = ! empty( $field['likert_rows'] ) ? $field['likert_rows'] : $this->defaults['likert_rows']; |
| 134 | $lbl = $this->field_element( |
| 135 | 'label', |
| 136 | $field, |
| 137 | array( |
| 138 | 'slug' => 'likert_rows', |
| 139 | 'value' => esc_html__( 'Rows', 'everest-forms' ), |
| 140 | 'tooltip' => esc_html__( 'Add rows to the likert.', 'everest-forms' ), |
| 141 | ), |
| 142 | false |
| 143 | ); |
| 144 | $fld = sprintf( |
| 145 | '<ul data-next-id="%s" class="evf-choices-list evf-survey-choices" data-field-id="%s" data-field-type="%s" data-choice-type="%s">', |
| 146 | max( array_keys( $values ) ) + 1, |
| 147 | $field['id'], |
| 148 | $this->type, |
| 149 | 'likert_rows' |
| 150 | ); |
| 151 | foreach ( $values as $key => $value ) { |
| 152 | $fld .= sprintf( '<li data-key="%d">', $key ); |
| 153 | $fld .= '<span class="sort"><svg width="18" height="18" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 18 18" role="img" aria-hidden="true" focusable="false"><path d="M13,8c0.6,0,1-0.4,1-1s-0.4-1-1-1s-1,0.4-1,1S12.4,8,13,8z M5,6C4.4,6,4,6.4,4,7s0.4,1,1,1s1-0.4,1-1S5.6,6,5,6z M5,10 c-0.6,0-1,0.4-1,1s0.4,1,1,1s1-0.4,1-1S5.6,10,5,10z M13,10c-0.6,0-1,0.4-1,1s0.4,1,1,1s1-0.4,1-1S13.6,10,13,10z M9,6 C8.4,6,8,6.4,8,7s0.4,1,1,1s1-0.4,1-1S9.6,6,9,6z M9,10c-0.6,0-1,0.4-1,1s0.4,1,1,1s1-0.4,1-1S9.6,10,9,10z"></path></svg></span>'; |
| 154 | $fld .= '<div class="evf-choice-list-input">'; |
| 155 | $fld .= sprintf( '<input type="text" name="form_fields[%s][likert_rows][%s]" value="%s" class="label">', esc_attr( $field['id'] ), $key, esc_attr( $value ) ); |
| 156 | $fld .= '</div>'; |
| 157 | $fld .= '<a class="add" href="#" title="' . esc_attr__( 'Add likert scale row', 'everest-forms' ) . '"><i class="dashicons dashicons-plus-alt"></i></a>'; |
| 158 | $fld .= '<a class="remove" href="# title="' . esc_attr__( 'Remove likert scale row', 'everest-forms' ) . '"><i class="dashicons dashicons-dismiss"></i></a>'; |
| 159 | $fld .= '</li>'; |
| 160 | } |
| 161 | $fld .= '</ul>'; |
| 162 | $this->field_element( |
| 163 | 'row', |
| 164 | $field, |
| 165 | array( |
| 166 | 'slug' => 'rows', |
| 167 | 'content' => $lbl . $fld, |
| 168 | ) |
| 169 | ); |
| 170 | |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Columns field option. |
| 175 | * |
| 176 | * @param array $field Field settings. |
| 177 | */ |
| 178 | public function likert_columns( $field ) { |
| 179 | $values = ! empty( $field['likert_columns'] ) ? $field['likert_columns'] : $this->defaults['likert_columns']; |
| 180 | $lbl = $this->field_element( |
| 181 | 'label', |
| 182 | $field, |
| 183 | array( |
| 184 | 'slug' => 'likert_columns', |
| 185 | 'value' => esc_html__( 'Columns', 'everest-forms' ), |
| 186 | 'tooltip' => esc_html__( 'Add columns to the likert.', 'everest-forms' ), |
| 187 | ), |
| 188 | false |
| 189 | ); |
| 190 | $fld = sprintf( |
| 191 | '<ul data-next-id="%s" class="evf-choices-list evf-survey-choices" data-field-id="%s" data-field-type="%s" data-choice-type="%s">', |
| 192 | max( array_keys( $values ) ) + 1, |
| 193 | $field['id'], |
| 194 | $this->type, |
| 195 | 'likert_columns' |
| 196 | ); |
| 197 | foreach ( $values as $key => $value ) { |
| 198 | $fld .= sprintf( '<li data-key="%d">', $key ); |
| 199 | $fld .= '<span class="sort"><svg width="18" height="18" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 18 18" role="img" aria-hidden="true" focusable="false"><path d="M13,8c0.6,0,1-0.4,1-1s-0.4-1-1-1s-1,0.4-1,1S12.4,8,13,8z M5,6C4.4,6,4,6.4,4,7s0.4,1,1,1s1-0.4,1-1S5.6,6,5,6z M5,10 c-0.6,0-1,0.4-1,1s0.4,1,1,1s1-0.4,1-1S5.6,10,5,10z M13,10c-0.6,0-1,0.4-1,1s0.4,1,1,1s1-0.4,1-1S13.6,10,13,10z M9,6 C8.4,6,8,6.4,8,7s0.4,1,1,1s1-0.4,1-1S9.6,6,9,6z M9,10c-0.6,0-1,0.4-1,1s0.4,1,1,1s1-0.4,1-1S9.6,10,9,10z"></path></svg></span>'; |
| 200 | $fld .= '<div class="evf-choice-list-input">'; |
| 201 | $fld .= sprintf( '<input type="text" name="form_fields[%s][likert_columns][%s]" value="%s" class="label">', esc_attr( $field['id'] ), $key, esc_attr( $value ) ); |
| 202 | $fld .= '</div>'; |
| 203 | $fld .= '<a class="add" href="#" title="' . esc_attr__( 'Add likert scale row', 'everest-forms' ) . '"><i class="dashicons dashicons-plus-alt"></i></a>'; |
| 204 | $fld .= '<a class="remove" href="# title="' . esc_attr__( 'Remove likert scale row', 'everest-forms' ) . '"><i class="dashicons dashicons-dismiss"></i></a>'; |
| 205 | $fld .= '</li>'; |
| 206 | } |
| 207 | $fld .= '</ul>'; |
| 208 | $this->field_element( |
| 209 | 'row', |
| 210 | $field, |
| 211 | array( |
| 212 | 'slug' => 'columns', |
| 213 | 'content' => $lbl . $fld, |
| 214 | ) |
| 215 | ); |
| 216 | |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Dropdown choices field option. |
| 221 | * |
| 222 | * @param array $field Field settings. |
| 223 | */ |
| 224 | public function drop_down_choices( $field ) { |
| 225 | $values = ! empty( $field['drop_down_choices'] ) ? $field['drop_down_choices'] : $this->defaults['drop_down_choices']; |
| 226 | $input_type = isset( $field['input_type'] ) ? $field['input_type'] : 'radio'; |
| 227 | $class = 'select' === $input_type ? 'everest-forms-likert-dd-options everest-forms-show' : 'everest-forms-likert-dd-options everest-forms-hidden'; |
| 228 | echo sprintf( '<div class="%s">', esc_attr( $class ) ); |
| 229 | $lbl = $this->field_element( |
| 230 | 'label', |
| 231 | $field, |
| 232 | array( |
| 233 | 'slug' => 'drop_down_choices', |
| 234 | 'value' => esc_html__( 'Dropdown Choices', 'everest-forms' ), |
| 235 | 'tooltip' => esc_html__( 'Add dropdown choices.', 'everest-forms' ), |
| 236 | ), |
| 237 | false |
| 238 | ); |
| 239 | $fld = sprintf( |
| 240 | '<ul data-next-id="%s" class="evf-choices-list evf-survey-choices" data-field-id="%s" data-field-type="%s" data-choice-type="%s">', |
| 241 | max( array_keys( $values ) ) + 1, |
| 242 | $field['id'], |
| 243 | $this->type, |
| 244 | 'drop_down_choices' |
| 245 | ); |
| 246 | |
| 247 | foreach ( $values as $key => $value ) { |
| 248 | $default = ! empty( $value['default'] ) ? $value['default'] : ''; |
| 249 | $label = isset( $value['label'] ) ? $value['label'] : ''; |
| 250 | $fld .= sprintf( '<li data-key="%d">', $key ); |
| 251 | $fld .= '<span class="sort"><svg width="18" height="18" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 18 18" role="img" aria-hidden="true" focusable="false"><path d="M13,8c0.6,0,1-0.4,1-1s-0.4-1-1-1s-1,0.4-1,1S12.4,8,13,8z M5,6C4.4,6,4,6.4,4,7s0.4,1,1,1s1-0.4,1-1S5.6,6,5,6z M5,10 c-0.6,0-1,0.4-1,1s0.4,1,1,1s1-0.4,1-1S5.6,10,5,10z M13,10c-0.6,0-1,0.4-1,1s0.4,1,1,1s1-0.4,1-1S13.6,10,13,10z M9,6 C8.4,6,8,6.4,8,7s0.4,1,1,1s1-0.4,1-1S9.6,6,9,6z M9,10c-0.6,0-1,0.4-1,1s0.4,1,1,1s1-0.4,1-1S9.6,10,9,10z"></path></svg></span>'; |
| 252 | $fld .= sprintf( '<input type="radio" name="form_fields[%s][drop_down_choices][%s][default]" class="default" value="1" %s>', esc_attr( $field['id'] ), $key, checked( '1', $default, false ) ); |
| 253 | $fld .= '<div class="evf-choice-list-input">'; |
| 254 | $fld .= sprintf( '<input type="text" name="form_fields[%s][drop_down_choices][%s][label]" value="%s" class="label">', esc_attr( $field['id'] ), $key, esc_attr( $label ) ); |
| 255 | $fld .= '</div>'; |
| 256 | $fld .= '<a class="add" href="#" title="' . esc_attr__( 'Add likert scale row', 'everest-forms' ) . '"><i class="dashicons dashicons-plus-alt"></i></a>'; |
| 257 | $fld .= '<a class="remove" href="#" title="' . esc_attr__( 'Remove likert scale row', 'everest-forms' ) . '"><i class="dashicons dashicons-dismiss"></i></a>'; |
| 258 | $fld .= '</li>'; |
| 259 | } |
| 260 | |
| 261 | $fld .= '</ul>'; |
| 262 | $this->field_element( |
| 263 | 'row', |
| 264 | $field, |
| 265 | array( |
| 266 | 'slug' => 'drop_down_choices', |
| 267 | 'content' => $lbl . $fld, |
| 268 | ) |
| 269 | ); |
| 270 | echo '</div>'; |
| 271 | |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Define additional field properties. |
| 276 | * |
| 277 | * @since 1.0.0 |
| 278 | * |
| 279 | * @param array $properties Field properties. |
| 280 | * @param array $field Field settings. |
| 281 | * @param array $form_data Form data and settings. |
| 282 | * |
| 283 | * @return array of additional field properties. |
| 284 | */ |
| 285 | public function field_properties( $properties, $field, $form_data ) { |
| 286 | // Remove the attributes since this field only appers with survey, polls and quiz. |
| 287 | unset( $properties['inputs']['primary'] ); |
| 288 | |
| 289 | $form_id = $form_data['id']; |
| 290 | $field_id = $field['id']; |
| 291 | foreach ( (array) $field['likert_columns'] as $column_key => $column ) { |
| 292 | foreach ( $field['likert_rows'] as $row_key => $row ) { |
| 293 | if ( 'checkbox' === $field['input_type'] ) { |
| 294 | $name = "everest_forms[form_fields][{$field_id}][{$row_key}][]"; |
| 295 | } elseif ( 'select' === $field['input_type'] ) { |
| 296 | $name = "everest_forms[form_fields][{$field_id}][{$row_key}][{$column_key}]"; |
| 297 | } else { |
| 298 | $name = "everest_forms[form_fields][{$field_id}][{$row_key}]"; |
| 299 | } |
| 300 | $properties['inputs'][ "rows{$row_key}_columns{$column_key}" ] = array( |
| 301 | 'attr' => array( |
| 302 | 'name' => $name, |
| 303 | 'value' => $column_key, |
| 304 | ), |
| 305 | 'block' => array(), |
| 306 | 'class' => array( 'everest-forms-likert-field-option', 'input-text' ), |
| 307 | 'data' => array(), |
| 308 | 'id' => "everest_forms-{$form_id}-field_{$field_id}_{$row_key}_{$column_key}", |
| 309 | 'required' => ! empty( $field['required'] ) ? 'required' : '', |
| 310 | 'sublabel' => array( |
| 311 | 'hidden' => 1, |
| 312 | 'value' => sanitize_text_field( "{$row} {$column}" ), |
| 313 | ), |
| 314 | ); |
| 315 | |
| 316 | if ( ! empty( $properties['error']['value'][ "rows{$row_key}" ] ) ) { |
| 317 | $properties['inputs'][ "rows{$row_key}_columns{$column_key}" ]['class'][] = 'evf-error'; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return $properties; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Customize the information stored in the entry_field database. |
| 327 | * |
| 328 | * @param array $field Field settings. |
| 329 | * @param array $form_data Form data. |
| 330 | * @param int $entry_id Entry ID. |
| 331 | */ |
| 332 | public function save_field( $field, $form_data, $entry_id ) { |
| 333 | if ( $this->type === $field['type'] && ! empty( $field['value'] ) ) { |
| 334 | $field['value'] = wp_json_encode( |
| 335 | array( |
| 336 | 'value' => $field['value'], |
| 337 | 'value_raw' => $field['value_raw'], |
| 338 | ) |
| 339 | ); |
| 340 | } |
| 341 | |
| 342 | return $field; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Add default rows in the $field data. |
| 347 | * |
| 348 | * @param array $field Field settings. |
| 349 | */ |
| 350 | public function add_default_likert_rows( $field ) { |
| 351 | $field['likert_rows'] = $this->defaults['likert_rows']; |
| 352 | return $field; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Format field value for CSV export. |
| 357 | * |
| 358 | * @since 1.1.1 |
| 359 | * |
| 360 | * @param string $processed_value Processed or sanitized field value. |
| 361 | * @param string $raw_value Raw field value that hasn't been processed. |
| 362 | * @param string $meta_key Field meta key. |
| 363 | * @param string $field_label Field label. |
| 364 | * @param array $columns Columns in CSV data. |
| 365 | * @param object $entry Entry data. |
| 366 | * |
| 367 | * @return string |
| 368 | */ |
| 369 | public function format_csv_data( $processed_value, $raw_value, $meta_key, $field_label, $columns, $entry ) { |
| 370 | if ( ! empty( $meta_key ) ) { |
| 371 | $fields = evf_get_form_fields( $entry->form_id, array( 'likert' ) ); |
| 372 | |
| 373 | foreach ( $fields as $field_id => $field ) { |
| 374 | if ( $field['meta-key'] === $meta_key ) { |
| 375 | $processed_value = evf_is_json( $raw_value ) ? json_decode( $raw_value, true ) : $raw_value; |
| 376 | $processed_value = isset( $processed_value['value'] ) ? $processed_value['value'] : $processed_value; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | return $processed_value; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Filter callback for outputting formatted data. |
| 385 | * |
| 386 | * @param array $field Field Data. |
| 387 | */ |
| 388 | public function field_exporter( $field ) { |
| 389 | |
| 390 | $export_field = array(); |
| 391 | |
| 392 | $export_field['label'] = ! empty( $field['name'] ) ? $field['name'] : ucfirst( str_replace( '_', ' ', $field['type'] ) ) . " - {$field['id']}"; |
| 393 | $export_field['value'] = false; |
| 394 | |
| 395 | if ( ! empty( $field['value'] ) ) { |
| 396 | $field_value = $field['value']; |
| 397 | |
| 398 | $items = preg_split( "/\r\n|\n|\r/", $field_value ); |
| 399 | |
| 400 | $output = '<style type="text/css"> |
| 401 | #evf-likert>table { |
| 402 | border-collapse: collapse; |
| 403 | } |
| 404 | .evf-likert-td { |
| 405 | border: 1px solid black; |
| 406 | border-collapse: collapse; |
| 407 | } |
| 408 | </style>'; |
| 409 | |
| 410 | $output .= '<div id="evf-likert"><table cellpadding="8" border="1">'; |
| 411 | |
| 412 | $total_items = count( $items ); |
| 413 | |
| 414 | for ( $i = 0; $i < $total_items; $i++ ) { |
| 415 | $output .= '<tr><td class="evf-likert-td">' . $items[ $i++ ] . '</td>'; |
| 416 | $output .= '<td class="evf-likert-td">' . $items[ $i ] . '</td></tr>'; |
| 417 | } |
| 418 | |
| 419 | $output .= '</table></div>'; |
| 420 | |
| 421 | $export_field['value'] = $output; |
| 422 | } |
| 423 | |
| 424 | return $export_field; |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * Allow this field to be editable. |
| 429 | * |
| 430 | * @param bool $is_editable True if editable. False if not. |
| 431 | * @param string $field_type Field type to check for editable. |
| 432 | */ |
| 433 | public function field_editable( $is_editable, $field_type ) { |
| 434 | return ! empty( $field_type ) && $field_type === $this->type ? true : $is_editable; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Field preview inside the builder. |
| 439 | * |
| 440 | * @since 1.0.0 |
| 441 | * |
| 442 | * @param array $field Field data and settings. |
| 443 | */ |
| 444 | public function field_preview( $field ) { |
| 445 | // Define data. |
| 446 | $likert_rows = ! empty( $field['likert_rows'] ) ? $field['likert_rows'] : $this->defaults['likert_rows']; |
| 447 | $likert_columns = ! empty( $field['likert_columns'] ) ? $field['likert_columns'] : $this->defaults['likert_columns']; |
| 448 | $dd_options = ! empty( $field['drop_down_choices'] ) ? $field['drop_down_choices'] : $this->defaults['drop_down_choices']; |
| 449 | $placeholder = ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : ''; |
| 450 | $input_type = ! empty( $field['input_type'] ) ? $field['input_type'] : 'radio'; |
| 451 | $width = round( 80 / count( $likert_columns ), 4 ); |
| 452 | |
| 453 | // Label. |
| 454 | $this->field_preview_option( 'label', $field ); |
| 455 | ?> |
| 456 | |
| 457 | <table cellspacing="0" cellpadding="0" class="everest-forms-likert-table"> |
| 458 | <thead> |
| 459 | <tr> |
| 460 | <?php |
| 461 | echo '<th style="width:20%;"></th>'; |
| 462 | foreach ( $likert_columns as $column_key => $column ) { |
| 463 | printf( |
| 464 | '<th style="width:%d%%;">%s</th>', |
| 465 | esc_attr( $width ), |
| 466 | esc_html( sanitize_text_field( $column ) ) |
| 467 | ); |
| 468 | } |
| 469 | ?> |
| 470 | </tr> |
| 471 | </thead> |
| 472 | <tbody> |
| 473 | <?php |
| 474 | foreach ( $likert_rows as $row_key => $row ) { |
| 475 | echo '<tr>'; |
| 476 | echo '<th>' . esc_html( sanitize_text_field( $row ) ) . '</th>'; |
| 477 | foreach ( $likert_columns as $column_key => $column ) { |
| 478 | echo '<td>'; |
| 479 | if ( 'select' === $input_type ) { |
| 480 | echo '<select disabled>'; |
| 481 | foreach ( $dd_options as $key => $option ) { |
| 482 | $default = isset( $option['default'] ) ? $option['default'] : ''; |
| 483 | $selected = ! empty( $placeholder ) ? '' : selected( '1', $default, false ); |
| 484 | $label = ! empty( $option['label'] ) ? $option['label'] : ''; |
| 485 | |
| 486 | printf( '<option %s>%s</option>', esc_attr( $selected ), esc_html( $label ) ); |
| 487 | } |
| 488 | echo '</select>'; |
| 489 | } else { |
| 490 | echo '<input type="' . esc_attr( $input_type ) . '" disabled>'; |
| 491 | echo '<label></label>'; |
| 492 | } |
| 493 | echo '</td>'; |
| 494 | } |
| 495 | echo '</tr>'; |
| 496 | } |
| 497 | ?> |
| 498 | </tbody> |
| 499 | </table> |
| 500 | <?php |
| 501 | |
| 502 | // Description. |
| 503 | $this->field_preview_option( 'description', $field ); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Field display on the form front-end. |
| 508 | * |
| 509 | * @since 1.0.0 |
| 510 | * |
| 511 | * @param array $field Field Data. |
| 512 | * @param array $field_atts Field attributes. |
| 513 | * @param array $form_data All Form Data. |
| 514 | */ |
| 515 | public function field_display( $field, $field_atts, $form_data ) { |
| 516 | $inputs = $field['properties']['inputs']; |
| 517 | $input_type = ! empty( $field['input_type'] ) ? $field['input_type'] : 'radio'; |
| 518 | $drop_down_choices = ! empty( $field['drop_down_choices'] ) ? $field['drop_down_choices'] : array(); |
| 519 | $size = 'large'; |
| 520 | $width = 80; |
| 521 | $conditional_id = isset( $field['properties']['inputs']['primary']['attr']['conditional_id'] ) ? $field['properties']['inputs']['primary']['attr']['conditional_id'] : ''; |
| 522 | $conditional_rules = isset( $field['properties']['inputs']['primary']['attr']['conditional_rules'] ) ? $field['properties']['inputs']['primary']['attr']['conditional_rules'] : ''; |
| 523 | |
| 524 | if ( ! empty( $field['likert_columns'] ) ) { |
| 525 | $width = round( 80 / count( $field['likert_columns'] ), 4 ); |
| 526 | } |
| 527 | ?> |
| 528 | |
| 529 | <table cellspacing="0" cellpadding="0" class="everest-forms-field-likert"> |
| 530 | <thead> |
| 531 | <tr> |
| 532 | <?php |
| 533 | echo '<td class="evf-td-head" style="width:20%;"></td>'; |
| 534 | foreach ( $field['likert_columns'] as $column_key => $column ) { |
| 535 | printf( |
| 536 | '<th style="width:%d%%;">%s</th>', |
| 537 | esc_attr( $width ), |
| 538 | esc_html( evf_string_translation( $form_data['id'], $field['id'], sanitize_text_field( $column ), '-likert-column-' . $column_key ) ) |
| 539 | ); |
| 540 | } |
| 541 | ?> |
| 542 | </tr> |
| 543 | </thead> |
| 544 | <tbody> |
| 545 | <?php |
| 546 | foreach ( (array) $field['likert_rows'] as $row_key => $row ) { |
| 547 | echo '<tr class="evf-' . absint( $form_data['id'] ) . '-field_' . $field['id'] . '">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 548 | echo '<th>'; |
| 549 | echo esc_html( evf_string_translation( $form_data['id'], $field['id'], sanitize_text_field( $row ), '-likert-row-' . $row_key ) ); |
| 550 | $this->field_display_error( "rows{$row_key}", $field ); |
| 551 | echo '</th>'; |
| 552 | foreach ( $field['likert_columns'] as $column_key => $column ) { |
| 553 | $input = $inputs[ "rows{$row_key}_columns{$column_key}" ]; |
| 554 | echo '<td>'; |
| 555 | if ( 'select' === $input_type ) { |
| 556 | printf( |
| 557 | '<select %s %s conditional_rules="%s" conditional_id="%s">', |
| 558 | evf_html_attributes( $input['id'], $input['class'], $input['data'], $input['attr'] ), |
| 559 | $input['required'], // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 560 | esc_attr( $conditional_rules ), |
| 561 | esc_attr( $conditional_id ) |
| 562 | ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 563 | foreach ( $drop_down_choices as $key => $choice ) { |
| 564 | $selected = isset( $choice['default'] ) ? '1' : '0'; |
| 565 | $label = isset( $choice['label'] ) ? evf_string_translation( $form_data['id'], $field['id'], $choice['label'], '-likert-dropdown-choice-' . $key ) : ''; |
| 566 | |
| 567 | printf( '<option value="%s" %s>%s</option>', esc_attr( $label ), selected( '1', $selected, false ), $label ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 568 | } |
| 569 | echo '</select>'; |
| 570 | } else { |
| 571 | printf( |
| 572 | '<input type="%s" %s %s conditional_rules="%s" conditional_id="%s">', |
| 573 | esc_attr( $input_type ), |
| 574 | evf_html_attributes( $input['id'], $input['class'], $input['data'], $input['attr'] ), |
| 575 | $input['required'], // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 576 | esc_attr( $conditional_rules ), |
| 577 | esc_attr( $conditional_id ) |
| 578 | ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 579 | } |
| 580 | echo '<label for="' . esc_attr( sanitize_html_class( $input['id'] ) ) . '">'; |
| 581 | echo ! empty( $input['sublabel']['hidden'] ) ? '<span class="everest-forms-screen-reader-element">' : '<span>'; |
| 582 | echo esc_html( sanitize_text_field( $input['sublabel']['value'] ) ); |
| 583 | echo '</span>'; |
| 584 | echo '</label>'; |
| 585 | echo '</td>'; |
| 586 | } |
| 587 | echo '</tr>'; |
| 588 | } |
| 589 | ?> |
| 590 | </tbody> |
| 591 | </table> |
| 592 | <?php |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Edit form field display on the entry back-end. |
| 597 | * |
| 598 | * @since 1.7.1 |
| 599 | * |
| 600 | * @param array $entry_field Entry field data. |
| 601 | * @param array $field Field data. |
| 602 | * @param array $form_data Form data and settings. |
| 603 | */ |
| 604 | public function edit_form_field_display( $entry_field, $field, $form_data ) { |
| 605 | if ( ! empty( $entry_field['value_raw'] ) && is_array( $entry_field['value_raw'] ) ) { |
| 606 | foreach ( $entry_field['value_raw'] as $row => $col ) { |
| 607 | foreach ( (array) $col as $col_selected ) { |
| 608 | $index = sprintf( 'rows%d_columns%d', (int) $row, (int) $col_selected ); |
| 609 | $field['properties']['inputs'][ $index ]['attr']['checked'] = true; |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | $this->field_display( $field, null, $form_data ); |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Validates field on form submit. |
| 619 | * |
| 620 | * @param int $field_id Field ID. |
| 621 | * @param array $field_submit Submitted form field data. |
| 622 | * @param array $form_data Form data. |
| 623 | */ |
| 624 | public function validate( $field_id, $field_submit, $form_data ) { |
| 625 | $form_id = $form_data['id']; |
| 626 | $row_keys = array_keys( $form_data['form_fields'][ $field_id ]['likert_rows'] ); |
| 627 | $entry = isset( $form_data['entry'] ) ? $form_data['entry'] : array(); |
| 628 | $visible = apply_filters( 'everest_forms_visible_fields', true, $form_data['form_fields'][ $field_id ], $entry, $form_data ); |
| 629 | $required_message = isset( $form_data['form_fields'][ $field_id ]['required-field-message'], $form_data['form_fields'][ $field_id ]['required_field_message_setting'] ) && ! empty( $form_data['form_fields'][ $field_id ]['required-field-message'] ) && 'individual' == $form_data['form_fields'][ $field_id ]['required_field_message_setting'] ? $form_data['form_fields'][ $field_id ]['required-field-message'] : get_option( 'everest_forms_required_validation' ); |
| 630 | |
| 631 | $x = 1; |
| 632 | |
| 633 | if ( false === $visible ) { |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | update_option( 'evf_validation_error', '' ); |
| 638 | |
| 639 | if ( empty( $form_data['form_fields'][ $field_id ]['required'] ) ) { |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | foreach ( $row_keys as $row_key ) { |
| 644 | if ( empty( $field_submit[ $row_key ] ) ) { |
| 645 | evf()->task->errors[ $form_id ][ $field_id ][ "rows{$row_key}" ] = $required_message; |
| 646 | update_option( 'evf_validation_error', 'yes' ); |
| 647 | } |
| 648 | $x++; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Format field. |
| 654 | * |
| 655 | * @param string $field_id Field ID. |
| 656 | * @param array $field_submit Submitted form field data. |
| 657 | * @param array $form_data Form data. |
| 658 | * @param string $meta_key Meta Key value. |
| 659 | */ |
| 660 | public function format( $field_id, $field_submit, $form_data, $meta_key ) { |
| 661 | $name = ! empty( $form_data['form_fields'][ $field_id ]['label'] ) ? $form_data['form_fields'][ $field_id ]['label'] : ''; |
| 662 | $value = ''; |
| 663 | $rows = $form_data['form_fields'][ $field_id ]['likert_rows']; |
| 664 | $columns = $form_data['form_fields'][ $field_id ]['likert_columns']; |
| 665 | $type = $form_data['form_fields'][ $field_id ]['input_type']; |
| 666 | if ( 'select' === $type ) { |
| 667 | $value_raw = ! empty( $field_submit ) ? (array) $field_submit : ''; |
| 668 | } else { |
| 669 | $value_raw = ! empty( $field_submit ) ? $this->sanitize_field_submit( (array) $field_submit ) : ''; |
| 670 | } |
| 671 | |
| 672 | $show_empty = true; |
| 673 | |
| 674 | // Process submitted data. |
| 675 | if ( ! empty( $value_raw ) ) { |
| 676 | $x = 1; |
| 677 | foreach ( $rows as $row_key => $row_label ) { |
| 678 | $answers = isset( $value_raw[ $row_key ] ) ? (array) $value_raw[ $row_key ] : array(); |
| 679 | $selected = array(); |
| 680 | |
| 681 | foreach ( $columns as $column_id => $column_label ) { |
| 682 | if ( 'select' === $type ) { |
| 683 | foreach ( $answers as $ans_id => $answer ) { |
| 684 | if ( $ans_id === $column_id ) { |
| 685 | $selected[] = sanitize_text_field( $answer ); |
| 686 | } |
| 687 | } |
| 688 | } else { |
| 689 | if ( in_array( $column_id, $answers, true ) ) { |
| 690 | $selected[] = sanitize_text_field( $column_label ); |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | if ( $x > 1 && ( ! empty( $selected ) || $show_empty ) ) { |
| 696 | $value .= "\n"; |
| 697 | } |
| 698 | |
| 699 | if ( ! empty( $selected ) ) { |
| 700 | $value .= sanitize_text_field( $row_label ) . ":\n" . implode( ', ', $selected ); |
| 701 | } else { |
| 702 | if ( $show_empty ) { |
| 703 | $value .= sanitize_text_field( $row_label ) . ":\n" . esc_html__( '(Empty)', 'everest-forms' ); |
| 704 | } |
| 705 | } |
| 706 | $x++; |
| 707 | } |
| 708 | } |
| 709 | EVF()->task->form_fields[ $field_id ] = array( |
| 710 | 'name' => sanitize_text_field( $name ), |
| 711 | 'value' => $value, |
| 712 | 'value_raw' => $value_raw, |
| 713 | 'id' => $field_id, |
| 714 | 'type' => $this->type, |
| 715 | 'meta_key' => $meta_key, |
| 716 | ); |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * Sanitize the submitted data. All values and keys should integers. |
| 721 | * |
| 722 | * @param array $field_submit Submitted data for Likert field. |
| 723 | */ |
| 724 | public function sanitize_field_submit( $field_submit = array() ) { |
| 725 | if ( ! is_array( $field_submit ) || ! count( $field_submit ) ) { |
| 726 | return array(); |
| 727 | } |
| 728 | |
| 729 | foreach ( $field_submit as $key => $value ) { |
| 730 | if ( is_int( $key ) ) { |
| 731 | if ( is_array( $value ) ) { |
| 732 | $field_submit[ $key ] = $this->sanitize_field_submit( $value ); |
| 733 | } else { |
| 734 | $field_submit[ $key ] = absint( $value ); |
| 735 | } |
| 736 | } else { |
| 737 | unset( $field_submit[ $key ] ); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | return $field_submit; |
| 742 | } |
| 743 | } |
| 744 |