forked from gravitywiz/snippet-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gw-field-to-field-conditional-logic.php
259 lines (199 loc) · 7.39 KB
/
gw-field-to-field-conditional-logic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* Gravity Wiz // Gravity Forms // Field to Field Conditional Logic
*
* Compare fields in Gravity Forms conditional logic. Is Field A greater than Field B? Is the emergency contact's name different than
* the patient's? Is Date A after Date B?
*
* Plugin Name: GF Field to Field Conditional Logic
* Plugin URI: http://gravitywiz.com/
* Description: Compare fields in Gravity Forms conditional logic.
* Author: Gravity Wiz
* Version: 0.9.1
* Author URI: http://gravitywiz.com
*
* @todo
* - Add UI for selecting merge tags.
* - Add support for CL on next/prev buttons.
*/
class GF_Field_To_Field_Conditional_Logic {
public function __construct() {
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
add_filter( 'gform_admin_pre_render', array( $this, 'enqueue_inline_admin_script' ) );
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
add_filter( 'gform_rule_pre_evaluation', array( $this, 'modify_rule' ), 10, 5 );
}
public function enqueue_inline_admin_script( $return ) {
add_filter( 'admin_footer', array( $this, 'output_admin_inline_script' ) );
return $return;
}
public function output_admin_inline_script() {
?>
<script>
gform.addFilter( 'gform_conditional_logic_values_input', function( markup, objectType, ruleIndex, selectedFieldId, selectedValue ) {
var selectedField = GetFieldById( selectedFieldId );
if ( ! selectedField || ! selectedField.choices.length ) {
return markup;
}
// If this is not a <select> return the markup unmodified.
var match = markup.match( /(<select.+?>)((?:.|\n)+?)(<\/select>)/ );
if ( ! match ) {
return markup;
}
var choiceOptions = match ? match[2] : markup;
var fieldOptions = [];
for ( var field of window.form.fields ) {
if ( ! IsConditionalLogicField( field ) ) {
continue;
}
var value = '{:' + field.id + ':value}';
var isSelected = value === selectedValue;
fieldOptions.push( '<option value="{0}" {2}>{1}</option>'.format( value, GetLabel( field ), isSelected ? 'selected' : '' ) );
if ( isSelected ) {
var $choiceSelect = jQuery( '<select>' + choiceOptions + '</select>' );
$choiceSelect.find( 'option:selected' ).remove();
choiceOptions = $choiceSelect.html();
$choiceSelect.remove();
}
}
markup = '{0}<optgroup label="Field Choices">{1}</optgroup><optgroup label="Fields">{2}</optgroup>{3}'.format(
match ? match[1] : '',
choiceOptions,
fieldOptions.join( "\n" ),
match ? match[3] : '',
);
return markup;
}, 9 );
</script>
<?php
}
public function load_form_script( $form, $is_ajax_enabled ) {
if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
add_action( 'wp_footer', array( $this, 'output_script' ) );
add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
}
return $form;
}
public function output_script() {
?>
<script type="text/javascript">
( function( $ ) {
window.GWFieldToFieldConditionalLogic = function( args ) {
var self = this;
// copy all args to current object: (list expected props)
for( var prop in args ) {
if( args.hasOwnProperty( prop ) ) {
self[ prop ] = args[ prop ];
}
}
self.init = function() {
// Let GF know that when are target field's value changes, we need to re-evaluate conditional logic.
for ( var prop in self.dependencies ) {
if( self.dependencies.hasOwnProperty( prop ) ) {
if ( typeof window.gf_form_conditional_logic[ self.formId ].fields[ prop ] === 'undefined' ) {
window.gf_form_conditional_logic[ self.formId ].fields[ prop ] = [];
}
window.gf_form_conditional_logic[ self.formId ].fields[ prop ] = window.gf_form_conditional_logic[ self.formId ].fields[ prop ].concat( self.dependencies[ prop ] );
}
}
// Replace the field merge tag in the rule value before the rule is evaluated.
gform.addFilter( 'gform_rule_pre_evaluation', function( rule, formId ) {
var mergeTags = GFMergeTag.parseMergeTags( rule.value );
if ( ! mergeTags.length ) {
return rule;
}
rule.value = GFMergeTag.getMergeTagValue( formId, mergeTags[0][1], mergeTags[0][3] );
return rule;
} );
};
self.init();
}
} )( jQuery );
</script>
<?php
}
public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return;
}
$args = array(
'formId' => $form['id'],
'dependencies' => $this->get_cl_dependencies( $form ),
);
$script = 'new GWFieldToFieldConditionalLogic( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array( 'gw_field_to_field_conditional_logic', $form['id'] ) );
GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
public function get_cl_dependencies( $object, $dependencies = array() ) {
foreach ( $object as $prop => $value ) {
if ( $prop === 'conditionalLogic' && ! empty( $value ) ) {
foreach ( $object[ $prop ]['rules'] as $rule ) {
// GF core only supports comparing fields to values but Gravity Perks supports other comparisons.
if ( ! is_numeric( $rule['fieldId'] ) ) {
continue;
}
$matches = $this->parse_merge_tags( $rule['value'] );
if ( ! empty( $matches ) ) {
$tag_field_id = $matches[0][1];
if ( ! isset( $dependencies[ $tag_field_id ] ) ) {
$dependencies[ $tag_field_id ] = array();
}
// Submit button conditional logic is 0.
array_push( $dependencies[ $tag_field_id ], rgar( $object, 'id', 0 ) );
}
}
} elseif ( is_array( $value ) || is_a( $value, 'GF_Field' ) ) {
$dependencies = $this->get_cl_dependencies( $value, $dependencies );
}
}
return $dependencies;
}
public function parse_merge_tags( $string, $pattern = '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi' ) {
preg_match_all( $pattern, $string, $matches, PREG_SET_ORDER );
return $matches;
}
public function is_applicable_form( $form ) {
// @todo we will need to recursively search for field-to-field conditional logic (see GPCLD).
return true;
}
/**
* Parse merge tags in rule values on submission (and other times conditional logic is evaluated).
*
* @param $rule
* @param $form
* @param $logic
* @param $field_values
* @param $entry
*
* @return mixed
*/
public function modify_rule( $rule, $form, $logic, $field_values, $entry ) {
static $_is_modifying_rule;
static $_rule_cache;
if ( $_is_modifying_rule ) {
return $rule;
}
if ( $entry === null ) {
$_is_modifying_rule = true;
$entry = GFFormsModel::get_current_lead();
$_is_modifying_rule = false;
}
if ( ! isset( $_rule_cache[ $entry['id'] ] ) ) {
$_rule_cache[ $entry['id'] ] = array();
}
if ( isset( $_rule_cache[ $entry['id'] ][ $rule['value'] ] ) ) {
$value = $_rule_cache[ $entry['id'] ][ $rule['value'] ];
} else {
$value = GFCommon::replace_variables( $rule['value'], $form, $entry );
$_rule_cache[ $entry['id'] ][ $rule['value'] ] = $value;
}
$rule['value'] = $value;
return $rule;
}
}
# Configuration
new GF_Field_To_Field_Conditional_Logic();