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

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