1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WITR\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use WITR\Http\Requests\Admin\DJ as Requests; |
6
|
|
|
use WITR\Http\Controllers\Controller; |
7
|
|
|
use WITR\DJ; |
8
|
|
|
use WITR\TimeSlot; |
9
|
|
|
use Input; |
10
|
|
|
use File; |
11
|
|
|
use Hash; |
12
|
|
|
|
13
|
|
|
use Illuminate\Http\Request; |
14
|
|
|
use Illuminate\Contracts\Auth\PasswordBroker; |
15
|
|
|
|
16
|
|
|
class DJController extends Controller { |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->middleware('auth'); |
21
|
|
|
$this->middleware('admin'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Display a listing of the djs. |
26
|
|
|
* |
27
|
|
|
* @return Response |
28
|
|
|
*/ |
29
|
|
|
public function index() |
30
|
|
|
{ |
31
|
|
|
$djs = DJ::orderBy('name', 'asc')->get(); |
32
|
|
|
return view('admin.djs.index', ['djs' => $djs]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function create() |
36
|
|
|
{ |
37
|
|
|
return view('admin.djs.create'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Show the form for creating a new resource. |
42
|
|
|
* |
43
|
|
|
* @return Response |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function store(Requests\CreateRequest $request) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$dj = new DJ($request->all()); |
48
|
|
|
|
49
|
|
|
if($request->hasFile('picture')) |
50
|
|
|
{ |
51
|
|
|
$file = $request->file('picture'); |
52
|
|
|
$dj->uploadFile('picture', $file); |
53
|
|
|
} |
54
|
|
|
else |
55
|
|
|
{ |
56
|
|
|
$dj->picture = 'default.jpg'; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$dj->save(); |
60
|
|
|
|
61
|
|
|
return redirect()->route('admin.djs.index') |
62
|
|
|
->with('success', 'DJ Created!'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Show the form for editing the specified resource. |
67
|
|
|
* |
68
|
|
|
* @param int $id |
69
|
|
|
* @return Response |
70
|
|
|
*/ |
71
|
|
|
public function edit($id) |
72
|
|
|
{ |
73
|
|
|
$dj = DJ::findOrFail($id); |
74
|
|
|
|
75
|
|
|
return view('admin.djs.edit', ['dj' => $dj]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Update the specified resource in storage. |
80
|
|
|
* |
81
|
|
|
* @param int $id |
82
|
|
|
* @return Response |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function update(Requests\UpdateRequest $request, $id) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$dj = DJ::findOrFail($id); |
87
|
|
|
$dj->fill($request->except(['picture'])); |
88
|
|
|
|
89
|
|
|
if($request->hasFile('picture')) |
90
|
|
|
{ |
91
|
|
|
$file = $request->file('picture'); |
92
|
|
|
$dj->uploadFile('picture', $file); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$dj->save(); |
96
|
|
|
return redirect()->route('admin.djs.index') |
97
|
|
|
->with('success', 'DJ Saved!'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Remove the specified resource from storage. |
102
|
|
|
* |
103
|
|
|
* @param int $id |
104
|
|
|
* @return Response |
105
|
|
|
*/ |
106
|
|
|
public function delete($id) |
107
|
|
|
{ |
108
|
|
|
$timeslots = TimeSlot::where('dj', $id)->count(); |
109
|
|
|
if ($timeslots > 0) { |
110
|
|
|
return redirect()->back()->with('error', 'Remove DJ from schedule before deleting.'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$dj = DJ::findOrFail($id); |
114
|
|
|
File::delete(public_path().'/img/djs/'.$dj->picture); |
115
|
|
|
DJ::destroy($id); |
116
|
|
|
return redirect()->route('admin.djs.index') |
117
|
|
|
->with('success', 'DJ Deleted!'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
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.