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

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