-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathChatterDiscussionController.php
More file actions
310 lines (256 loc) · 9.98 KB
/
ChatterDiscussionController.php
File metadata and controls
310 lines (256 loc) · 9.98 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
namespace DevDojo\Chatter\Controllers;
use Auth;
use Carbon\Carbon;
use DevDojo\Chatter\Events\ChatterAfterNewDiscussion;
use DevDojo\Chatter\Events\ChatterBeforeNewDiscussion;
use DevDojo\Chatter\Models\Models;
use Event;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as Controller;
use Illuminate\Support\Str;
use Validator;
class ChatterDiscussionController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
/*$total = 10;
$offset = 0;
if ($request->total) {
$total = $request->total;
}
if ($request->offset) {
$offset = $request->offset;
}
$discussions = Models::discussion()->with('user')->with('post')->with('postsCount')->with('category')->orderBy('created_at', 'ASC')->take($total)->offset($offset)->get();*/
// Return an empty array to avoid exposing user data to the public.
// This index function is not being used anywhere.
return response()->json([]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$categories = Models::category()->all();
return view('chatter::discussion.create', compact('categories'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->request->add(['body_content' => strip_tags($request->body)]);
$validator = Validator::make($request->all(), [
'title' => 'required|min:5|max:255',
'body_content' => 'required|min:10',
'chatter_category_id' => 'required',
],[
'title.required' => trans('chatter::alert.danger.reason.title_required'),
'title.min' => [
'string' => trans('chatter::alert.danger.reason.title_min'),
],
'title.max' => [
'string' => trans('chatter::alert.danger.reason.title_max'),
],
'body_content.required' => trans('chatter::alert.danger.reason.content_required'),
'body_content.min' => trans('chatter::alert.danger.reason.content_min'),
'chatter_category_id.required' => trans('chatter::alert.danger.reason.category_required'),
]);
// Event::fire(new ChatterBeforeNewDiscussion($request, $validator));
if (function_exists('chatter_before_new_discussion')) {
chatter_before_new_discussion($request, $validator);
}
if ($validator->fails()) {
return back()->withErrors($validator)->withInput();
}
$user_id = Auth::user()->id;
if (config('chatter.security.limit_time_between_posts')) {
if ($this->notEnoughTimeBetweenDiscussion()) {
$minutes = trans_choice('chatter::messages.words.minutes', config('chatter.security.time_between_posts'));
$chatter_alert = [
'chatter_alert_type' => 'danger',
'chatter_alert' => trans('chatter::alert.danger.reason.prevent_spam', [
'minutes' => $minutes,
]),
];
return redirect('/'.config('chatter.routes.home'))->with($chatter_alert)->withInput();
}
}
// *** Let's gaurantee that we always have a generic slug *** //
$slug = str_slug($request->title, '-');
$slug = Str::slug($request->title, '-');
$discussion_exists = Models::discussion()->where('slug', '=', $slug)->withTrashed()->first();
$incrementer = 1;
$new_slug = $slug;
while (isset($discussion_exists->id)) {
$new_slug = $slug.'-'.$incrementer;
$discussion_exists = Models::discussion()->where('slug', '=', $new_slug)->withTrashed()->first();
$incrementer += 1;
}
if ($slug != $new_slug) {
$slug = $new_slug;
}
$new_discussion = [
'title' => $request->title,
'chatter_category_id' => $request->chatter_category_id,
'user_id' => $user_id,
'slug' => $slug,
'color' => $request->color,
];
$category = Models::category()->find($request->chatter_category_id);
if (!isset($category->slug)) {
$category = Models::category()->first();
}
$discussion = Models::discussion()->create($new_discussion);
$new_post = [
'chatter_discussion_id' => $discussion->id,
'user_id' => $user_id,
'body' => $request->body,
];
if (config('chatter.editor') == 'simplemde'):
$new_post['markdown'] = 1;
endif;
// add the user to automatically be notified when new posts are submitted
$discussion->users()->attach($user_id);
$post = Models::post()->create($new_post);
if ($post->id) {
// Event::fire(new ChatterAfterNewDiscussion($request, $discussion, $post));
if (function_exists('chatter_after_new_discussion')) {
chatter_after_new_discussion($request);
}
$chatter_alert = [
'chatter_alert_type' => 'success',
'chatter_alert' => trans('chatter::alert.success.reason.created_discussion'),
];
return redirect('/'.config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$category->slug.'/'.$slug)->with($chatter_alert);
} else {
$chatter_alert = [
'chatter_alert_type' => 'danger',
'chatter_alert' => trans('chatter::alert.danger.reason.create_discussion'),
];
return redirect('/'.config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$category->slug.'/'.$slug)->with($chatter_alert);
}
}
private function notEnoughTimeBetweenDiscussion()
{
$user = Auth::user();
$past = Carbon::now()->subMinutes(config('chatter.security.time_between_posts'));
$last_discussion = Models::discussion()->where('user_id', '=', $user->id)->where('created_at', '>=', $past)->first();
if (isset($last_discussion)) {
return true;
}
return false;
}
/**
* Display the specified resource.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function show($category, $slug = null)
{
if (!isset($category) || !isset($slug)) {
return redirect(config('chatter.routes.home'));
}
$discussion = Models::discussion()->where('slug', '=', $slug)->first();
if (is_null($discussion)) {
abort(404);
}
$discussion_category = Models::category()->find($discussion->chatter_category_id);
if ($category != $discussion_category->slug) {
return redirect(config('chatter.routes.home').'/'.config('chatter.routes.discussion').'/'.$discussion_category->slug.'/'.$discussion->slug);
}
$posts = Models::post()->with('user')->where('chatter_discussion_id', '=', $discussion->id)->orderBy(config('chatter.order_by.posts.order'), config('chatter.order_by.posts.by'))->paginate(10);
$chatter_editor = config('chatter.editor');
if ($chatter_editor == 'simplemde') {
// Dynamically register markdown service provider
\App::register('GrahamCampbell\Markdown\MarkdownServiceProvider');
}
$discussion->increment('views');
return view('chatter::discussion', compact('discussion', 'posts', 'chatter_editor'));
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
private function sanitizeContent($content)
{
libxml_use_internal_errors(true);
// create a new DomDocument object
$doc = new \DOMDocument();
// load the HTML into the DomDocument object (this would be your source HTML)
$doc->loadHTML($content);
$this->removeElementsByTagName('script', $doc);
$this->removeElementsByTagName('style', $doc);
$this->removeElementsByTagName('link', $doc);
// output cleaned html
return $doc->saveHtml();
}
private function removeElementsByTagName($tagName, $document)
{
$nodeList = $document->getElementsByTagName($tagName);
for ($nodeIdx = $nodeList->length; --$nodeIdx >= 0;) {
$node = $nodeList->item($nodeIdx);
$node->parentNode->removeChild($node);
}
}
public function toggleEmailNotification($category, $slug = null)
{
if (!isset($category) || !isset($slug)) {
return redirect(config('chatter.routes.home'));
}
$discussion = Models::discussion()->where('slug', '=', $slug)->first();
$user_id = Auth::user()->id;
// if it already exists, remove it
if ($discussion->users->contains($user_id)) {
$discussion->users()->detach($user_id);
return response()->json(0);
} else { // otherwise add it
$discussion->users()->attach($user_id);
return response()->json(1);
}
}
}