PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.4
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.4
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 / Entry.php

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

206 lines 6.3 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\Framework\Support\Arr;
8
9 class Entry extends Model
10 {
11 /**
12 * The table associated with the model.
13 *
14 * @var string
15 */
16 protected $table = 'fluentform_submissions';
17
18 /**
19 * A formMeta is owned by a form.
20 *
21 * @return \FluentForm\Framework\Database\Orm\Relations\BelongsTo
22 */
23 public function form()
24 {
25 return $this->belongsTo(Form::class, 'form_id', 'id');
26 }
27
28 /**
29 * An entry has many meta.
30 *
31 * @return \FluentForm\Framework\Database\Orm\Relations\HasMany
32 */
33 public function entryMeta()
34 {
35 return $this->hasMany(EntryMeta::class, 'response_id', 'id');
36 }
37
38 /**
39 * An entry has many logs.
40 *
41 * @return \FluentForm\Framework\Database\Orm\Relations\HasMany
42 */
43 public function logs()
44 {
45 return $this->hasMany(Log::class, 'source_id', 'id');
46 }
47
48 /**
49 * An entry has many entry details.
50 *
51 * @return \FluentForm\Framework\Database\Orm\Relations\HasMany
52 */
53 public function entryDetails()
54 {
55 return $this->hasMany(EntryDetails::class, 'submission_id', 'id');
56 }
57
58 public function paginateEntries($attributes = [])
59 {
60 $formId = Arr::get($attributes, 'form_id');
61 $isFavourite = Arr::get($attributes, 'is_favourite');
62 $status = Arr::get($attributes, 'status');
63 $startDate = Arr::get($attributes, 'start_date');
64 $endDate = Arr::get($attributes, 'end_date');
65 $search = Arr::get($attributes, 'search');
66 $wheres = Arr::get($attributes, 'wheres');
67
68 $sortBy = \FluentForm\App\Helpers\Helper::sanitizeOrderValue(Arr::get($attributes, 'sort_by', 'DESC'));
69 $query = $this->orderBy('id', $sortBy)
70 ->when($formId, function ($q) use ($formId) {
71 return $q->where('form_id', $formId);
72 })
73 ->when($isFavourite, function ($q) {
74 return $q->where('is_favourite', true);
75 })
76 ->where(function ($q) use ($status) {
77 $operator = '=';
78
79 if (!$status) {
80 $operator = '!=';
81 $status = 'trashed';
82 }
83
84 return $q->where('status', $operator, $status);
85 })
86 ->when($startDate && $endDate, function ($q) use ($startDate, $endDate) {
87 $endDate .= ' 23:59:59';
88
89 return $q->where('created_at', '>=', $startDate)
90 ->where('created_at', '<=', $endDate);
91 })
92 ->when($search, function ($q) use ($search) {
93 global $wpdb;
94 $escaped = $wpdb->esc_like($search);
95 return $q->where(function ($q) use ($escaped) {
96 return $q->where('id', 'LIKE', "%{$escaped}%")
97 ->orWhere('response', 'LIKE', "%{$escaped}%")
98 ->orWhere('status', 'LIKE', "%{$escaped}%")
99 ->orWhere('created_at', 'LIKE', "%{$escaped}%");
100 });
101 })
102 ->when($wheres, function ($q) use ($wheres) {
103 foreach ($wheres as $where) {
104 if (is_array($where) && count($where) > 1) {
105 if (count($where) > 2) {
106 $column = $where[0];
107 $operator = $where[1];
108 $value = $where[2];
109 } else {
110 $column = $where[0];
111 $operator = '=';
112 $value = $where[1];
113 }
114
115 if (is_array($value)) {
116 return $q->whereIn($column, $value);
117 } else {
118 return $q->where($column, $operator, $value);
119 }
120 }
121 }
122 });
123
124 $response = $query->paginate();
125 $response = apply_filters_deprecated(
126 'fluentform_get_raw_responses',
127 [
128 $response,
129 $formId
130 ],
131 FLUENTFORM_FRAMEWORK_UPGRADE,
132 'fluentform/get_raw_responses',
133 'Use fluentform/get_raw_responses instead of fluentform_get_raw_responses.'
134 );
135
136 return apply_filters('fluentform/get_raw_responses', $response, $formId);
137 }
138
139 public function countByGroup($formId)
140 {
141 $statuses = $this->selectRaw('status, COUNT(*) as count')
142 ->where('form_id', $formId)
143 ->groupBy('status')
144 ->get();
145
146 $counts = [];
147
148 foreach ($statuses as $status) {
149 $counts[$status->status] = (int) $status->count;
150 }
151
152 $counts['all'] = array_sum($counts);
153
154 if (isset($counts['trashed'])) {
155 $counts['all'] -= $counts['trashed'];
156 }
157
158 $favorites = $this->where('form_id', $formId)
159 ->where('is_favourite', 1)
160 ->where('status', '!=', 'trashed')
161 ->count();
162
163 $counts['favorites'] = $favorites;
164
165 return array_merge([
166 'unread' => 0,
167 'read' => 0,
168 'spam' => 0,
169 'trashed' => 0,
170 ], $counts);
171 }
172
173 public function amend($id, $data = [])
174 {
175 $this->where('id', $id)->update($data);
176 }
177
178 public static function remove($entryIds)
179 {
180 static::whereIn('id', $entryIds)->delete();
181
182 EntryMeta::whereIn('response_id', $entryIds)->delete();
183
184 Log::whereIn('source_id', $entryIds)
185 ->where('source_type', 'submission_item')
186 ->delete();
187
188 EntryDetails::whereIn('submission_id', $entryIds)->delete();
189
190 try {
191 if (PaymentHelper::hasPaymentSettings()) {
192 OrderItem::whereIn('submission_id', $entryIds)->delete();
193 Transaction::whereIn('submission_id', $entryIds)->delete();
194 Subscription::whereIn('submission_id', $entryIds)->delete();
195 }
196
197 wpFluent()->table('ff_scheduled_actions')
198 ->whereIn('origin_id', $entryIds)
199 ->where('type', 'submission_action')
200 ->delete();
201 } catch (Exception $exception) {
202 // ...
203 }
204 }
205 }
206