1
|
|
|
<?php |
2
|
|
|
namespace Chp\TextContent; |
3
|
|
|
|
4
|
1 |
|
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 = CHP_TC_POSTSPERPAGE; |
26
|
|
|
private $urlPrefix = CHP_TC_URLPREFIX; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Initialize the controller |
30
|
|
|
* |
31
|
|
|
* @Return Void |
32
|
|
|
*/ |
33
|
2 |
View Code Duplication |
public function initialize(){ |
|
|
|
|
34
|
2 |
|
if(!is_object($this->di)) |
35
|
2 |
|
throw new \Anax\Exception\InternalServerErrorException('"$this->di" is not valid!'); |
36
|
|
|
|
37
|
2 |
|
$this->content = new \Chp\TextContent\Content(); |
38
|
2 |
|
$this->content->setDI($this->di); |
39
|
2 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Index action - uses tagAction whitout tag |
43
|
|
|
* |
44
|
|
|
* @Return Void |
45
|
|
|
*/ |
46
|
|
|
public function indexAction($page = null){ |
47
|
|
|
$posts = $this->getBlogContent($page); |
48
|
|
|
|
49
|
|
|
if(count($posts) > 0){ |
50
|
|
|
foreach($posts AS $key => $post){ |
51
|
|
|
// Prepare blog post for show in view |
52
|
|
|
$posts[$key] = $this->preparePost($post); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Show blog posts in view |
57
|
|
|
$this->postsToView($posts, null); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get blog posts by tag |
62
|
|
|
* |
63
|
|
|
* @Param String $tag Blog-tag |
64
|
|
|
* @Param Integer $page Page on paging |
65
|
|
|
* @Return Void |
66
|
|
|
*/ |
67
|
|
|
public function tagAction($tag = null, $page = null){ |
68
|
|
|
$posts = $this->getBlogContent($page, $tag); |
69
|
|
|
|
70
|
|
|
if(count($posts) == 0 && !is_null($tag)) |
71
|
|
|
$this->response->redirect($this->url->create($this->urlPrefix . 'blog/')); |
72
|
|
|
|
73
|
|
|
foreach($posts AS $key => $post){ |
74
|
|
|
// Prepare blog post for show in view |
75
|
|
|
$posts[$key] = $this->preparePost($post); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->postsToView($posts, $tag); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get blog post by slug |
83
|
|
|
* |
84
|
|
|
* @Param String $slug Blog-tag |
85
|
|
|
* @Return Void |
86
|
|
|
*/ |
87
|
1 |
|
public function readAction($slug = null){ |
88
|
|
|
|
89
|
|
|
if(is_null($slug)){ |
90
|
|
|
$this->response->redirect($this->url->create($this->urlPrefix . 'blog/')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$post = $this->content->getContentBySlug($slug, 'blog-post'); |
94
|
|
|
|
95
|
|
|
if(empty($post)){ |
96
|
|
|
$this->response->redirect($this->url->create($this->urlPrefix . 'blog/')); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Prepare blog post for show in view |
100
|
|
|
$post = $this->preparePost($post); |
101
|
|
|
|
102
|
|
|
$title = "Blog - {$post->title}"; |
103
|
|
|
|
104
|
|
|
$this->theme->setTitle($title); |
105
|
|
|
$this->views->add('text-content/blog-post', |
106
|
|
|
[ |
107
|
|
|
'title' => $title, |
108
|
|
|
'post' => $post, |
109
|
1 |
|
'blogIndexUrl' => $this->url->create($this->urlPrefix . 'blog/') |
110
|
|
|
] |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
/*$this->dispatcher->forward([ |
114
|
|
|
'controller' => 'comment', |
115
|
|
|
'action' => 'view', |
116
|
|
|
'params' => ["blog/read/{$post->slug}"] |
117
|
|
|
]);*/ |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get blog posts by tag or type |
122
|
|
|
* |
123
|
|
|
* @Param Int $page Page asked for |
124
|
|
|
* @Param String $tag Tag-slug to search for |
125
|
|
|
* @Return Array $posts A array with post objects |
126
|
|
|
*/ |
127
|
1 |
|
public function getBlogContent($page = null, $tag = null){ |
128
|
1 |
|
if(!is_null($tag)) |
129
|
1 |
|
return $this->content->getAllContentOfTag($tag, 'blog-post', $page, $this->postsPerPage); |
130
|
|
|
else |
131
|
1 |
|
return $this->content->getAllContentOfType('blog-post', $page, $this->postsPerPage); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Prepare blog post to show in view |
136
|
|
|
* |
137
|
|
|
* @Param Object $post Blog post object |
138
|
|
|
* @Return Object $result Prepared blog post object |
139
|
|
|
*/ |
140
|
1 |
|
public function preparePost($post){ |
141
|
1 |
|
$result = null; |
142
|
|
|
|
143
|
1 |
|
if(is_object($post) && property_exists($post, 'title')){ |
144
|
1 |
|
$result = (object)[]; |
145
|
|
|
|
146
|
1 |
|
foreach($post as $key => $value){ |
147
|
1 |
|
$result->{$key} = $value; |
148
|
1 |
|
} |
149
|
|
|
|
150
|
1 |
|
$result->title = htmlspecialchars($post->title, ENT_QUOTES); |
151
|
1 |
|
$result->ingress = htmlspecialchars($post->ingress, ENT_QUOTES); |
152
|
1 |
|
$result->text = $this->textFilter->doFilter(htmlspecialchars($post->text, ENT_QUOTES), $post->filters); |
153
|
1 |
|
$result->editUrl = $this->url->create("content/edit/{$post->id}"); |
154
|
1 |
|
$result->showUrl = $this->url->create("blog/read/" . $post->slug); |
155
|
|
|
//$result->authorId = $post->author; |
156
|
|
|
//$result->authorName = htmlspecialchars($post->name, ENT_QUOTES); |
157
|
|
|
//$result->authorUrl = $this->url->create('users/id/' . $post->author); |
158
|
|
|
|
159
|
|
|
//unset($result->author); |
160
|
|
|
|
161
|
1 |
|
$tags = $this->content->getTagsForContent($post->id); |
162
|
|
|
|
163
|
1 |
|
foreach($tags AS $item_key => $item){ |
164
|
1 |
|
$tags[$item_key]->tag = htmlspecialchars($item->tag, ENT_QUOTES); |
165
|
1 |
|
$tags[$item_key]->url = $this->url->create($this->urlPrefix . "blog/tag/{$item->slug}"); |
166
|
1 |
|
} |
167
|
|
|
|
168
|
1 |
|
$result->tags = $tags; |
169
|
1 |
|
} |
170
|
|
|
|
171
|
1 |
|
return $result; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Show blog posts in view |
176
|
|
|
* |
177
|
|
|
* @Param Object $posts Object of blog post objects |
178
|
|
|
* @Param String $tag Tag-slug which has give this result |
179
|
|
|
* @Return Void |
180
|
|
|
*/ |
181
|
|
|
private function postsToView($posts, $tag = null){ |
182
|
|
|
$tag_title = null; |
183
|
|
|
|
184
|
|
|
if(!is_null($tag)) |
185
|
|
|
$tag_title = $this->content->getTagBySlug($tag); |
186
|
|
|
|
187
|
|
|
$title = "Blog" . ((!is_null($tag_title)) ? " - tag: {$tag_title}" : NULL); |
188
|
|
|
|
189
|
|
|
$this->theme->setTitle($title); |
190
|
|
|
$this->views->add('text-content/blog-index', |
191
|
|
|
[ |
192
|
|
|
'title' => $title, |
193
|
|
|
'posts' => $posts, |
194
|
|
|
'tag' => $tag_title |
195
|
|
|
] |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.