PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.2
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.2
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 / Models / Submission.php

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

423 lines 13.9 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\Models;
4
5 use Exception;
6 use FluentForm\App\Modules\Payments\PaymentHelper;
7 use FluentForm\App\Services\Manager\FormManagerService;
8 use FluentForm\Framework\Support\Arr;
9
10 class Submission extends Model
11 {
12 /**
13 * The table associated with the model.
14 *
15 * @var string
16 */
17 protected $table = 'fluentform_submissions';
18
19 /**
20 * A submission is owned by a User.
21 *
22 * @return \FluentForm\Framework\Database\Orm\Relations\BelongsTo
23 */
24 public function user()
25 {
26 return $this->belongsTo(User::class, 'user_id', 'ID');
27 }
28
29 /**
30 * A submission is owned by a form.
31 *
32 * @return \FluentForm\Framework\Database\Orm\Relations\BelongsTo
33 */
34 public function form()
35 {
36 return $this->belongsTo(Form::class, 'form_id', 'id');
37 }
38
39 /**
40 * A submission has many meta.
41 *
42 * @return \FluentForm\Framework\Database\Orm\Relations\HasMany
43 */
44 public function submissionMeta()
45 {
46 return $this->hasMany(SubmissionMeta::class, 'response_id', 'id');
47 }
48
49 /**
50 * A submission has many logs.
51 *
52 * @return \FluentForm\Framework\Database\Orm\Relations\HasMany
53 */
54 public function logs()
55 {
56 return $this->hasMany(Log::class, 'source_id', 'id');
57 }
58
59 /**
60 * A submission has many entry details.
61 *
62 * @return \FluentForm\Framework\Database\Orm\Relations\HasMany
63 */
64 public function entryDetails()
65 {
66 return $this->hasMany(EntryDetails::class, 'submission_id', 'id');
67 }
68
69 /**
70 * A submission has many transactions.
71 *
72 * @return \FluentForm\Framework\Database\Orm\Relations\HasMany
73 */
74 public function transactions()
75 {
76 return $this->hasMany(Transaction::class, 'submission_id', 'id');
77 }
78
79 /**
80 * A submission has many subscriptions.
81 *
82 * @return \FluentForm\Framework\Database\Orm\Relations\HasMany
83 */
84 public function subscriptions()
85 {
86 return $this->hasMany(Subscription::class, 'submission_id', 'id');
87 }
88
89 /**
90 * A submission has many order items.
91 *
92 * @return \FluentForm\Framework\Database\Orm\Relations\HasMany
93 */
94 public function orderItems()
95 {
96 return $this->hasMany(OrderItem::class, 'submission_id', 'id');
97 }
98
99 public function customQuery($attributes = [], $searchExtender = null)
100 {
101 $entryType = Arr::get($attributes, 'entry_type');
102 $dateRange = Arr::get($attributes, 'date_range');
103 $isFavourite = false;
104 $status = $entryType;
105
106 // We have to handle favorites separately because status and favorites are different
107 if ('favorites' === $entryType) {
108 $isFavourite = true;
109 $status = false;
110 }
111
112 $formId = Arr::get($attributes, 'form_id');
113 $startDate = Arr::get($dateRange, 0);
114 $endDate = Arr::get($dateRange, 1);
115 $search = Arr::get($attributes, 'search');
116 $sortBy = \FluentForm\App\Helpers\Helper::sanitizeOrderValue(Arr::get($attributes, 'sort_by', 'DESC'));
117
118 $wheres = [];
119 $paymentStatuses = Arr::get($attributes, 'payment_statuses');
120
121 if ($paymentStatuses && is_array($paymentStatuses)) {
122 $wheres[] = ['payment_status', $paymentStatuses];
123 }
124
125 $query = $this->orderBy('fluentform_submissions.id', $sortBy)
126 ->when($formId, function ($q) use ($formId) {
127 return $q->where('fluentform_submissions.form_id', $formId);
128 })
129 ->when($isFavourite, function ($q) {
130 return $q->where('is_favourite', true);
131 })
132 ->where(function ($q) use ($status) {
133 $operator = '=';
134
135 if (!$status) {
136 $operator = '!=';
137 $status = 'trashed';
138 }
139
140 return $q->where('fluentform_submissions.status', $operator, $status);
141 })
142 ->when($startDate && $endDate, function ($q) use ($startDate, $endDate) {
143 $endDate .= ' 23:59:59';
144
145 return $q->where('fluentform_submissions.created_at', '>=', $startDate)
146 ->where('fluentform_submissions.created_at', '<=', $endDate);
147 })
148 ->when($search, function ($q) use ($search, $searchExtender) {
149 global $wpdb;
150 $escaped = $wpdb->esc_like($search);
151 return $q->where(function ($q) use ($escaped, $searchExtender) {
152 $q->where('fluentform_submissions.id', 'LIKE', "%{$escaped}%")
153 ->orWhere('response', 'LIKE', "%{$escaped}%")
154 ->orWhere('fluentform_submissions.status', 'LIKE', "%{$escaped}%")
155 ->orWhere('fluentform_submissions.created_at', 'LIKE', "%{$escaped}%");
156 if ($searchExtender) {
157 $searchExtender($q, $escaped);
158 }
159 });
160 })
161 ->when($wheres, function ($q) use ($wheres) {
162 foreach ($wheres as $where) {
163 if (is_array($where) && count($where) > 1) {
164 if (count($where) > 2) {
165 $column = $where[0];
166 $operator = $where[1];
167 $value = $where[2];
168 } else {
169 $column = $where[0];
170 $operator = '=';
171 $value = $where[1];
172 }
173
174 if (is_array($value)) {
175 return $q->whereIn($column, $value);
176 } else {
177 return $q->where($column, $operator, $value);
178 }
179 }
180 }
181 });
182
183 return $query;
184 }
185
186 public function paginateEntries($attributes = [])
187 {
188 $formId = Arr::get($attributes, 'form_id');
189 $allowFormIds = FormManagerService::getUserAllowedFormsScope();
190 $query = $this->customQuery($attributes);
191 $query = $query->when(false !== $allowFormIds, function ($q) use ($allowFormIds) {
192 return $q->whereIn('fluentform_submissions.form_id', $allowFormIds ?: [0]);
193 });
194 if (Arr::get($attributes, 'advanced_filter')) {
195 $query = apply_filters('fluentform/apply_entries_advance_filter', $query, $attributes);
196 }
197 $response = $query->paginate();
198 $response = apply_filters_deprecated(
199 'fluentform_get_raw_responses',
200 [
201 $response,
202 $formId
203 ],
204 FLUENTFORM_FRAMEWORK_UPGRADE,
205 'fluentform/get_raw_responses',
206 'Use fluentform/get_raw_responses instead of fluentform_get_raw_responses.'
207 );
208
209 return apply_filters('fluentform/get_raw_responses', $response, $formId);
210 }
211
212 public function findAdjacentSubmission($attributes = [])
213 {
214 $sortBy = Arr::get($attributes, 'sort_by', 'DESC');
215
216 $direction = Arr::get($attributes, 'direction', 'next');
217
218 $operator = 'ASC' === $sortBy && 'previous' === $direction ? '>' : '<';
219
220 if ('previous' === $direction) {
221 $operator = 'ASC' === $sortBy ? '>' : '<';
222 } else {
223 $operator = 'ASC' === $sortBy ? '<' : '>';
224 $attributes['sort_by'] = 'ASC' === $sortBy ? 'DESC' : 'ASC';
225 }
226
227 $entryId = Arr::get($attributes, 'entry_id');
228
229 $columns = Arr::get($attributes, 'columns', 'id');
230
231 $query = $this->customQuery($attributes);
232
233 $submission = $query->select($columns)->where('id', $operator, $entryId)->first();
234
235 return apply_filters('fluentform/next_submission', $submission, $entryId, $attributes);
236 }
237
238 public function countByGroup($formId)
239 {
240 $statuses = $this->selectRaw('status, COUNT(*) as count')
241 ->where('form_id', $formId)
242 ->groupBy('status')
243 ->get();
244
245 $counts = [];
246
247 foreach ($statuses as $status) {
248 $counts[$status->status] = (int) $status->count;
249 }
250
251 $counts['all'] = array_sum($counts);
252
253 if (isset($counts['trashed'])) {
254 $counts['all'] -= $counts['trashed'];
255 }
256
257 $favorites = $this->where('form_id', $formId)
258 ->where('is_favourite', 1)
259 ->where('status', '!=', 'trashed')
260 ->count();
261
262 $counts['favorites'] = $favorites;
263
264 return array_merge([
265 'unread' => 0,
266 'read' => 0,
267 'spam' => 0,
268 'trashed' => 0,
269 ], $counts);
270 }
271
272 public function amend($id, $data = [])
273 {
274 $this->where('id', $id)->update($data);
275 }
276
277 public static function remove($submissionIds)
278 {
279 static::whereIn('id', $submissionIds)->delete();
280
281 SubmissionMeta::whereIn('response_id', $submissionIds)->delete();
282
283 Log::whereIn('source_id', $submissionIds)
284 ->where('source_type', 'submission_item')
285 ->delete();
286
287 EntryDetails::whereIn('submission_id', $submissionIds)->delete();
288
289 try {
290 if (PaymentHelper::hasPaymentSettings()) {
291 OrderItem::whereIn('submission_id', $submissionIds)->delete();
292 Transaction::whereIn('submission_id', $submissionIds)->delete();
293 Subscription::whereIn('submission_id', $submissionIds)->delete();
294 }
295
296 wpFluent()->table('ff_scheduled_actions')
297 ->whereIn('origin_id', $submissionIds)
298 ->where('type', 'submission_action')
299 ->delete();
300
301 } catch (Exception $exception) {
302 // ...
303 }
304 }
305
306 public function allSubmissions($attributes = []) {
307 $searchExtender = function ($q, $escaped) {
308 $q->orWhereHas('form', function ($q) use ($escaped) {
309 $q->where('title', 'LIKE', "%{$escaped}%");
310 });
311 };
312 $customQuery = $this->customQuery($attributes, $searchExtender);
313 $allowFormIds = FormManagerService::getUserAllowedFormsScope();
314
315 $result = $customQuery
316 ->with([
317 'form' => function ($q) {
318 $q->select(['id', 'title']);
319 }
320 ])
321 ->select(['id', 'form_id', 'status', 'created_at', 'browser', 'currency', 'total_paid'])
322 ->when(false !== $allowFormIds, function ($q) use ($allowFormIds){
323 return $q->whereIn('form_id', $allowFormIds ?: [0]);
324 })
325 ->paginate()
326 ->toArray();
327
328 $useHumanDate = apply_filters('fluentform/entries_human_date', false);
329
330 foreach ($result['data'] as &$entry) {
331 $entry['entry_url'] = admin_url('admin.php?page=fluent_forms&route=entries&form_id=' . $entry['form_id'] . '#/entries/' . $entry['id']);
332
333 if ($useHumanDate) {
334 $entry['human_date'] = human_time_diff(strtotime($entry['created_at']), strtotime(current_time('mysql')));
335 }
336 }
337
338 $result['available_forms'] = $this->availableForms();
339
340 return $result;
341 }
342
343 public function availableForms()
344 {
345 $form = new Form();
346 if (false !== ($allowForms = FormManagerService::getUserAllowedFormsScope())) {
347 return $form->select('id', 'title')->whereIn('id', $allowForms ?: [0])->get();
348 }
349 return $form->select('id', 'title')->get();
350 }
351
352 public static function report($attributes)
353 {
354 $from = date('Y-m-d H:i:s', strtotime('-30 days'));
355 $to = date('Y-m-d H:i:s', strtotime('+1 days'));
356 $formId = Arr::get($attributes, 'form_id');
357 $allowFormIds = FormManagerService::getUserAllowedFormsScope();
358 $status = Arr::get($attributes, 'entry_status');
359 $start = Arr::get($attributes, 'date_range.0', '');
360 $end = Arr::get($attributes, 'date_range.1', '');
361 $dateRange = Arr::get($attributes, 'date_range');
362
363 if ('all' === $dateRange) {
364 $firstItem = self::orderBy('created_at', 'ASC')
365 ->when(false !== $allowFormIds, function ($q) use ($allowFormIds) {
366 return $q->whereIn('form_id', $allowFormIds ?: [0]);
367 })
368 ->when($formId, function ($q) use ($formId) {
369 return $q->where('form_id', $formId);
370 })
371 ->when($status, function ($q2) use ($status) {
372 return $q2->where('status', $status);
373 })
374 ->first();
375
376 if ($firstItem && $firstItem->created_at) {
377 $from = date('Y-m-d H:i:s', strtotime($firstItem->created_at));
378 $to = date('Y-m-d H:i:s');
379 }
380 }
381
382
383 if ($start && $startTime = strtotime($start)) {
384 $from = date('Y-m-d 00:00:00', $startTime);
385 }
386
387 if ($end && $endTime = strtotime($end)) {
388 $to = date('Y-m-d 23:59:59', $endTime);
389 }
390
391 $period = new \DatePeriod(new \DateTime($from), new \DateInterval('P1D'), new \DateTime($to));
392
393 $range = [];
394
395 foreach ($period as $date) {
396 $range[$date->format('Y-m-d')] = 0;
397 }
398
399 $items = self::selectRaw('DATE(created_at) AS date')
400 ->selectRaw('COUNT(id) AS count')
401 ->whereBetween('created_at', [$from, $to])
402 ->groupBy('date')
403 ->orderBy('date', 'ASC')
404 ->when(false !== $allowFormIds, function ($q) use ($allowFormIds) {
405 return $q->whereIn('form_id', $allowFormIds ?: [0]);
406 })
407 ->when($formId, function ($q) use ($formId) {
408 return $q->where('form_id', $formId);
409 })
410 ->when($status, function ($q2) use ($status) {
411 return $q2->where('status', $status);
412 })
413 ->get();
414
415
416 foreach ($items as $item) {
417 $range[$item->date] = $item->count;
418 }
419
420 return $range;
421 }
422 }
423