|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* rmarchiv.tk |
|
5
|
|
|
* (c) 2016-2017 by Marcel 'ryg' Hering |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace App\Http\Controllers; |
|
9
|
|
|
|
|
10
|
|
|
use Carbon\Carbon; |
|
11
|
|
|
use App\Models\Game; |
|
12
|
|
|
use App\Models\UserList; |
|
13
|
|
|
use App\Models\UserListItem; |
|
14
|
|
|
use Illuminate\Http\Request; |
|
15
|
|
|
|
|
16
|
|
|
class UserListController extends Controller |
|
17
|
|
|
{ |
|
18
|
|
|
public function create() |
|
19
|
|
|
{ |
|
20
|
|
|
return view('users.lists.create'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function store(Request $request) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->validate($request, [ |
|
26
|
|
|
'title' => 'required', |
|
27
|
|
|
'desc' => 'required', |
|
28
|
|
|
]); |
|
29
|
|
|
|
|
30
|
|
|
\DB::table('user_lists')->insert([ |
|
31
|
|
|
'user_id' => \Auth::id(), |
|
32
|
|
|
'title' => $request->get('title'), |
|
33
|
|
|
'desc_md' => $request->get('desc'), |
|
34
|
|
|
'desc_html' => \Markdown::convertToHtml($request->get('desc')), |
|
35
|
|
|
'created_at' => Carbon::now(), |
|
36
|
|
|
]); |
|
37
|
|
|
|
|
38
|
|
|
return \Redirect::back(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function delete_game($listid, $itemid) |
|
42
|
|
|
{ |
|
43
|
|
|
$list = UserList::whereId($listid)->first(); |
|
44
|
|
|
|
|
45
|
|
|
if (\Auth::check() and \Auth::user()->id == $list->user_id) { |
|
|
|
|
|
|
46
|
|
|
\DB::table('user_list_items') |
|
47
|
|
|
->where('list_id', '=', $listid) |
|
48
|
|
|
->where('content_id', '=', $itemid) |
|
49
|
|
|
->where('content_type', '=', 'game') |
|
50
|
|
|
->delete(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return \Redirect::back(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function delete($listid) |
|
57
|
|
|
{ |
|
58
|
|
|
if (\Auth::check()) { |
|
59
|
|
|
if (\Auth::user()->hasRole('admin')) { |
|
60
|
|
|
\DB::table('user_list_items') |
|
61
|
|
|
->where('list_id', '=', $listid) |
|
62
|
|
|
->delete(); |
|
63
|
|
|
|
|
64
|
|
|
\DB::table('user_lists') |
|
65
|
|
|
->where('id', '=', $listid) |
|
66
|
|
|
->delete(); |
|
67
|
|
|
} else { |
|
68
|
|
|
$list = \DB::table('user_lists') |
|
69
|
|
|
->where('id', '=', $listid) |
|
70
|
|
|
->where('user_id', '=', \Auth::id()) |
|
71
|
|
|
->get(); |
|
72
|
|
|
|
|
73
|
|
|
if ($list->count() != 0) { |
|
74
|
|
|
\DB::table('user_lists') |
|
75
|
|
|
->where('id', '=', $listid) |
|
76
|
|
|
->delete(); |
|
77
|
|
|
|
|
78
|
|
|
\DB::table('user_list_items') |
|
79
|
|
|
->where('list_id', '=', $listid) |
|
80
|
|
|
->delete(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return \Redirect::back(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function add_game($listid, $gameid) |
|
89
|
|
|
{ |
|
90
|
|
|
if (\Auth::check()) { |
|
91
|
|
|
$check = \DB::table('user_list_items') |
|
92
|
|
|
->where('content_id', '=', $gameid) |
|
93
|
|
|
->where('content_type', '=', 'game') |
|
94
|
|
|
->where('list_id', '=', $listid) |
|
95
|
|
|
->get(); |
|
96
|
|
|
if ($check->count() == 0) { |
|
97
|
|
|
\DB::table('user_list_items')->insert([ |
|
98
|
|
|
'content_id' => $gameid, |
|
99
|
|
|
'content_type' => 'game', |
|
100
|
|
|
'user_id' => \Auth::id(), |
|
101
|
|
|
'list_id' => $listid, |
|
102
|
|
|
'created_at' => Carbon::now(), |
|
103
|
|
|
]); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return \Redirect::action('GameController@show', $gameid); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
View Code Duplication |
public function show($userid, $listid) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
$list = UserList::whereId($listid)->first(); |
|
|
|
|
|
|
113
|
|
|
$arr = UserListItem::whereListId($listid)->pluck('content_id')->toArray(); |
|
|
|
|
|
|
114
|
|
|
$games = Game::find($arr); |
|
115
|
|
|
|
|
116
|
|
|
return view('users.lists.show', [ |
|
117
|
|
|
'list' => $list, |
|
118
|
|
|
'games' => $games, |
|
119
|
|
|
'orderby' => '', |
|
120
|
|
|
'direction' => '', |
|
121
|
|
|
]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function index($userid) |
|
125
|
|
|
{ |
|
126
|
|
|
$lists = \DB::table('user_lists') |
|
127
|
|
|
->where('user_id', '=', $userid) |
|
128
|
|
|
->select([ |
|
129
|
|
|
'id', |
|
130
|
|
|
'title', |
|
131
|
|
|
'user_id', |
|
132
|
|
|
'created_at', |
|
133
|
|
|
]) |
|
134
|
|
|
->selectRaw('(SELECT COUNT(*) FROM user_list_items WHERE list_id = user_lists.id) as count') |
|
135
|
|
|
->orderBy('title') |
|
136
|
|
|
->get(); |
|
137
|
|
|
|
|
138
|
|
|
return view('users.lists.index', [ |
|
139
|
|
|
'lists' => $lists, |
|
140
|
|
|
]); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.