PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / trunk
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder vtrunk
6.2.15 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 All 197 releases
fluentform / app / Services / Report / ReportService.php

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

230 lines 6.5 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 $breakdown = ReportHelper::getPaymentBreakdown($startDate, $endDate, $formId);
185 $data['payment_types'] = [
186 'subscription' => $breakdown['subscription'] ?? [],
187 'onetime' => $breakdown['onetime'] ?? [],
188 ];
189 return $data;
190 }
191
192 /**
193 * Sanitize and prepare common parameters
194 *
195 * @param array $data
196 * @return array
197 */
198 public function sanitizeCommonParams($data)
199 {
200 $data = Sanitizer::sanitize($data, [
201 'start_date' => 'sanitizeTextField',
202 'end_date' => 'sanitizeTextField',
203 'metric' => 'sanitizeTextField',
204 ]);
205
206 $startDate = Arr::get($data, 'start_date');
207 $endDate = Arr::get($data, 'end_date');
208 $formId = Acl::normalizeFormId(Arr::get($data, 'form_id'));
209
210 // Default to the last 30 days, measured on the SITE's clock. created_at is
211 // written by current_time('mysql') and compared here as a raw string, while
212 // PHP's default timezone is UTC under WordPress — so a bare new \DateTime()
213 // would close the window at the end of the UTC day and drop everything
214 // submitted since local midnight on any site east of UTC.
215 if (!$startDate || !$endDate) {
216 $today = current_time('Y-m-d');
217
218 $endDate = $today . ' 23:59:59';
219 $startDate = (new \DateTime($today))->modify('-30 days')->format('Y-m-d 00:00:00');
220 }
221
222 return [
223 'start_date' => $startDate,
224 'end_date' => $endDate,
225 'form_id' => $formId,
226 'metric' => Arr::get($data, 'metric', 'entries')
227 ];
228 }
229 }
230