main_controller::get_response()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 4
dl 0
loc 9
ccs 0
cts 4
cp 0
crap 12
rs 10
c 0
b 0
f 0
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\types */
30
	protected $content_types;
31
32
	/** @var \blitze\content\services\feed */
33
	protected $feed;
34
35
	/** @var \blitze\content\services\poll */
36
	protected $poll;
37
38
	/** @var \blitze\content\services\views\views_factory */
39
	protected $views_factory;
40
41
	/** @var \blitze\content\services\comments\factory */
42
	protected $comments_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\types						$content_types		Content types object
53
	 * @param \blitze\content\services\feed							$feed				Feed object
54
	 * @param \blitze\content\services\poll							$poll				Poll object
55
	 * @param \blitze\content\services\views\views_factory			$views_factory		Views handlers
56
	 * @param \blitze\content\services\comments\factory				$comments_factory	Comments factory
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\types $content_types, \blitze\content\services\feed $feed, \blitze\content\services\poll $poll, \blitze\content\services\views\views_factory $views_factory, \blitze\content\services\comments\factory $comments_factory)
59
	{
60
		$this->db = $db;
61
		$this->helper = $helper;
62
		$this->request = $request;
63
		$this->template = $template;
64
		$this->user = $user;
65
		$this->content_types = $content_types;
66
		$this->feed = $feed;
67
		$this->poll = $poll;
68
		$this->views_factory = $views_factory;
69
		$this->comments_factory = $comments_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', 'detail');
105
		$view = (!in_array($view, ['detail', 'print'])) ? 'detail' : $view;
106
107
		$update_count = array();
108
		$entity = $this->get_type_entity($type);
109
		$redirect = $this->get_referrer();
110
111
		$view_handler = $this->views_factory->get($entity->get_content_view());
112
		$topic_data = $view_handler->render_detail($entity, $topic_id, $view, $redirect, $update_count);
113
114
		$this->add_navlink($topic_data['topic_title'], $topic_data['topic_url']);
115
		$this->template->assign_var('TOPIC_POLL', $this->poll->display($topic_data));
116
		$this->template->assign_vars($entity->to_array());
117
118
		if ($view !== 'print')
119
		{
120
			$this->update_views($topic_id, $update_count);
121
			$template_file = $view_handler->get_detail_template();
122
123
			if (($comments = $this->comments_factory->get($entity->get_comments())) !== null)
124
			{
125
				$comments->show_comments($type, $topic_data, $update_count, $entity->get_comments_settings());
126
				$comments->show_form($topic_data);
127
			}
128
		}
129
		else
130
		{
131
			$template_file = 'views/print.html';
132
			$this->template->assign_var('TOPIC_URL', generate_board_url(true) . $topic_data['topic_url']);
133
		}
134
135
		return $this->helper->render($template_file, $topic_data['topic_title']);
136
	}
137
138
	/**
139
	 * Filter topics by a filter
140
	 *
141
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
142
	 */
143
	public function types()
144
	{
145
		$types = $this->content_types->get_all_types();
146
147
		foreach ($types as &$type)
148
		{
149
			/** @var \blitze\content\model\entity\type $type */
150
			$type = array(
151
				'name'	=> $type->get_content_langname(),
152
				'desc'	=> $type->get_content_desc(),
153
				'color'	=> $type->get_content_colour(),
154
				'url'	=> $this->helper->route('blitze_content_type', array('type' => $type->get_content_name())),
155
			);
156
		}
157
		$this->template->assign_var('types', $types);
158
159
		return $this->helper->render('content_types.html', '');
160
	}
161
162
	/**
163
	 * Filter topics by a filter
164
	 *
165
	 * @param string $_format
166
	 * @param string $filter_type
167
	 * @param mixed $filter_value
168
	 * @param int $page
169
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
170
	 */
171
	public function filter($_format = '', $filter_type = '', $filter_value = '', $page = 1)
172
	{
173
		$filter = $this->get_filter($filter_type, $filter_value);
174
175
		/** @var \blitze\content\services\views\driver\portal $view_handler */
176
		$view_handler = $this->views_factory->get('blitze.content.view.portal');
177
		$max_update_time = $view_handler->render_filter($filter, $page);
178
179
		return $this->get_response($max_update_time, $_format, $view_handler->get_index_template());
180
	}
