PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.21
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.21
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 3.6.66 All 195 releases
fluentform / app / Modules / Entries / Export.php

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

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