-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPostsController.php
More file actions
170 lines (121 loc) · 4.4 KB
/
Copy pathPostsController.php
File metadata and controls
170 lines (121 loc) · 4.4 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
<?php
namespace App\Controllers;
use App\Controllers\Controller;
use App\Models\User;
use Respect\Validation\Validator as v;
use App\Models\Post;
class PostsController extends Controller{
/**
* List all users
*
* @return
*/
public function index($request, $response, $args){
//find all posts
if(isset($args['user_id'])){
$posts = Post::where('user_id',$args['user_id'] )->get();
//get the user's details
$user = User::find($args['user_id']);
return $this->view->render($response,'posts/index.twig', ['posts'=>$posts, 'user'=>$user]);
}else{
$posts = Post::all();
return $this->view->render($response,'posts/index.twig', ['posts'=>$posts]);
}
}
/**
* Display a post
*
* @return
*/
public function view($request, $response, $args){
$post = Post::find( $args['id']);
return $this->view->render($response,'posts/view.twig', ['post'=>$post]);
}
/**
* Create A New Post
*
* @return
*/
public function add($request, $response, $args){
if($request->isPost()){
/**
* validate input before submission
* @var
*
*/
$validation = $this->validator->validate($request, [
'title' => v::notEmpty(),
'body' => v::notEmpty(),
]);
//redirect if validation fails
if($validation->failed()){
$this->flash->addMessage('error', 'Validation Failed!');
return $response->withRedirect($this->router->pathFor('posts/add.twig'));
}
$post = Post::create([
'title' => $request->getParam('title'),
'body' => $request->getParam('body'),
'user_id' => $this->auth->user()->id,
]);
$this->flash->addMessage('success', 'Post Added Successfully');
//redirect to eg. posts/view/8
return $response->withRedirect($this->router->pathFor('posts.view', ['id'=>$post->id]));
}
return $this->view->render($response,'posts/add.twig');
}
/**
* Edit post
*
* @return
*/
public function edit($request, $response, $args){
//find the post
$post = Post::find( $args['id']);
//only admin and the person that created the post can edit or delete it.
if(($this->auth->user()->id != $post->user_id) AND ($this->auth->user()->role_id > 2 ) ){
$this->flash->addMessage('error', 'You are not allowed to perform this action!');
return $this->view->render($response,'posts/edit.twig', ['post'=>$post]);
}
//if form was submitted
if($request->isPost()){
$validation = $this->validator->validate($request, [
'title' => v::notEmpty(),
'body' => v::notEmpty(),
]);
//redirect if validation fails
if($validation->failed()){
$this->flash->addMessage('error', 'Validation Failed!');
return $this->view->render($response,'posts/edit.twig', ['post'=>$post]);
}
//save Data
$post = Post::where('id', $args['id'])
->update([
'title' => $request->getParam('title'),
'body' => $request->getParam('body')
]);
if($post){
$this->flash->addMessage('success', 'Post Updated Successfully');
//redirect to eg. posts/view/8
return $response->withRedirect($this->router->pathFor('posts.view', ['id'=>$args['id']]));
}
}
return $this->view->render($response,'posts/edit.twig', ['post'=>$post]);
}
/**
* Delete a post
*
* @return
*/
public function delete($request, $response, $args){
$user = Post::find( $args['id']);
//only owner and admin can delete
if(($this->auth->user()->id != $post->user_id) AND ($this->auth->user()->role_id > 2 ) ){
$this->flash->addMessage('error', 'You are not allowed to perform this action!');
return $this->view->render($response,'posts/view.twig', ['post'=>$post]);
}
if($user->delete()){
$this->flash->addMessage('success', 'Post Deleted Successfully');
return $response->withRedirect($this->router->pathFor('posts.index', ['user_id'=>$this->auth->user()->id]));
}
}
}