Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluentform/app/Models/Submission.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
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
+
public function customQuery($attributes = [])
70
+
{
71
+
$entryType = Arr::get($attributes, 'entry_type');
72
+
$dateRange = Arr::get($attributes, 'date_range');
73
+
$isFavourite = false;
74
+
$status = $entryType;
75
+
76
+
// We have to handle favorites separately because status and favorites are different
77
+
if ('favorites' === $entryType) {
78
+
$isFavourite = true;
79
+
$status = false;
80
+
}
81
+
82
+
$formId = Arr::get($attributes, 'form_id');
83
+
$startDate = Arr::get($dateRange, 0);
84
+
$endDate = Arr::get($dateRange, 1);
85
+
$search = Arr::get($attributes, 'search');
86
+
$sortBy = Arr::get($attributes, 'sort_by', 'DESC');
87
+
88
+
$wheres = [];
89
+
$paymentStatuses = Arr::get($attributes, 'payment_statuses');
90
+
91
+
if ($paymentStatuses && is_array($paymentStatuses)) {
92
+
$wheres[] = ['payment_status', $paymentStatuses];
93
+
}
94
+
95
+
$query = $this->orderBy('fluentform_submissions.id', $sortBy)
96
+
->when($formId, function ($q) use ($formId) {
97
+
return $q->where('fluentform_submissions.form_id', $formId);
98
+
})
99
+
->when($isFavourite, function ($q) {
100
+
return $q->where('is_favourite', true);
101
+
})
102
+
->where(function ($q) use ($status) {
103
+
$operator = '=';
104
+
105
+
if (!$status) {
106
+
$operator = '!=';
107
+
$status = 'trashed';
108
+
}
109
+
110
+
return $q->where('fluentform_submissions.status', $operator, $status);
111
+
})
112
+
->when($startDate && $endDate, function ($q) use ($startDate, $endDate) {
113
+
$endDate .= ' 23:59:59';
114
+
115
+
return $q->where('fluentform_submissions.created_at', '>=', $startDate)
116
+
->where('fluentform_submissions.created_at', '<=', $endDate);
117
+
})
118
+
->when($search, function ($q) use ($search) {
119
+
return $q->where(function ($q) use ($search) {
120
+
return $q->where('fluentform_submissions.id', 'LIKE', "%{$search}%")
121
+
->orWhere('response', 'LIKE', "%{$search}%")
122
+
->orWhere('fluentform_submissions.status', 'LIKE', "%{$search}%")
123
+
->orWhere('fluentform_submissions.created_at', 'LIKE', "%{$search}%");
124
+
});
125
+
})
126
+
->when($wheres, function ($q) use ($wheres) {
127
+
foreach ($wheres as $where) {
128
+
if (is_array($where) && count($where) > 1) {
129
+
if (count($where) > 2) {
130
+
$column = $where[0];
131
+
$operator = $where[1];
132
+
$value = $where[2];
133
+
} else {
134
+
$column = $where[0];
135
+
$operator = '=';
136
+
$value = $where[1];
137
+
}
138
+
139
+
if (is_array($value)) {
140
+
return $q->whereIn($column, $value);
141
+
} else {
142
+
return $q->where($column, $operator, $value);
143
+
}
144
+
}
145
+
}
146
+
});
147
+
148
+
return $query;
149
+
}
150
+
151
+
public function paginateEntries($attributes = [])
152
+
{
153
+
$formId = Arr::get($attributes, 'form_id');
154
+
$query = $this->customQuery($attributes);
155
+
if (Arr::get($attributes, 'advanced_filter')) {
156
+
$query = apply_filters('fluentform/apply_entries_advance_filter', $query, $attributes);
157
+
}
158
+
$response = $query->paginate();
159
+
$response = apply_filters_deprecated(
160
+
'fluentform_get_raw_responses',
161
+
[
162
+
$response,
163
+
$formId
164
+
],
165
+
FLUENTFORM_FRAMEWORK_UPGRADE,
166
+
'fluentform/get_raw_responses',
167
+
'Use fluentform/get_raw_responses instead of fluentform_get_raw_responses.'
168
+
);
169
+
170
+
return apply_filters('fluentform/get_raw_responses', $response, $formId);
171
+
}
172
+
173
+
public function findPreviousSubmission($attributes = [])
174
+
{
175
+
$query = $this->customQuery($attributes);
176
+
177
+
$sortBy = Arr::get($attributes, 'sort_by', 'DESC');
178
+
179
+
$operator = 'ASC' === $sortBy ? '<' : '>';
180
+
181
+
$entryId = Arr::get($attributes, 'entry_id');
182
+
183
+
$columns = Arr::get($attributes, 'columns', 'id');
184
+
185
+
$submission = $query->select($columns)->where('id', $operator, $entryId)->first();
186
+
187
+
return apply_filters('fluentform/next_submission', $submission, $entryId, $attributes);
188
+
}
189
+
190
+
public function findAdjacentSubmission($attributes = [])
191
+
{
192
+
$sortBy = Arr::get($attributes, 'sort_by', 'DESC');
193
+
194
+
$direction = Arr::get($attributes, 'direction', 'next');
195
+
196
+
$operator = 'ASC' === $sortBy && 'previous' === $direction ? '>' : '<';
197
+
198
+
if ('previous' === $direction) {
199
+
$operator = 'ASC' === $sortBy ? '>' : '<';
200
+
} else {
201
+
$operator = 'ASC' === $sortBy ? '<' : '>';
202
+
$attributes['sort_by'] = 'ASC' === $sortBy ? 'DESC' : 'ASC';
203
+
}
204
+
205
+
$entryId = Arr::get($attributes, 'entry_id');
206
+
207
+
$columns = Arr::get($attributes, 'columns', 'id');
208
+
209
+
$query = $this->customQuery($attributes);
210
+
211
+
$submission = $query->select($columns)->where('id', $operator, $entryId)->first();
212
+
213
+
return apply_filters('fluentform/next_submission', $submission, $entryId, $attributes);
214
+
}
215
+
216
+
public function countByGroup($formId)
217
+
{
218
+
$statuses = $this->selectRaw('status, COUNT(*) as count')
219
+
->where('form_id', $formId)
220
+
->groupBy('status')
221
+
->get();
222
+
223
+
$counts = [];
224
+
225
+
foreach ($statuses as $status) {
226
+
$counts[$status->status] = (int) $status->count;
227
+
}
228
+
229
+
$counts['all'] = array_sum($counts);
230
+
231
+
if (isset($counts['trashed'])) {
232
+
$counts['all'] -= $counts['trashed'];
233
+
}
234
+
235
+
$favorites = $this->where('form_id', $formId)
236
+
->where('is_favourite', 1)
237
+
->where('status', '!=', 'trashed')
238
+
->count();
239
+
240
+
$counts['favorites'] = $favorites;
241
+
242
+
return array_merge([
243
+
'unread' => 0,
244
+
'read' => 0,
245
+
'spam' => 0,
246
+
'trashed' => 0,
247
+
], $counts);
248
+
}
249
+
250
+
public function amend($id, $data = [])
251
+
{
252
+
$this->where('id', $id)->update($data);
253
+
}
254
+
255
+
public static function remove($submissionIds)
256
+
{
257
+
static::whereIn('id', $submissionIds)->delete();
258
+
259
+
SubmissionMeta::whereIn('response_id', $submissionIds)->delete();
260
+
261
+
Log::whereIn('source_id', $submissionIds)
262
+
->where('source_type', 'submission_item')
263
+
->delete();
264
+
265
+
EntryDetails::whereIn('submission_id', $submissionIds)->delete();
266
+
267
+
//delete models this way for now
268
+
// todo: update wpFluent to the framework model
269
+
try {
270
+
if (PaymentHelper::hasPaymentSettings()) {
271
+
wpFluent()->table('fluentform_order_items')
272
+
->whereIn('submission_id', $submissionIds)
273
+
->delete();
274
+
275
+
wpFluent()->table('fluentform_transactions')
276
+
->whereIn('submission_id', $submissionIds)
277
+
->delete();
278
+
279
+
wpFluent()->table('fluentform_subscriptions')
280
+
->whereIn('submission_id', $submissionIds)
281
+
->delete();
282
+
}
283
+
284
+
wpFluent()->table('ff_scheduled_actions')
285
+
->whereIn('origin_id', $submissionIds)
286
+
->where('type', 'submission_action')
287
+
->delete();
288
+
289
+
} catch (Exception $exception) {
290
+
// ...
291
+
}
292
+
}
293
+
294
+
public function allSubmissions($attributes = []) {
295
+
$customQuery = $this->customQuery($attributes);
296
+
$search = Arr::get($attributes, 'search');
297
+
$allowFormIds = FormManagerService::getUserAllowedForms();
298
+
299
+
$result = $customQuery
300
+
->with([
301
+
'form' => function ($q) {
302
+
$q->select(['id', 'title']);
303
+
}
304
+
])
305
+
->select(['id', 'form_id', 'status', 'created_at', 'browser', 'currency', 'total_paid'])
306
+
->when($allowFormIds, function ($q) use ($allowFormIds){
307
+
return $q->whereIn('form_id', $allowFormIds);
308
+
})
309
+
->when($search, function ($q) use ($search){
310
+
return $q->orWhereHas('form', function ($q) use ($search) {
311
+
return $q->orWhere('title', 'LIKE', "%{$search}%");
312
+
});
313
+
})
314
+
->paginate()
315
+
->toArray();
316
+
317
+
foreach ($result['data'] as &$entry) {
318
+
$entry['entry_url'] = admin_url('admin.php?page=fluent_forms&route=entries&form_id=' . $entry['form_id'] . '#/entries/' . $entry['id']);
319
+
320
+
if (apply_filters('fluentform/entries_human_date', false)) {
321
+
$entry['human_date'] = human_time_diff(strtotime($entry['created_at']), strtotime(current_time('mysql')));
322
+
}
323
+
}
324
+
325
+
$result['available_forms'] = $this->availableForms();
326
+
327
+
return $result;
328
+
}
329
+
330
+
public function availableForms()
331
+
{
332
+
$form = new Form();
333
+
if ($allowForms = FormManagerService::getUserAllowedForms()) {
334
+
return $form->select('id', 'title')->whereIn('id', $allowForms)->get();
335
+
}
336
+
return $form->select('id', 'title')->get();
337
+
}
338
+
339
+
public static function report($attributes)
340
+
{
341
+
$from = date('Y-m-d H:i:s', strtotime('-30 days'));
342
+
$to = date('Y-m-d H:i:s', strtotime('+1 days'));
343
+
$formId = Arr::get($attributes, 'form_id');
344
+
$status = Arr::get($attributes, 'entry_status');
345
+
$start = Arr::get($attributes, 'date_range.0', '');
346
+
$end = Arr::get($attributes, 'date_range.1', '');
347
+
$dateRange = Arr::get($attributes, 'date_range');
348
+
349
+
if ('all' === $dateRange) {
350
+
$firstItem = self::orderBy('created_at', 'ASC')
351
+
->when($formId, function ($q) use ($formId) {
352
+
return $q->where('form_id', $formId);
353
+
})
354
+
->when($status, function ($q2) use ($status) {
355
+
return $q2->where('status', $status);
356
+
})
357
+
->first();
358
+
359
+
if ($firstItem && $firstItem->created_at) {
360
+
$from = date('Y-m-d H:i:s', strtotime($firstItem->created_at));
361
+
$to = date('Y-m-d H:i:s');
362
+
}
363
+
}
364
+
365
+
366
+
if ($start && $startTime = strtotime($start)) {
367
+
$from = date('Y-m-d 00:00:00', $startTime);
368
+
}
369
+
370
+
if ($end && $endTime = strtotime($end)) {
371
+
$to = date('Y-m-d 23:59:59', $endTime);
372
+
}
373
+
374
+
$period = new \DatePeriod(new \DateTime($from), new \DateInterval('P1D'), new \DateTime($to));
375
+
376
+
$range = [];
377
+
378
+
foreach ($period as $date) {
379
+
$range[$date->format('Y-m-d')] = 0;
380
+
}
381
+
382
+
$items = self::selectRaw('DATE(created_at) AS date')
383
+
->selectRaw('COUNT(id) AS count')
384
+
->whereBetween('created_at', [$from, $to])
385
+
->groupBy('date')
386
+
->orderBy('date', 'ASC')
387
+
->when($formId, function ($q) use ($formId) {
388
+
return $q->where('form_id', $formId);
389
+
})
390
+
->when($status, function ($q2) use ($status) {
391
+
return $q2->where('status', $status);
392
+
})
393
+
->get();
394
+
395
+
396
+
foreach ($items as $item) {
397
+
$range[$item->date] = $item->count;
398
+
}
399
+
400
+
return $range;
401
+
}
402
+
}
403
+