PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.14
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.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 3.6.66 All 195 releases
fluentform / app / Modules / Entries / EntryQuery.php

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

196 lines 5.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\Modules\Entries;
4
5 use FluentForm\App;
6
7 class EntryQuery
8 {
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 = array();
25
26 protected $startDate;
27 protected $endDate;
28
29 public function __construct()
30 {
31 $this->request = App::make('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', $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 $responses = apply_filters('fluentform_get_raw_responses', $responses, $this->formId);
102
103 return [
104 'data' => $responses,
105 'paginate' => [
106 'total' => $total,
107 'per_page' => $this->per_page,
108 'current_page' => $this->page_number,
109 'last_page' => ceil($total / $this->per_page)
110 ]
111 ];
112 }
113
114 public function getResponse($entryId)
115 {
116 return wpFluent()->table('fluentform_submissions')->find($entryId);
117 }
118
119 public function getNextResponse($entryId)
120 {
121 $query = $this->getNextPrevEntryQuery();
122
123 $operator = $this->sort_by == 'ASC' ? '>' : '<';
124
125 return $query->select('id')
126 ->where('id', $operator, $entryId)
127 ->orderBy('id', $this->sort_by)
128 ->first();
129 }
130
131 public function getPrevResponse($entryId)
132 {
133 $query = $this->getNextPrevEntryQuery();
134
135 $operator = $this->sort_by == 'ASC' ? '<' : '>';
136
137 $orderBy = $this->sort_by == 'ASC' ? 'DESC' : 'ASC';
138
139 return $query->select('id')
140 ->where('id', $operator, $entryId)
141 ->orderBy('id', $orderBy)
142 ->first();
143 }
144
145 protected function getNextPrevEntryQuery()
146 {
147 $query = wpFluent()->table('fluentform_submissions')->limit(1);
148
149 if ($this->is_favourite) {
150 $query->where('is_favourite', $this->is_favourite)->where('status', '!=', 'trashed');
151 } else {
152 if (!$this->status) {
153 $query->where('status', '!=', 'trashed');
154 } else {
155 $query->where('status', $this->status);
156 }
157 }
158
159 if ($this->search) {
160 $query->where('response', 'LIKE', "%{$this->search}%");
161 }
162
163 return $query->where('form_id', $this->formId);
164 }
165
166 public function groupCount($form_id)
167 {
168 $statuses = $this->responseModel
169 ->select($this->responseModel->raw('status, COUNT(*) as count'))
170 ->where('form_id', $form_id)
171 ->groupBy('status')
172 ->get();
173
174 $counts = [];
175 foreach ($statuses as $status) {
176 $counts[$status->status] = $status->count;
177 }
178
179 $counts['all'] = array_sum($counts);
180 if (isset($counts['trashed'])) {
181 $counts['all'] -= $counts['trashed'];
182 }
183
184 $favorites = wpFluent()
185 ->table('fluentform_submissions')
186 ->where('form_id', $form_id)
187 ->where('is_favourite', 1)
188 ->where('status', '!=', 'trashed')
189 ->count();
190
191 $counts['favourites'] = $favorites;
192
193 return $counts;
194 }
195 }
196