forked from linuxserver/Heimdall
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagController.php
More file actions
211 lines (178 loc) · 5.39 KB
/
Copy pathTagController.php
File metadata and controls
211 lines (178 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
namespace App\Http\Controllers;
use App\Item;
use App\User;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
class TagController extends Controller
{
public function __construct()
{
$this->middleware('allowed');
}
/**
* Display a listing of the resource.
*
* @return Application|Factory|View
*/
public function index(Request $request): \Illuminate\View\View
{
$trash = (bool) $request->input('trash');
$data['apps'] = Item::ofType('tag')->where('id', '>', 0)->orderBy('title', 'asc')->get();
$data['trash'] = Item::ofType('tag')->where('id', '>', 0)->onlyTrashed()->get();
if ($trash) {
return view('tags.trash', $data);
} else {
return view('tags.list', $data);
}
}
/**
* Show the form for creating a new resource.
*
* @return Application|Factory|View
*/
public function create(): \Illuminate\View\View
{
$data = [];
return view('tags.create', $data);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
$validatedData = $request->validate([
'title' => 'required|max:255',
'file' => 'image'
]);
if ($request->hasFile('file')) {
$path = $request->file('file')->store('icons', 'public');
$request->merge([
'icon' => $path,
]);
}
$slug = str_slug($request->title, '-', 'en_US');
$current_user = User::currentUser();
// set item type to tag
$request->merge([
'type' => '1',
'url' => $slug,
'user_id' => $current_user->getId(),
]);
//die(print_r($request->all()));
Item::create($request->all());
$route = route('dash', []);
return redirect($route)
->with('success', __('app.alert.success.tag_created'));
}
/**
* Display the specified resource.
*
* @param $slug
*/
public function show($slug, Request $request): View
{
$item = Item::whereUrl($slug)->first();
//print_r($item);
if (config('app.auth_roles_enable')) {
$roles = explode(config('app.auth_roles_delimiter'), $request->header(config('app.auth_roles_header')));
$data['apps'] = $item->children()->whereIn('role', $roles)->pinned()->orderBy('order', 'asc')->get();
} else {
$data['apps'] = $item->children()->pinned()->orderBy('order', 'asc')->get();
}
$data['tag'] = $item->id;
$data['all_apps'] = $item->children;
$data['taglist'] = Item::ofType('tag')->where('id', '>', 0)->orderBy('title', 'asc')->get();
return view('welcome', $data);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(int $id): View
{
// Get the item
$item = Item::find($id);
// show the edit form and pass the nerd
return view('tags.edit')
->with('item', $item);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, int $id): RedirectResponse
{
$validatedData = $request->validate([
'title' => 'required|max:255',
'file' => 'image'
]);
if ($request->hasFile('file')) {
$path = $request->file('file')->store('icons', 'public');
$request->merge([
'icon' => $path,
]);
}
$slug = str_slug($request->title, '-', 'en_US');
// set item type to tag
$request->merge([
'url' => $slug,
]);
Item::find($id)->update($request->all());
$route = route('dash', []);
return redirect($route)
->with('success', __('app.alert.success.tag_updated'));
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Request $request, int $id): RedirectResponse
{
//
$force = (bool) $request->input('force');
if ($force) {
Item::withTrashed()
->where('id', $id)
->forceDelete();
} else {
Item::find($id)->delete();
}
$route = route('tags.index', []);
return redirect($route)
->with('success', __('app.alert.success.item_deleted'));
}
/**
* Restore the specified resource from soft deletion.
*/
public function restore(int $id): RedirectResponse
{
//
Item::withTrashed()
->where('id', $id)
->restore();
$route = route('tags.index', []);
return redirect($route)
->with('success', __('app.alert.success.item_restored'));
}
/**
* Add item to tag
*
* @param $tag
* @param $item
* @return int 1|0
*/
public function add($tag, $item): int
{
$tag = Item::find($tag);
$item = Item::find($item);
if ($tag && $item) {
// only add items, not cats
if ((int) $item->type === 0) {
$tag->children()->attach($item);
return 1;
}
}
return 0;
}
}