PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 5.2.9
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v5.2.9
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Modules / Entries / Export.php

Export.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 5.2.9, at app/Modules/Entries/Export.php

257 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Modules\Entries;
4
5 use FluentForm\App\Modules\Form\FormDataParser;
6 use FluentForm\App\Modules\Form\FormFieldsParser;
7 use FluentForm\Framework\Foundation\Application;
8 use FluentForm\Framework\Helpers\ArrayHelper as Arr;
9 use FluentForm\App\Helpers\Helper;
10
11 /**
12 * @deprecated deprecated use FluentForm\App\Services\Transfer
13 */
14 class Export
15 {
16 /**
17 * App instance
18 *
19 * @var \FluentForm\Framework\Foundation\Application
20 */
21 protected $app;
22
23 /**
24 * Request object
25 *
26 * @var \FluentForm\Framework\Request\Request
27 */
28 protected $request;
29
30 /**
31 * Table name
32 *
33 * @var String table/data source name
34 */
35 protected $tableName;
36
37 /**
38 * Export constructor.
39 *
40 * @param \FluentForm\Framework\Foundation\Application $application
41 */
42 public function __construct(Application $application, $tableName = 'fluentform_submissions')
43 {
44 $this->app = $application;
45 $this->request = $application->request;
46 $this->tableName = $tableName;
47 }
48
49 /**
50 * Only used exports form partial entries
51 *
52 * @deprecated deprecated use FluentForm\App\Services\Transfer::exportEntries
53 * @todo:: refactor.
54 */
55 public function index()
56 {
57 if (!defined('FLUENTFORM_EXPORTING_ENTRIES')) {
58 define('FLUENTFORM_EXPORTING_ENTRIES', true);
59 }
60
61 $formId = intval($this->request->get('form_id'));
62
63 $form = wpFluent()->table('fluentform_forms')->find($formId);
64
65 if (!$form) {
66 exit('No Form Found');
67 }
68
69 $type = sanitize_key($this->request->get('format', 'csv'));
70 if (!in_array($type, ['csv', 'ods', 'xlsx', 'json'])) {
71 exit('Invalid requested format');
72 }
73
74 if ('json' == $type) {
75 $this->exportAsJSON($form);
76 }
77
78 if (!defined('FLUENTFORM_DOING_CSV_EXPORT')) {
79 define('FLUENTFORM_DOING_CSV_EXPORT', true);
80 }
81
82 $formInputs = FormFieldsParser::getEntryInputs($form, ['admin_label', 'raw']);
83
84 $inputLabels = FormFieldsParser::getAdminLabels($form, $formInputs);
85
86 $submissions = $this->getSubmissions($formId);
87
88 $submissions = FormDataParser::parseFormEntries($submissions, $form, $formInputs);
89 $exportData = [];
90
91 foreach ($submissions as $submission) {
92 $submission->response = json_decode($submission->response, true);
93 $temp = [];
94 foreach ($inputLabels as $field => $label) {
95 // format tabular grid data for CSV/XLSV/ODS export
96 if (isset($formInputs[$field]['element']) && "tabular_grid" === $formInputs[$field]['element']) {
97 $gridRawData = Arr::get($submission->response, $field);
98 $content = Helper::getTabularGridFormatValue($gridRawData, Arr::get($formInputs, $field), ' | ');
99 } else {
100 $content = trim(
101 wp_strip_all_tags(
102 FormDataParser::formatValue(
103 Arr::get($submission->user_inputs, $field)
104 )
105 )
106 );
107 }
108 $temp[] = Helper::sanitizeForCSV($content);
109 }
110
111 if ($form->has_payment && 'fluentform_submissions' == $this->tableName) {
112 $temp[] = round($submission->payment_total / 100, 1);
113 $temp[] = $submission->payment_status;
114 $temp[] = $submission->currency;
115 }
116
117 $temp[] = @$submission->id;
118 $temp[] = @$submission->status;
119 $temp[] = @$submission->created_at;
120
121 $exportData[] = $temp;
122 }
123
124 $extraLabels = [];
125 if ($form->has_payment && 'fluentform_submissions' == $this->tableName) {
126 $extraLabels[] = 'payment_total';
127 $extraLabels[] = 'payment_status';
128 $extraLabels[] = 'currency';
129 }
130
131 $extraLabels[] = 'entry_id';
132 $extraLabels[] = 'entry_status';
133 $extraLabels[] = 'created_at';
134
135 $inputLabels = array_merge($inputLabels, $extraLabels);
136
137 $data = array_merge([array_values($inputLabels)], $exportData);
138
139 $data = apply_filters_deprecated(
140 'fluentform_export_data',
141 [
142 $data,
143 $form,
144 $exportData,
145 $inputLabels
146 ],
147 FLUENTFORM_FRAMEWORK_UPGRADE,
148 'fluentform/export_data',
149 'Use fluentform/export_data instead of fluentform_export_data.'
150 );
151
152 $data = apply_filters('fluentform/export_data', $data, $form, $exportData, $inputLabels);
153
154 $fileName = sanitize_title($form->title, 'export', 'view') . '-' . date('Y-m-d');
155
156 $this->downloadOfficeDoc($data, $type, $fileName);
157 }
158
159 private function downloadOfficeDoc($data, $type = 'csv', $fileName = null)
160 {
161 $data = array_map(function ($item) {
162 return array_map(function ($itemValue) {
163 if (is_array($itemValue)) {
164 return implode(', ', $itemValue);
165 }
166 return $itemValue;
167 }, $item);
168 }, $data);
169 require_once $this->app->make('path.app') . '/Services/Spout/Autoloader/autoload.php';
170 $fileName = ($fileName) ? $fileName . '.' . $type : 'export-data-' . date('d-m-Y') . '.' . $type;
171 $writer = \Box\Spout\Writer\WriterFactory::create($type);
172 $writer->openToBrowser($fileName);
173 $writer->addRows($data);
174 $writer->close();
175 die();
176 }
177
178 private function exportAsJSON($form)
179 {
180 $formInputs = FormFieldsParser::getEntryInputs($form, ['admin_label', 'raw']);
181
182 $inputLabels = FormFieldsParser::getAdminLabels($form, $formInputs);
183
184 $submissions = $this->getSubmissions($form->id);
185
186 $submissions = FormDataParser::parseFormEntries($submissions, $form, $formInputs);
187 $exportData = [];
188
189 foreach ($submissions as $submission) {
190 $submission->response = json_decode($submission->response, true);
191 }
192
193 header('Content-disposition: attachment; filename=' . sanitize_title($form->title, 'export', 'view') . '-' . date('Y-m-d') . '.json');
194 header('Content-type: application/json');
195 echo json_encode($submissions); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $submissions is escaped before being passed in.
196 exit();
197 }
198
199 private function getSubmissions($formId)
200 {
201 $query = wpFluent()->table($this->tableName)
202 ->where('form_id', $formId)
203 ->orderBy('id', $this->request->get('sort_by', 'DESC'));
204
205 if ('fluentform_submissions' == $this->tableName) {
206 $dateRange = $this->request->get('date_range');
207 if ($dateRange) {
208 $query->where('created_at', '>=', $dateRange[0] . ' 00:00:01');
209 $query->where('created_at', '<=', $dateRange[1] . ' 23:59:59');
210 }
211
212 $isFavourite = $this->request->get('is_favourite');
213
214 if ('yes' == $isFavourite) {
215 $query->where('is_favourite', '1');
216 }
217
218 $status = $this->request->get('entry_type');
219
220 if ('trashed' == $status) {
221 $query->where('status', 'trashed');
222 } elseif ($status && 'all' != $status) {
223 $query->where('status', $status);
224 } else {
225 $query->where('status', '!=', 'trashed');
226 }
227 $entries = fluentFormSanitizer($this->request->get('entries', []));
228
229 if (is_array($entries) && (count($entries) > 0)) {
230 $query->whereIn('id', $entries);
231 }
232
233 if ($paymentStatuses = $this->request->get('payment_statuses')) {
234 if (is_array($paymentStatuses)) {
235 $query->whereIn('payment_status', $paymentStatuses);
236 }
237 }
238 }
239
240 $searchString = $this->request->get('search');
241
242 if ($searchString) {
243 $query->where(function ($q) use ($searchString) {
244 $q->where('id', 'LIKE', "%{$searchString}%")
245 ->orWhere('response', 'LIKE', "%{$searchString}%");
246
247 if ('fluentform_submissions' == $this->tableName) {
248 $q->orWhere('status', 'LIKE', "%{$searchString}%")
249 ->orWhere('created_at', 'LIKE', "%{$searchString}%");
250 }
251 });
252 }
253
254 return $query->get();
255 }
256 }
257