1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2016 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\content\controller; |
11
|
|
|
|
12
|
|
|
class main_controller |
13
|
|
|
{ |
14
|
|
|
/** @var \phpbb\db\driver\driver_interface */ |
15
|
|
|
protected $db; |
16
|
|
|
|
17
|
|
|
/** @var \phpbb\controller\helper */ |
18
|
|
|
protected $helper; |
19
|
|
|
|
20
|
|
|
/** @var \phpbb\request\request_interface */ |
21
|
|
|
protected $request; |
22
|
|
|
|
23
|
|
|
/** @var \phpbb\template\template */ |
24
|
|
|
protected $template; |
25
|
|
|
|
26
|
|
|
/** @var \phpbb\user */ |
27
|
|
|
protected $user; |
28
|
|
|
|
29
|
|
|
/* @var \blitze\content\services\comments\comments_interface */ |
30
|
|
|
protected $comments; |
31
|
|
|
|
32
|
|
|
/** @var \blitze\content\services\types */ |
33
|
|
|
protected $content_types; |
34
|
|
|
|
35
|
|
|
/** @var \blitze\content\services\feed */ |
36
|
|
|
protected $feed; |
37
|
|
|
|
38
|
|
|
/** @var \blitze\content\services\poll */ |
39
|
|
|
protected $poll; |
40
|
|
|
|
41
|
|
|
/** @var \blitze\content\services\views\views_factory */ |
42
|
|
|
protected $views_factory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor |
46
|
|
|
* |
47
|
|
|
* @param \phpbb\db\driver\driver_interface $db Database object |
48
|
|
|
* @param \phpbb\controller\helper $helper Helper object |
49
|
|
|
* @param \phpbb\request\request_interface $request Request object |
50
|
|
|
* @param \phpbb\template\template $template Template object |
51
|
|
|
* @param \phpbb\user $user User object |
52
|
|
|
* @param \blitze\content\services\comments\comments_interface $comments Comments object |
53
|
|
|
* @param \blitze\content\services\types $content_types Content types object |
54
|
|
|
* @param \blitze\content\services\feed $feed Feed object |
55
|
|
|
* @param \blitze\content\services\poll $poll Poll object |
56
|
|
|
* @param \blitze\content\services\views\views_factory $views_factory Views handlers |
57
|
|
|
*/ |
58
|
|
|
public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\controller\helper $helper, \phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\user $user, \blitze\content\services\comments\comments_interface $comments, \blitze\content\services\types $content_types, \blitze\content\services\feed $feed, \blitze\content\services\poll $poll, \blitze\content\services\views\views_factory $views_factory) |
59
|
|
|
{ |
60
|
|
|
$this->db = $db; |
61
|
|
|
$this->helper = $helper; |
62
|
|
|
$this->request = $request; |
63
|
|
|
$this->template = $template; |
64
|
|
|
$this->user = $user; |
65
|
|
|
$this->comments = $comments; |
66
|
|
|
$this->content_types = $content_types; |
67
|
|
|
$this->feed = $feed; |
68
|
|
|
$this->poll = $poll; |
69
|
|
|
$this->views_factory = $views_factory; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Display list of topics for content type |
74
|
|
|
* |
75
|
|
|
* @param string $type |
76
|
|
|
* @param string $_format |
77
|
|
|
* @param string $filter_type |
78
|
|
|
* @param mixed $filter_value |
79
|
|
|
* @param int $page |
80
|
|
|
* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object |
81
|
|
|
*/ |
82
|
|
|
public function index($type, $_format = '', $filter_type = '', $filter_value = '', $page = 1) |
83
|
|
|
{ |
84
|
|
|
$entity = $this->get_type_entity($type); |
85
|
|
|
$filter = $this->get_filter($filter_type, $filter_value); |
86
|
|
|
|
87
|
|
|
$this->template->assign_vars($entity->to_array()); |
88
|
|
|
|
89
|
|
|
$view_handler = $this->views_factory->get($entity->get_content_view()); |
90
|
|
|
$max_update_time = $view_handler->render_index($entity, $page, $filter); |
91
|
|
|
|
92
|
|
|
return $this->get_response($max_update_time, $_format, $view_handler->get_index_template(), $entity->get_content_langname()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Display a single topic |
97
|
|
|
* |
98
|
|
|
* @param string $type |
99
|
|
|
* @param int $topic_id |
100
|
|
|
* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object |
101
|
|
|
*/ |
102
|
|
|
public function show($type, $topic_id) |
103
|
|
|
{ |
104
|
|
|
$view = $this->request->variable('view', ''); |
105
|
|
|
$entity = $this->get_type_entity($type); |
106
|
|
|
|
107
|
|
|
$update_count = array(); |
108
|
|
|
$view_handler = $this->views_factory->get($entity->get_content_view()); |
109
|
|
|
$topic_data = $view_handler->render_detail($entity, $topic_id, $update_count); |
110
|
|
|
|
111
|
|
|
$this->add_navlink($topic_data['topic_title'], $topic_data['topic_url']); |
112
|
|
|
$this->template->assign_var('TOPIC_POLL', $this->poll->display($topic_data)); |
113
|
|
|
$this->template->assign_vars($entity->to_array()); |
114
|
|
|
|
115
|
|
|
if ($entity->get_allow_comments()) |
116
|
|
|
{ |
117
|
|
|
$this->comments->show_comments($type, $topic_data, $update_count); |
118
|
|
|
$this->comments->show_form($topic_data); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($view !== 'print') |
122
|
|
|
{ |
123
|
|
|
$this->update_views($topic_id, $update_count); |
124
|
|
|
$template_file = $view_handler->get_detail_template(); |
125
|
|
|
} |
126
|
|
|
else |
127
|
|
|
{ |
128
|
|
|
$template_file = 'views/print.html'; |
129
|
|
|
$this->template->assign_var('TOPIC_URL', generate_board_url(true) . $topic_data['topic_url']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->helper->render($template_file, $topic_data['topic_title']); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Filter topics by a filter |
137
|
|
|
* |
138
|
|
|
* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object |
139
|
|
|
*/ |
140
|
|
|
public function types() |
141
|
|
|
{ |
142
|
|
|
$types = $this->content_types->get_all_types(); |
143
|
|
|
|
144
|
|
|
foreach ($types as &$type) |
145
|
|
|
{ |
146
|
|
|
$type = array( |
147
|
|
|
'name' => $type->get_content_langname(), |
148
|
|
|
'desc' => $type->get_content_desc(), |
149
|
|
|
'color' => $type->get_content_colour(), |
150
|
|
|
'url' => $this->helper->route('blitze_content_type', array('type' => $type->get_content_name())), |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
$this->template->assign_var('types', $types); |
154
|
|
|
|
155
|
|
|
return $this->helper->render('content_types.html', ''); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Filter topics by a filter |
160
|
|
|
* |
161
|
|
|
* @param string $_format |
162
|
|
|
* @param string $filter_type |
163
|
|
|
* @param mixed $filter_value |
164
|
|
|
* @param int $page |
165
|
|
|
* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object |
166
|
|
|
*/ |
167
|
|
|
public function filter($_format = '', $filter_type = '', $filter_value = '', $page = 1) |
168
|
|
|
{ |
169
|
|
|
$filter = $this->get_filter($filter_type, $filter_value); |
170
|
|
|
|
171
|
|
|
/** @var \blitze\content\services\views\driver\portal $view_handler */ |
172
|
|
|
$view_handler = $this->views_factory->get('blitze.content.view.portal'); |
173
|
|
|
$max_update_time = $view_handler->render_filter($filter, $page); |
174
|
|
|
|
175
|
|
|
return $this->get_response($max_update_time, $_format, $view_handler->get_index_template()); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $type |
180
|
|
|
* @return \blitze\content\model\entity\type |
181
|
|
|
*/ |
182
|
|
|
protected function get_type_entity($type) |
183
|
|
|
{ |
184
|
|
|
/** @var \blitze\content\model\entity\type $entity */ |
185
|
|
|
$entity = $this->content_types->get_type($type, true); |
186
|
|
|
|
187
|
|
|
$this->add_navlink($entity->get_content_langname(), $this->helper->route('blitze_content_type', array('type' => $type))); |
188
|
|
|
|
189
|
|
|
$this->template->assign_vars(array( |
190
|
|
|
'S_COMMENTS' => $entity->get_allow_comments(), |
191
|
|
|
'S_VIEWS' => $entity->get_allow_views(), |
192
|
|
|
'S_TOOLS' => true, |
193
|
|
|
)); |
194
|
|
|
|
195
|
|
|
return $entity; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param string $title |
200
|
|
|
* @param string $url |
201
|
|
|
* @return void |
202
|
|
|
*/ |
203
|
|
|
protected function add_navlink($title, $url) |
204
|
|
|
{ |
205
|
|
|
$this->template->assign_block_vars('navlinks', array( |
206
|
|
|
'FORUM_NAME' => $title, |
207
|
|
|
'U_VIEW_FORUM' => $url, |
208
|
|
|
)); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Update topic view and if necessary attachment view counters ... but only for humans and if this is the first 'page view' |
213
|
|
|
* @param int $topic_id |
214
|
|
|
* @param array $update_count |
215
|
|
|
* @return void |
216
|
|
|
*/ |
217
|
|
|
protected function update_views($topic_id, array $update_count) |
218
|
|
|
{ |
219
|
|
|
if (!$this->user->data['is_bot'] && !$this->request->is_set('page')) |
220
|
|
|
{ |
221
|
|
|
$sql = 'UPDATE ' . TOPICS_TABLE . ' |
222
|
|
|
SET topic_views = topic_views + 1, topic_last_view_time = ' . time() . " |
223
|
|
|
WHERE topic_id = $topic_id"; |
224
|
|
|
$this->db->sql_query($sql); |
225
|
|
|
|
226
|
|
|
// Update the attachment download counts |
227
|
|
|
if (sizeof($update_count)) |
228
|
|
|
{ |
229
|
|
|
$sql = 'UPDATE ' . ATTACHMENTS_TABLE . ' |
230
|
|
|
SET download_count = download_count + 1 |
231
|
|
|
WHERE ' . $this->db->sql_in_set('attach_id', array_unique($update_count)); |
232
|
|
|
$this->db->sql_query($sql); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param string $filter_type |
239
|
|
|
* @param mixed $filter_value |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
|
|
protected function get_filter($filter_type, $filter_value) |
243
|
|
|
{ |
244
|
|
|
if ($filter_type) |
245
|
|
|
{ |
246
|
|
|
$filters = array($filter_type => (array) $filter_value); |
247
|
|
|
} |
248
|
|
|
else |
249
|
|
|
{ |
250
|
|
|
$filters = $this->request->variable('filters', array('' => array('' => '')), true); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return array_filter($filters); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param int $max_update_time |
258
|
|
|
* @param string $_format |
259
|
|
|
* @param string $view_template |
260
|
|
|
* @param string $page_title |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
|
|
protected function get_response($max_update_time, $_format, $view_template, $page_title = '') |
264
|
|
|
{ |
265
|
|
|
if ($_format === 'xml') |
266
|
|
|
{ |
267
|
|
|
return $this->feed->render($max_update_time); |
|
|
|
|
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return $this->helper->render($view_template, $page_title); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.