Completed
Push — master ( be7609...08a63c )
by David
13:27
created

ApiController::forumPostStore()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 26
rs 8.8571
cc 3
eloc 15
nc 3
nop 0
1
<?php namespace Taskforcedev\LaravelForum\Http\Controllers;
2
3
use \Auth;
4
use \Event;
5
use \Redirect;
6
use \Request;
7
use \Response;
8
use \Schema;
9
10
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Cannot use Illuminate\Http\Request as Request because the name is already in use
Loading history...
11
12
use Taskforcedev\LaravelForum\Forum;
13
use Taskforcedev\LaravelForum\ForumCategory;
14
use Taskforcedev\LaravelForum\ForumPost;
15
use Taskforcedev\LaravelForum\ForumReply;
16
use Taskforcedev\LaravelForum\Events\PostCreated;
17
use Taskforcedev\LaravelForum\Events\PostReply;
18
19
/**
20
 * Class ApiController
21
 * @package Taskforcedev\LaravelForum\Http\Controllers
22
 */
23
class ApiController extends BaseController
24
{
25
    public function forumCategoryStore(Request $request)
26
    {
27
        $data = [
28
            "name" => $request->input('name'),
29
        ];
30
31
        $response = $this->adminCheck();
32
        if (isset($response)) {
33
            return $response;
34
        }
35
36
        /* If data invalid return bad request */
37
        if (!ForumCategory::valid($data)) {
38
            return response('Bad Request', 400);
39
        }
40
41
        ForumCategory::create($data);
42
    }
43
44
    public function forumStore(Request $request)
45
    {
46
        $data = [
47
            "name" => $request->input('name'),
48
            "description" => $request->input('description'),
49
            "category_id" => $request->input('category'),
50
        ];
51
52
        $response = $this->adminCheck();
53
        if (isset($response)) {
54
            return $response;
55
        }
56
57
        if (!Forum::valid($data)) {
58
            return response('Bad Request', 400);
59
        }
60
61
        Forum::create($data);
62
    }
63
64
    public function forumPostStore(Request $request)
65
    {
66
        if (!Auth::check()) {
67
            return response('Unauthorized', 401);
68
        }
69
70
        $user = Auth::user();
71
72
        $forum_id = $request->input('forum_id');
73
74
        $data = [
75
            "author_id" => $user->id,
76
            "title" => $request->input('title'),
77
            "body" => $this->sanitizeData(Request::input('body')),
78
            "forum_id" => $forum_id
79
        ];
80
81
        if (!ForumPost::valid($data)) {
82
            return response('Bad Request', 400);
83
        }
84
85
        $post = ForumPost::create($data);
86
87
        event(new PostCreated($post, $user));
88
        return redirect()->route('laravel-forum.view.post', [$forum_id , $post->id]);
89
    }
90
91
    public function forumReplyStore(Request $request)
92
    {
93
        if (!Auth::check()) {
94
            return response('Unauthorized', 401);
95
        }
96
97
        $user = Auth::user();
98
99
        $forum_id = $request->input('forum_id');
100
        $post_id = $request->input('post_id');
101
102
        $data = [
103
            'author_id' => $user->id,
104
            'body' => $this->sanitizeData(Request::input('body')),
105
            'post_id' => $post_id,
106
        ];
107
108
        if (!ForumReply::valid($data)) {
109
            return redirect()->route('laravel-forum.view.post', [$forum_id, $post_id]);
110
        }
111
112
        $reply = ForumReply::create($data);
113
114
        event(new PostReply($reply, $user));
115
        return redirect()->route('laravel-forum.view.post', [$forum_id, $post_id]);
116
    }
117
118
    private function adminCheck()
119
    {
120
        if (!$this->canAdministrate()) {
121
            return response('Unauthorised', 401);
122
        }
123
    }
124
125
    private function sanitizeData($data)
126
    {
127
        /* Sanitize post input */
128
        $removals = [
129
            '/<script\b[^>]*>/',
130
            '/<\/script\b[^>]*>/'
131
        ];
132
        foreach ($removals as $r) {
133
            $data = preg_replace($r, '', $data);
134
        }
135
        return $data;
136
    }
137
138
    public function lockPost(Request $request, $id)
139
    {
140
        if (!$this->canAdministrate() && !$this->canModerate()) {
141
            return response('Unauthorised', 401);
142
        }
143
144
        $post = $this->postExists($id);
145
        if (!$post) {
146
            return response('Post not found', 404);
147
        }
148
149
        $post->locked = 1;
150
        $post->save();
151
        return response('Post Locked', 200);
152
    }
153
154
    public function unlockPost(Request $request, $id)
155
    {
156
        if (!$this->canAdministrate() && !$this->canModerate()) {
157
            return response('Unauthorised', 401);
158
        }
159
160
        $post = $this->postExists($id);
161
        if (!$post) {
162
            return response('Post not found', 404);
163
        }
164
165
        $post->locked = 0;
166
        $post->save();
167
        return response('Post Unlocked', 200);
168
    }
169
170
    public function stickyPost(Request $request, $id)
171
    {
172
        if (!$this->canAdministrate() && !$this->canModerate()) {
173
            return response('Unauthorised', 401);
174
        }
175
176
        $post = $this->postExists($id);
177
        if (!$post) {
178
            return response('Post not found', 404);
179
        }
180
181
        $post->sticky = 1;
182
        $post->save();
183
        return response('Post Unlocked', 200);
184
    }
185
186
    public function unstickyPost(Request $request, $id)
187
    {
188
        if (!$this->canAdministrate() && !$this->canModerate()) {
189
            return response('Unauthorised', 401);
190
        }
191
192
        $post = $this->postExists($id);
193
        if (!$post) {
194
            return response('Post not found', 404);
195
        }
196
197
        $post->sticky = 0;
198
        $post->save();
199
        return response('Post Unlocked', 200);
200
    }
201
202
    private function postExists($post_id)
203
    {
204
        try {
205
            $post = ForumPost::where('id', $post_id)->firstOrFail();
206
            return $post;
207
        } catch (\Exception $e) {
208
            return false;
209
        }
210
    }
211
212
    public function postDelete($forum_id, $post_id)
213
    {
214
        if (!$this->canAdministrate() && !$this->canModerate()) {
215
            return response('Unauthorised', 401);
216
        }
217
218
        $post = $this->postExists($post_id);
219
        if (!$post) {
220
            return response('Post not found', 404);
221
        }
222
223
        $post->delete();
224
        return response('Post Deleted', 200);
225
    }
226
227
    public function forumDelete(Request $request)
228
    {
229
        if (!$this->canAdministrate() && !$this->canModerate()) {
230
            return response('Unauthorised', 401);
231
        }
232
233
        $forum_id = Request::input('forum_id');
234
235
        $forum = $this->forumExists($forum_id);
236
        if (!$forum) {
237
            return response('Forum not found', 404);
238
        }
239
240
        $forum->delete();
241
        return response('Forum Deleted', 200);
242
    }
243
244
    private function forumExists($id)
245
    {
246
        try {
247
            $forum = Forum::where('id', $id)->firstOrFail();
248
            return $forum;
249
        } catch (\Exception $e) {
250
            return false;
251
        }
252
    }
253
254
    public function forumCategoryDelete(Request $request)
255
    {
256
        if (!$this->canAdministrate() && !$this->canModerate()) {
257
            return response('Unauthorised', 401);
258
        }
259
260
        $cat_id = $request->input('category_id');
261
262
        $cat = $this->forumCategoryExists($cat_id);
263
        if (!$cat) {
264
            return response('Forum Category not found', 404);
265
        }
266
267
        $cat->delete();
268
        return response('Forum Category Deleted', 200);
269
    }
270
271
    private function forumCategoryExists(Request $request, $id)
272
    {
273
        try {
274
            $cat = ForumCategory::where('id', $id)->firstOrFail();
275
            return $cat;
276
        } catch (\Exception $e) {
277
            return false;
278
        }
279
    }
280
}
281