Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluentform/app/Modules/Entries/Export.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
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 + // Load Composer autoloader for OpenSpout
170 + require_once FLUENTFORM_DIR_PATH . '/vendor/autoload.php';
171 + $fileName = ($fileName) ? $fileName . '.' . $type : 'export-data-' . date('d-m-Y') . '.' . $type;
172 +
173 + // Create writer based on type using WriterEntityFactory
174 + switch (strtolower($type)) {
175 + case 'csv':
176 + $writer = \OpenSpout\Writer\Common\Creator\WriterEntityFactory::createCSVWriter();
177 + break;
178 + case 'xlsx':
179 + $writer = \OpenSpout\Writer\Common\Creator\WriterEntityFactory::createXLSXWriter();
180 + break;
181 + case 'ods':
182 + $writer = \OpenSpout\Writer\Common\Creator\WriterEntityFactory::createODSWriter();
183 + break;
184 + default:
185 + throw new \Exception(sprintf('Unsupported file type: %s', esc_html($type)));
186 + }
187 + $writer->openToBrowser($fileName);
188 +
189 + // Convert data arrays to Row objects for OpenSpout v3
190 + $rows = array_map(function ($rowData) {
191 + return \OpenSpout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray($rowData);
192 + }, $data);
193 +
194 + $writer->addRows($rows);
195 + $writer->close();
196 + die();
197 + }
198 +
199 + private function exportAsJSON($form)
200 + {
201 + $formInputs = FormFieldsParser::getEntryInputs($form, ['admin_label', 'raw']);
202 +
203 + $inputLabels = FormFieldsParser::getAdminLabels($form, $formInputs);
204 +
205 + $submissions = $this->getSubmissions($form->id);
206 +
207 + $submissions = FormDataParser::parseFormEntries($submissions, $form, $formInputs);
208 + $exportData = [];
209 +
210 + foreach ($submissions as $submission) {
211 + $submission->response = json_decode($submission->response, true);
212 + }
213 +
214 + header('Content-disposition: attachment; filename=' . sanitize_title($form->title, 'export', 'view') . '-' . date('Y-m-d') . '.json');
215 + header('Content-type: application/json');
216 + echo json_encode($submissions); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $submissions is escaped before being passed in.
217 + exit();
218 + }
219 +
220 + private function getSubmissions($formId)
221 + {
222 + $query = wpFluent()->table($this->tableName)
223 + ->where('form_id', $formId)
224 + ->orderBy('id', $this->request->get('sort_by', 'DESC'));
225 +
226 + if ('fluentform_submissions' == $this->tableName) {
227 + $dateRange = $this->request->get('date_range');
228 + if ($dateRange) {
229 + $query->where('created_at', '>=', $dateRange[0] . ' 00:00:01');
230 + $query->where('created_at', '<=', $dateRange[1] . ' 23:59:59');
231 + }
232 +
233 + $isFavourite = $this->request->get('is_favourite');
234 +
235 + if ('yes' == $isFavourite) {
236 + $query->where('is_favourite', '1');
237 + }
238 +
239 + $status = $this->request->get('entry_type');
240 +
241 + if ('trashed' == $status) {
242 + $query->where('status', 'trashed');
243 + } elseif ($status && 'all' != $status) {
244 + $query->where('status', $status);
245 + } else {
246 + $query->where('status', '!=', 'trashed');
247 + }
248 + $entries = fluentFormSanitizer($this->request->get('entries', []));
249 +
250 + if (is_array($entries) && (count($entries) > 0)) {
251 + $query->whereIn('id', $entries);
252 + }
253 +
254 + if ($paymentStatuses = $this->request->get('payment_statuses')) {
255 + if (is_array($paymentStatuses)) {
256 + $query->whereIn('payment_status', $paymentStatuses);
257 + }
258 + }
259 + }
260 +
261 + $searchString = $this->request->get('search');
262 +
263 + if ($searchString) {
264 + $query->where(function ($q) use ($searchString) {
265 + $q->where('id', 'LIKE', "%{$searchString}%")
266 + ->orWhere('response', 'LIKE', "%{$searchString}%");
267 +
268 + if ('fluentform_submissions' == $this->tableName) {
269 + $q->orWhere('status', 'LIKE', "%{$searchString}%")
270 + ->orWhere('created_at', 'LIKE', "%{$searchString}%");
271 + }
272 + });
273 + }
274 +
275 + return $query->get();
276 + }
277 + }
278 +