1
+ <?php
2
+ class Posts extends Controller {
3
+
4
+ public function __construct (){
5
+ if (!isLoggedIn ()){
6
+ redirect ('users/login ' );
7
+ }
8
+ //new model instance
9
+ $ this ->postModel = $ this ->model ('Post ' );
10
+ $ this ->userModel = $ this ->model ('User ' );
11
+ }
12
+
13
+ public function index (){
14
+
15
+ $ posts = $ this ->postModel ->getPosts ();
16
+ $ data = [
17
+ 'posts ' => $ posts
18
+ ];
19
+
20
+ $ this ->view ('posts/index ' , $ data );
21
+ }
22
+ public function getUser ()
23
+ {
24
+ if (isset ($ _POST ['search_user ' ])) {
25
+ $ checkUser = $ this ->model ->getUser ($ _POST ['name ' ]);
26
+ }
27
+ }
28
+ //add new post
29
+ public function add (){
30
+ $ _POST = filter_input_array (INPUT_POST , FILTER_SANITIZE_STRING );
31
+ if ($ _SERVER ['REQUEST_METHOD ' ] == 'POST ' ){
32
+ //$img_path = URLROOT;
33
+ $ uploads_dir = '/public/img ' ;
34
+
35
+ $ tmp_name = $ _FILES ["img " ]["name " ];
36
+ $ name = basename ($ _FILES ["img " ]["name " ]);
37
+
38
+ move_uploaded_file ($ tmp_name , "$ uploads_dir/ $ name " );
39
+
40
+ $ data = [
41
+ 'title ' => trim ($ _POST ['title ' ]),
42
+ //'img' => (isset($_POST['img']) ? move_uploaded_file($_POST['img']) : ''),
43
+ 'img ' => trim ($ tmp_name ),
44
+ 'body ' => trim ($ _POST ['body ' ]),
45
+ 'user_id ' => $ _SESSION ['user_id ' ],
46
+ 'title_err ' => '' ,
47
+ 'body_err ' => '' ,
48
+ 'img_err ' => '' ,
49
+ ];
50
+
51
+ if (empty ($ data ['title ' ])){
52
+ $ data ['title_err ' ] = 'Please enter post title ' ;
53
+ }
54
+
55
+ // echo '<pre>';
56
+ // print_r($data);
57
+ // echo '</pre>';exit;
58
+
59
+ if (empty ($ tmp_name )){
60
+ $ data ['img_err ' ] = 'Please upload image ' ;
61
+ }
62
+ if (empty ($ data ['body ' ])){
63
+ $ data ['body_err ' ] = 'Please enter the post content ' ;
64
+ }
65
+
66
+ //validate error free
67
+ if (empty ($ data ['title_err ' ]) && empty ($ data ['body_err ' ])){
68
+ if ($ this ->postModel ->addPost ($ data )){
69
+ flash ('post_message ' , 'Your post have been added ' );
70
+ redirect ('posts ' );
71
+ }else {
72
+ die ('something went wrong ' );
73
+ }
74
+
75
+ //load view with error
76
+ }else {
77
+ $ this ->view ('posts/add ' , $ data );
78
+ }
79
+ }
80
+ else {
81
+ $ data = [
82
+ 'title ' => (isset ($ _POST ['title ' ]) ? trim ($ _POST ['title ' ]) : '' ),
83
+ // 'img' => (isset($_POST['img']) ? move_uploaded_file($_POST['img']) : ''),
84
+ 'body ' => (isset ($ _POST ['body ' ])? trim ($ _POST ['body ' ]) : '' )
85
+ ];
86
+ $ this ->view ('posts/add ' , $ data );
87
+ }
88
+ }
89
+
90
+ //show single post
91
+ public function show ($ id ){
92
+
93
+ $ post = $ this ->postModel ->getPostById ($ id );
94
+ // print_r($post); exit;
95
+ $ user = $ this ->userModel ->getUserById ($ post ->user_id );
96
+
97
+ $ data = [
98
+ 'post ' => $ post ,
99
+ 'user ' => $ user
100
+ ];
101
+
102
+ $ this ->view ('posts/show ' , $ data );
103
+ }
104
+
105
+ //edit post
106
+ public function edit ($ id ){
107
+
108
+ if ($ _SERVER ['REQUEST_METHOD ' ] == 'POST ' ){
109
+ $ _POST = filter_input_array (INPUT_POST , FILTER_SANITIZE_STRING );
110
+
111
+ $ uploads_dir = '/public/img ' ;
112
+
113
+ $ tmp_name = $ _FILES ["img " ]["name " ];
114
+ $ name = basename ($ _FILES ["img " ]["name " ]);
115
+
116
+ move_uploaded_file ($ tmp_name , "$ uploads_dir/ $ name " );
117
+
118
+ $ oldimg = $ this ->postModel ->getPostById ($ id );
119
+
120
+
121
+
122
+ $ data = [
123
+ 'id ' => $ id ,
124
+ 'title ' => trim ($ _POST ['title ' ]),
125
+ 'body ' => trim ($ _POST ['body ' ]),
126
+ 'img ' => (!empty ($ tmp_name )? trim ($ tmp_name ): $ oldimg ->img ),
127
+ 'user_id ' => $ _SESSION ['user_id ' ],
128
+ 'title_err ' => '' ,
129
+ 'body_err ' => '' ,
130
+ ];
131
+ //validate the title
132
+ if (empty ($ data ['title ' ])){
133
+ $ data ['title_err ' ] = 'Please enter post titke ' ;
134
+ }
135
+
136
+ //validate the image
137
+ if (empty ($ tmp_name )){
138
+ $ data ['img_err ' ] = 'Please upload image ' ;
139
+
140
+ }
141
+ //validate the body
142
+ if (empty ($ data ['body ' ])){
143
+ $ data ['body_err ' ] = 'Please enter the post content ' ;
144
+ }
145
+
146
+ //validate error free
147
+ if (empty ($ data ['title_err ' ]) && empty ($ _data ['img_err ' ]) && empty ($ data ['body_err ' ])){
148
+ if ($ this ->postModel ->updatePost ($ data )){
149
+ flash ('post_message ' , 'Your post have been updated ' );
150
+ redirect ('posts ' );
151
+ }else {
152
+ die ('something went wrong ' );
153
+ }
154
+
155
+ //load with the error
156
+ }else {
157
+ $ this ->view ('posts/edit ' , $ data );
158
+ }
159
+ }else {
160
+ //check for the owner and call method from post model
161
+ $ post = $ this ->postModel ->getPostById ($ id );
162
+ if ($ post ->user_id != $ _SESSION ['user_id ' ]){
163
+ redirect ('posts ' );
164
+ }
165
+ $ data = [
166
+ 'id ' => $ id ,
167
+ 'title ' => $ post ->title ,
168
+ 'img ' => $ post ->img ,
169
+ 'body ' => $ post ->body
170
+ ];
171
+
172
+ $ this ->view ('posts/edit ' , $ data );
173
+ }
174
+ }
175
+
176
+ //delete post
177
+ public function delete ($ id ){
178
+ if ($ _SERVER ['REQUEST_METHOD ' ] == 'POST ' ){
179
+ //check for owner
180
+ $ post = $ this ->postModel ->getPostById ($ id );
181
+ if ($ post ->user_id != $ _SESSION ['user_id ' ]){
182
+ redirect ('posts ' );
183
+ }
184
+
185
+ //call delete method from post mode
186
+ if ($ this ->postModel ->deletePost ($ id )){
187
+ flash ('post_message ' , 'Post Removed ' );
188
+ redirect ('posts ' );
189
+ }else {
190
+ die ('Something went wrong...! ' );
191
+ }
192
+ } else {
193
+ redirect ('posts ' );
194
+ }
195
+ }
196
+ }
197
+ ?>
0 commit comments