1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Itil\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Itil\Controllers\BaseServiceDeskController; |
6
|
|
|
use Exception; |
7
|
|
|
use App\Itil\Models\Common\Cab; |
8
|
|
|
use Datatable; |
9
|
|
|
use App\User; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use App\Itil\Model\Common\CabVote; |
12
|
|
|
|
13
|
|
View Code Duplication |
class CabController extends BaseServiceDeskController { |
|
|
|
|
14
|
|
|
|
15
|
|
|
public function __construct() { |
16
|
|
|
$this->middleware('auth'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function index() { |
20
|
|
|
try { |
21
|
|
|
return view('itil::cab.index'); |
22
|
|
|
} catch (Exception $ex) { |
23
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function getCab() { |
28
|
|
|
$cabs = new Cab(); |
29
|
|
|
return Datatable::Collection($cabs->select('name', 'head', 'id')->get()) |
|
|
|
|
30
|
|
|
->showColumns('name') |
31
|
|
|
->addColumn('head', function($model) { |
32
|
|
|
$users = new User(); |
33
|
|
|
$head = "--"; |
34
|
|
|
$user = $users->find($model->head); |
|
|
|
|
35
|
|
|
if ($user) { |
36
|
|
|
$head = $user->email; |
37
|
|
|
} |
38
|
|
|
return $head; |
39
|
|
|
}) |
40
|
|
|
->addColumn('action', function($model) { |
41
|
|
|
return "<a href=" . url('service-desk/cabs/' . $model->id . '/edit') . " class='btn btn-info'>Edit</a>"; |
42
|
|
|
}) |
43
|
|
|
->orderColumns('name', 'head') |
44
|
|
|
->searchColumns('name', 'head') |
45
|
|
|
->make(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function edit($id) { |
49
|
|
|
try { |
50
|
|
|
$cabs = new Cab(); |
51
|
|
|
$agents = User::where('role', 'agent')->orWhere('role', 'admin')->lists('email', 'id')->toArray(); |
52
|
|
|
$cab = $cabs->find($id); |
|
|
|
|
53
|
|
|
return view('itil::cab.edit', compact('cab', 'agents')); |
54
|
|
|
} catch (Exception $ex) { |
55
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function create() { |
60
|
|
|
try { |
61
|
|
|
$agents = User::where('role', 'agent')->orWhere('role', 'admin')->lists('email', 'id')->toArray(); |
62
|
|
|
return view('itil::cab.create', compact('agents')); |
63
|
|
|
} catch (Exception $ex) { |
64
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function store(Request $request) { |
69
|
|
|
$this->validate($request, [ |
70
|
|
|
'name' => 'required', |
71
|
|
|
]); |
72
|
|
|
try { |
73
|
|
|
$cabs = new Cab(); |
74
|
|
|
$cabs->fill($request->input())->save(); |
|
|
|
|
75
|
|
|
return redirect()->back()->with('success', 'Saved Successfully'); |
76
|
|
|
} catch (Exception $ex) { |
77
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function update($id, Request $request) { |
82
|
|
|
$this->validate($request, [ |
83
|
|
|
'name' => 'required', |
84
|
|
|
]); |
85
|
|
|
try { |
86
|
|
|
$cabs = new Cab(); |
87
|
|
|
$cab = $cabs->find($id); |
|
|
|
|
88
|
|
|
$cab->fill($request->input())->save(); |
89
|
|
|
return redirect()->back()->with('success', 'Saved Successfully'); |
90
|
|
|
} catch (Exception $ex) { |
91
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function vote($cabid, $owner) { |
96
|
|
|
try { |
97
|
|
|
$check = \App\Itil\Controllers\UtilityController::checkCabUser($cabid); |
98
|
|
|
if (!$check) { |
99
|
|
|
return redirect('/service-desk/problems')->with('fails', 'Unautherized'); |
100
|
|
|
} |
101
|
|
|
$authid = \Auth::user()->id; |
102
|
|
|
$votes = new CabVote(); |
103
|
|
|
$vote = $votes->where('cab_id', $cabid)->where('user_id', $authid)->where('owner', $owner)->first(); |
104
|
|
|
if ($vote) { |
105
|
|
|
return redirect('/service-desk/problems')->with('fails', 'Already voted'); |
106
|
|
|
} |
107
|
|
|
return view('itil::cab.vote', compact('cabid', 'owner')); |
108
|
|
|
} catch (Exception $ex) { |
109
|
|
|
dd($ex); |
110
|
|
|
return redirect('/service-desk/problems')->with('fails', $ex->getMessage()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function postVote($cabid, $owner, Request $request) { |
115
|
|
|
try { |
116
|
|
|
$check = \App\Itil\Controllers\UtilityController::checkCabUser($cabid); |
117
|
|
|
if (!$check) { |
118
|
|
|
return redirect('/service-desk/problems')->with('fails', 'Unautherized'); |
119
|
|
|
} |
120
|
|
|
$authid = \Auth::user()->id; |
121
|
|
|
$votes = new CabVote(); |
122
|
|
|
$vote = $votes->where('cab_id', $cabid)->where('user_id', $authid)->where('owner', $owner)->first(); |
123
|
|
|
if ($vote) { |
124
|
|
|
return redirect('/service-desk/problems')->with('fails', 'Already voted'); |
125
|
|
|
} |
126
|
|
|
$votes->create([ |
127
|
|
|
'cab_id' => $cabid, |
128
|
|
|
'user_id' => $authid, |
129
|
|
|
'comment' => $request->input('comment'), |
130
|
|
|
'vote' => $request->input('vote'), |
131
|
|
|
'owner' => $owner, |
132
|
|
|
]); |
133
|
|
|
return redirect('/service-desk/problems')->with('success', 'Voted successfully'); |
134
|
|
|
} catch (Exception $ex) { |
135
|
|
|
return redirect('/service-desk/problems')->with('fails', $ex->getMessage()); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function showVotes($id, $owner) { |
140
|
|
|
try { |
141
|
|
|
$vote = new CabVote(); |
142
|
|
|
$votes = $vote->where('cab_id', $id)->where('owner', $owner)->get(); |
143
|
|
|
return view('itil::cab.show-vote', compact('votes')); |
144
|
|
|
} catch (Exception $ex) { |
145
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public static function votedUser($userid) { |
150
|
|
|
$users = new \App\User(); |
151
|
|
|
$user = $users->find($userid); |
|
|
|
|
152
|
|
|
$name = ""; |
153
|
|
|
if ($user){ |
154
|
|
|
$name = $user->first_name." ".$user->last_name; |
155
|
|
|
if($name==" "){ |
156
|
|
|
$name = $user->user_name; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
return ucfirst($name); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public static function checkVote($vote){ |
163
|
|
|
$value = ""; |
|
|
|
|
164
|
|
|
if($vote==1){ |
165
|
|
|
$value = "Voted for proceed"; |
166
|
|
|
}else{ |
167
|
|
|
$value = "Voted for not proceed"; |
168
|
|
|
} |
169
|
|
|
return $value; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.