Completed
Push — develop ( 733603...f85365 )
by Daniel
15:29 queued 09:35
created

forum_topics::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 11
cts 11
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 9
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\blocks;
11
12
use blitze\sitemaker\services\blocks\driver\block;
13
use Nickvergessen\TrimMessage\TrimMessage;
14
15
/**
16
 * Forum Topics Block
17
 */
18
class forum_topics extends block
19
{
20
	/** @var \phpbb\auth\auth */
21
	protected $auth;
22
23
	/** @var \phpbb\content_visibility */
24
	protected $content_visibility;
25
26
	/** @var \phpbb\language\language */
27
	protected $translator;
28
29
	/** @var \phpbb\user */
30
	protected $user;
31
32
	/** @var \blitze\sitemaker\services\date_range */
33
	protected $date_range;
34
35
	/** @var \blitze\sitemaker\services\forum\data */
36
	protected $forum_data;
37
38
	/** @var \blitze\sitemaker\services\forum\options */
39
	protected $forum_options;
40
41
	/** @var string */
42
	protected $phpbb_root_path;
43
44
	/** @var string */
45
	protected $php_ext;
46
47
	/** @var array */
48
	private $fields = array();
49
50
	/** @var array */
51
	private $settings = array();
52
53
	/** @var array */
54
	private $topic_tracking_info = array();
55
56
	const FORUMS_ORDER_FIRST_POST = 0;
57
	const FORUMS_ORDER_LAST_POST = 1;
58
	const FORUMS_ORDER_LAST_READ = 2;
59
60
	/**
61
	 * Constructor
62
	 *
63
	 * @param \phpbb\auth\auth							$auth				Permission object
64
	 * @param \phpbb\content_visibility					content_visibility	Content visibility object
65
	 * @param \phpbb\language\language					$translator			Language object
66
	 * @param \phpbb\user								$user				User object
67
	 * @param \blitze\sitemaker\services\date_range		$date_range			Date Range Object
68
	 * @param \blitze\sitemaker\services\forum\data		$forum_data			Forum Data object
69
	 * @param \blitze\sitemaker\services\forum\options	$forum_options		Forum Data object
70
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
71
	 * @param string									$php_ext			php file extension
72
	 */
73 6
	public function __construct(\phpbb\auth\auth $auth, \phpbb\content_visibility $content_visibility, \phpbb\language\language $translator, \phpbb\user $user, \blitze\sitemaker\services\date_range $date_range, \blitze\sitemaker\services\forum\data $forum_data, \blitze\sitemaker\services\forum\options $forum_options, $phpbb_root_path, $php_ext)
74
	{
75 6
		$this->auth = $auth;
76 6
		$this->content_visibility = $content_visibility;
77 6
		$this->translator = $translator;
78 6
		$this->user = $user;
79 6
		$this->date_range = $date_range;
80 6
		$this->forum_data = $forum_data;
81 6
		$this->forum_options = $forum_options;
82 6
		$this->phpbb_root_path = $phpbb_root_path;
83 6
		$this->php_ext = $php_ext;
84 6
	}
85
86
	/**
87
	 * {@inheritdoc}
88
	 */
89 1
	public function get_config(array $settings)
90
	{
91 1
		$forum_options = $this->forum_options->get_all();
92 1
		$topic_type_options = $this->get_topic_type_options();
93 1
		$preview_options = $this->get_preview_options();
94 1
		$range_options = $this->get_range_options();
95 1
		$sort_options = $this->get_sorting_options();
96 1
		$template_options = $this->get_view_options();
97
98
		return array(
99 1
			'legend1'		=> 'SETTINGS',
100 1
			'forum_ids'			=> array('lang' => 'SELECT_FORUMS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $forum_options, 'default' => array(), 'explain' => false),
101 1
			'topic_type'		=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'checkbox', 'options' => $topic_type_options, 'default' => array(), 'explain' => false),
102 1
			'max_topics'		=> array('lang' => 'MAX_TOPICS', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'explain' => false, 'default' => 5),
103 1
			'date_range'		=> array('lang' => 'LIMIT_POST_TIME', 'validate' => 'string', 'type' => 'select', 'options' => $range_options, 'default' => '', 'explain' => false),
104 1
			'order_by'			=> array('lang' => 'ORDER_BY', 'validate' => 'string', 'type' => 'select', 'options' => $sort_options, 'default' => self::FORUMS_ORDER_LAST_POST, 'explain' => false),
105
106 1
			'legend2'		=> 'DISPLAY',
107 1
			'enable_tracking'	=> array('lang' => 'ENABLE_TOPIC_TRACKING', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false, 'default' => false),
108 1
			'topic_title_limit'	=> array('lang' => 'TOPIC_TITLE_LIMIT', 'validate' => 'int:0:255', 'type' => 'number:0:255', 'maxlength' => 3, 'explain' => false, 'default' => 25),
109 1
			'template'			=> array('lang' => 'TEMPLATE', 'validate' => 'string', 'type' => 'select', 'options' => $template_options, 'default' => 'titles', 'explain' => false),
110 1
			'context'			=> array('lang' => 'CONTEXT', 'validate' => 'string', 'type' => 'select', 'options' => $preview_options, 'default' => 'last', 'explain' => false),
111 1
			'preview_chars'		=> array('lang' => 'PREVIEW_MAX_CHARS', 'validate' => 'int:0:255', 'type' => 'number:0:255', 'maxlength' => 3, 'explain' => false, 'default' => 0),
112 1
		);
113
	}
114
115
	/**
116
	 * {@inheritdoc}
117
	 */
118 5
	public function display(array $bdata, $edit_mode = false)
119
	{
120 5
		$this->settings = $bdata['settings'];
121
122 5
		$topic_data = $this->get_topic_data();
123
124 5
		$content = '';
125 5
		if (sizeof($topic_data))
126 5
		{
127 5
			$content = $this->get_block_content($topic_data);
128 5
		}
129
130
		return array(
131 5
			'title'		=> $this->get_block_title(),
132 5
			'content'	=> $content,
133 5
		);
134
	}
135
136
	/**
137
	 * @param array $topic_data
138
	 * @return string
139
	 */
140 5
	protected function get_block_content(array $topic_data)
141
	{
142 5
		$this->set_display_fields();
143
144 5
		$view = 'S_' . strtoupper($this->settings['template']);
145 5
		$post_data = $this->get_post_data($topic_data);
146 5
		$topic_data = array_values($topic_data);
147
148 5
		$this->show_topics($topic_data, $post_data);
149 5
		unset($topic_data, $post_data);
150
151 5
		$this->ptemplate->assign_vars(array(
152 5
			$view				=> true,
153 5
			'S_IS_BOT'			=> $this->user->data['is_bot'],
154 5
			'LAST_POST_IMG'		=> $this->user->img('icon_topic_latest'),
155 5
			'NEWEST_POST_IMG'	=> $this->user->img('icon_topic_newest'),
156 5
		));
157
158 5
		return $this->ptemplate->render_view('blitze/sitemaker', 'blocks/forum_topics.html', 'forum_topics_block');
159
	}
160
161
	/**
162
	 * @param array $topic_data
163
	 * @param array $post_data
164
	 */
165 5
	protected function show_topics(array &$topic_data, array &$post_data)
166
	{
167 5
		$user_data = $this->forum_data->get_posters_info();
168
169 5
		for ($i = 0, $size = sizeof($topic_data); $i < $size; $i++)
170
		{
171 5
			$row = $topic_data[$i];
172 5
			$forum_id = $row['forum_id'];
173 5
			$topic_id = $row['topic_id'];
174 5
			$author = $user_data[$row[$this->fields['user_id']]];
175 5
			$last_poster = $user_data[$row['topic_last_poster_id']];
176
177 5
			$this->ptemplate->assign_block_vars('topicrow', array(
178 5
				'USERNAME'			=> $author['username_full'],
179 5
				'AVATAR'			=> $author['avatar'],
180 5
				'LAST_POSTER'		=> $last_poster['username_full'],
181 5
				'LAST_AVATAR'		=> $last_poster['avatar'],
182
183 5
				'FORUM_TITLE'		=> $row['forum_name'],
184 5
				'TOPIC_TITLE'		=> truncate_string(censor_text($row['topic_title']), $this->settings['topic_title_limit'], 255, false, '...'),
185 5
				'TOPIC_PREVIEW'		=> $this->get_post_preview(array_pop($post_data[$topic_id])),
186 5
				'TOPIC_POST_TIME'	=> $this->user->format_date($row[$this->fields['time']]),
187 5
				'ATTACH_ICON_IMG'	=> $this->get_attachment_icon($forum_id, $row['topic_attachment']),
188 5
				'REPLIES'			=> $this->content_visibility->get_count('topic_posts', $row, $forum_id) - 1,
189 5
				'VIEWS'				=> $row['topic_views'],
190 5
				'S_UNREAD_TOPIC'	=> $this->is_unread_topic($forum_id, $topic_id, $row['topic_last_post_time']),
191
192 5
				'U_VIEWTOPIC'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id"),
193 5
				'U_VIEWFORUM'		=> append_sid($this->phpbb_root_path . 'viewforum.' . $this->php_ext, "f=$forum_id"),
194 5
				'U_NEW_POST'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
195 5
				'U_LAST_POST'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id&amp;p=" . $row['topic_last_post_id']) . '#p' . $row['topic_last_post_id'],
196 5
			));
197 5
			unset($topic_data[$i], $post_data[$topic_id]);
198 5
		}
199 5
	}
200
201
	/**
202
	 * @return string
203
	 */
204 5
	protected function get_block_title()
205
	{
206
		$types = array(
207 5
			POST_GLOBAL		=> 'FORUM_GLOBAL_ANNOUNCEMENTS',
208 5
			POST_ANNOUNCE	=> 'FORUM_ANNOUNCEMENTS',
209 5
			POST_STICKY		=> 'FORUM_STICKY_POSTS',
210 5
			POST_NORMAL		=> 'FORUM_RECENT_TOPICS',
211 5
		);
212
213
		// if more than one topic type is selected, we default to RECENT_TOPICS
214 5
		$topic_type = join(',', $this->settings['topic_type']);
215
216 5
		return ($this->settings['order_by'] !== self::FORUMS_ORDER_LAST_READ) ? (isset($types[$topic_type]) ? $types[$topic_type] : 'FORUM_RECENT_TOPICS') : 'TOPICS_LAST_READ';
217
	}
218
219
	/**
220
	 * @param array $row
221
	 * @return string
222
	 */
223 5
	protected function get_post_preview(array $row)
224
	{
225 5
		$preview = '';
226 5
		if ($this->settings['preview_chars'])
227 5
		{
228 2
			$method = ($this->settings['template'] === 'context') ? 'get_trimmed_text' : 'get_tooltip_text';
229 2
			$preview = call_user_func_array(array($this, $method), array($row));
230 2
		}
231
232 5
		return $preview;
233
	}
234
235
	/**
236
	 * @param array $row
237
	 */
238
	protected function get_trimmed_text(array $row)
239
	{
240
		$trim = new TrimMessage($row['post_text'], $row['bbcode_uid'], $this->settings['preview_chars']);
241
		$row['post_text'] = $trim->message();
242
		unset($trim);
243
244
		$parse_flags = ($row['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
245
		return generate_text_for_display($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $parse_flags, true);
246
	}
247
248
	/**
249
	 * @param array $row
250
	 */
251 2
	protected function get_tooltip_text(array $row)
252
	{
253 2
		strip_bbcode($row['post_text'], $row['bbcode_uid']);
254
255 2
		$row['post_text'] = truncate_string($row['post_text'], $this->settings['preview_chars']);
256 2
		return wordwrap($row['post_text'], 40, "\n");
257
	}
258
259
	/**
260
	 * @return array
261
	 */
262 5
	private function get_topic_data()
263
	{
264
		$sort_order = array(
265 5
			self::FORUMS_ORDER_FIRST_POST		=> 't.topic_time',
266 5
			self::FORUMS_ORDER_LAST_POST		=> 't.topic_last_post_time',
267 5
			self::FORUMS_ORDER_LAST_READ		=> 't.topic_last_view_time'
268 5
		);
269
270 5
		$range_info = $this->date_range->get($this->settings['date_range']);
271
272 5
		$this->forum_data->query($this->settings['enable_tracking'])
273 5
			->fetch_forum($this->settings['forum_ids'])
274 5
			->fetch_topic_type($this->settings['topic_type'])
275 5
			->fetch_date_range($range_info['start'], $range_info['stop'])
276 5
			->set_sorting($sort_order[$this->settings['order_by']])
277 5
			->build();
278
279 5
		$topic_data = $this->forum_data->get_topic_data($this->settings['max_topics']);
280 5
		$this->topic_tracking_info = $this->forum_data->get_topic_tracking_info();
281
282 5
		return $topic_data;
283
	}
284
285
	/**
286
	 * @param array $topic_data
287
	 * @return array
288
	 */
289 5
	private function get_post_data(array $topic_data)
290
	{
291 5
		if ($this->settings['context'] && $this->settings['preview_chars'])
292 5
		{
293 2
			$post_data = $this->forum_data->get_post_data($this->settings['context']);
294 2
		}
295
		else
296
		{
297 3
			$post_data = array_fill_keys(array_keys($topic_data), array(array('post_text' => '', 'bbcode_uid' => '', 'bbcode_bitfield' => '')));
298
		}
299
300 5
		return $post_data;
301
	}
302
303
	/**
304
	 *
305
	 */
306 5
	private function set_display_fields()
307
	{
308 5
		if ($this->settings['context'] === 'last')
309 5
		{
310 3
			$this->fields['time'] = 'topic_last_post_time';
311 3
			$this->fields['user_id'] = 'topic_last_poster_id';
312
313 3
			$this->ptemplate->assign_var('L_POST_BY_AUTHOR', $this->translator->lang('LAST_POST_BY_AUTHOR'));
314 3
		}
315
		else
316
		{
317 2
			$this->fields['time'] = 'topic_time';
318 2
			$this->fields['user_id'] = 'topic_poster';
319
		}
320 5
	}
321
322
	/**
323
	 * @param int $forum_id
324
	 * @param int $topic_attachment
325
	 * @return string
326
	 */
327 5
	private function get_attachment_icon($forum_id, $topic_attachment)
328
	{
329 5
		return ($this->user_can_view_attachments($forum_id) && $topic_attachment) ? $this->user->img('icon_topic_attach', $this->translator->lang('TOTAL_ATTACHMENTS')) : '';
330
	}
331
332
	/**
333
	 * @param int $forum_id
334
	 * @return bool
335
	 */
336 5
	private function user_can_view_attachments($forum_id)
337
	{
338 5
		return ($this->auth->acl_get('u_download') && $this->auth->acl_get('f_download', $forum_id)) ? true : false;
339
	}
340
341
	/**
342
	 * @param int $forum_id
343
	 * @param int $topic_id
344
	 * @param int $topic_last_post_time
345
	 * @return bool
346
	 */
347 5
	private function is_unread_topic($forum_id, $topic_id, $topic_last_post_time)
348
	{
349 5
		return (isset($this->topic_tracking_info[$forum_id][$topic_id]) && $topic_last_post_time > $this->topic_tracking_info[$forum_id][$topic_id]) ? true : false;
350
	}
351
352
	/**
353
	 * @return array
354
	 */
355 1
	private function get_topic_type_options()
356
	{
357
		return array(
358 1
			POST_NORMAL     => 'POST_NORMAL',
359 1
			POST_STICKY     => 'POST_STICKY',
360 1
			POST_ANNOUNCE   => 'POST_ANNOUNCEMENT',
361 1
			POST_GLOBAL     => 'POST_GLOBAL',
362 1
		);
363
	}
364
365
	/**
366
	 * @return array
367
	 */
368 1
	private function get_preview_options()
369
	{
370
		return array(
371 1
			'last'  => 'SHOW_LAST_POST',
372 1
			'first' => 'SHOW_FIRST_POST',
373 1
		);
374
	}
375
376
	/**
377
	 * @return array
378
	 */
379 1
	private function get_range_options()
380
	{
381
		return array(
382 1
			''      => 'ALL_TIME',
383 1
			'today' => 'TODAY',
384 1
			'week'  => 'THIS_WEEK',
385 1
			'month' => 'THIS_MONTH',
386 1
			'year'  => 'THIS_YEAR',
387 1
		);
388
	}
389
390
	/**
391
	 * @return array
392
	 */
393 1
	private function get_sorting_options()
394
	{
395
		return array(
396 1
			self::FORUMS_ORDER_FIRST_POST => 'FIRST_POST_TIME',
397 1
			self::FORUMS_ORDER_LAST_POST  => 'LAST_POST_TIME',
398 1
			self::FORUMS_ORDER_LAST_READ  => 'LAST_READ_TIME',
399 1
		);
400
	}
401
402
	/**
403
	 * @return array
404
	 */
405 1
	private function get_view_options()
406
	{
407
		return array(
408 1
			'titles'    => 'TITLES',
409 1
			'mini'      => 'MINI',
410 1
			'context'   => 'CONTEXT',
411 1
		);
412
	}
413
}
414