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

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