181
182
	/**
183
	 * @param string $type
184
	 * @return \blitze\content\model\entity\type
185
	 */
186
	protected function get_type_entity($type)
187
	{
188
		/** @var \blitze\content\model\entity\type $entity */
189
		$entity = $this->content_types->get_type($type, true);
190
191
		$this->add_navlink($entity->get_content_langname(), $this->helper->route('blitze_content_type', array('type' => $type)));
192
193
		$this->template->assign_vars(array(
194
			'S_COMMENTS'	=> $entity->get_comments(),
195
			'S_VIEWS'		=> $entity->get_allow_views(),
196
			'S_TOOLS'		=> true,
197
			'FIELD_TYPES'	=> $entity->get_field_types(),
198
		));
199
200
		return $entity;
201
	}
202
203
	/**
204
	 * @param string $title
205
	 * @param string $url
206
	 * @return void
207
	 */
208
	protected function add_navlink($title, $url)
209
	{
210
		$this->template->assign_block_vars('navlinks', array(
211
			'FORUM_NAME'	=> $title,
212
			'U_VIEW_FORUM'	=> $url,
213
		));
214
	}
215
216
	/**
217
	 * Update topic view and if necessary attachment view counters ... but only for humans and if this is the first 'page view'
218
	 * @param int $topic_id
219
	 * @param array $update_count
220
	 * @return void
221
	 */
222
	protected function update_views($topic_id, array $update_count)
223
	{
224
		if (!$this->user->data['is_bot'] && !$this->request->is_set('page'))
225
		{
226
			$sql = 'UPDATE ' . TOPICS_TABLE . '
227
				SET topic_views = topic_views + 1, topic_last_view_time = ' . time() . "
228
				WHERE topic_id = $topic_id";
229
			$this->db->sql_query($sql);
230
231
			// Update the attachment download counts
232
			if (sizeof($update_count))
233
			{
234
				$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
235
					SET download_count = download_count + 1
236
					WHERE ' . $this->db->sql_in_set('attach_id', array_unique($update_count));
237
				$this->db->sql_query($sql);
238
			}
239
		}
240
	}
241
242
	/**
243
	 * @param string $filter_type
244
	 * @param mixed $filter_value
245
	 * @return array
246
	 */
247
	protected function get_filter($filter_type, $filter_value)
248
	{
249
		if ($filter_type)
250
		{
251
			$filters = array($filter_type => (array) $filter_value);
252
		}
253
		else
254
		{
255
			$filters = $this->request->variable('filters', array('' => array('' => '')), true);
256
		}
257
258
		return array_filter($filters);
259
	}
260
261
	/**
262
	 * @param int $max_update_time
263
	 * @param string $_format
264
	 * @param string $view_template
265
	 * @param string $page_title
266
	 * @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
267
	 */
268
	protected function get_response($max_update_time, $_format, $view_template, $page_title = '')
269
	{
270
		if ($_format === 'xml')
271
		{
272
			
273
			return $this->feed->render($max_update_time ?: time());
274
		}
275
276
		return $this->helper->render($view_template, $page_title);
277
	}
278
279
	/**
280
	 * @return string
281
	 */
282
	protected function get_referrer()
283
	{
284
		$referrer = $this->request->get_super_global(\phpbb\request\request_interface::SERVER)['HTTP_REFERER'];
0 ignored issues
show
Bug introduced by
phpbb\request\request_interface::SERVER of type integer is incompatible with the type phpbb\request\request_interface expected by parameter $super_global of phpbb\request\request_in...ace::get_super_global(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

284
		$referrer = $this->request->get_super_global(/** @scrutinizer ignore-type */ \phpbb\request\request_interface::SERVER)['HTTP_REFERER'];
Loading history...
285
		$referrer = parse_url($referrer, PHP_URL_PATH);
286
		return urlencode(str_replace(array('&amp;', $this->user->page['root_script_path']), array('&', ''), $referrer));
287
	}
288
}
289