1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\ProviderCreateRequest; |
6
|
|
|
use App\Http\Requests\ProviderUpdateRequest; |
7
|
|
|
use App\Repositories\ProviderRepository; |
8
|
|
|
use App\Validators\ProviderValidator; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
|
11
|
|
|
class ProvidersController extends Controller |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ProviderRepository |
15
|
|
|
*/ |
16
|
|
|
protected $repository; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ProviderValidator |
20
|
|
|
*/ |
21
|
|
|
protected $validator; |
22
|
|
|
|
23
|
|
|
public function __construct(ProviderRepository $repository, ProviderValidator $validator) |
24
|
|
|
{ |
25
|
|
|
$this->repository = $repository; |
26
|
|
|
$this->validator = $validator; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Display a listing of the resource. |
31
|
|
|
* |
32
|
|
|
* @return \Illuminate\Http\Response |
33
|
|
|
*/ |
34
|
|
|
public function index() |
35
|
|
|
{ |
36
|
|
|
$this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria')); |
37
|
|
|
$providers = $this->repository->all(); |
38
|
|
|
|
39
|
|
|
if (request()->wantsJson()) { |
40
|
|
|
return response()->json([ |
41
|
|
|
'data' => $providers, |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$providers = Country::paginate(5); |
46
|
|
|
|
47
|
|
|
return view('manteniments/providers/index', ['providers' => $providers]); |
48
|
|
|
|
49
|
|
|
//return view('providers.index', compact('providers')); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Store a newly created resource in storage. |
54
|
|
|
* |
55
|
|
|
* @param ProviderCreateRequest $request |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Http\Response |
58
|
|
|
*/ |
59
|
|
|
public function store(ProviderCreateRequest $request) |
60
|
|
|
{ |
61
|
|
|
return view('manteniments/providers/create'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Display the specified resource. |
66
|
|
|
* |
67
|
|
|
* @param int $id |
68
|
|
|
* |
69
|
|
|
* @return \Illuminate\Http\Response |
70
|
|
|
*/ |
71
|
|
|
public function show($id) |
72
|
|
|
{ |
73
|
|
|
$this->validateInput($request); |
|
|
|
|
74
|
|
|
Country::create([ |
75
|
|
|
'name' => $request['name'], |
76
|
|
|
'shortName' => $request['shortName'], |
77
|
|
|
'description' => $request['description'], |
78
|
|
|
'date_entrance' => $request['date_entrance'], |
79
|
|
|
'last_update' => $request['last_update'], |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
return redirect()->intended('manteniments/providers'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Show the form for editing the specified resource. |
87
|
|
|
* |
88
|
|
|
* @param int $id |
89
|
|
|
* |
90
|
|
|
* @return \Illuminate\Http\Response |
91
|
|
|
*/ |
92
|
|
|
public function edit($id) |
93
|
|
|
{ |
94
|
|
|
$providers = Providers::find($id); |
95
|
|
|
// Redirect to country list if updating country wasn't existed |
96
|
|
|
if ($providers == null || count($providers) == 0) { |
97
|
|
|
return redirect()->intended('/manteniments/providers'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return view('manteniments/providers/edit', ['providers' => $providers]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Update the specified resource in storage. |
105
|
|
|
* |
106
|
|
|
* @param ProviderUpdateRequest $request |
107
|
|
|
* @param string $id |
108
|
|
|
* |
109
|
|
|
* @return Response |
110
|
|
|
*/ |
111
|
|
|
public function update(ProviderUpdateRequest $request, $id) |
112
|
|
|
{ |
113
|
|
|
$providers = Providers::findOrFail($id); |
|
|
|
|
114
|
|
|
$input = [ |
115
|
|
|
'name' => $request['name'], |
116
|
|
|
'shortName' => $request['shortName'], |
117
|
|
|
'description' => $request['description'], |
118
|
|
|
'date_entrance' => $request['date_entrance'], |
119
|
|
|
'last_update' => $request['last_update'], |
120
|
|
|
]; |
121
|
|
|
$this->validate($request, [ |
122
|
|
|
'name' => 'required|max:60', |
123
|
|
|
]); |
124
|
|
|
Providers::where('id', $id) |
125
|
|
|
->update($input); |
126
|
|
|
|
127
|
|
|
return redirect()->intended('manteniments/providers'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Remove the specified resource from storage. |
132
|
|
|
* |
133
|
|
|
* @param int $id |
134
|
|
|
* |
135
|
|
|
* @return \Illuminate\Http\Response |
136
|
|
|
*/ |
137
|
|
|
public function destroy($id) |
138
|
|
|
{ |
139
|
|
|
Providers::where('id', $id)->delete(); |
140
|
|
|
|
141
|
|
|
return redirect()->intended('manteniments/providers'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function search(Request $request) |
145
|
|
|
{ |
146
|
|
|
$constraints = [ |
147
|
|
|
'name' => $request['name'], |
148
|
|
|
'shortName' => $request['shortName'], |
149
|
|
|
]; |
150
|
|
|
$providers = $this->doSearchingQuery($constraints); |
151
|
|
|
|
152
|
|
|
return view('manteniments/providers/index', ['providers' => $providers, 'searchingVals' => $constraints]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
private function doSearchingQuery($constraints) |
156
|
|
|
{ |
157
|
|
|
$query = providers::query(); |
158
|
|
|
$fields = array_keys($constraints); |
159
|
|
|
$index = 0; |
160
|
|
|
foreach ($constraints as $constraint) { |
161
|
|
|
if ($constraint != null) { |
162
|
|
|
$query = $query->where($fields[$index], 'like', '%'.$constraint.'%'); |
163
|
|
|
} |
164
|
|
|
$index++; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $query->paginate(5); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
private function validateInput($request) |
171
|
|
|
{ |
172
|
|
|
$this->validate($request, [ |
173
|
|
|
'name' => 'required|max:60|unique:providers', |
174
|
|
|
'shortName' => 'required|max:6|unique:providers', |
175
|
|
|
]); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.