Submission.php
20.4 KB
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
<?php if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Class NF_Database_Models_Submission
*/
final class NF_Database_Models_Submission
{
protected $_id = '';
protected $_status = '';
protected $_user_id = '';
protected $_form_id = '';
protected $_seq_num = '';
protected $_sub_date = '';
protected $_mod_date = '';
protected $_field_values = array();
protected $_extra_values = array();
/**
* Delimiter that uniquely identifies a field as type 'repeater'
*
* Requests for a field can be made by either an (int) field id or a
* (string) field reference, which prior to fieldset repeaters had been
* for the field key only. For disambiguation, a fieldset repeater field
* request for a specific field within the fieldset is in the form of:
* {fieldsetFieldId}{delimiter}{fieldIdOfFieldWithinFieldset}
*
* @var string
*/
protected $_fieldsetDelimiter='.';
/**
* Delimiter that uniquely identifies multiple fieldset repeater submissions
*
* Fieldset Repeaters can have multiple values submitted on any given
* submission. Each repeated value for a field in the fieldset is
* delimited in the submission data with an incremented index value
* @var string
*/
protected $_fieldsetRepetitionDelimiter='_';
public function __construct( $id = '', $form_id = '' )
{
$this->_id = $id;
$this->_form_id = $form_id;
if( $this->_id ){
$sub = get_post( $this->_id );
if ($sub) {
$this->_status = $sub->post_status;
$this->_user_id = $sub->post_author;
$this->_sub_date = $sub->post_date;
$this->_mod_date = $sub->post_modified;
}
}
if( $this->_id && ! $this->_form_id ){
$this->_form_id = get_post_meta( $this->_id, '_form_id', TRUE );
}
if( $this->_id && $this->_form_id ){
$this->_seq_num = get_post_meta( $this->_id, '_seq_num', TRUE );
}
}
/**
* Get Submission ID
*
* @return int
*/
public function get_id()
{
return intval( $this->_id );
}
public function get_status()
{
return $this->_status;
}
public function get_user()
{
return get_user_by( 'id', $this->_user_id );
}
public function get_form_id()
{
return intval( $this->_form_id );
}
public function get_form_title()
{
$form = Ninja_Forms()->form( $this->_form_id )->get();
return $form->get_setting( 'title' );
}
public function get_seq_num()
{
return intval( $this->_seq_num );
}
public function get_sub_date( $format = 'm/d/Y' )
{
return date( $format, strtotime( $this->_sub_date ) );
}
public function get_mod_date( $format = 'm/d/Y' )
{
return date( $format, strtotime( $this->_mod_date ) );
}
/**
* Get Field Value
*
* Returns a single submission value by field ID or field key.
*
* @param int|string $field_ref
* @return string
*/
public function get_field_value( $field_ref )
{
// Bypass existing method if fieldset repeater
if(Ninja_Forms()->fieldsetRepeater->isRepeaterFieldByFieldReference($field_ref) ){
$parsedField = Ninja_Forms()->fieldsetRepeater
->parseFieldsetFieldReference($field_ref);
$return = $this->get_field_value_for_fieldset_child($parsedField['fieldId'], $parsedField['fieldsetFieldId']);
return $return;
}
$field_id = ( is_numeric( $field_ref ) ) ? $field_ref : $this->get_field_id_by_key( $field_ref );
$field = '_field_' . $field_id;
if( isset( $this->_field_values[ $field ] ) ) return $this->_field_values[ $field ];
$this->_field_values[ $field ] = get_post_meta($this->_id, $field, TRUE);
$this->_field_values[ $field_ref ] = get_post_meta($this->_id, $field, TRUE);
return WPN_Helper::htmlspecialchars( $this->_field_values[ $field ] );
}
/**
* Get field values of a single child field within a fieldset repeater field
*
* get_field_value(), which calls this method, is expected to return a
* string. Fieldset Repeater child fields have a unique field reference,
* differentiated by their delimiter that ensures that the requesting
* external caller knows that it is requesting a fieldset repeater field.
* This this method returns a serialized string of values, honoring the
* get_field_value() method with the expectation that the external
* caller will unserialize this value.
*
* @param int $fieldsetId
* @param int $childFieldId
*/
protected function get_field_value_for_fieldset_child($fieldsetId, $childFieldId) {
if (!isset($this->_field_values[$fieldsetId])) {
$this->_field_values[$fieldsetId] = get_post_meta($this->_id, '_field_' . $fieldsetId, true);
}
$valueCollection = [];
if(!empty($this->_field_values[$fieldsetId] )){
foreach ($this->_field_values[$fieldsetId] as $submissionKey => $value) {
$explodedFieldset = explode($this->_fieldsetDelimiter, $submissionKey);
if (!isset($explodedFieldset[1])) {
// data is corrupted as we cannot determine field id construct
break;
}
$explodedChildField = explode($this->_fieldsetRepetitionDelimiter, $explodedFieldset[1]);
if (!isset($explodedChildField[1])) {
// data is corrupted as we cannote determine child field id construct
break;
}
$submissionChildFieldId = $explodedChildField[0];
$submissionIndex = $explodedChildField[1];
if ($submissionChildFieldId === $childFieldId) {
$valueCollection[$submissionIndex] = WPN_Helper::htmlspecialchars($value);
}
}
}
$return = serialize($valueCollection);
return $return;
}
/**
* Get Field Values
*
* @return array|mixed
*/
public function get_field_values()
{
if( ! empty( $this->_field_values ) ) return $this->_field_values;
$field_values = get_post_meta( $this->_id, '' );
foreach( $field_values as $field_id => $field_value ){
$this->_field_values[ $field_id ] = implode( ', ', $field_value );
if( 0 === strpos( $field_id, '_field_' ) ){
$field_id = substr( $field_id, 7 );
}
if( ! is_numeric( $field_id ) ) continue;
$field = Ninja_Forms()->form()->get_field( $field_id );
$key = $field->get_setting( 'key' );
if( $key ) {
$this->_field_values[ $key ] = implode(', ', $field_value);
}
}
return $this->_field_values;
}
/**
* Update Field Value
*
* @param $field_ref
* @param $value
* @return $this
*/
public function update_field_value( $field_ref, $value )
{
$field_id = ( is_numeric( $field_ref ) ) ? $field_ref : $this->get_field_id_by_key( $field_ref );
$this->_field_values[ $field_id ] = WPN_Helper::kses_post( $value );
return $this;
}
/**
* Update Field Values
*
* @param $data
* @return $this
*/
public function update_field_values( $data )
{
foreach( $data as $field_ref => $value )
{
$this->update_field_value( $field_ref, $value );
}
return $this;
}
public function get_extra_value( $key )
{
if( ! isset( $this->_extra_values[ $key ] ) || ! $this->_extra_values[ $key ] ){
$id = ( $this->_id ) ? $this->_id : 0;
$this->_extra_values[ $key ] = get_post_meta( $id, $key, TRUE );
}
return $this->_extra_values[ $key ];
}
public function get_extra_values( $keys )
{
$values = array();
foreach( $keys as $key ) {
$values[ $key ] = $this->get_extra_value( $key );
}
return $values;
}
public function update_extra_value( $key, $value )
{
if( property_exists( $this, $key ) ) return FALSE;
return $this->_extra_values[ $key ] = $value;
}
public function update_extra_values( $values )
{
foreach( $values as $key => $value ){
$this->update_extra_value( $key, $value );
}
}
/**
* Find Submissions
*
* @param $form_id
* @param array $where
* @return array
*/
public function find( $form_id, array $where = array(), array $ids = array() )
{
$this->_form_id = $form_id;
$args = array(
'post_type' => 'nf_sub',
'posts_per_page' => -1,
'meta_query' => $this->format_meta_query( $where )
);
if ( ! empty ( $ids ) ) {
$args[ 'post__in' ] = $ids;
}
$subs = get_posts( $args );
$class = get_class( $this );
$return = array();
foreach( $subs as $sub ){
$return[] = new $class( $sub->ID, $this->_form_id );
}
return $return;
}
/**
* Delete Submission
*/
public function delete()
{
if( ! $this->_id ) return;
wp_delete_post( $this->_id );
}
/**
* Trash Submission
*/
public function trash()
{
if( ! $this->_id ) return;
wp_trash_post( $this->_id );
}
/**
* Save Submission
*
* @return $this|NF_Database_Models_Submission|void
*/
public function save()
{
if( ! $this->_id ){
$sub = array(
'post_type' => 'nf_sub',
'post_status' => 'publish'
);
$this->_id = wp_insert_post( $sub );
// Log Error
if( ! $this->_id ) return;
}
if( ! $this->_seq_num && $this->_form_id ){
$this->_seq_num = NF_Database_Models_Form::get_next_sub_seq( $this->_form_id );
}
$this->_save_extra_values();
return $this->_save_field_values();
}
public static function export( $form_id, array $sub_ids = array(), $return = FALSE )
{
$date_format = Ninja_Forms()->get_setting( 'date_format' );
/*
* Labels
*/
$field_labels = array(
'_seq_num' => '#',
'_date_submitted' => esc_html__( 'Date Submitted', 'ninja-forms' )
);
// Legacy Filter from 2.9.*
$field_labels = apply_filters( 'nf_subs_csv_label_array_before_fields', $field_labels, $sub_ids );
$fields = Ninja_Forms()->form( $form_id )->get_fields();
/*
* If we are using an add-on that filters our field order, we don't want to call sort again.
*
* TODO: This is probably not the most effecient way to handle this. It should be re-thought.
*/
if ( ! has_filter( 'ninja_forms_get_fields_sorted' ) ) {
uasort( $fields, array( 'NF_Database_Models_Submission', 'sort_fields' ) );
}
$hidden_field_types = apply_filters( 'nf_sub_hidden_field_types', array() );
/*
* Submissions
*/
$subs = Ninja_Forms()->form( $form_id )->get_subs( array(), FALSE, $sub_ids );
foreach( $subs as $sub ){
$value[ '_seq_num' ] = $sub->get_seq_num();
$value[ '_date_submitted' ] = $sub->get_sub_date( $date_format );
// boolean - does this submission use a repeater
$hasRepeater = false;
// How many repeater submissions does this submission have
$submissionCount = 0;
// Ids of fields in the repeater
$fieldsetFieldIds=[];
foreach ($fields as $field_id => $field) {
// Bypass existing method if fieldset repeater
if('repeater'===$field->get_setting('type')){
$hasRepeater = true;
$fieldsetSubmission= $sub->get_field_value( $field_id );
$fieldsetSettings = $field->get_settings();
$fieldsetLabels = Ninja_Forms()->fieldsetRepeater
->getFieldsetLabels($field_id, $fieldsetSettings, true);
foreach($fieldsetLabels as $fieldsetFieldId =>$fieldsetFieldLabel){
$fieldsetFieldIds[]=$fieldsetFieldId;
$field_labels[$fieldsetFieldId]=$fieldsetFieldLabel;
$fieldType = Ninja_Forms()->fieldsetRepeater->getFieldtype($fieldsetFieldId, $fieldsetSettings);
$fieldsetFieldSubmissionCollection=Ninja_Forms()->fieldsetRepeater
->extractSubmissionsByFieldsetField($fieldsetFieldId, $fieldsetSubmission);
$submissionCount = count($fieldsetFieldSubmissionCollection);
foreach ($fieldsetFieldSubmissionCollection as &$fieldsetFieldSubmission) {
if(is_array($fieldsetFieldSubmission['value'])){
$fieldsetFieldSubmission['value']= implode(', ',$fieldsetFieldSubmission['value']);
}
}
$value[$fieldsetFieldId]= array_column($fieldsetFieldSubmissionCollection,'value');
}
}else{
if (!is_int($field_id)) continue;
if( in_array( $field->get_setting( 'type' ), $hidden_field_types ) ) continue;
if ( $field->get_setting( 'admin_label' ) ) {
$field_labels[ $field->get_id() ] = $field->get_setting( 'admin_label' );
} else {
$field_labels[ $field->get_id() ] = $field->get_setting( 'label' );
}
$field_value = maybe_unserialize( $sub->get_field_value( $field_id ) );
$field_value = apply_filters('nf_subs_export_pre_value', $field_value, $field_id);
$field_value = apply_filters('ninja_forms_subs_export_pre_value', $field_value, $field_id, $form_id);
$field_value = apply_filters( 'ninja_forms_subs_export_field_value_' . $field->get_setting( 'type' ), $field_value, $field );
if ( is_array($field_value ) ) {
$field_value = implode( ',', $field_value );
}
$value[ $field_id ] = $field_value;
}
}
if(!$hasRepeater){
$value_array[] = $value;
}else{
// The the submission has repeater fields, create an indexed array first
$repeatingValueArray=[];
$index = 0;
do {
// iterate each column in the row 'value'
foreach($value as $fieldId=>$columnValue){
// If the column in the row value is not a repeater
// fieldset field, simply copy it into a new row of the
// repeating value array
if(!in_array($fieldId,$fieldsetFieldIds)){
$repeatingValueArray[$index][]=$columnValue;
}else{
// If the column in the row value is a repeater
// fieldset field, copy the next submission index value
$repeatingValueArray[$index][]=$columnValue[$index];
}
}
// at the end of the row value columns, increment the index
// until all the submission index values are added
$index++;
} while ($index < $submissionCount);
// After iterating the row value once for each submission index,
// add the repeatingValueArray to the value array
$value_array[]=$repeatingValueArray;
}
}
$value_array = WPN_Helper::stripslashes( $value_array );
// Legacy Filter from 2.9.*
$value_array = apply_filters( 'nf_subs_csv_value_array', $value_array, $sub_ids );
$csv_array[ 0 ][] = $field_labels;
$csv_array[ 1 ][] = $value_array;
// Get any extra data from our other plugins...
$csv_array = apply_filters( 'nf_subs_csv_extra_values', $csv_array, $subs, $form_id );
$today = date( $date_format, current_time( 'timestamp' ) );
$filename = apply_filters( 'nf_subs_csv_filename', 'nf_subs_' . $today );
$filename = $filename . ".csv";
if( $return ){
return WPN_Helper::str_putcsv( $csv_array,
apply_filters( 'nf_sub_csv_delimiter', ',' ),
apply_filters( 'nf_sub_csv_enclosure', '"' ),
apply_filters( 'nf_sub_csv_terminator', "\n" )
);
}else{
header( 'Content-type: application/csv');
header( 'Content-Disposition: attachment; filename="'.$filename .'"' );
header( 'Pragma: no-cache');
header( 'Expires: 0' );
echo apply_filters( 'nf_sub_csv_bom',"\xEF\xBB\xBF" ) ; // Byte Order Mark
echo WPN_Helper::str_putcsv( $csv_array,
apply_filters( 'nf_sub_csv_delimiter', ',' ),
apply_filters( 'nf_sub_csv_enclosure', '"' ),
apply_filters( 'nf_sub_csv_terminator', "\n" )
);
die();
}
}
/*
* PROTECTED METHODS
*/
/**
* Save Field Value
*
* @param $field_id
* @param $value
* @return $this
*/
protected function _save_field_value( $field_id, $value )
{
update_post_meta( $this->_id, '_field_' . $field_id, $value );
return $this;
}
/**
* Save Field Values
*
* @return $this|void
*/
protected function _save_field_values()
{
if( ! $this->_field_values ) return FALSE;
foreach( $this->_field_values as $field_id => $value )
{
$this->_save_field_value( $field_id, $value );
}
update_post_meta( $this->_id, '_form_id', $this->_form_id );
update_post_meta( $this->_id, '_seq_num', $this->_seq_num );
return $this;
}
protected function _save_extra_values()
{
if( ! $this->_extra_values ) return FALSE;
foreach( $this->_extra_values as $key => $value )
{
if( property_exists( $this, $key ) ) continue;
update_post_meta( $this->_id, $key, $value );
}
}
/*
* UTILITIES
*/
/**
* Format Meta Query
*
* @param array $where
* @return array
*/
protected function format_meta_query( array $where = array() )
{
$return = array(
array(
'key' => '_form_id',
'value' => $this->_form_id
)
);
if( ! empty( $where ) ) {
foreach ($where as $ref => $value) {
$field_id = ( is_int( $ref ) ) ? $ref : $this->get_field_id_by_key( $ref );
$return[] = ( is_array($value) ) ? $value : array('key' => "_field_$field_id", 'value' => $value);
}
}
return $return;
}
/**
* Get Field ID By Key
*
* @param $field_key
* @return mixed
*/
protected function get_field_id_by_key( $field_key )
{
global $wpdb;
$field_id = $wpdb->get_var( $wpdb->prepare(
"SELECT id FROM {$wpdb->prefix}nf3_fields WHERE `key` = %s AND `parent_id` = {$this->_form_id}",
$field_key
));
return $field_id;
}
public static function sort_fields( $a, $b )
{
if ( $a->get_setting( 'order' ) == $b->get_setting( 'order' ) ) {
return 0;
}
return ( $a->get_setting( 'order' ) < $b->get_setting( 'order' ) ) ? -1 : 1;
}
} // End NF_Database_Models_Submission