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