Passed
Branch master (7fff21)
by refat
05:12
created

SearchTrait   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 89
dl 0
loc 160
rs 9.68
c 0
b 0
f 0
wmc 34

13 Methods

Rating   Name   Duplication   Size   Complexity  
A online() 0 7 3
A gender() 0 6 2
A registration() 0 16 3
A formatStatus() 0 11 2
A active() 0 7 3
A generateSql() 0 18 2
A zip() 0 6 2
A offline() 0 7 3
A search() 0 25 4
A inactive() 0 7 3
A pending() 0 7 3
A formatLogin() 0 11 2
A country() 0 6 2
1
<?php
2
3
namespace App\Controllers\Admin\User\Traits;
4
5
trait SearchTrait
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);
0 ignored issues
show
Bug introduced by
It seems like formatUsers() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

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