1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\Theme; |
6
|
|
|
use App\Models\User; |
7
|
|
|
use Auth; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Support\Facades\Input; |
10
|
|
|
use Validator; |
11
|
|
|
|
12
|
|
|
class ThemesManagementController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Create a new controller instance. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->middleware('auth'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Display a listing of the resource. |
26
|
|
|
* |
27
|
|
|
* @return \Illuminate\Http\Response |
28
|
|
|
*/ |
29
|
|
|
public function index() |
30
|
|
|
{ |
31
|
|
|
$users = User::all(); |
32
|
|
|
|
33
|
|
|
$themes = Theme::orderBy('name', 'asc')->get(); |
34
|
|
|
|
35
|
|
|
return View('themesmanagement.show-themes', compact('themes', 'users')); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Show the form for creating a new resource. |
40
|
|
|
* |
41
|
|
|
* @return \Illuminate\Http\Response |
42
|
|
|
*/ |
43
|
|
|
public function create() |
44
|
|
|
{ |
45
|
|
|
return view('themesmanagement.add-theme'); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Store a newly created resource in storage. |
50
|
|
|
* |
51
|
|
|
* @param \Illuminate\Http\Request $request |
52
|
|
|
* |
53
|
|
|
* @return \Illuminate\Http\Response |
54
|
|
|
*/ |
55
|
|
|
public function store(Request $request) |
56
|
|
|
{ |
57
|
|
|
$input = Input::only('name', 'link', 'notes', 'status'); |
58
|
|
|
|
59
|
|
|
$validator = Validator::make($input, Theme::rules()); |
60
|
|
|
|
61
|
|
|
if ($validator->fails()) { |
62
|
|
|
return back()->withErrors($validator)->withInput(); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$theme = Theme::create([ |
66
|
|
|
'name' => $request->input('name'), |
67
|
|
|
'link' => $request->input('link'), |
68
|
|
|
'notes' => $request->input('notes'), |
69
|
|
|
'status' => $request->input('status'), |
70
|
|
|
'taggable_id' => 0, |
71
|
|
|
'taggable_type' => 'theme', |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
$theme->taggable_id = $theme->id; |
75
|
|
|
$theme->save(); |
76
|
|
|
|
77
|
|
|
return redirect('themes/'.$theme->id)->with('success', trans('themes.createSuccess')); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Display the specified resource. |
82
|
|
|
* |
83
|
|
|
* @param int $id |
84
|
|
|
* |
85
|
|
|
* @return \Illuminate\Http\Response |
86
|
|
|
*/ |
87
|
|
|
public function show($id) |
88
|
|
|
{ |
89
|
|
|
$theme = Theme::find($id); |
90
|
|
|
$users = User::all(); |
91
|
|
|
$themeUsers = []; |
92
|
|
|
|
93
|
|
|
foreach ($users as $user) { |
94
|
|
|
if ($user->profile && $user->profile->theme_id === $theme->id) { |
95
|
|
|
$themeUsers[] = $user; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$data = [ |
100
|
|
|
'theme' => $theme, |
101
|
|
|
'themeUsers' => $themeUsers, |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
return view('themesmanagement.show-theme')->with($data); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Show the form for editing the specified resource. |
109
|
|
|
* |
110
|
|
|
* @param int $id |
111
|
|
|
* |
112
|
|
|
* @return \Illuminate\Http\Response |
113
|
|
|
*/ |
114
|
|
|
public function edit($id) |
115
|
|
|
{ |
116
|
|
|
$theme = Theme::find($id); |
117
|
|
|
$users = User::all(); |
118
|
|
|
$themeUsers = []; |
119
|
|
|
|
120
|
|
|
foreach ($users as $user) { |
121
|
|
|
if ($user->profile && $user->profile->theme_id === $theme->id) { |
122
|
|
|
$themeUsers[] = $user; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$data = [ |
127
|
|
|
'theme' => $theme, |
128
|
|
|
'themeUsers' => $themeUsers, |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
return view('themesmanagement.edit-theme')->with($data); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Update the specified resource in storage. |
136
|
|
|
* |
137
|
|
|
* @param \Illuminate\Http\Request $request |
138
|
|
|
* @param int $id |
139
|
|
|
* |
140
|
|
|
* @return \Illuminate\Http\Response |
141
|
|
|
*/ |
142
|
|
|
public function update(Request $request, $id) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$theme = Theme::find($id); |
145
|
|
|
|
146
|
|
|
$input = Input::only('name', 'link', 'notes', 'status'); |
147
|
|
|
|
148
|
|
|
$validator = Validator::make($input, Theme::rules($id)); |
149
|
|
|
|
150
|
|
|
if ($validator->fails()) { |
151
|
|
|
return back()->withErrors($validator)->withInput(); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$theme->fill($input)->save(); |
155
|
|
|
|
156
|
|
|
return redirect('themes/'.$theme->id)->with('success', trans('themes.updateSuccess')); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Remove the specified resource from storage. |
161
|
|
|
* |
162
|
|
|
* @param int $id |
163
|
|
|
* |
164
|
|
|
* @return \Illuminate\Http\Response |
165
|
|
|
*/ |
166
|
|
|
public function destroy($id) |
167
|
|
|
{ |
168
|
|
|
$default = Theme::findOrFail(1); |
169
|
|
|
$theme = Theme::findOrFail($id); |
170
|
|
|
|
171
|
|
|
if ($theme->id != $default->id) { |
172
|
|
|
$theme->delete(); |
173
|
|
|
|
174
|
|
|
return redirect('themes')->with('success', trans('themes.deleteSuccess')); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return back()->with('error', trans('themes.deleteSelfError')); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|