Newer
Older
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<?php
/**
* ACFE Base Form Action Class.
*
* Holds methods common to ACFE Form Action classes.
*
* @package Conditional_Form_Actions_For_ACFE
* @since 0.1
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* CiviCRM Profile Sync "Base" ACFE Form Action Class.
*
* A class that is extended by CiviCRM Profile Sync ACFE Form Action classes.
* *
* @since 0.1
*/
class CFAFA_Form_Action_Base {
/**
* Form Action Name.
*
* @since 0.1
* @access public
* @var string $action_name The unique name of the Form Action.
*/
public $action_name = '';
/**
* Form Action Label.
*
* @since 0.1
* @access public
* @var string $action_label The label of the Form Action.
*/
public $action_label = '';
/**
* Form Action Alias Placeholder.
*
* @since 0.1
* @access public
* @var string $alias_placeholder The alias placeholder for the Form Action.
*/
public $alias_placeholder = '';
/**
* Field Key Prefix.
*
* @since 0.1
* @access public
* @var string $field_key The prefix for the Field Key.
*/
public $field_key = '';
/**
* Field Name Prefix.
*
* @since 0.1
* @access public
* @var string $field_name The prefix for the Field Name.
*/
public $field_name = '';
/**
* Constructor.
*
* @since 0.1
*/
public function __construct() {
// Callback for the "acfe/form/load/..." hook.
add_filter( 'acfe/form/load/' . $this->action_name, [ $this, 'load' ], 10, 3 );
// Callback for the "acfe/form/make/..." hook.
add_action( 'acfe/form/make/' . $this->action_name, [ $this, 'make' ], 10, 3 );
// Generic callback for ACFE Form Actions hook.
add_filter( 'acfe/form/actions', [ $this, 'action_add' ] );
}
/**
* Allow classes to configure themselves prior to the Layout being returned.
*
* @since 0.1
*/
public function configure() {}
/**
* Performs tasks when the Form that the Action is attached to is loaded.
*
* @since 0.1
*
* @param array $form The array of Form data.
* @param integer $current_post_id The ID of the Post in which the Form has been embedded.
* @param string $action The customised name of the action.
*/
public function load( $form, $current_post_id, $action ) {
return $form;
}
/**
* Saves the result of the Action for use by subsequent Actions.
*
* @since 0.1
*
* @param string $action The name of the Action.
* @param array $data The result of the Action.
*/
public function load_action_save( $action = '', $data ) {
// Get the existing array of Action results.
$actions = get_query_var( 'acfe_form_actions', [] );
$actions[$this->action_name] = $data;
if ( ! empty( $action ) ) {
$actions[$action] = $data;
}
// Update array of Action results.
set_query_var( 'acfe_form_actions', $actions );
}
/**
* Performs the Action when the Form the Action is attached to is submitted.
*
* @since 0.1
*
* @param array $form The array of Form data.
* @param integer $current_post_id The ID of the Post from which the Form has been submitted.
* @param string $action The customised name of the action.
*/
public function make( $form, $current_post_id, $action ) {}
/**
* Maybe skip the Action when the Form the Action is attached to is submitted.
*
* @since 0.1
*
* @param array $form The array of Form data.
* @param integer $current_post_id The ID of the Post from which the Form has been submitted.
* @param string $action The customised name of the Form Action.
* @return bool $prepare The net result of the set of filters.
*/
public function make_skip( $form, $current_post_id, $action ) {
// Get some Form details.
$form_name = acf_maybe_get( $form, 'name' );
$form_id = acf_maybe_get( $form, 'ID' );
// Assume we're good to go.
$prepare = true;
/**
* Allow others to prevent Form Action.
*
* Returning false for any of these filters will skip the Action.
*
* @since 0.1
*
* @param bool $prepare True by default so that the Form Action goes ahead.
* @param array $form The array of Form data.
* @param integer $current_post_id The ID of the Post from which the Form has been submitted.
* @param string $action The customised name of the Form Action.
*/
$filter = 'acfe/form/prepare/' . $this->action_name;
$prepare = apply_filters( $filter, $prepare, $form, $current_post_id, $action );
$prepare = apply_filters( $filter . '/form=' . $form_name, $prepare, $form, $current_post_id, $action );
if ( ! empty( $action ) ) {
$prepare = apply_filters( $filter . '/action=' . $action, $prepare, $form, $current_post_id, $action );
}
// --<
return $prepare;
}
/**
* Saves the result of the Action for use by subsequent Actions.
*
* @since 0.1
*
* @param string $action The name of the Action.
* @param array $data The result of the Action.
*/
public function make_action_save( $action = '', $data ) {
// Get the existing array of Action results.
$actions = get_query_var( 'acfe_form_actions', [] );
$actions[$this->action_name] = $data;
if ( ! empty( $action ) ) {
$actions[$action] = $data;
}
// Update array of Action results.
set_query_var( 'acfe_form_actions', $actions );
}
/**
* Defines the action by adding a layout.
*
* The "name" value of the layout determines the construction of the
* "acfe/form/load/..." and "acfe/form/make/..." actions.
*
* @since 0.1
*
* @param array $layouts The existing layouts.
* @return array $layouts The modified layouts.
*/
public function action_add( $layouts ) {
// Let the classes that extend this one configure themselves.
$this->configure();
// Init our layout.
$layout = [
'key' => 'layout_' . $this->action_name,
'name' => $this->action_name,
'label' => $this->action_label,
'display' => 'row',
'min' => '',
'max' => '',
];
// Build Action Tab.
$action_tab_fields = $this->tab_action_add();
// Build Mapping Tab.
$mapping_tab_fields = $this->tab_mapping_add();
// Combine Sub-Fields.
$sub_fields = array_merge(
$action_tab_fields,
$mapping_tab_fields
);
/**
* Let the classes that extend this one modify the Sub-Fields.
*
* @since 0.1
*
* @param array $sub_fields The array of Sub-Fields.
*/
$layout['sub_fields'] = apply_filters( 'cfafa/acfe/form/actions/sub_fields', $sub_fields );
// Add our completed layout to the layouts array.
$layouts['layout_' . $this->action_name] = $layout;
// --<
return $layouts;
}
/**
* Defines the "Action" Tab.
*
* These Fields are required to configure the Form Action.
*
* The ACFE "Action name" Field has a pre-defined format, e.g. it must be
* assigned the "acfe_slug" Field Type and have "acfe_form_custom_alias" as
* its "name". Only its "placeholder" attribute needs to be configured.
*
* @since 0.1
*
* @return array $fields The array of Fields for this section.
*/
public function tab_action_add() {
// Init Fields array.
$fields = [];
// "Action" Tab wrapper.
$fields[] = [
'key' => $this->field_key . 'tab_action',
'label' => __( 'Action', 'conditional-form-actions-for-acfe' ),
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-no-preference' => true,
],
'acfe_permissions' => '',
'placement' => 'top',
'endpoint' => 0,
];
// "Action name" Field.
$fields[] = [
'key' => $this->field_key . 'custom_alias',
'label' => __( 'Action name', 'conditional-form-actions-for-acfe' ),
'name' => 'acfe_form_custom_alias',
'type' => 'acfe_slug',
'instructions' => __( '(Required) Name this action so it can be referenced.', 'conditional-form-actions-for-acfe' ),
'required' => 1,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-instruction-placement' => 'field',
],
'acfe_permissions' => '',
'default_value' => '',
'placeholder' => $this->alias_placeholder,
'prepend' => '',
'append' => '',
'maxlength' => '',
];
// Add any further Fields.
$action_extras = $this->tab_action_append();
if ( ! empty( $action_extras ) ) {
$fields = array_merge(
$fields,
$action_extras
);
}
// --<
return $fields;
}
/**
* Defines additional Fields for the "Action" Tab.
*
* @since 0.1
*
* @return array $fields The array of Fields for this section.
*/
public function tab_action_append() {
$fields = [];
return $fields;
}
/**
* Defines the "Mapping" Tab.
*
* @since 0.1
*
* @return array $fields The array of Fields for this section.
*/
public function tab_mapping_add() {
$fields = [];
return $fields;
}
/**
* Defines the "Mapping" Tab Header.
*
* @since 0.1
*
* @return array $fields The array of Fields for this section.
*/
public function tab_mapping_header() {
// "Mapping" Tab wrapper.
$mapping_tab = [ [
'key' => $this->field_key . 'tab_load',
'label' => __( 'Mapping', 'conditional-form-actions-for-acfe' ),
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-no-preference' => true,
],
'acfe_permissions' => '',
'placement' => 'top',
'endpoint' => 0,
] ];
// Combine Fields.
$fields = array_merge(
$mapping_tab
);
// --<
return $fields;
}
/**
* Gets the array that defines a "Map Field" for the "Mapping" Tab.
*
* @since 0.1
*
* @param string $code The unique code for the Field.
* @param string $label The label for the Field.
* @param array $conditional_logic The conditional logic for the Field.
* @return array $field The array of Field data.
*/
public function mapping_field_get( $code, $label, $conditional_logic = [] ) {
// Build the Field array.
$field = [
'key' => $this->field_key . 'map_' . $code,
'label' => $label,
'name' => $this->field_name . 'map_' . $code,
'type' => 'select',
'instructions' => '',
'required' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
],
'acfe_permissions' => '',
'choices' => [],
'default_value' => [],
'allow_null' => 1,
'multiple' => 0,
'ui' => 1,
'return_format' => 'value',
'placeholder' => __( 'Default', 'conditional-form-actions-for-acfe' ),
'ajax' => 0,
'search_placeholder' => __( 'Enter a custom value or template tag. (See "Cheatsheet" tab)', 'conditional-form-actions-for-acfe' ),
'allow_custom' => 1,
];
// Default conditional logic.
$field['conditional_logic'] = 0;
// Maybe replace with custom conditional logic.
if ( ! empty( $conditional_logic ) ) {
$field['conditional_logic'] = $conditional_logic;
}
// --<
return $field;
}
/**
* Adds filters that configure "Mapping Fields" when loaded.
*
* @since 0.1
*
* @param string $code The unique code for the Field.
*/
public function mapping_field_filters_add( $code ) {
// Grab reference to ACFE Helper object.
$helpers = acf_get_instance( 'acfe_dynamic_forms_helpers' );
// Populate mapping Fields.
add_filter( 'acf/prepare_field/name=' . $this->field_name . 'map_' . $code, [ $helpers, 'map_fields_deep_no_custom' ] );
}
/**
* Prepare the data from an ACFE Form.
*
* @since 0.1
*
* @param array $form_data The array of data from the ACFE Form.
* @return array $filtered_data The filtered data.
*/
public function form_data_prepare( $form_data ) {
// Init filtered data.
$filtered_data = [];
// Bail if we have no Form data to save.
if ( empty( $form_data ) ) {
return $filtered_data;
}
// Populate Activity data from the Form data.
foreach ( $form_data as $param => $value ) {
// Allow (string) "0" as valid data.
if ( ! empty( $value ) || $value === '0' ) {
$filtered_data[$param] = $value;
}
}
// --<
return $filtered_data;
}
} // Class ends.