1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Seongbae\Discuss\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Seongbae\Discuss\Models\Thread; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Seongbae\Discuss\Models\Channel; |
8
|
|
|
use App\Http\Controllers\Controller; |
9
|
|
|
use Auth; |
10
|
|
|
use Illuminate\Support\Facades\Log; |
11
|
|
|
|
12
|
|
|
class ThreadsController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* ThreadsController constructor. |
16
|
|
|
*/ |
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
if (config('discuss.view_mode') == 'public') |
20
|
|
|
$this->middleware('auth', ['only' => ['create', 'store', 'edit','update', 'delete']]); |
21
|
|
|
else |
22
|
|
|
$this->middleware('auth'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Display a listing of the resource. |
27
|
|
|
* |
28
|
|
|
* @return \Illuminate\Http\Response |
29
|
|
|
*/ |
30
|
|
|
public function index($slug=null) |
31
|
|
|
{ |
32
|
|
|
$subscribed = false; |
33
|
|
|
$channel = null; |
34
|
|
|
|
35
|
|
|
if ($slug) |
36
|
|
|
{ |
37
|
|
|
$threads = Thread::whereHas('channel', function($q) use($slug) { |
38
|
|
|
$q->where('channels.slug', $slug); |
39
|
|
|
})->latest()->paginate(config('discuss.page_count')); |
40
|
|
|
|
41
|
|
|
$channel = Channel::where('slug', $slug)->first(); |
42
|
|
|
|
43
|
|
|
if (Auth::user()) |
44
|
|
|
$subscribed = Auth::user()->subscribedTo($channel); |
45
|
|
|
else |
46
|
|
|
$subscribed = false; |
47
|
|
|
} |
48
|
|
|
else |
49
|
|
|
{ |
50
|
|
|
if (request()->get('user')) |
51
|
|
|
{ |
52
|
|
|
$user = Auth::user(); |
53
|
|
|
$threads = Thread::where('user_id',$user->id)->latest()->paginate(config('discuss.page_count')); |
54
|
|
|
} |
55
|
|
|
else |
56
|
|
|
$threads = Thread::latest()->paginate(config('discuss.page_count')); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return view('discuss::threads.index', compact('threads', 'channel','subscribed')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Show the form for creating a new resource. |
64
|
|
|
* |
65
|
|
|
* @return \Illuminate\Http\Response |
66
|
|
|
*/ |
67
|
|
|
public function create() |
68
|
|
|
{ |
69
|
|
|
$channels = Channel::all(); |
70
|
|
|
|
71
|
|
|
return view('discuss::threads.create') |
|
|
|
|
72
|
|
|
->with('channels', $channels); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Store a newly created resource in storage. |
77
|
|
|
* |
78
|
|
|
* @param \Illuminate\Http\Request $request |
79
|
|
|
* @return \Illuminate\Http\Response |
80
|
|
|
*/ |
81
|
|
|
public function store(Request $request) |
82
|
|
|
{ |
83
|
|
|
$this->validate($request, [ |
84
|
|
|
'title'=>'required', |
85
|
|
|
'body'=>'required', |
86
|
|
|
'channel_id'=>'required|exists:channels,id' |
87
|
|
|
]); |
88
|
|
|
|
89
|
|
|
$user = Auth::user(); |
90
|
|
|
|
91
|
|
|
$thread = $user->discussThreads()->create([ |
92
|
|
|
'user_id' => $user->id, |
93
|
|
|
'title' => request('title'), |
94
|
|
|
'slug' => $this->slugify(request('title')), |
95
|
|
|
'channel_id' => request('channel_id'), |
96
|
|
|
'body' => request('body') |
97
|
|
|
]); |
98
|
|
|
|
99
|
|
|
$thread->attachSubscriber($user); |
100
|
|
|
|
101
|
|
View Code Duplication |
if ($request->ajax()) |
|
|
|
|
102
|
|
|
return $request->json([$thread], 200); |
|
|
|
|
103
|
|
|
else |
104
|
|
|
return redirect()->route('discuss.index')->with('success','Successfully created'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Display the specified resource. |
109
|
|
|
* |
110
|
|
|
* @param \App\Thread $thread |
111
|
|
|
* @return \Illuminate\Http\Response |
112
|
|
|
*/ |
113
|
|
|
public function show($channelId, Thread $thread) |
114
|
|
|
{ |
115
|
|
|
$channels = Channel::all(); |
116
|
|
|
|
117
|
|
|
$thread->view_count += 1; |
118
|
|
|
$thread->save(); |
119
|
|
|
|
120
|
|
|
$user = Auth::user(); |
121
|
|
|
|
122
|
|
|
return view('discuss::threads.show', compact(['thread', 'channels','user'])); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Show the form for editing the specified resource. |
127
|
|
|
* |
128
|
|
|
* @param \App\Thread $thread |
129
|
|
|
* @return \Illuminate\Http\Response |
130
|
|
|
*/ |
131
|
|
|
public function edit(Thread $thread) |
132
|
|
|
{ |
133
|
|
|
// |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Update the specified resource in storage. |
138
|
|
|
* |
139
|
|
|
* @param \Illuminate\Http\Request $request |
140
|
|
|
* @param \App\Thread $thread |
141
|
|
|
* @return \Illuminate\Http\Response |
142
|
|
|
*/ |
143
|
|
|
public function update(Channel $channel, Thread $thread, Request $request) |
144
|
|
|
{ |
145
|
|
|
$this->validate(request(), [ |
146
|
|
|
'title'=>'required', |
147
|
|
|
'body'=>'required', |
148
|
|
|
'channel_id'=>'required|exists:channels,id' |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
$thread->update(request(['title','body','channel_id'])); |
152
|
|
|
|
153
|
|
|
if ($request->ajax()) |
154
|
|
|
return $request->json([$thread], 200); |
|
|
|
|
155
|
|
|
else |
156
|
|
|
return redirect()->back(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Remove the specified resource from storage. |
161
|
|
|
* |
162
|
|
|
* @param \App\Thread $thread |
163
|
|
|
* @return \Illuminate\Http\Response |
164
|
|
|
*/ |
165
|
|
|
public function destroy(Request $request, Channel $channel, Thread $thread) |
166
|
|
|
{ |
167
|
|
|
$thread->delete(); |
168
|
|
|
|
169
|
|
View Code Duplication |
if ($request->ajax()) |
|
|
|
|
170
|
|
|
return $request->json([], 200); |
|
|
|
|
171
|
|
|
else |
172
|
|
|
return redirect()->route('discuss.index')->with('success','Successfully deleted'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
private function slugify($string){ |
176
|
|
|
$slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string), '-')); |
177
|
|
|
|
178
|
|
|
$thread = Thread::where('slug', $slug)->first(); |
179
|
|
|
|
180
|
|
|
if ($thread) |
181
|
|
|
$slug = $slug.'-'.$this->generate_string(4); |
182
|
|
|
|
183
|
|
|
return $slug; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
private function generate_string($strength = 4) { |
188
|
|
|
$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyz'; |
189
|
|
|
$input_length = strlen($permitted_chars); |
190
|
|
|
$random_string = ''; |
191
|
|
|
for($i = 0; $i < $strength; $i++) { |
192
|
|
|
$random_character = $permitted_chars[mt_rand(0, $input_length - 1)]; |
193
|
|
|
$random_string .= $random_character; |
194
|
|
|
} |
195
|
|
|
return $random_string; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: