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

ReportService.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.6, at app/Services/Report/ReportService.php

227 lines 6.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\Services\Report;
4
5 use Exception;
6 use FluentForm\App\Models\Form;
7 use FluentForm\App\Models\Submission;
8 use FluentForm\App\Modules\Acl\Acl;
9 use FluentForm\App\Services\Manager\FormManagerService;
10 use FluentForm\Framework\Helpers\ArrayHelper as Arr;
11 use FluentForm\Framework\Support\Sanitizer;
12
13
14 class ReportService
15 {
16 /**
17 * Get Form Report
18 * @param array $attr
19 * @return array|mixed $response
20 * @throws Exception
21 */
22 public function form($attr = [])
23 {
24 $formId = (int) Arr::get($attr, 'form_id');
25 ReportHelper::maybeMigrateData($formId);
26 try {
27 $form = Form::findOrFail($formId);
28 } catch (Exception $e) {
29 throw new \Exception("The form couldn't be found.");
30 }
31
32 $statuses = Arr::get($attr, 'statuses', ['read', 'unread', 'unapproved', 'approved', 'declined', 'unconfirmed', 'confirmed']);
33 return ReportHelper::generateReport($form, $statuses);
34 }
35
36 /**
37 * Get Submissions Report
38 *
39 * @throws Exception
40 */
41 public function submissions($args)
42 {
43 try {
44 return Submission::report($args);
45 } catch (Exception $e) {
46 throw new Exception(esc_html($e->getMessage()));
47 }
48 }
49
50 /**
51 * Get forms for dropdown
52 *
53 * @return array
54 */
55 public function getFormsDropdown()
56 {
57 $forms = Form::select(['id', 'title', 'has_payment'])
58 ->when(false !== ($allowFormIds = FormManagerService::getUserAllowedFormsScope()), function ($query) use ($allowFormIds) {
59 return $query->whereIn('id', $allowFormIds ?: [0]);
60 })
61 ->orderBy('id', 'DESC')
62 ->get();
63
64 return [
65 'forms' => $forms
66 ];
67 }
68
69 /**
70 * Get Overview Chart Data
71 *
72 * @param array $data
73 * @return array
74 */
75 public function getOverviewChart($data)
76 {
77 $data = $this->sanitizeCommonParams($data);
78 $startDate = $data['start_date'];
79 $endDate = $data['end_date'];
80 $formId = $data['form_id'];
81
82 return [
83 'overview_chart' => ReportHelper::getOverviewChartData($startDate, $endDate, $formId, 'activity')
84 ];
85 }
86
87 /**
88 * Get Revenue Chart Data
89 *
90 * @param array $data
91 * @return array
92 */
93 public function getRevenueChart($data)
94 {
95 $data = $this->sanitizeCommonParams($data);
96 $startDate = $data['start_date'];
97 $endDate = $data['end_date'];
98 $formId = $data['form_id'];
99
100 return [
101 'revenue_chart' => ReportHelper::getOverviewChartData($startDate, $endDate, $formId, 'revenue')
102 ];
103 }
104
105
106 /**
107 * Get Form Stats Data
108 *
109 * @param array $data
110 * @return array
111 */
112 public function getFormStats($data)
113 {
114 $data = $this->sanitizeCommonParams($data);
115 $startDate = $data['start_date'];
116 $endDate = $data['end_date'];
117 $formId = $data['form_id'];
118
119 return [
120 'form_stats' => ReportHelper::getFormStats($startDate, $endDate, $formId)
121 ];
122 }
123
124
125 /**
126 * Get API Logs Data
127 *
128 * @param array $data
129 * @return array
130 */
131 public function getApiLogs($data)
132 {
133 $data = $this->sanitizeCommonParams($data);
134 $startDate = $data['start_date'];
135 $endDate = $data['end_date'];
136 $formId = $data['form_id'];
137
138 return [
139 'api_logs' => ReportHelper::getApiLogs($startDate, $endDate, $formId)
140 ];
141 }
142
143 /**
144 * Get Top Performing Forms Data
145 *
146 * @param array $data
147 * @return array
148 */
149 public function getTopPerformingForms($data)
150 {
151 $data = $this->sanitizeCommonParams($data);
152 $startDate = $data['start_date'];
153 $endDate = $data['end_date'];
154 $metric = Arr::get($data, 'metric', 'entries');
155 $allowedFormIds = FormManagerService::getUserAllowedFormsScope();
156 $requestedFormId = Arr::get($data, 'form_id');
157 $scopedFormIds = $allowedFormIds;
158
159 if (false === $allowedFormIds) {
160 $scopedFormIds = $requestedFormId ? [$requestedFormId] : false;
161 } elseif ($requestedFormId) {
162 $scopedFormIds = in_array($requestedFormId, $allowedFormIds, true) ? [$requestedFormId] : [];
163 }
164
165 if (!in_array($metric, ['entries', 'payments', 'views'])) {
166 $metric = 'entries';
167 }
168
169 $result = ReportHelper::getTopPerformingForms($startDate, $endDate, $metric, $scopedFormIds);
170 return [
171 'top_performing_forms' => Arr::get($result, 'data', []),
172 'disable_message' => Arr::get($result, 'disable_message', '')
173 ];
174 }
175
176
177 public function getPaymentTypes($data)
178 {
179 $data = $this->sanitizeCommonParams($data);
180
181 $startDate = $data['start_date'];
182 $endDate = $data['end_date'];
183 $formId = $data['form_id'];
184 $data['payment_types'] = [
185 'subscription' => ReportHelper::getPaymentsByType($startDate, $endDate, 'subscription', $formId),
186 'onetime' => ReportHelper::getPaymentsByType($startDate, $endDate, 'onetime', $formId),
187 ];
188 return $data;
189 }
190
191 /**
192 * Sanitize and prepare common parameters
193 *
194 * @param array $data
195 * @return array
196 */
197 public function sanitizeCommonParams($data)
198 {
199 $data = Sanitizer::sanitize($data, [
200 'start_date' => 'sanitizeTextField',
201 'end_date' => 'sanitizeTextField',
202 'metric' => 'sanitizeTextField',
203 ]);
204
205 $startDate = Arr::get($data, 'start_date');
206 $endDate = Arr::get($data, 'end_date');
207 $formId = Acl::normalizeFormId(Arr::get($data, 'form_id'));
208
209 // Set default date range if not provided
210 if (!$startDate || !$endDate) {
211 $now = new \DateTime();
212 $endDate = $now->format('Y-m-d 23:59:59');
213
214 $thirtyDaysAgo = new \DateTime();
215 $thirtyDaysAgo->modify('-30 days');
216 $startDate = $thirtyDaysAgo->format('Y-m-d 00:00:00');
217 }
218
219 return [
220 'start_date' => $startDate,
221 'end_date' => $endDate,
222 'form_id' => $formId,
223 'metric' => Arr::get($data, 'metric', 'entries')
224 ];
225 }
226 }
227