PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.2
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.2
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 / Report.php

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

353 lines 12.3 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\Helpers\Helper;
6 use FluentForm\App\Modules\Form\FormFieldsParser;
7 use FluentForm\App\Services\Submission\SubmissionService;
8 use FluentForm\Framework\Foundation\Application;
9 use FluentForm\Framework\Helpers\ArrayHelper;
10
11 /**
12 *
13 * @deprecated use FluentForm\App\Services\Report\ReportHelper
14 * all used method reference updated with new ReportHelper, except Fluentformpro
15 *
16 */
17 class Report
18 {
19 private $app;
20 private $formModel;
21
22 public function __construct(Application $app)
23 {
24 $this->app = $app;
25 $this->formModel = wpFluent()->table('fluentform_forms');
26 }
27
28 /**
29 * Get report
30 *
31 * @param bool $formId
32 */
33 public function getReport($formId = false)
34 {
35 if (!$formId) {
36 $formId = intval($this->app->request->get('form_id'));
37 }
38
39 $this->maybeMigrateData($formId);
40
41 $statuses = $this->app->request->get('statuses');
42
43 $form = $this->formModel->find($formId);
44
45 $report = $this->generateReport($form, $statuses);
46
47 wp_send_json_success($report);
48 }
49
50 public function generateReport($form, $statuses = [])
51 {
52 $formInputs = FormFieldsParser::getEntryInputs($form, ['admin_label', 'element', 'options']);
53
54 $inputLabels = FormFieldsParser::getAdminLabels($form, $formInputs);
55
56 $elements = [];
57
58 foreach ($formInputs as $inputName => $input) {
59 $elements[$inputName] = $input['element'];
60 if ('select_country' == $input['element']) {
61 $formInputs[$inputName]['options'] = getFluentFormCountryList();
62 }
63 }
64
65 $reportableInputs = Helper::getReportableInputs();
66
67 $formReportableInputs = array_intersect($reportableInputs, array_values($elements));
68
69 $reportableInputs = Helper::getSubFieldReportableInputs();
70 $formSubFieldInputs = array_intersect($reportableInputs, array_values($elements));
71
72 if (!$formReportableInputs && !$formSubFieldInputs) {
73 return [
74 'report_items' => (object) [],
75 'total_entries' => 0,
76 ];
77 }
78
79 $inputs = [];
80 $subfieldInputs = [];
81 foreach ($elements as $elementKey => $element) {
82 if (in_array($element, $formReportableInputs)) {
83 $inputs[$elementKey] = $element;
84 }
85 if (in_array($element, $formSubFieldInputs)) {
86 $subfieldInputs[$elementKey] = $element;
87 }
88 }
89
90 $whereClasuses = [];
91
92 if ($statuses) {
93 $whereClasuses['fluentform_submissions.status'] = [
94 'method' => 'whereIn',
95 'values' => $statuses,
96 ];
97 }
98
99 $reports = $this->getInputReport($form->id, array_keys($inputs), $whereClasuses);
100
101 $subFieldReports = $this->getSubFieldInputReport($form->id, array_keys($subfieldInputs), $whereClasuses);
102
103 $reports = array_merge($reports, $subFieldReports);
104
105 foreach ($reports as $reportKey => $report) {
106 $reports[$reportKey]['label'] = $inputLabels[$reportKey];
107 $reports[$reportKey]['element'] = ArrayHelper::get($inputs, $reportKey, []);
108 $reports[$reportKey]['options'] = $formInputs[$reportKey]['options'];
109 }
110
111 return [
112 'report_items' => $reports,
113 'total_entries' => $this->getEntryCounts($form->id, $statuses),
114 'browsers' => $this->getbrowserCounts($form->id, $statuses),
115 'devices' => $this->getDeviceCounts($form->id, $statuses),
116 ];
117 }
118
119 public function getInputReport($formId, $fieldNames, $whereClasuses)
120 {
121 if (!$fieldNames) {
122 return [];
123 }
124 global $wpdb;
125 $reportQuery = wpFluent()->table('fluentform_entry_details')
126 ->select([
127 'fluentform_entry_details.field_name',
128 'fluentform_entry_details.sub_field_name',
129 'fluentform_entry_details.field_value',
130 wpFluent()->raw('count(' . $wpdb->prefix . 'fluentform_entry_details.field_name) as total_count')
131 ])
132 ->where('fluentform_entry_details.form_id', $formId)
133 ->whereIn('fluentform_entry_details.field_name', $fieldNames)
134 ->rightJoin('fluentform_submissions', 'fluentform_submissions.id', '=', 'fluentform_entry_details.submission_id');
135
136 if ($whereClasuses) {
137 foreach ($whereClasuses as $clauseColumn => $clasus) {
138 $reportQuery = $reportQuery->{$clasus['method']}($clauseColumn, $clasus['values']);
139 }
140 }
141
142 $reports = $reportQuery->groupBy(['fluentform_entry_details.field_name', 'fluentform_entry_details.field_value'])
143 ->get();
144
145 $formattedReports = [];
146 foreach ($reports as $report) {
147 $formattedReports[$report->field_name]['reports'][] = [
148 'value' => Helper::safeUnserialize($report->field_value),
149 'count' => $report->total_count,
150 'sub_field' => $report->sub_field_name,
151 ];
152 $formattedReports[$report->field_name]['total_entry'] = $this->getEntryTotal($report->field_name, $formId, $whereClasuses);
153 }
154
155 return $formattedReports;
156 }
157
158 public function getSubFieldInputReport($formId, $fieldNames, $whereClasuses)
159 {
160 if (!$fieldNames) {
161 return [];
162 }
163
164 global $wpdb;
165 $reportQuery = wpFluent()->table('fluentform_entry_details')
166 ->select([
167 'fluentform_entry_details.field_name',
168 'fluentform_entry_details.sub_field_name',
169 'fluentform_entry_details.field_value',
170 wpFluent()->raw('count(' . $wpdb->prefix . 'fluentform_entry_details.field_name) as total_count')
171 ])
172 ->where('fluentform_entry_details.form_id', $formId)
173 ->whereIn('fluentform_entry_details.field_name', $fieldNames)
174 ->leftJoin('fluentform_submissions', 'fluentform_submissions.id', '=', 'fluentform_entry_details.submission_id');
175
176 if ($whereClasuses) {
177 foreach ($whereClasuses as $clauseColumn => $clasus) {
178 $reportQuery = $reportQuery->{$clasus['method']}($clauseColumn, $clasus['values']);
179 }
180 }
181
182 $reports = $reportQuery->groupBy(['fluentform_entry_details.field_name', 'fluentform_entry_details.field_value', 'fluentform_entry_details.sub_field_name'])
183 ->get();
184
185 return $this->getFormattedReportsForSubInputs($reports, $formId, $whereClasuses);
186 }
187
188 protected function getFormattedReportsForSubInputs($reports, $formId, $whereClasuses)
189 {
190 if (!count($reports)) {
191 return [];
192 }
193
194 $formattedReports = [];
195
196 foreach ($reports as $report) {
197 $this->setReportForSubInput((array) $report, $formattedReports);
198 }
199
200 foreach ($formattedReports as $fieldName => $val) {
201 $formattedReports[$fieldName]['total_entry'] = $this->getEntryTotal(
202 $report->field_name,
203 $formId,
204 $whereClasuses
205 );
206
207 $formattedReports[$fieldName]['reports'] = array_values(
208 $formattedReports[$fieldName]['reports']
209 );
210 }
211
212 return $formattedReports;
213 }
214
215 protected function setReportForSubInput($report, &$formattedReports)
216 {
217 $filedValue = Helper::safeUnserialize($report['field_value']);
218
219 if (is_array($filedValue)) {
220 foreach ($filedValue as $fVal) {
221 $this->setReportForSubInput(
222 array_merge($report, ['field_value' => $fVal]),
223 $formattedReports
224 );
225 }
226 } else {
227 $value = $report['sub_field_name'] . ' : ' . $filedValue;
228 $count = ArrayHelper::get($formattedReports, $report['field_name'] . '.reports.' . $value . '.count');
229 $count = $count ? $count + $report['total_count'] : $report['total_count'];
230
231 $formattedReports[$report['field_name']]['reports'][$value] = [
232 'value' => $value,
233 'count' => $count,
234 'sub_field' => $report['sub_field_name'],
235 ];
236 }
237 }
238
239 public function getEntryTotal($fieldName, $formId, $whereClasuses)
240 {
241 $query = wpFluent()->table('fluentform_entry_details')
242 ->select('fluentform_entry_details.id')
243 ->where('fluentform_entry_details.form_id', $formId)
244 ->where('fluentform_entry_details.field_name', $fieldName)
245 ->groupBy(['fluentform_entry_details.field_name', 'fluentform_entry_details.submission_id'])
246 ->leftJoin('fluentform_submissions', 'fluentform_submissions.id', '=', 'fluentform_entry_details.submission_id');
247
248 if ($whereClasuses) {
249 foreach ($whereClasuses as $clauseColumn => $clasus) {
250 $query = $query->{$clasus['method']}($clauseColumn, $clasus['values']);
251 }
252 }
253
254 return $query->count();
255 }
256
257 private function maybeMigrateData($formId)
258 {
259 // We have to check if we need to migrate the data
260 if ('yes' == Helper::getFormMeta($formId, 'report_data_migrated')) {
261 return true;
262 }
263 global $wpdb;
264 // let's migrate the data
265 $unmigratedData = wpFluent()
266 ->table('fluentform_submissions')
267 ->select([
268 'fluentform_submissions.id',
269 'fluentform_submissions.response',
270 ])
271 ->where('fluentform_submissions.form_id', $formId)
272 ->whereRaw(wpFluent()->raw($wpdb->prefix . 'fluentform_submissions.id NOT IN (SELECT submission_id from ' . $wpdb->prefix . 'fluentform_entry_details)'))
273 ->get();
274
275 if (!$unmigratedData) {
276 return Helper::setFormMeta($formId, 'report_data_migrated', 'yes');
277 }
278
279 $submissionService = new SubmissionService();
280 foreach ($unmigratedData as $datum) {
281 $value = json_decode($datum->response, true);
282 $submissionService->recordEntryDetails($datum->id, $formId, $value);
283 }
284
285 return true;
286 }
287
288 private function getEntryCounts($formId, $statuses = false)
289 {
290 $totalEntries = wpFluent()
291 ->table('fluentform_submissions')
292 ->where('fluentform_submissions.form_id', $formId);
293
294 if ($statuses) {
295 $totalEntries = $totalEntries->whereIn('fluentform_submissions.status', $statuses);
296 } else {
297 $totalEntries = $totalEntries->where('fluentform_submissions.status', '!=', 'trashed');
298 }
299 return $totalEntries->count();
300 }
301
302 private function getBrowserCounts($formId, $statuses)
303 {
304 global $wpdb;
305 $browserCounts = wpFluent()->table('fluentform_submissions')
306 ->select([
307 wpFluent()->raw('count(' . $wpdb->prefix . 'fluentform_submissions.id) as total_count'),
308 'browser',
309 ])
310 ->where('form_id', $formId);
311 if ($statuses) {
312 $browserCounts = $browserCounts->whereIn('status', $statuses);
313 } else {
314 $browserCounts = $browserCounts->where('status', '!=', 'trashed');
315 }
316
317 $browserCounts = $browserCounts->groupBy('browser')
318 ->get();
319
320 $formattedData = [];
321 foreach ($browserCounts as $browser) {
322 $formattedData[$browser->browser] = $browser->total_count;
323 }
324
325 return $formattedData;
326 }
327
328 private function getDeviceCounts($formId, $statuses)
329 {
330 global $wpdb;
331 $deviceCounts = wpFluent()->table('fluentform_submissions')
332 ->select([
333 wpFluent()->raw('count(' . $wpdb->prefix . 'fluentform_submissions.id) as total_count'),
334 'device',
335 ])
336 ->where('form_id', $formId);
337 if ($statuses) {
338 $deviceCounts = $deviceCounts->whereIn('status', $statuses);
339 } else {
340 $deviceCounts = $deviceCounts->where('status', '!=', 'trashed');
341 }
342
343 $deviceCounts = $deviceCounts->groupBy('device')
344 ->get();
345
346 $formattedData = [];
347 foreach ($deviceCounts as $deviceCount) {
348 $formattedData[$deviceCount->device] = $deviceCount->total_count;
349 }
350 return $formattedData;
351 }
352 }
353