PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.13
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.13
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.13, at app/Modules/Entries/Export.php

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