Completed
Branch master (a182de)
by Daniel
03:00
created

forum_topics::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

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