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 App\Models\News; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
|
13
|
|
|
class NewsController extends Controller |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Display a listing of the resource. |
17
|
|
|
* |
18
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
19
|
|
|
*/ |
20
|
|
|
public function index() |
21
|
|
|
{ |
22
|
|
|
/* |
|
|
|
|
23
|
|
|
$news = \DB::table('news') |
24
|
|
|
->leftJoin('users', 'news.user_id', '=', 'users.id') |
25
|
|
|
->leftJoin('comments', function ($join) { |
26
|
|
|
$join->on('comments.content_id', '=', 'news.id'); |
27
|
|
|
$join->on('comments.content_type', '=', \DB::raw("'news'")); |
28
|
|
|
}) |
29
|
|
|
->select(['news.id', 'news.title', 'news.user_id', 'users.name', 'news.created_at', 'news.approved', 'news.news_html']) |
30
|
|
|
->selectRaw('COUNT(comments.id) as counter') |
31
|
|
|
->orderBy('news.created_at', 'desc') |
32
|
|
|
->groupBy('news.id') |
33
|
|
|
->get(); |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
$news = News::orderBy('created_at', 'desc')->paginate(25); |
37
|
|
|
|
38
|
|
|
return view('news.index', [ |
39
|
|
|
'news' => $news, |
40
|
|
|
]); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Show the form for creating a new resource. |
45
|
|
|
* |
46
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function create() |
49
|
|
|
{ |
50
|
|
|
if (\Auth::check()) { |
51
|
|
|
return view('news.create'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Store a newly created resource in storage. |
57
|
|
|
* |
58
|
|
|
* @param \Illuminate\Http\Request $request |
59
|
|
|
* |
60
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
public function store(Request $request) |
63
|
|
|
{ |
64
|
|
|
if (\Auth::check()) { |
65
|
|
|
if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) { |
66
|
|
|
$n = new News(); |
|
|
|
|
67
|
|
|
$n->news_category = $request->get('cat'); |
68
|
|
|
$n->user_id = \Auth::id(); |
|
|
|
|
69
|
|
|
$n->news_md = $request->get('msg'); |
|
|
|
|
70
|
|
|
$n->news_html = \Markdown::convertToHtml($request->get('msg')); |
|
|
|
|
71
|
|
|
$n->title = $request->get('title'); |
|
|
|
|
72
|
|
|
$n->approved = 0; |
|
|
|
|
73
|
|
|
|
74
|
|
|
$n->save(); |
75
|
|
|
|
76
|
|
|
return redirect()->action('NewsController@show', $n->id); |
|
|
|
|
77
|
|
|
} else { |
78
|
|
|
return redirect()->action('IndexController@index'); |
79
|
|
|
} |
80
|
|
|
} else { |
81
|
|
|
return redirect()->action('IndexController@index'); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Display the specified resource. |
87
|
|
|
* |
88
|
|
|
* @param int $id |
89
|
|
|
* |
90
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
public function show($id) |
93
|
|
|
{ |
94
|
|
|
$news = News::whereId($id)->first(); |
95
|
|
|
|
96
|
|
|
return view('news.show', [ |
97
|
|
|
'news' => $news, |
98
|
|
|
]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Show the form for editing the specified resource. |
103
|
|
|
* |
104
|
|
|
* @param int $id |
105
|
|
|
* |
106
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
107
|
|
|
*/ |
108
|
|
|
public function edit($id) |
109
|
|
|
{ |
110
|
|
|
if (\Auth::check()) { |
111
|
|
|
if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) { |
112
|
|
|
$news = News::whereId($id)->first(); |
113
|
|
|
|
114
|
|
|
return view('news.edit', [ |
115
|
|
|
'news' => $news, |
116
|
|
|
]); |
117
|
|
|
} else { |
118
|
|
|
return redirect()->action('IndexController@index'); |
119
|
|
|
} |
120
|
|
|
} else { |
121
|
|
|
return redirect()->action('IndexController@index'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Update the specified resource in storage. |
127
|
|
|
* |
128
|
|
|
* @param \Illuminate\Http\Request $request |
129
|
|
|
* @param int $id |
130
|
|
|
* |
131
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
132
|
|
|
*/ |
133
|
|
|
public function update(Request $request, $id) |
134
|
|
|
{ |
135
|
|
|
if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) { |
136
|
|
|
$this->validate($request, [ |
137
|
|
|
'title' => 'required', |
138
|
|
|
'msg' => 'required', |
139
|
|
|
'cat' => 'required', |
140
|
|
|
]); |
141
|
|
|
|
142
|
|
|
$news = News::whereId($id)->first(); |
143
|
|
|
|
144
|
|
|
$news->title = $request->get('title'); |
|
|
|
|
145
|
|
|
$news->news_md = $request->get('msg'); |
|
|
|
|
146
|
|
|
$news->news_html = \Markdown::convertToHtml($request->get('msg')); |
|
|
|
|
147
|
|
|
$news->news_category = $request->get('cat'); |
148
|
|
|
$news->save(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return redirect()->action('NewsController@show', $id); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Remove the specified resource from storage. |
156
|
|
|
* |
157
|
|
|
* @param int $id |
158
|
|
|
* |
159
|
|
|
* @return \Illuminate\Http\Response |
|
|
|
|
160
|
|
|
*/ |
161
|
|
|
public function destroy($id) |
162
|
|
|
{ |
163
|
|
|
if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) { |
164
|
|
|
$news = News::whereId($id)->first(); |
165
|
|
|
$news->delete(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return redirect()->action('NewsController@index'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function approve($id, $approve) |
172
|
|
|
{ |
173
|
|
|
if (\Auth::user()->hasRole(['admin', 'owner', 'moderator'])) { |
174
|
|
|
$news = News::whereId($id)->first(); |
|
|
|
|
175
|
|
|
$news->approved = $approve; |
176
|
|
|
$news->save(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return redirect()->action('NewsController@show', $id); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.