PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.7
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.7
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 6.2.7, at app/Api/Submission.php

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