Completed
Push — development ( 2be1cb...034e49 )
by Ashutosh
11:52
created

AdvanceSearchController::getUsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Controllers\User;
4
5
use App\Http\Controllers\Controller;
6
use App\User;
7
8
class AdvanceSearchController extends Controller
9
{
10
    /**
11
     * Serach for Mobile,Email,Country.
12
     */
13
    public function getMobEmCoun($join, $mobile, $email, $country)
14
    {
15
        if ($mobile) {
16
            $join = $join->where('mobile', $mobile);
17
        }
18
        if ($email) {
19
            $join = $join->where('email', 'LIKE', '%'.$email.'%');
20
        }
21
        if ($country) {
22
            $join = $join->where('country', $country);
23
        }
24
25
        return $join;
26
    }
27
28
    /**
29
     * Serach for industry,company_type,company_size.
30
     */
31
    public function getInCtCs($join, $industry, $company_type, $company_size)
32
    {
33
        if ($industry) {
34
            $join = $join->where('bussiness', $industry);
35
        }
36
        if ($company_type) {
37
            $join = $join->where('company_type', $company_type);
38
        }
39
        if ($company_size) {
40
            $join = $join->where('company_size', $company_size);
41
        }
42
43
        return $join;
44
    }
45
46
    /**
47
     * Serach for Role,Position.
48
     */
49
    public function getRolPos($join, $role, $position)
50
    {
51
        if ($role) {
52
            $join = $join->where('role', $role);
53
        }
54
        if ($position) {
55
            $join = $join->where('position', $position);
56
        }
57
58
        return $join;
59
    }
60
61
    /**
62
     * Serach for Registered From,tILL.
63
     */
64
    public function getregFromTill($join, $reg_from, $reg_till)
65
    {
66
        if ($reg_from) {
67
            $fromdate = date_create($reg_from);
68
69
            $from = date_format($fromdate, 'Y-m-d H:m:i');
70
            $tills = date('Y-m-d H:m:i');
71
            $cont = new \App\Http\Controllers\Order\ExtendedOrderController();
72
            $tillDate = $cont->getTillDate($from, $reg_till, $tills);
73
            $join = $join->whereBetween('created_at', [$from, $tillDate]);
74
        }
75
        if ($reg_till) {
76
            $tilldate = date_create($reg_till);
77
            $till = date_format($tilldate, 'Y-m-d H:m:i');
78
            $froms = User::first()->created_at;
79
            $cont = new \App\Http\Controllers\Order\ExtendedOrderController();
80
            $fromDate = $cont->getFromDate($reg_from, $froms);
81
            $join = $join->whereBetween('created_at', [$fromDate, $till]);
82
        }
83
84
        return $join;
85
    }
86
87
    /**
88
     * Serach for Name,UserName,Company.
89
     */
90
    public function getNamUserCom($join, $name, $username, $company)
91
    {
92
        if ($name) {
93
            $join = $join->where('first_name', 'LIKE', '%'.$name.'%')
94
                    ->orWhere('last_name', 'LIKE', '%'.$name.'%');
95
        }
96
        if ($username) {
97
            $join = $join->where('user_name', 'LIKE', '%'.$username.'%');
98
        }
99
        if ($company) {
100
            $join = $join->where('company', 'LIKE', '%'.$company.'%');
101
        }
102
103
        return $join;
104
    }
105
106
        public function advanceSearch(
107
        $name = '',
108
        $username = '',
109
        $company = '',
110
        $mobile = '',
111
        $email = '',
112
        $country = '',
113
        $industry = '',
114
        $company_type = '',
115
        $company_size = '',
116
        $role = '',
117
        $position = '',
118
        $reg_from = '',
119
        $reg_till = ''
120
    ) {
121
        $join = \DB::table('users');
122
        $join = $this->getNamUserCom($join, $name, $username, $company);
123
        $join = $this->getMobEmCoun($join, $mobile, $email, $country);
124
        $join = $this->getInCtCs($join, $industry, $company_type, $company_size);
125
        $join = $this->getRolPos($join, $role, $position);
126
        $join = $this->getregFromTill($join, $reg_from, $reg_till);
127
128
        $join = $join->orderBy('created_at', 'desc')
129
        ->select(
130
            'id',
131
            'first_name',
132
            'last_name',
133
            'email',
134
            'created_at',
135
            'active',
136
            'mobile_verified',
137
            'role',
138
            'position'
139
        );
140
141
        return $join;
142
    }
143
144
145
    public function search(Request $request)
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\User\Request was not found. Did you mean Request? If so, make sure to prefix the type with \.
Loading history...
146
    {
147
        try {
148
            $term = trim($request->q);
149
            if (empty($term)) {
150
                return \Response::json([]);
151
            }
152
            $users = User::where('email', 'LIKE', '%'.$term.'%')
153
             ->orWhere('first_name', 'LIKE', '%'.$term.'%')
154
             ->orWhere('last_name', 'LIKE', '%'.$term.'%')
155
             ->select('id', 'email', 'profile_pic', 'first_name', 'last_name')->get();
156
            $formatted_tags = [];
157
158
            foreach ($users as $user) {
159
                $formatted_users[] = ['id'     => $user->id, 'text' => $user->email, 'profile_pic' => $user->profile_pic,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
160
                'first_name'                   => $user->first_name, 'last_name' => $user->last_name, ];
161
            }
162
163
            return \Response::json($formatted_users);
164
        } catch (\Exception $e) {
165
            // returns if try fails with exception meaagse
166
            return redirect()->back()->with('fails', $e->getMessage());
167
        }
168
    }
169
170
    public function getUsers(Request $request)
171
    {
172
        $options = $this->user
173
                ->select('email AS text', 'id AS value')
174
                ->get();
175
176
        return response()->json(compact('options'));
177
    }
178
}
179