SubmissionCsvExport.php
9.75 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
<?php
use NF_Exports_Interfaces_SubmissionCsvExportInterface As SubmissionCsvExportInterface;
use NF_Exports_Interfaces_SubmissionCollectionInterface As SubmissionCollectionInterface;
use NinjaForms\Includes\Entities\SubmissionField;
use NinjaForms\Includes\Entities\SingleSubmission;
use NinjaForms\Includes\Handlers\SubmissionAggregateCsvExportAdapter;
use NinjaForms\Includes\Handlers\SubmissionAggregate;
/**
*
*/
class NF_Exports_SubmissionCsvExport implements SubmissionCsvExportInterface {
/**
* Submission Collection
* @var SubmissionCollectionInterface
*/
public $submissionCollection;
/** @var SubmissionAggregate */
protected $submissionAggregate;
/** @var SubmissionAggregateCsvExportAdapter */
protected $submissionAggregateCsvExportAdapter;
/**
* Use admin labels boolean
* @var bool
*/
protected $useAdminLabels = false;
/**
* Date format
* @var string
*/
protected $dateFormat = 'm/d/Y';
/**
* Array of submission ids contained in collection
*
* @var array
*/
protected $submissionIds;
/**
* Lookup of NF submission SeqNum by collection Index
*
* @var array
*/
protected $seqNumLookup;
/**
* Field labels keyed on field key
* @var array
*/
protected $fieldLabels = [];
/**
* Field types keyed on field key
* @var array
*/
protected $fieldTypes = [];
/**
* Field Ids keyed on field key
* @var array
*/
protected $fieldIds = [];
/**
* Labels row for CSV
* @var array
*/
protected $csvLabels = [];
/**
* Complete array for CSV, including labels row
* @var array
*/
protected $csvValuesCollection = [];
/**
* Generate CSV output and return
* @return string
*/
public function handle()/* :string*/
{
$this->constructLabels();
$this->csvValuesCollection[0][0] = $this->csvLabels;
$this->appendRows();
$returned = $this->prepareCsv();
return $returned;
}
/** @inheritDoc */
public function reverseSubmissionOrder(): array
{
$submissionCollection = $this->submissionAggregateCsvExportAdapter->submissionAggregate->getAggregatedSubmissions();
$indicesOriginalOrder= array_keys($submissionCollection);
$return = array_reverse($indicesOriginalOrder);
return $return;
}
/** @inheritDoc */
public function constructRow( $aggregatedKey):array
{
$singleSubmission = $this->submissionAggregateCsvExportAdapter->submissionAggregate->getSubmissionValuesByAggregatedKey($aggregatedKey);
$this->constructSeqNumLookup($aggregatedKey, $singleSubmission);
$row = $this->constructSubmissionRow($aggregatedKey, $singleSubmission);
return $row;
}
/**
* Construct string output from previously set params, mark submissions read
* @return string
*/
protected function prepareCsv()
{
$nfSubs = [];
foreach($this->submissionIds as $submissionId){
$nfSubs[]=Ninja_Forms()->form( )->get_sub( $submissionId );
}
// Get any extra data from our other plugins...
$csv_array = apply_filters( 'nf_subs_csv_extra_values', $this->csvValuesCollection, $nfSubs, $this->submissionAggregateCsvExportAdapter->submissionAggregate->getMasterFormId() );
$output = 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" )
);
return $output;
}
/**
* Append each submission from the collection as a row
*/
protected function appendRows()
{
$indices = $this->reverseSubmissionOrder();
// populate submission values for each submission in the collection, then append
foreach ($indices as $index) {
$row = $this->constructRow($index);
$this->csvValuesCollection[1][0][] = $row;
}
}
/**
* For NF CPT, construct lookup from index for SeqNum
*
* @param string $aggregatedKey
* @param SingleSubmission $singleSubmission
* @return void
*/
protected function constructSeqNumLookup(string $aggregatedKey, SingleSubmission $singleSubmission): void
{
$dataSource = $singleSubmission->getDataSource();
// only add seq number for NF CPT
if('nf_post'!== $dataSource){
return;
}
$this->seqNumLookup[$aggregatedKey]= get_post_meta($singleSubmission->getSubmissionRecordId(), '_seq_num', TRUE);
}
/**
* Construct a single row in the CSV from a submission
*
* @todo Refactor to remove DB call for NF()->form()->field() on each iteration
* @param SingleSubmission $submission
* @return array
*/
protected function constructSubmissionRow(string $aggregatedKey, SingleSubmission $submission)/* :array */ {
// Add the standard fields
$seqNum = '';
if(isset($this->seqNumLookup[$aggregatedKey])){
$seqNum = $this->seqNumLookup[$aggregatedKey];
}
$row['_seq_num'] = $seqNum;
$row['_date_submitted'] = $this->formatTimestamp($submission->getTimestamp());
$columnValues = $this->submissionAggregateCsvExportAdapter->getColumnValuesByAggregatedKey($aggregatedKey);
$row= array_merge($row,$columnValues);
$strippedRow = WPN_Helper::stripslashes($row);
// Legacy Filter from 2.9.*
$filteredRow = apply_filters('nf_subs_csv_value_array', $strippedRow, $this->submissionIds);
return $filteredRow;
}
/**
* Format timestamp for output
*
* @param string $timestamp
* @return string
*/
protected function formatTimestamp(string $timestamp): string
{
$dt = DateTime::createFromFormat('Y-m-d H:i:s',$timestamp);
$return = $dt->format($this->dateFormat);
return $return;
}
/**
* Construct labels array
*
* Indexed array of labels, which serves as the column headers
*/
protected function constructLabels()
{
$this->csvLabels = array_merge($this->getFieldLabelsBeforeFields(), array_values($this->fieldLabels));
}
/**
* Return labels for the CSV, including SeqNum and Date
*
* @return array
*/
public function getLabels( ): array
{
if(empty($this->csvLabels)){
$this->constructLabels();
}
return $this->csvLabels;
}
/**
* Return filtered array of labels preceding fields
*
* @return array
*/
protected function getFieldLabelsBeforeFields()/* :array */ {
$preFilterLabels = array(
'_seq_num' => '#',
'_date_submitted' => esc_html__('Date Submitted', 'ninja-forms')
);
// Legacy Filter from 2.9.*
$return = apply_filters('nf_subs_csv_label_array_before_fields', $preFilterLabels, $this->submissionIds);
return $return;
}
/**
* Set submission collection used in generating the CSV
*
* @todo Investigate reason for commented out type declarations
* @param SubmissionCollectionInterface $submissionCollection
* @return SubmissionCsvExportInterface
*/
public function setSubmissionCollection(/* SubmissionCollectionInterface */$submissionCollection)/* :SubmissionCsvExportInterface */
{
return $this;
}
/**
* Set SubmissionAggregateCsvExport Adapter used in generating the CSV
*
* @param SubmissionAggregateCsvExportAdapter $submissionAggregateCsvExportAdapter
* @return SubmissionCsvExportInterface
*/
public function setSubmissionAggregateCsvExportAdapter(SubmissionAggregateCsvExportAdapter $submissionAggregateCsvExportAdapter)/* :SubmissionCsvExportInterface */
{
$this->setDateFormat();
$this->submissionAggregateCsvExportAdapter = $submissionAggregateCsvExportAdapter;
$this->submissionAggregateCsvExportAdapter->setHiddenFieldTypes([
'html', 'submit', 'divider', 'hr', 'note', 'unknown', 'button', 'confirm'
]);
$this->fieldLabels = $this->submissionAggregateCsvExportAdapter->getLabels($this->useAdminLabels);
$this->fieldTypes = $this->submissionAggregateCsvExportAdapter->getFieldTypes();
$this->fieldIds = $this->submissionAggregateCsvExportAdapter->getFieldIds();
$this->submissionIds = $this->submissionAggregateCsvExportAdapter->getSubmissionIds();
return $this;
}
/**
* Set boolean useAdminLabels
*
* @param bool $useAdminLabels
* @return SubmissionCsvExportInterface
*/
public function setUseAdminLabels($useAdminLabels) :SubmissionCsvExportInterface {
$this->useAdminLabels = $useAdminLabels;
return $this;
}
/**
* Set date format
*
* @param string $dateFormat
* @return SubmissionCsvExportInterface
*/
public function setDateFormat(/* string */$dateFormat = null)/* :SubmissionCsvExportInterface */ {
if(!empty($dateFormat)) {
//Set new date format
$date_format = $dateFormat;
} else if( !empty( Ninja_Forms()->get_setting( 'date_format' ) ) ) {
//Or get NF Date format set
$date_format = Ninja_Forms()->get_setting( 'date_format' );
} else if(!empty( get_option('date_format'))) {
//Or get WP date format set
$date_format = get_option('date_format');
} else {
//Or leave default
$date_format = $this->dateFormat;
}
$this->dateFormat = $date_format;
return $this;
}
}