|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
namespace Anax\Blog;
|
|
4
|
|
|
|
|
5
|
|
|
/**
|
|
6
|
|
|
* A controller for a blog and its posts.
|
|
7
|
|
|
*
|
|
8
|
|
|
*/
|
|
9
|
|
|
class BlogController implements \Anax\DI\IInjectionAware
|
|
10
|
|
|
{
|
|
11
|
|
|
use \Anax\DI\TInjectable,
|
|
12
|
|
|
\Anax\MVC\TRedirectHelpers;
|
|
13
|
|
|
|
|
14
|
|
|
public $blog;
|
|
15
|
|
|
|
|
16
|
|
|
/**
|
|
17
|
|
|
* Initialize the controller.
|
|
18
|
|
|
*
|
|
19
|
|
|
* @return void
|
|
20
|
|
|
*/
|
|
21
|
|
|
public function initialize()
|
|
22
|
|
|
{
|
|
23
|
|
|
$this->blog = new \Anax\Blog\Blog();
|
|
24
|
|
|
$this->blog->setDI($this->di);
|
|
|
|
|
|
|
25
|
|
|
}
|
|
26
|
|
|
|
|
27
|
|
|
public function setupAction()
|
|
28
|
|
|
{
|
|
29
|
|
|
require('setup.php');
|
|
30
|
|
|
|
|
31
|
|
|
$this->redirectTo('blog/list');
|
|
32
|
|
|
}
|
|
33
|
|
|
|
|
34
|
|
|
public function indexAction()
|
|
35
|
|
|
{
|
|
36
|
|
|
$all = $this->blog->findAllOrder('id', 'DESC');
|
|
37
|
|
|
|
|
38
|
|
|
$this->theme->setTitle("A blog");
|
|
|
|
|
|
|
39
|
|
|
$this->views->add('blog/view-blog', [
|
|
|
|
|
|
|
40
|
|
|
'posts' => $all,
|
|
41
|
|
|
'title' => "A Blog",
|
|
42
|
|
|
]);
|
|
43
|
|
|
}
|
|
44
|
|
|
|
|
45
|
|
|
/**
|
|
46
|
|
|
* List all blogposts.
|
|
47
|
|
|
*
|
|
48
|
|
|
* @return void
|
|
49
|
|
|
*/
|
|
50
|
|
View Code Duplication |
public function listAction()
|
|
|
|
|
|
|
51
|
|
|
{
|
|
52
|
|
|
$all = $this->blog->findAll();
|
|
53
|
|
|
|
|
54
|
|
|
$this->theme->setTitle("List all posts");
|
|
|
|
|
|
|
55
|
|
|
$this->views->add('blog/list', [
|
|
|
|
|
|
|
56
|
|
|
'posts' => $all,
|
|
57
|
|
|
'title' => "All blogposts",
|
|
58
|
|
|
]);
|
|
59
|
|
|
}
|
|
60
|
|
|
|
|
61
|
|
|
/**
|
|
62
|
|
|
* View blogpost with slug.
|
|
63
|
|
|
*
|
|
64
|
|
|
* @param int $slug of post to display
|
|
65
|
|
|
*
|
|
66
|
|
|
* @return void
|
|
67
|
|
|
*/
|
|
68
|
|
View Code Duplication |
public function viewAction($slug = null)
|
|
|
|
|
|
|
69
|
|
|
{
|
|
70
|
|
|
$blog = $this->blog->findWhere('slug', $slug);
|
|
71
|
|
|
|
|
72
|
|
|
if (empty($blog)) {
|
|
73
|
|
|
die('No such post!');
|
|
74
|
|
|
}
|
|
75
|
|
|
|
|
76
|
|
|
$post = $blog[0];
|
|
77
|
|
|
|
|
78
|
|
|
$this->theme->setTitle("View post with id");
|
|
|
|
|
|
|
79
|
|
|
$this->views->add('blog/view-post', [
|
|
|
|
|
|
|
80
|
|
|
'post' => $post,
|
|
81
|
|
|
]);
|
|
82
|
|
|
}
|
|
83
|
|
|
|
|
84
|
|
|
/**
|
|
85
|
|
|
* Add new blogpost.
|
|
86
|
|
|
*
|
|
87
|
|
|
*
|
|
88
|
|
|
* @return void
|
|
89
|
|
|
*/
|
|
90
|
|
View Code Duplication |
public function addAction()
|
|
|
|
|
|
|
91
|
|
|
{
|
|
92
|
|
|
|
|
93
|
|
|
$form = new \Anax\Blog\CFormAddPost();
|
|
94
|
|
|
$form->setDI($this->di);
|
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
// Check the status of the form
|
|
97
|
|
|
$form->check();
|
|
98
|
|
|
|
|
99
|
|
|
$this->di->theme->setTitle("Add blogpost");
|
|
100
|
|
|
$this->di->views->add('default/page', [
|
|
101
|
|
|
'title' => "Add a blogpost",
|
|
102
|
|
|
'content' => $form->getHTML()
|
|
103
|
|
|
]);
|
|
104
|
|
|
}
|
|
105
|
|
|
|
|
106
|
|
|
/**
|
|
107
|
|
|
* Edit a blogpost.
|
|
108
|
|
|
*
|
|
109
|
|
|
* @param string $id of post to edit.
|
|
110
|
|
|
*
|
|
111
|
|
|
* @return void
|
|
112
|
|
|
*/
|
|
113
|
|
View Code Duplication |
public function updateAction($id = null)
|
|
|
|
|
|
|
114
|
|
|
{
|
|
115
|
|
|
|
|
116
|
|
|
if (!$id) {
|
|
|
|
|
|
|
117
|
|
|
$this->redirectTo('blog');
|
|
118
|
|
|
}
|
|
119
|
|
|
|
|
120
|
|
|
$blog = $this->blog->find($id);
|
|
121
|
|
|
|
|
122
|
|
|
$form = new \Anax\Blog\CFormUpdatePost($blog);
|
|
123
|
|
|
$form->setDI($this->di);
|
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
// Check the status of the form
|
|
126
|
|
|
$form->check();
|
|
127
|
|
|
|
|
128
|
|
|
$this->di->theme->setTitle("Update blogpost");
|
|
129
|
|
|
$this->di->views->add('default/page', [
|
|
130
|
|
|
'title' => "Update a blogpost",
|
|
131
|
|
|
'content' => $form->getHTML()
|
|
132
|
|
|
]);
|
|
133
|
|
|
}
|
|
134
|
|
|
|
|
135
|
|
|
/**
|
|
136
|
|
|
* Delete blogpost.
|
|
137
|
|
|
*
|
|
138
|
|
|
* @param integer $id of post to delete.
|
|
139
|
|
|
*
|
|
140
|
|
|
* @return void
|
|
141
|
|
|
*/
|
|
142
|
|
|
public function deleteAction($id = null)
|
|
143
|
|
|
{
|
|
144
|
|
|
if (!isset($id)) {
|
|
145
|
|
|
die("Missing id");
|
|
146
|
|
|
}
|
|
147
|
|
|
|
|
148
|
|
|
$res = $this->blog->delete($id);
|
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
$this->redirectTo('blog/list');
|
|
151
|
|
|
}
|
|
152
|
|
|
|
|
153
|
|
|
/**
|
|
154
|
|
|
* Delete (soft) blogpost.
|
|
155
|
|
|
*
|
|
156
|
|
|
* @param integer $id of post to delete.
|
|
157
|
|
|
*
|
|
158
|
|
|
* @return void
|
|
159
|
|
|
*/
|
|
160
|
|
View Code Duplication |
public function softDeleteAction($id = null)
|
|
|
|
|
|
|
161
|
|
|
{
|
|
162
|
|
|
if (!isset($id)) {
|
|
163
|
|
|
die("Missing id");
|
|
164
|
|
|
}
|
|
165
|
|
|
|
|
166
|
|
|
$now = gmdate('Y-m-d H:i:s');
|
|
167
|
|
|
|
|
168
|
|
|
$blog = $this->blog->find($id);
|
|
169
|
|
|
|
|
170
|
|
|
$blog->deleted = $now;
|
|
171
|
|
|
$blog->save();
|
|
172
|
|
|
|
|
173
|
|
|
$this->redirectTo('blog/trash');
|
|
174
|
|
|
}
|
|
175
|
|
|
|
|
176
|
|
|
/**
|
|
177
|
|
|
* Restore (soft) deleted blogpost.
|
|
178
|
|
|
*
|
|
179
|
|
|
* @param integer $id of post to restore.
|
|
180
|
|
|
*
|
|
181
|
|
|
* @return void
|
|
182
|
|
|
*/
|
|
183
|
|
View Code Duplication |
public function restoreAction($id = null)
|
|
|
|
|
|
|
184
|
|
|
{
|
|
185
|
|
|
if (!isset($id)) {
|
|
186
|
|
|
die("Missing id");
|
|
187
|
|
|
}
|
|
188
|
|
|
|
|
189
|
|
|
$blog = $this->blog->find($id);
|
|
190
|
|
|
|
|
191
|
|
|
$blog->deleted = null;
|
|
192
|
|
|
$blog->save();
|
|
193
|
|
|
|
|
194
|
|
|
$this->redirectTo('blog');
|
|
195
|
|
|
}
|
|
196
|
|
|
|
|
197
|
|
|
/**
|
|
198
|
|
|
* Activate blogpost.
|
|
199
|
|
|
*
|
|
200
|
|
|
* @param integer $id of upost to activate.
|
|
201
|
|
|
*
|
|
202
|
|
|
* @return void
|
|
203
|
|
|
*/
|
|
204
|
|
|
public function activateAction($id = null)
|
|
205
|
|
|
{
|
|
206
|
|
|
if (!isset($id)) {
|
|
207
|
|
|
die("Missing id");
|
|
208
|
|
|
}
|
|
209
|
|
|
|
|
210
|
|
|
$blog = $this->blog->find($id);
|
|
211
|
|
|
|
|
212
|
|
|
$blog->inactivated = null;
|
|
213
|
|
|
$blog->save();
|
|
214
|
|
|
|
|
215
|
|
|
$this->redirectTo('blog/list');
|
|
216
|
|
|
}
|
|
217
|
|
|
|
|
218
|
|
|
/**
|
|
219
|
|
|
* Inactivate blogpost.
|
|
220
|
|
|
*
|
|
221
|
|
|
* @param integer $id of post to inactivate.
|
|
222
|
|
|
*
|
|
223
|
|
|
* @return void
|
|
224
|
|
|
*/
|
|
225
|
|
View Code Duplication |
public function inactivateAction($id = null)
|
|
|
|
|
|
|
226
|
|
|
{
|
|
227
|
|
|
if (!isset($id)) {
|
|
228
|
|
|
die("Missing id");
|
|
229
|
|
|
}
|
|
230
|
|
|
|
|
231
|
|
|
$now = gmdate('Y-m-d H:i:s');
|
|
232
|
|
|
|
|
233
|
|
|
$blog = $this->blog->find($id);
|
|
234
|
|
|
|
|
235
|
|
|
$blog->inactivated = $now;
|
|
236
|
|
|
$blog->save();
|
|
237
|
|
|
|
|
238
|
|
|
$this->redirectTo('blog/inactive');
|
|
239
|
|
|
}
|
|
240
|
|
|
|
|
241
|
|
|
/**
|
|
242
|
|
|
* List all active and not deleted blogposts.
|
|
243
|
|
|
*
|
|
244
|
|
|
* @return void
|
|
245
|
|
|
*/
|
|
246
|
|
View Code Duplication |
public function activeAction()
|
|
|
|
|
|
|
247
|
|
|
{
|
|
248
|
|
|
$all = $this->blog->query()
|
|
249
|
|
|
->where('inactivated IS NULL')
|
|
250
|
|
|
->andWhere('deleted is NULL')
|
|
251
|
|
|
->execute();
|
|
252
|
|
|
|
|
253
|
|
|
$this->theme->setTitle("Posts that are active");
|
|
|
|
|
|
|
254
|
|
|
$this->views->add('blog/active', [
|
|
|
|
|
|
|
255
|
|
|
'posts' => $all,
|
|
256
|
|
|
'title' => "Blogposts that are active",
|
|
257
|
|
|
]);
|
|
258
|
|
|
}
|
|
259
|
|
|
|
|
260
|
|
|
/**
|
|
261
|
|
|
* List all inactive blogposts.
|
|
262
|
|
|
*
|
|
263
|
|
|
* @return void
|
|
264
|
|
|
*/
|
|
265
|
|
View Code Duplication |
public function inactiveAction()
|
|
|
|
|
|
|
266
|
|
|
{
|
|
267
|
|
|
$all = $this->blog->query()
|
|
268
|
|
|
->where('inactivated IS NOT NULL')
|
|
269
|
|
|
->execute();
|
|
270
|
|
|
|
|
271
|
|
|
$this->theme->setTitle("Posts that are inactive");
|
|
|
|
|
|
|
272
|
|
|
$this->views->add('blog/inactive', [
|
|
|
|
|
|
|
273
|
|
|
'posts' => $all,
|
|
274
|
|
|
'title' => "Blogposts that are inactive",
|
|
275
|
|
|
]);
|
|
276
|
|
|
}
|
|
277
|
|
|
|
|
278
|
|
|
/**
|
|
279
|
|
|
* List all deleted blogposts.
|
|
280
|
|
|
*
|
|
281
|
|
|
* @return void
|
|
282
|
|
|
*/
|
|
283
|
|
View Code Duplication |
public function trashAction()
|
|
|
|
|
|
|
284
|
|
|
{
|
|
285
|
|
|
$all = $this->blog->query()
|
|
286
|
|
|
->where('deleted IS NOT NULL')
|
|
287
|
|
|
->execute();
|
|
288
|
|
|
|
|
289
|
|
|
$this->theme->setTitle("Posts that are deleted");
|
|
|
|
|
|
|
290
|
|
|
$this->views->add('blog/deleted', [
|
|
|
|
|
|
|
291
|
|
|
'posts' => $all,
|
|
292
|
|
|
'title' => "Blogposts that are deleted",
|
|
293
|
|
|
]);
|
|
294
|
|
|
}
|
|
295
|
|
|
|
|
296
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.