Skip to content

Commit f7c6258

Browse files
authored
Merge pull request #141 from BoldGrid/issue-61-pro
Fixes format of entry value if needed
2 parents a0cede0 + 1c11ebe commit f7c6258

File tree

8 files changed

+168
-68
lines changed

8 files changed

+168
-68
lines changed

includes/class-ajax.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ public function get_entry_detail() {
528528
$has_empty = false;
529529
$answers = [];
530530
$respondentPoints = isset( $form_settings['total_points'] ) ? floatval( $form_settings['total_points'] ) : 0;
531-
531+
$fields_formatted = array();
532532
foreach ( $fields as $key => $field ) {
533533
if ( $form_settings['quiz_form'] == 'yes' ) {
534534
$selectedAnswers = isset( $field['selected_answers'] ) ? $field['selected_answers'] : '';
@@ -539,7 +539,6 @@ public function get_entry_detail() {
539539

540540
if ( $template == 'radio_field' || $template == 'dropdown_field' ) {
541541
$answers[$field['name']] = true;
542-
543542
if ( empty( $givenAnswer ) ) {
544543
$answers[$field['name']] = false;
545544
$respondentPoints -= $fieldPoints;
@@ -576,11 +575,13 @@ public function get_entry_detail() {
576575
} elseif ( empty( $field['value'] ) ) {
577576
$has_empty = true;
578577
break;
578+
} else {
579+
$field = WeForms_Form_Entry_Manager::format_entry_value( $field );
580+
array_push( $fields_formatted, $field );
579581
}
580582
}
581-
582583
$response = [
583-
'form_fields' => $fields,
584+
'form_fields' => $fields_formatted,
584585
'form_settings' => $form_settings,
585586
'meta_data' => $metadata,
586587
'payment_data' => $payment,
@@ -589,6 +590,7 @@ public function get_entry_detail() {
589590
'answers' => $answers,
590591
];
591592

593+
592594
wp_send_json_success( $response );
593595
}
594596

includes/class-form-entry-manager.php

Lines changed: 93 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,97 @@
77
*/
88
class WeForms_Form_Entry_Manager {
99

10-
/**
11-
* The form id
12-
*
13-
* @var int
14-
*/
15-
private $id = 0;
16-
17-
/**
18-
* The form object
19-
*
20-
* @var \WeForms_Form
21-
*/
22-
private $form;
23-
24-
/**
25-
* The constructor
26-
*
27-
* @param int $form_id
28-
* @param \WeForms_Form $form
29-
*/
30-
public function __construct( $form_id, $form ) {
31-
$this->id = $form_id;
32-
$this->form = $form;
33-
}
34-
35-
/**
36-
* Get all the form entries
37-
*
38-
* @return array
39-
*/
40-
public function all() {
41-
return weforms_get_form_entries( $this->id );
42-
}
43-
44-
/**
45-
* Get a single entry
46-
*
47-
* @param int $entry_id
48-
*
49-
* @return mixed
50-
*/
51-
public function get( $entry_id ) {
52-
return new WeForms_Form_Entry( $entry_id, $this->form );
53-
}
10+
/**
11+
* The form id
12+
*
13+
* @var int
14+
*/
15+
private $id = 0;
16+
17+
/**
18+
* The form object
19+
*
20+
* @var \WeForms_Form
21+
*/
22+
private $form;
23+
24+
/**
25+
* The constructor
26+
*
27+
* @param int $form_id
28+
* @param \WeForms_Form $form
29+
*/
30+
public function __construct( $form_id, $form ) {
31+
$this->id = $form_id;
32+
$this->form = $form;
33+
}
34+
35+
/**
36+
* Get all the form entries
37+
*
38+
* @return array
39+
*/
40+
public function all() {
41+
return weforms_get_form_entries( $this->id );
42+
}
43+
44+
/**
45+
* Get a single entry
46+
*
47+
* @param int $entry_id
48+
*
49+
* @return mixed
50+
*/
51+
public function get( $entry_id ) {
52+
return new WeForms_Form_Entry( $entry_id, $this->form );
53+
}
54+
55+
/**
56+
* Format Entry Value.
57+
*
58+
* @param array $field Form Field data.
59+
*
60+
* @return array $field Formatted field data values.
61+
*/
62+
public static function format_entry_value( $field ) {
63+
switch ( $field['template'] ) {
64+
65+
case 'radio_field':
66+
$value = array_search( $field['value'], $field['options'], true );
67+
$field['value'] = esc_html( 'Option: ' . $field['value'] . ' - ' . 'Value: ' . $value );
68+
69+
break;
70+
71+
case 'checkbox_field':
72+
$field_formatted = array();
73+
foreach ( $field['value'] as $option ) {
74+
$value = array_search( $option, $field['options'], true );
75+
$field_formatted[] = esc_html( 'Option: ' . $option . ' - ' . 'Value: ' . $value );
76+
}
77+
$field['value'] = array_replace( $field['value'], $field_formatted );
78+
79+
break;
80+
81+
case 'multiple_select':
82+
$field_formatted = array();
83+
foreach ( $field['value'] as $option ) {
84+
$value = array_search( $option, $field['options'], true );
85+
$field_formatted[] = esc_html( 'Option: ' . $option . ' - ' . 'Value: ' . $value );
86+
}
87+
$field['value'] = array_replace( $field['value'], $field_formatted );
88+
89+
break;
90+
91+
case 'dropdown_field':
92+
$value = array_search( $field['value'], $field['options'], true );
93+
$field['value'] = esc_html( 'Option: ' . $field['value'] . ' - ' . 'Value: ' . $value );
94+
95+
break;
96+
97+
default:
98+
// Do nothing if value format does not need to be changed.
99+
break;
100+
}
101+
return $field;
102+
}
54103
}

includes/class-form-entry.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,34 @@ public function get_payment_data() {
421421

422422
return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}weforms_payments WHERE entry_id = {$this->id} " );
423423
}
424+
425+
/**
426+
* Get Form from entry id.
427+
*
428+
* @param int $entry_id The entry id.
429+
* @global object $wpdb The Wordpress database object.
430+
*
431+
* @return object The form object.
432+
*/
433+
public static function get_form( $entry_id ) {
434+
$form_id = self::get_form_id( $entry_id );
435+
436+
return ! empty( $form_id ) ? weforms()->form->get( $form_id ) : null;
437+
}
438+
439+
/**
440+
* Get form id from entry id.
441+
*
442+
* @param int $entry_id The entry id.
443+
* @global object $wpdb The Wordpress database object.
444+
*
445+
* @return int The form id.
446+
*/
447+
public static function get_form_id( $entry_id ) {
448+
global $wpdb;
449+
450+
$results = $wpdb->get_results( "SELECT form_id FROM {$wpdb->prefix}weforms_entries WHERE id = {$entry_id} " );
451+
452+
return ! empty( $results[0]->form_id ) ? $results[0]->form_id : null;
453+
}
424454
}

includes/class-notification.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -463,32 +463,51 @@ public function get_merge_value( $tag ) {
463463
}
464464

465465
/**
466-
* Parse out the custom fields with entry meta values
466+
* Parse out the custom fields with options or values. Since the options are what is stored, options may need to be
467+
* used to find the value from the field settings.
467468
*
468-
* @param string $text
469+
* For example, let's say we have the following options:
470+
* DEPARTMENT / EMAIL
471+
* Support / [email protected]
472+
469473
*
470-
* @return string
474+
* Users need the ability to pass {field:department} and get "Support",
475+
* and {value:department} to get [email protected]
476+
*
477+
* @param string $text The text to parse.
478+
* @param int $entry_id The entry ID.
479+
*
480+
* @return string $text The parsed text.
471481
*/
472482
public static function replace_field_tags( $text, $entry_id ) {
473-
$pattern = '/{field:(\w*)}/';
474-
475-
preg_match_all( $pattern, $text, $matches );
476-
477-
// bail out if nothing found to be replaced
478-
if ( !$matches ) {
479-
return $text;
483+
// Validate data.
484+
if ( empty( $text ) || empty( $entry_id ) ) {
485+
return;
480486
}
481487

482-
foreach ( $matches[1] as $index => $meta_key ) {
483-
$meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true );
488+
// Users looking for {field:something} or {value:something}, determine which one.
489+
$is_field = preg_match( '/{field:(\w*)}/', $text, $matches_field );
490+
$is_value = preg_match( '/{value:(\w*)}/', $text, $matches_value );
484491

492+
if ( $is_field ) {
493+
$meta_key = $matches_field[1];
494+
$meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true );
485495
if ( is_array( $meta_value ) ) {
486496
$meta_value = implode( WeForms::$field_separator, $meta_value );
487497
}
488-
489-
$text = str_replace( $matches[0][$index], $meta_value, $text );
498+
// $text may include HTML tags, only replace tag that was matched.
499+
$text = str_replace( $matches_field[0], $meta_value, $text );
500+
} elseif ( $is_value ) {
501+
$meta_key = $matches_value[1];
502+
$form_field_values = WeForms_Form_Entry::get_form( $entry_id )->get_field_values()[ $meta_key ]['options'];
503+
$meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true );
504+
$modified_value = array_search( $meta_value, $form_field_values );
505+
if ( is_array( $modified_value ) ) {
506+
$modified_value = implode( WeForms::$field_separator, $modified_value );
507+
}
508+
// $text may include HTML tags, only replace tag that was matched.
509+
$text = str_replace( $matches_value[0], $modified_value, $text );
490510
}
491-
492511
return $text;
493512
}
494513

@@ -662,7 +681,7 @@ public function replace_all_fields( $text = '' ) {
662681
$table .= '—';
663682
}
664683
} elseif ( in_array( $value['type'], array( 'google_map' ) ) ) {
665-
$table .= $field_value['address'];
684+
$table .= $field_value['address'];
666685
} else {
667686
$table .= $field_value;
668687
}

includes/fields/class-field-checkbox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function prepare_entry( $field, $args = [] ) {
118118
$new_val = [];
119119

120120
foreach ( $entry_value as $option_key ) {
121-
$new_val[] = isset( $field['options'][$option_key] ) ? $field['options'][$option_key] . ' , ' . $option_key : $option_key;
121+
$new_val[] = isset( $field['options'][$option_key] ) ? $field['options'][$option_key] : $option_key;
122122
}
123123

124124
$entry_value = implode( WeForms::$field_separator, $new_val );

includes/fields/class-field-multidropdown.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function prepare_entry( $field, $args = [] ) {
103103
$new_val = [];
104104

105105
foreach ( $entry_value as $option_key ) {
106-
$new_val[] = isset( $field['options'][$option_key] ) ? $field['options'][$option_key] . ' , ' . $option_key : $option_key;
106+
$new_val[] = isset( $field['options'][$option_key] ) ? $field['options'][$option_key] : $option_key;
107107
}
108108

109109
$entry_value = implode( WeForms::$field_separator, $new_val );

includes/fields/class-field-radio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,6 @@ public function prepare_entry( $field, $args = [] ) {
120120
$args = ! empty( $args ) ? $args : weforms_clean( $_POST );
121121
$val = $args[$field['name']];
122122

123-
return isset( $field['options'][$val] ) ? $field['options'][$val] . ' , ' . $val : $val;
123+
return isset( $field['options'][$val] ) ? $field['options'][$val] : $val;
124124
}
125125
}

includes/functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ function weforms_get_entry_payment( $entry_id ) {
177177
$query = 'SELECT transaction_id FROM ' . $wpdb->prefix . 'weforms_payments' .
178178
' WHERE entry_id = ' . $entry_id;
179179
$payment = $wpdb->get_row( $query, $entry_id );
180-
180+
181181
return $payment;
182182
}
183183

0 commit comments

Comments
 (0)