|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controllers\Admin\User\Traits; |
|
4
|
|
|
|
|
5
|
|
|
trait Search |
|
6
|
|
|
{ |
|
7
|
|
|
public function search() |
|
8
|
|
|
{ |
|
9
|
|
|
$gets = $this->request->gets(); |
|
10
|
|
|
|
|
11
|
|
|
if (empty($gets)) { |
|
12
|
|
|
$users = $this->load->model('User')->users(); |
|
13
|
|
|
$usersformatted = $this->formatUsers($users); |
|
|
|
|
|
|
14
|
|
|
return json_encode($usersformatted); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
$gender = $gets['gender'] ?? null; |
|
18
|
|
|
$zip = $gets['zip'] ?? null; |
|
19
|
|
|
$country = $gets['country'] ?? null; |
|
20
|
|
|
$registration_from = $gets['registration_from'] ?? null; |
|
21
|
|
|
$registration_to = $gets['registration_to'] ?? null; |
|
22
|
|
|
$active = $gets['active'] ?? null; |
|
23
|
|
|
$pending = $gets['pending'] ?? null; |
|
24
|
|
|
$inactive = $gets['inactive'] ?? null; |
|
25
|
|
|
$online = $gets['online'] ?? null; |
|
26
|
|
|
$offline = $gets['offline'] ?? null; |
|
27
|
|
|
|
|
28
|
|
|
$sql = ''; |
|
29
|
|
|
$wheres = []; |
|
30
|
|
|
|
|
31
|
|
|
list($sql, $wheres) = $this->active($active, $sql, $wheres); |
|
32
|
|
|
list($sql, $wheres) = $this->pending($pending, $sql, $wheres); |
|
33
|
|
|
list($sql, $wheres) = $this->inactive($inactive, $sql, $wheres); |
|
34
|
|
|
$sql = $this->formatStatus($sql); |
|
35
|
|
|
list($sql, $wheres) = $this->online($online, $sql, $wheres); |
|
36
|
|
|
list($sql, $wheres) = $this->offline($offline, $sql, $wheres); |
|
37
|
|
|
$sql = $this->formatLogin($sql); |
|
38
|
|
|
list($sql, $wheres) = $this->gender($gender, $sql, $wheres); |
|
39
|
|
|
list($sql, $wheres) = $this->zip($zip, $sql, $wheres); |
|
40
|
|
|
list($sql, $wheres) = $this->country($country, $sql, $wheres); |
|
41
|
|
|
list($sql, $wheres) = $this->registration($registration_from, $registration_to, $sql, $wheres); |
|
42
|
|
|
|
|
43
|
|
|
$sql = $this->formatSql($sql); |
|
44
|
|
|
|
|
45
|
|
|
if ($sql == '') { |
|
46
|
|
|
$users = $this->load->model('User')->users(); |
|
47
|
|
|
$usersformatted = $this->formatUsers($users); |
|
48
|
|
|
return json_encode($usersformatted); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$users = $this->load->model('User')->filter($sql, $wheres); |
|
52
|
|
|
|
|
53
|
|
|
$msg = null; |
|
54
|
|
|
if (!$users) { |
|
55
|
|
|
$msg = 'no users'; |
|
56
|
|
|
return json_encode($msg); |
|
57
|
|
|
} |
|
58
|
|
|
$msg = $this->formatUsers($users); |
|
59
|
|
|
return json_encode($msg); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function active($active, $sql, $wheres) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($active && $active == '1') { |
|
65
|
|
|
$sql .= 'status = ? AND '; |
|
66
|
|
|
array_push($wheres, '2'); |
|
67
|
|
|
} |
|
68
|
|
|
return [$sql, $wheres]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function pending($pending, $sql, $wheres) |
|
72
|
|
|
{ |
|
73
|
|
|
if ($pending && $pending == '1') { |
|
74
|
|
|
$sql .= 'status = ? AND '; |
|
75
|
|
|
array_push($wheres, '1'); |
|
76
|
|
|
} |
|
77
|
|
|
return [$sql, $wheres]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function inactive($inactive, $sql, $wheres) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($inactive && $inactive == '1') { |
|
83
|
|
|
$sql .= 'status = ? AND '; |
|
84
|
|
|
array_push($wheres, '0'); |
|
85
|
|
|
} |
|
86
|
|
|
return [$sql, $wheres]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function formatStatus($sql) |
|
90
|
|
|
{ |
|
91
|
|
|
$count_status = substr_count($sql, 'status = ?'); |
|
92
|
|
|
|
|
93
|
|
|
if ($count_status > 1) { |
|
94
|
|
|
$sql = str_replace('status = ? AND', 'status = ? OR', $sql); |
|
95
|
|
|
$sql = rtrim($sql, 'OR '); |
|
96
|
|
|
$sql = "( $sql )"; |
|
97
|
|
|
$sql .= ' AND '; |
|
98
|
|
|
} |
|
99
|
|
|
return $sql; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function online($online, $sql, $wheres) |
|
103
|
|
|
{ |
|
104
|
|
|
if ($online && $online == '1') { |
|
105
|
|
|
$sql .= 'is_login = ? AND '; |
|
106
|
|
|
array_push($wheres, '1'); |
|
107
|
|
|
} |
|
108
|
|
|
return [$sql, $wheres]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
private function offline($offline, $sql, $wheres) |
|
112
|
|
|
{ |
|
113
|
|
|
if ($offline && $offline == '1') { |
|
114
|
|
|
$sql .= 'is_login = ? AND '; |
|
115
|
|
|
array_push($wheres, '0'); |
|
116
|
|
|
} |
|
117
|
|
|
return [$sql, $wheres]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private function formatLogin($sql) |
|
121
|
|
|
{ |
|
122
|
|
|
$count_is_login = substr_count($sql, 'is_login = ?'); |
|
123
|
|
|
|
|
124
|
|
|
if ($count_is_login > 1) { |
|
125
|
|
|
$sql = str_replace('is_login = ? AND', 'is_login = ? OR', $sql); |
|
126
|
|
|
$sql = rtrim($sql, 'OR '); |
|
127
|
|
|
$sql = "( $sql )"; |
|
128
|
|
|
$sql .= ' AND '; |
|
129
|
|
|
} |
|
130
|
|
|
return $sql; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
private function gender($gender, $sql, $wheres) { |
|
134
|
|
|
if ($gender) { |
|
135
|
|
|
$sql .= 'gender = ? AND '; |
|
136
|
|
|
array_push($wheres, $gender); |
|
137
|
|
|
} |
|
138
|
|
|
return [$sql, $wheres]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
private function zip($zip, $sql, $wheres) { |
|
142
|
|
|
if ($zip) { |
|
143
|
|
|
$sql .= 'zip = ? AND '; |
|
144
|
|
|
array_push($wheres, $zip); |
|
145
|
|
|
} |
|
146
|
|
|
return [$sql, $wheres]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
private function country($country, $sql, $wheres) { |
|
150
|
|
|
if ($country) { |
|
151
|
|
|
$sql .= 'country = ? AND '; |
|
152
|
|
|
array_push($wheres, $country); |
|
153
|
|
|
} |
|
154
|
|
|
return [$sql, $wheres]; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
private function registration($registration_from, $registration_to, $sql, $wheres) { |
|
158
|
|
|
if ($registration_from) { |
|
159
|
|
|
$registration_from = date("Y-m-d", strtotime($registration_from)); |
|
160
|
|
|
|
|
161
|
|
|
if (!$registration_to) { |
|
162
|
|
|
$sql .= 'registration >= ? AND '; |
|
163
|
|
|
array_push($wheres, $registration_from); |
|
164
|
|
|
} else { |
|
165
|
|
|
$registration_to = date("Y-m-d", strtotime($registration_to)); |
|
166
|
|
|
|
|
167
|
|
|
$sql .= 'registration BETWEEN ? AND ? AND '; |
|
168
|
|
|
array_push($wheres, $registration_from); |
|
169
|
|
|
array_push($wheres, $registration_to); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
return [$sql, $wheres]; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
private function formatSql($sql) |
|
176
|
|
|
{ |
|
177
|
|
|
return $sql ? substr($sql, 0, -4) : $sql; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|