PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.17
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.17
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 / Api / Submission.php

Submission.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.17, at app/Api/Submission.php

279 lines 7.6 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\Api;
4
5 use FluentForm\Framework\Helpers\ArrayHelper;
6
7 class Submission
8 {
9 public function get($args = [])
10 {
11 $args = wp_parse_args($args, [
12 'per_page' => 10,
13 'page' => 1,
14 'search' => '',
15 'form_ids' => [],
16 'sort_type' => 'DESC',
17 'entry_type' => 'all',
18 'user_id' => false,
19 ]);
20
21 $offset = $args['per_page'] * ($args['page'] - 1);
22
23 $entryQuery = wpFluent()->table('fluentform_submissions')
24 ->orderBy('id', $args['sort_type'])
25 ->limit($args['per_page'])
26 ->offset($offset);
27
28 $type = $args['entry_type'];
29
30 if ($type && 'all' != $type) {
31 $entryQuery->where('status', $type);
32 }
33
34 if ($args['form_ids'] && is_array($args['form_ids'])) {
35 $entryQuery->whereIn('form_id', $args['form_ids']);
36 }
37
38 if ($searchString = $args['search']) {
39 $entryQuery->where(function ($q) use ($searchString) {
40 $q->where('id', 'LIKE', "%{$searchString}%")
41 ->orWhere('response', 'LIKE', "%{$searchString}%")
42 ->orWhere('status', 'LIKE', "%{$searchString}%")
43 ->orWhere('created_at', 'LIKE', "%{$searchString}%");
44 });
45 }
46
47 if ($args['user_id']) {
48 $entryQuery->where('user_id', (int) $args['user_id']);
49 }
50
51 $count = $entryQuery->count();
52
53 $data = $entryQuery->get();
54
55 $dataCount = count($data);
56
57 $from = $dataCount > 0 ? ($args['page'] - 1) * $args['per_page'] + 1 : null;
58
59 $to = $dataCount > 0 ? $from + $dataCount - 1 : null;
60 $lastPage = (int) ceil($count / $args['per_page']);
61
62 foreach ($data as $datum) {
63 $datum->response = json_decode($datum->response, true);
64 }
65
66 return [
67 'current_page' => $args['page'],
68 'per_page' => $args['per_page'],
69 'from' => $from,
70 'to' => $to,
71 'last_page' => $lastPage,
72 'total' => $count,
73 'data' => $data,
74 ];
75 }
76
77 public function find($submissionId)
78 {
79 $submission = wpFluent()->table('fluentform_submissions')->find($submissionId);
80 $submission->response = json_decode($submission->response);
81 return $submission;
82 }
83
84 public function transactions($columnValue, $column = 'submission_id')
85 {
86 if (!defined('FLUENTFORMPRO')) {
87 return [];
88 }
89
90 return wpFluent()->table('fluentform_transactions')
91 ->where($column, $columnValue)
92 ->get();
93 }
94
95 public function transaction($columnValue, $column = 'id')
96 {
97 if (!defined('FLUENTFORMPRO')) {
98 return [];
99 }
100
101 return wpFluent()->table('fluentform_transactions')
102 ->where($column, $columnValue)
103 ->first();
104 }
105
106 public function subscriptions($submissionId, $withTransactions = false)
107 {
108 if (!defined('FLUENTFORMPRO')) {
109 return [];
110 }
111
112 $subscriptions = wpFluent()->table('fluentform_subscriptions')
113 ->where('submission_id', $submissionId)
114 ->get();
115
116 if ($withTransactions) {
117 foreach ($subscriptions as $subscription) {
118 $subscription->transactions = $this->transactionsBySubscriptionId($subscription->id);
119 }
120 }
121
122 return $subscriptions;
123 }
124
125 public function getSubscription($subscriptionId, $withTransactions = false)
126 {
127 if (!defined('FLUENTFORMPRO')) {
128 return [];
129 }
130
131 $subscription = wpFluent()->table('fluentform_subscriptions')
132 ->where('id', $subscriptionId)
133 ->first();
134
135 if (!$subscription) {
136 return false;
137 }
138
139 if ($withTransactions) {
140 $subscription->transactions = $this->transactionsBySubscriptionId($subscription->id);
141 }
142
143 return $subscription;
144 }
145
146 public function transactionsByUserId($userId = false, $args = [])
147 {
148 if (!defined('FLUENTFORMPRO')) {
149 return [];
150 }
151
152 if (!$userId) {
153 $userId = get_current_user_id();
154 }
155 if (!$userId) {
156 return [];
157 }
158
159 $user = get_user_by('ID', $userId);
160
161 if (!$user) {
162 return [];
163 }
164
165 $args = wp_parse_args($args, [
166 'transaction_types' => [],
167 'statuses' => [],
168 'grouped' => false,
169 ]);
170
171 $query = wpFluent()->table('fluentform_transactions')
172 ->orderBy('id', 'DESC')
173 ->where(function ($q) use ($user) {
174 $q->where('user_id', $user->ID)
175 ->orderBy('id', 'DESC')
176 ->orWhere('payer_email', $user->user_email);
177 });
178
179 if (!empty($args['transaction_types'])) {
180 $query->whereIn('transaction_type', $args['transaction_types']);
181 }
182
183 if (!empty($args['statuses'])) {
184 $query->whereIn('status', $args['statuses']);
185 }
186
187 if (!empty($args['grouped'])) {
188 $query->groupBy('submission_id');
189 }
190
191 return $query->get();
192 }
193
194 public function transactionsBySubscriptionId($subscriptionId)
195 {
196 if (!defined('FLUENTFORMPRO')) {
197 return [];
198 }
199
200 return wpFluent()->table('fluentform_transactions')
201 ->where('subscription_id', $subscriptionId)
202 ->orderBy('id', 'DESC')
203 ->get();
204 }
205
206 public function transactionsBySubmissionId($submissionId)
207 {
208 if (!defined('FLUENTFORMPRO')) {
209 return [];
210 }
211
212 return wpFluent()->table('fluentform_transactions')
213 ->where('submission_id', $submissionId)
214 ->get();
215 }
216
217 public function subscriptionsByUserId($userId = false, $args = [])
218 {
219 if (!defined('FLUENTFORMPRO')) {
220 return [];
221 }
222
223 if (!$userId) {
224 $userId = get_current_user_id();
225 }
226 if (!$userId) {
227 return [];
228 }
229
230 $user = get_user_by('ID', $userId);
231
232 if (!$user) {
233 return [];
234 }
235
236 $args = wp_parse_args($args, [
237 'statuses' => [],
238 'form_title' => false,
239 ]);
240
241 $submissions = wpFluent()->table('fluentform_submissions')
242 ->select(['id', 'currency'])
243 ->where('user_id', $userId)
244 ->where('payment_type', 'subscription')
245 ->get();
246
247 if (!$submissions) {
248 return [];
249 }
250
251 $submissionIds = [];
252 $currencyMaps = [];
253 foreach ($submissions as $submission) {
254 $submissionIds[] = $submission->id;
255 $currencyMaps[$submission->id] = $submission->currency;
256 }
257
258 $query = wpFluent()->table('fluentform_subscriptions')
259 ->select(['fluentform_subscriptions.*'])
260 ->orderBy('id', 'DESC')
261 ->whereIn('submission_id', $submissionIds);
262
263 if ($args['statuses']) {
264 $query->whereIn('status', $args['statuses']);
265 }
266
267 if ($args['form_title']) {
268 $query->select(['fluentform_forms.title'])
269 ->leftJoin('fluentform_forms', 'fluentform_forms.id', '=', 'fluentform_subscriptions.form_id');
270 }
271
272 $subscriptions = $query->get();
273 foreach ($subscriptions as $subscription) {
274 $subscription->currency = ArrayHelper::get($currencyMaps, $subscription->submission_id);
275 }
276 return $subscriptions;
277 }
278 }
279