PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 5.2.9
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v5.2.9
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 / Modules / Entries / EntryQuery.php

EntryQuery.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 5.2.9, at app/Modules/Entries/EntryQuery.php

208 lines 6.1 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\Modules\Entries;
4
5 class EntryQuery
6 {
7 /**
8 * Request object
9 *
10 * @var \FluentForm\Framework\Request\Request $request
11 */
12 protected $request;
13 protected $formModel;
14 protected $responseModel;
15
16 protected $formId = false;
17
18 protected $per_page = 10;
19 protected $page_number = 1;
20 protected $status = false;
21 protected $is_favourite = null;
22 protected $sort_by = 'ASC';
23 protected $search = false;
24 protected $wheres = [];
25
26 protected $startDate;
27 protected $endDate;
28
29 public function __construct()
30 {
31 $this->request = wpFluentForm('request');
32 $this->formModel = wpFluent()->table('fluentform_forms');
33 $this->responseModel = wpFluent()->table('fluentform_submissions');
34 }
35
36 public function getResponses()
37 {
38 $query = $this->responseModel
39 ->where('form_id', $this->formId)
40 ->orderBy('id', \FluentForm\App\Helpers\Helper::sanitizeOrderValue($this->sort_by));
41
42 if ($this->per_page > 0) {
43 $query = $query->limit($this->per_page);
44 }
45
46 if ($this->page_number > 0) {
47 $query = $query->offset(($this->page_number - 1) * $this->per_page);
48 }
49
50 if ($this->is_favourite) {
51 $query->where('is_favourite', $this->is_favourite);
52 $query->where('status', '!=', 'trashed');
53 } else {
54 if (!$this->status) {
55 $query->where('status', '!=', 'trashed');
56 } else {
57 $query->where('status', $this->status);
58 }
59 }
60
61 if ($this->startDate && $this->endDate) {
62 $endDate = $this->endDate;
63 $endDate .= ' 23:59:59';
64 $query->where('created_at', '>=', $this->startDate);
65 $query->where('created_at', '<=', $endDate);
66 }
67
68 if ($this->search) {
69 $searchString = $this->search;
70 $query->where(function ($q) use ($searchString) {
71 $q->where('id', 'LIKE', "%{$searchString}%")
72 ->orWhere('response', 'LIKE', "%{$searchString}%")
73 ->orWhere('status', 'LIKE', "%{$searchString}%")
74 ->orWhere('created_at', 'LIKE', "%{$searchString}%");
75 });
76 }
77
78 if ($this->wheres) {
79 foreach ($this->wheres as $where) {
80 if (is_array($where) && count($where) > 1) {
81 if (count($where) > 2) {
82 $column = $where[0];
83 $operator = $where[1];
84 $value = $where[2];
85 } else {
86 $column = $where[0];
87 $operator = '=';
88 $value = $where[1];
89 }
90 if (is_array($value)) {
91 $query->whereIn($column, $value);
92 } else {
93 $query->where($column, $operator, $value);
94 }
95 }
96 }
97 }
98
99 $total = $query->count();
100 $responses = $query->get();
101
102 $responses = apply_filters_deprecated(
103 'fluentform_get_raw_responses',
104 [
105 $responses,
106 $this->formId
107 ],
108 FLUENTFORM_FRAMEWORK_UPGRADE,
109 'fluentform/get_raw_responses',
110 'Use fluentform/get_raw_responses instead of fluentform_get_raw_responses.'
111 );
112
113 $responses = apply_filters('fluentform/get_raw_responses', $responses, $this->formId);
114
115 return [
116 'data' => $responses,
117 'paginate' => [
118 'total' => $total,
119 'per_page' => $this->per_page,
120 'current_page' => $this->page_number,
121 'last_page' => ceil($total / $this->per_page),
122 ],
123 ];
124 }
125
126 public function getResponse($entryId)
127 {
128 return wpFluent()->table('fluentform_submissions')->find($entryId);
129 }
130
131 public function getNextResponse($entryId)
132 {
133 $query = $this->getNextPrevEntryQuery();
134
135 $operator = 'ASC' == $this->sort_by ? '>' : '<';
136
137 return $query->select('id')
138 ->where('id', $operator, $entryId)
139 ->orderBy('id', \FluentForm\App\Helpers\Helper::sanitizeOrderValue($this->sort_by))
140 ->first();
141 }
142
143 public function getPrevResponse($entryId)
144 {
145 $query = $this->getNextPrevEntryQuery();
146
147 $operator = 'ASC' == $this->sort_by ? '<' : '>';
148
149 $orderBy = 'ASC' == $this->sort_by ? 'DESC' : 'ASC';
150
151 return $query->select('id')
152 ->where('id', $operator, $entryId)
153 ->orderBy('id', $orderBy)
154 ->first();
155 }
156
157 protected function getNextPrevEntryQuery()
158 {
159 $query = wpFluent()->table('fluentform_submissions')->limit(1);
160
161 if ($this->is_favourite) {
162 $query->where('is_favourite', $this->is_favourite)->where('status', '!=', 'trashed');
163 } else {
164 if (!$this->status) {
165 $query->where('status', '!=', 'trashed');
166 } else {
167 $query->where('status', $this->status);
168 }
169 }
170
171 if ($this->search) {
172 $query->where('response', 'LIKE', "%{$this->search}%");
173 }
174
175 return $query->where('form_id', $this->formId);
176 }
177
178 public function groupCount($form_id)
179 {
180 $statuses = $this->responseModel
181 ->select($this->responseModel->raw('status, COUNT(*) as count'))
182 ->where('form_id', $form_id)
183 ->groupBy('status')
184 ->get();
185
186 $counts = [];
187 foreach ($statuses as $status) {
188 $counts[$status->status] = $status->count;
189 }
190
191 $counts['all'] = array_sum($counts);
192 if (isset($counts['trashed'])) {
193 $counts['all'] -= $counts['trashed'];
194 }
195
196 $favorites = wpFluent()
197 ->table('fluentform_submissions')
198 ->where('form_id', $form_id)
199 ->where('is_favourite', 1)
200 ->where('status', '!=', 'trashed')
201 ->count();
202
203 $counts['favourites'] = $favorites;
204
205 return $counts;
206 }
207 }
208