1
|
|
|
<?php |
2
|
|
|
namespace Chp\TextContent; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* A blog controller |
6
|
|
|
* Made by Rasmus Berg (c) 2014-2017 |
7
|
|
|
* |
8
|
|
|
* @Property Object $this->di Anax-MVC class handler |
9
|
|
|
* @Property Object $this->response Anax-MVC Php Header class |
10
|
|
|
* @Property Object $this->url Anax-MVC url-handler class |
11
|
|
|
* @Property Object $this->theme Anax-MVC theme-handler class |
12
|
|
|
* @Property Object $this->views Anax-MVC views-handler class |
13
|
|
|
* @Property Object $this->textFilter Anax-MVC textformat-handler class |
14
|
|
|
*/ |
15
|
|
|
class BlogController implements \Anax\DI\IInjectionAware |
16
|
|
|
{ |
17
|
|
|
use \Anax\DI\TInjectable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Properties |
21
|
|
|
*/ |
22
|
|
|
private $content = null; |
23
|
|
|
private $postsPerPage = null; |
24
|
|
|
private $urlPrefix = "content.php/"; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Initialize the controller |
28
|
|
|
* |
29
|
|
|
* @Return Void |
30
|
|
|
*/ |
31
|
2 |
|
public function initialize(){ |
32
|
2 |
|
$this->content = new \Chp\TextContent\Content(); |
33
|
2 |
|
$this->content->setDI($this->di); |
|
|
|
|
34
|
2 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Index action - uses tagAction whitout tag |
38
|
|
|
* |
39
|
|
|
* @Return Void |
40
|
|
|
*/ |
41
|
|
|
public function indexAction($page = null){ |
42
|
|
|
$posts = $this->getBlogContent($page); |
43
|
|
|
|
44
|
|
|
if(count($posts) > 0){ |
45
|
|
|
foreach($posts AS $key => $post){ |
46
|
|
|
// Prepare blog post for show in view |
47
|
|
|
$posts[$key] = $this->preparePost($post); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Show blog posts in view |
52
|
|
|
$this->postsToView($posts, null); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get blog posts by tag |
57
|
|
|
* |
58
|
|
|
* @Param String $tag Blog-tag |
59
|
|
|
* @Param Integer $page Page on paging |
60
|
|
|
* @Return Void |
61
|
|
|
*/ |
62
|
|
|
public function tagAction($tag = null, $page = null){ |
63
|
|
|
$posts = $this->getBlogContent($page, $tag); |
64
|
|
|
|
65
|
|
|
if(count($posts) == 0 && !is_null($tag)) |
66
|
|
|
$this->response->redirect($this->url->create($this->urlPrefix . 'blog/')); |
|
|
|
|
67
|
|
|
|
68
|
|
|
foreach($posts AS $key => $post){ |
69
|
|
|
// Prepare blog post for show in view |
70
|
|
|
$posts[$key] = $this->preparePost($post, $tag); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->postsToView($posts, $tag); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get blog post by slug |
78
|
|
|
* |
79
|
|
|
* @Param String $slug Blog-tag |
80
|
|
|
* @Return Void |
81
|
|
|
*/ |
82
|
|
|
public function readAction($slug = null){ |
83
|
|
|
|
84
|
|
|
if(is_null($slug)){ |
85
|
|
|
$this->response->redirect($this->url->create($this->urlPrefix . 'blog/')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$post = $this->content->getContentBySlug($slug, 'blog-post'); |
89
|
|
|
|
90
|
|
|
if(empty($post)){ |
91
|
|
|
$this->response->redirect($this->url->create($this->urlPrefix . 'blog/')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Prepare blog post for show in view |
95
|
|
|
$post = $this->preparePost($post); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$title = "Blog - {$post->title}"; |
98
|
|
|
|
99
|
|
|
$this->theme->setTitle($title); |
100
|
|
|
$this->views->add('text-content/blog-post', |
101
|
|
|
[ |
102
|
|
|
'title' => $title, |
103
|
|
|
'post' => $post, |
104
|
|
|
'blogIndexUrl' => $this->url->create($this->urlPrefix . 'blog/') |
105
|
|
|
] |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
/*$this->dispatcher->forward([ |
109
|
|
|
'controller' => 'comment', |
110
|
|
|
'action' => 'view', |
111
|
|
|
'params' => ["blog/read/{$post->slug}"] |
112
|
|
|
]);*/ |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get blog posts by tag or type |
117
|
|
|
* |
118
|
|
|
* @Param Int $page Page asked for |
119
|
|
|
* @Param String $tag Tag-slug to search for |
120
|
|
|
* @Return Array $posts A array with post objects |
121
|
|
|
*/ |
122
|
1 |
|
public function getBlogContent($page = null, $tag = null){ |
123
|
1 |
|
if(!is_null($tag)) |
124
|
1 |
|
return $this->content->getAllContentOfTag($tag, 'blog-post', $page, $this->postsPerPage); |
125
|
|
|
else |
126
|
1 |
|
return $this->content->getAllContentOfType('blog-post', $page, $this->postsPerPage); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Prepare blog post to show in view |
131
|
|
|
* |
132
|
|
|
* @Param Array $post Blog post object |
133
|
|
|
* @Return Object $result Prepared blog post object |
134
|
|
|
*/ |
135
|
1 |
|
public function preparePost($post = null){ |
136
|
1 |
|
$result = null; |
137
|
|
|
|
138
|
1 |
|
if(!is_null($post)){ |
139
|
1 |
|
$result = (object)[]; |
140
|
|
|
|
141
|
1 |
|
foreach($post as $key => $value){ |
142
|
1 |
|
$result->{$key} = $value; |
143
|
1 |
|
} |
144
|
|
|
|
145
|
1 |
|
$result->title = htmlspecialchars($post->title, ENT_QUOTES); |
146
|
1 |
|
$result->ingress = htmlspecialchars($post->ingress, ENT_QUOTES); |
147
|
1 |
|
$result->text = $this->textFilter->doFilter(htmlspecialchars($post->text, ENT_QUOTES), $post->filters); |
|
|
|
|
148
|
1 |
|
$result->editUrl = $this->url->create("content/edit/{$post->id}"); |
|
|
|
|
149
|
1 |
|
$result->showUrl = $this->url->create("blog/read/" . $post->slug); |
|
|
|
|
150
|
|
|
//$result->authorId = $post->author; |
151
|
|
|
//$result->authorName = htmlspecialchars($post->name, ENT_QUOTES); |
152
|
|
|
//$result->authorUrl = $this->url->create('users/id/' . $post->author); |
153
|
|
|
|
154
|
|
|
//unset($result->author); |
155
|
|
|
|
156
|
1 |
|
$tags = $this->content->getTagsForContent($post->id); |
157
|
|
|
|
158
|
1 |
|
foreach($tags AS $item_key => $item){ |
159
|
1 |
|
$tags[$item_key]->tag = htmlspecialchars($item->tag, ENT_QUOTES); |
160
|
1 |
|
$tags[$item_key]->url = $this->url->create($this->urlPrefix . "blog/tag/{$item->slug}"); |
161
|
1 |
|
} |
162
|
|
|
|
163
|
1 |
|
$result->tags = $tags; |
164
|
1 |
|
} |
165
|
|
|
|
166
|
1 |
|
return $result; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Show blog posts in view |
171
|
|
|
* |
172
|
|
|
* @Param Array $posts Array of blog post objects |
173
|
|
|
* @Param String $tag Tag-slug which has give this result |
174
|
|
|
* @Return Void |
175
|
|
|
*/ |
176
|
|
|
private function postsToView($posts = array(), $tag = null){ |
177
|
|
|
$tag_title = null; |
178
|
|
|
|
179
|
|
|
if(!is_null($tag)) |
180
|
|
|
$tag_title = $this->content->getTagBySlug($tag); |
181
|
|
|
|
182
|
|
|
$title = "Blog" . ((!is_null($tag_title)) ? " - tag: {$tag_title}" : NULL); |
183
|
|
|
|
184
|
|
|
$this->theme->setTitle($title); |
185
|
|
|
$this->views->add('text-content/blog-index', |
186
|
|
|
[ |
187
|
|
|
'title' => $title, |
188
|
|
|
'posts' => $posts, |
189
|
|
|
'tag' => $tag_title |
190
|
|
|
] |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
} |
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.