|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin\helpdesk; |
|
4
|
|
|
|
|
5
|
|
|
// controller |
|
6
|
|
|
use App\Http\Controllers\Common\PhpMailController; |
|
7
|
|
|
use App\Http\Controllers\Controller; |
|
8
|
|
|
// request |
|
9
|
|
|
use App\Http\Requests\helpdesk\AgentRequest; |
|
10
|
|
|
use App\Http\Requests\helpdesk\AgentUpdate; |
|
11
|
|
|
// model |
|
12
|
|
|
use App\Model\helpdesk\Agent\Assign_team_agent; |
|
13
|
|
|
use App\Model\helpdesk\Agent\Department; |
|
14
|
|
|
use App\Model\helpdesk\Agent\Groups; |
|
15
|
|
|
use App\Model\helpdesk\Agent\Teams; |
|
16
|
|
|
use App\Model\helpdesk\Utility\CountryCode; |
|
17
|
|
|
use App\Model\helpdesk\Utility\Timezones; |
|
18
|
|
|
use App\User; |
|
19
|
|
|
// classes |
|
20
|
|
|
use DB; |
|
21
|
|
|
use Exception; |
|
22
|
|
|
use GeoIP; |
|
23
|
|
|
use Hash; |
|
24
|
|
|
use Lang; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* AgentController |
|
28
|
|
|
* This controller is used to CRUD Agents. |
|
29
|
|
|
* |
|
30
|
|
|
* @author Ladybird <[email protected]> |
|
31
|
|
|
*/ |
|
32
|
|
|
class AgentController extends Controller |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Create a new controller instance. |
|
36
|
|
|
* constructor to check |
|
37
|
|
|
* 1. authentication |
|
38
|
|
|
* 2. user roles |
|
39
|
|
|
* 3. roles must be agent. |
|
40
|
|
|
* |
|
41
|
|
|
* @return void |
|
|
|
|
|
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(PhpMailController $PhpMailController) |
|
44
|
|
|
{ |
|
45
|
|
|
// creating an instance for the PhpmailController |
|
46
|
|
|
$this->PhpMailController = $PhpMailController; |
|
|
|
|
|
|
47
|
|
|
// checking authentication |
|
48
|
|
|
$this->middleware('auth'); |
|
49
|
|
|
// checking admin roles |
|
50
|
|
|
$this->middleware('roles'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get all agent list page. |
|
55
|
|
|
* |
|
56
|
|
|
* @return type view |
|
57
|
|
|
*/ |
|
58
|
|
|
public function index() |
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
|
|
return view('themes.default1.admin.helpdesk.agent.agents.index'); |
|
62
|
|
|
} catch (Exception $e) { |
|
63
|
|
|
return redirect()->back()->with('fails', $e->getMessage()); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* creating a new agent. |
|
69
|
|
|
* |
|
70
|
|
|
* @param Assign_team_agent $team_assign_agent |
|
|
|
|
|
|
71
|
|
|
* @param Timezones $timezone |
|
72
|
|
|
* @param Groups $group |
|
73
|
|
|
* @param Department $department |
|
74
|
|
|
* @param Teams $team_all |
|
75
|
|
|
* |
|
76
|
|
|
* @return type view |
|
77
|
|
|
*/ |
|
78
|
|
|
public function create(Timezones $timezone, Groups $group, Department $department, Teams $team_all, CountryCode $code) |
|
79
|
|
|
{ |
|
80
|
|
|
try { |
|
81
|
|
|
// gte all the teams |
|
82
|
|
|
$team = $team_all->where('status', '=', 1)->get(); |
|
|
|
|
|
|
83
|
|
|
// get all the timezones |
|
84
|
|
|
$timezones = $timezone->get(); |
|
|
|
|
|
|
85
|
|
|
// get all the groups |
|
86
|
|
|
$groups = $group->where('group_status', '=', 1)->get(); |
|
|
|
|
|
|
87
|
|
|
// get all department |
|
88
|
|
|
$departments = $department->get(); |
|
|
|
|
|
|
89
|
|
|
// list all the teams in a single variable |
|
90
|
|
|
$teams = $team->lists('id', 'name')->toArray(); |
|
91
|
|
|
$location = GeoIP::getLocation(); |
|
92
|
|
|
$phonecode = $code->where('iso', '=', $location->iso_code)->first(); |
|
|
|
|
|
|
93
|
|
|
// returns to the page with all the variables and their datas |
|
94
|
|
|
$send_otp = DB::table('common_settings')->select('status')->where('option_name', '=', 'send_otp')->first(); |
|
95
|
|
|
|
|
96
|
|
|
return view('themes.default1.admin.helpdesk.agent.agents.create', compact('assign', 'teams', 'agents', 'timezones', 'groups', 'departments', 'team', 'send_otp'))->with('phonecode', $phonecode->phonecode); |
|
|
|
|
|
|
97
|
|
|
} catch (Exception $e) { |
|
98
|
|
|
// returns if try fails with exception meaagse |
|
99
|
|
|
return redirect()->back()->with('fails', $e->getMessage()); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* store a new agent. |
|
105
|
|
|
* |
|
106
|
|
|
* @param User $user |
|
107
|
|
|
* @param AgentRequest $request |
|
108
|
|
|
* @param Assign_team_agent $team_assign_agent |
|
|
|
|
|
|
109
|
|
|
* |
|
110
|
|
|
* @return type Response |
|
111
|
|
|
*/ |
|
112
|
|
|
public function store(User $user, AgentRequest $request) |
|
113
|
|
|
{ |
|
114
|
|
|
if ($request->get('country_code') == '' && ($request->get('phone_number') != '' || $request->get('mobile') != '')) { |
|
115
|
|
|
return redirect()->back()->with(['fails2' => Lang::get('lang.country-code-required-error'), 'country_code' => 1])->withInput(); |
|
116
|
|
|
} else { |
|
117
|
|
|
$code = CountryCode::select('phonecode')->where('phonecode', '=', $request->get('country_code'))->get(); |
|
118
|
|
|
if (!count($code)) { |
|
119
|
|
|
return redirect()->back()->with(['fails2' => Lang::get('lang.incorrect-country-code-error'), 'country_code' => 1])->withInput(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
// fixing the user role to agent |
|
123
|
|
|
$user->fill($request->except(['group', 'primary_department', 'agent_time_zone', 'mobile']))->save(); |
|
124
|
|
|
if ($request->get('mobile')) { |
|
125
|
|
|
$user->mobile = $request->get('mobile'); |
|
|
|
|
|
|
126
|
|
|
} else { |
|
127
|
|
|
$user->mobile = null; |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
$user->assign_group = $request->group; |
|
|
|
|
|
|
130
|
|
|
$user->primary_dpt = $request->primary_department; |
|
|
|
|
|
|
131
|
|
|
$user->agent_tzone = $request->agent_time_zone; |
|
|
|
|
|
|
132
|
|
|
// generate password and has immediately to store |
|
133
|
|
|
$password = $this->generateRandomString(); |
|
134
|
|
|
$user->password = Hash::make($password); |
|
|
|
|
|
|
135
|
|
|
// fetching all the team details checked for this user |
|
136
|
|
|
$requests = $request->input('team'); |
|
137
|
|
|
// get user id of the inserted user detail |
|
138
|
|
|
$id = $user->id; |
|
|
|
|
|
|
139
|
|
|
// insert team |
|
140
|
|
|
foreach ($requests as $req) { |
|
|
|
|
|
|
141
|
|
|
// insert all the selected team id to the team and agent relationship table |
|
142
|
|
|
DB::insert('insert into team_assign_agent (team_id, agent_id) values (?,?)', [$req, $id]); |
|
143
|
|
|
} |
|
144
|
|
|
// save user credentails |
|
145
|
|
|
if ($user->save() == true) { |
|
|
|
|
|
|
146
|
|
|
// fetch user credentails to send mail |
|
147
|
|
|
$name = $user->first_name; |
|
|
|
|
|
|
148
|
|
|
$email = $user->email; |
|
|
|
|
|
|
149
|
|
View Code Duplication |
if ($request->input('send_email')) { |
|
|
|
|
|
|
150
|
|
|
try { |
|
151
|
|
|
// send mail on registration |
|
152
|
|
|
$this->PhpMailController->sendmail($from = $this->PhpMailController->mailfrom('1', '0'), $to = ['name' => $name, 'email' => $email], $message = ['subject' => null, 'scenario' => 'registration-notification'], $template_variables = ['user' => $name, 'email_address' => $email, 'user_password' => $password]); |
|
153
|
|
|
} catch (Exception $e) { |
|
154
|
|
|
// returns if try fails |
|
155
|
|
|
return redirect('agents')->with('warning', Lang::get('lang.agent_send_mail_error_on_agent_creation')); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
// returns for the success case |
|
159
|
|
|
if ($request->input('active') == '0' || $request->input('active') == 0) { |
|
160
|
|
|
\Event::fire(new \App\Events\LoginEvent($request)); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return redirect('agents')->with('success', Lang::get('lang.agent_creation_success')); |
|
164
|
|
|
} else { |
|
165
|
|
|
// returns if fails |
|
166
|
|
|
return redirect('agents')->with('fails', Lang::get('lang.failed_to_create_agent')); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Editing a selected agent. |
|
172
|
|
|
* |
|
173
|
|
|
* @param type int $id |
|
174
|
|
|
* @param type User $user |
|
175
|
|
|
* @param type Assign_team_agent $team_assign_agent |
|
176
|
|
|
* @param type Timezones $timezone |
|
177
|
|
|
* @param type Groups $group |
|
178
|
|
|
* @param type Department $department |
|
179
|
|
|
* @param type Teams $team |
|
180
|
|
|
* |
|
181
|
|
|
* @return type Response |
|
182
|
|
|
*/ |
|
183
|
|
|
public function edit($id, User $user, Assign_team_agent $team_assign_agent, Timezones $timezone, Groups $group, Department $department, Teams $team, CountryCode $code) |
|
184
|
|
|
{ |
|
185
|
|
|
try { |
|
186
|
|
|
$location = GeoIP::getLocation(); |
|
187
|
|
|
$phonecode = $code->where('iso', '=', $location->iso_code)->first(); |
|
|
|
|
|
|
188
|
|
|
$user = $user->whereId($id)->first(); |
|
|
|
|
|
|
189
|
|
|
$team = $team->where('status', '=', 1)->get(); |
|
|
|
|
|
|
190
|
|
|
$teams1 = $team->lists('name', 'id'); |
|
191
|
|
|
$timezones = $timezone->get(); |
|
|
|
|
|
|
192
|
|
|
$groups = $group->where('group_status', '=', 1)->get(); |
|
|
|
|
|
|
193
|
|
|
$departments = $department->get(); |
|
|
|
|
|
|
194
|
|
|
$table = $team_assign_agent->where('agent_id', $id)->first(); |
|
|
|
|
|
|
195
|
|
|
$teams = $team->lists('id', 'name')->toArray(); |
|
196
|
|
|
$assign = $team_assign_agent->where('agent_id', $id)->lists('team_id')->toArray(); |
|
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
return view('themes.default1.admin.helpdesk.agent.agents.edit', compact('teams', 'assign', 'table', 'teams1', 'selectedTeams', 'user', 'timezones', 'groups', 'departments', 'team', 'exp', 'counted'))->with('phonecode', $phonecode->phonecode); |
|
|
|
|
|
|
199
|
|
|
} catch (Exception $e) { |
|
200
|
|
|
return redirect('agents')->with('fail', Lang::get('lang.failed_to_edit_agent')); |
|
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Update the specified agent in storage. |
|
206
|
|
|
* |
|
207
|
|
|
* @param type int $id |
|
208
|
|
|
* @param type User $user |
|
209
|
|
|
* @param type AgentUpdate $request |
|
210
|
|
|
* @param type Assign_team_agent $team_assign_agent |
|
211
|
|
|
* |
|
212
|
|
|
* @return type Response |
|
213
|
|
|
*/ |
|
214
|
|
|
public function update($id, User $user, AgentUpdate $request, Assign_team_agent $team_assign_agent) |
|
215
|
|
|
{ |
|
216
|
|
|
if ($request->get('country_code') == '' && ($request->get('phone_number') != '' || $request->get('mobile') != '')) { |
|
217
|
|
|
return redirect()->back()->with(['fails2' => Lang::get('lang.country-code-required-error'), 'country_code' => 1])->withInput(); |
|
218
|
|
|
} else { |
|
219
|
|
|
$code = CountryCode::select('phonecode')->where('phonecode', '=', $request->get('country_code'))->get(); |
|
220
|
|
|
if (!count($code)) { |
|
221
|
|
|
return redirect()->back()->with(['fails2' => Lang::get('lang.incorrect-country-code-error'), 'country_code' => 1])->withInput(); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
// storing all the details |
|
225
|
|
|
$user = $user->whereId($id)->first(); |
|
|
|
|
|
|
226
|
|
|
$daylight_save = $request->input('daylight_save'); |
|
|
|
|
|
|
227
|
|
|
$limit_access = $request->input('limit_access'); |
|
|
|
|
|
|
228
|
|
|
$directory_listing = $request->input('directory_listing'); |
|
|
|
|
|
|
229
|
|
|
$vocation_mode = $request->input('vocation_mode'); |
|
|
|
|
|
|
230
|
|
|
//============================================== |
|
231
|
|
|
$table = $team_assign_agent->where('agent_id', $id); |
|
|
|
|
|
|
232
|
|
|
$table->delete(); |
|
233
|
|
|
$requests = $request->input('team'); |
|
234
|
|
|
// inserting team details |
|
235
|
|
|
foreach ($requests as $req) { |
|
|
|
|
|
|
236
|
|
|
DB::insert('insert into team_assign_agent (team_id, agent_id) values (?,?)', [$req, $id]); |
|
237
|
|
|
} |
|
238
|
|
|
//Todo For success and failure conditions |
|
239
|
|
|
try { |
|
240
|
|
|
if ($request->input('country_code') != '' or $request->input('country_code') != null) { |
|
241
|
|
|
$user->country_code = $request->input('country_code'); |
|
242
|
|
|
} |
|
243
|
|
|
$user->mobile = ($request->input('mobile') == '') ? null : $request->input('mobile'); |
|
244
|
|
|
$user->fill($request->except('daylight_save', 'limit_access', 'directory_listing', 'vocation_mode', 'assign_team', 'mobile')); |
|
245
|
|
|
$user->assign_group = $request->group; |
|
|
|
|
|
|
246
|
|
|
$user->primary_dpt = $request->primary_department; |
|
|
|
|
|
|
247
|
|
|
$user->agent_tzone = $request->agent_time_zone; |
|
|
|
|
|
|
248
|
|
|
$user->save(); |
|
249
|
|
|
|
|
250
|
|
|
return redirect('agents')->with('success', Lang::get('lang.agent_updated_sucessfully')); |
|
251
|
|
|
} catch (Exception $e) { |
|
252
|
|
|
return redirect('agents')->with('fails', Lang::get('lang.unable_to_update_agent').'<li>'.$e->errorInfo[2].'</li>'); |
|
|
|
|
|
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Remove the specified agent from storage. |
|
258
|
|
|
* |
|
259
|
|
|
* @param type $id |
|
260
|
|
|
* @param User $user |
|
261
|
|
|
* @param Assign_team_agent $team_assign_agent |
|
262
|
|
|
* |
|
263
|
|
|
* @throws Exception |
|
264
|
|
|
* |
|
265
|
|
|
* @return type Response |
|
266
|
|
|
*/ |
|
267
|
|
|
public function destroy($id, User $user, Assign_team_agent $team_assign_agent) |
|
268
|
|
|
{ |
|
269
|
|
|
/* Becouse of foreign key we delete team_assign_agent first */ |
|
270
|
|
|
error_reporting(E_ALL & ~E_NOTICE); |
|
271
|
|
|
$team_assign_agent = $team_assign_agent->where('agent_id', $id); |
|
|
|
|
|
|
272
|
|
|
$team_assign_agent->delete(); |
|
273
|
|
|
$user = $user->whereId($id)->first(); |
|
|
|
|
|
|
274
|
|
|
try { |
|
275
|
|
|
$error = Lang::get('lang.this_staff_is_related_to_some_tickets'); |
|
276
|
|
|
$user->id; |
|
277
|
|
|
$user->delete(); |
|
278
|
|
|
throw new \Exception($error); |
|
279
|
|
|
return redirect('agents')->with('success', Lang::get('lang.agent_deleted_sucessfully')); |
|
|
|
|
|
|
280
|
|
|
} catch (\Exception $e) { |
|
281
|
|
|
return redirect('agents')->with('fails', $error); |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Generate a random string for password. |
|
287
|
|
|
* |
|
288
|
|
|
* @param type $length |
|
289
|
|
|
* |
|
290
|
|
|
* @return string |
|
291
|
|
|
*/ |
|
292
|
|
View Code Duplication |
public function generateRandomString($length = 10) |
|
|
|
|
|
|
293
|
|
|
{ |
|
294
|
|
|
// list of supported characters |
|
295
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
296
|
|
|
// character length checked |
|
297
|
|
|
$charactersLength = strlen($characters); |
|
298
|
|
|
// creating an empty variable for random string |
|
299
|
|
|
$randomString = ''; |
|
300
|
|
|
// fetching random string |
|
301
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
302
|
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
|
303
|
|
|
} |
|
304
|
|
|
// return random string |
|
305
|
|
|
return $randomString; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.