Passed
Push — renovate/configure ( 96a2ff...f40186 )
by
unknown
24:12
created

forum_topics::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
c 0
b 0
f 0
nc 1
nop 10
dl 0
loc 13
rs 9.9332

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
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\blocks;
12
13
/**
14
 * Forum Topics Block
15
 */
16
class forum_topics extends forum_topics_config
17
{
18
	/** @var \phpbb\auth\auth */
19
	protected $auth;
20
21
	/** @var \phpbb\content_visibility */
22
	protected $content_visibility;
23
24
	/** @var \phpbb\language\language */
25
	protected $translator;
26
27
	/** @var \phpbb\user */
28
	protected $user;
29
30
	/** @var \Urodoz\Truncate\TruncateService */
31
	protected $truncator;
32
33
	/** @var \blitze\sitemaker\services\date_range */
34
	protected $date_range;
35
36
	/** @var \blitze\sitemaker\services\forum\data */
37
	protected $forum_data;
38
39
	/** @var \blitze\sitemaker\services\forum\options */
40
	protected $forum_options;
41
42
	/** @var string */
43
	protected $phpbb_root_path;
44
45
	/** @var string */
46
	protected $php_ext;
47
48
	/** @var array */
49
	protected $settings = array();
50
51
	/** @var array */
52
	private $fields = array();
53
54
	/** @var array */
55
	private $topic_tracking_info = array();
56
57
	/** @var array */
58
	protected $sort_order = array(
59
		self::FORUMS_ORDER_FIRST_POST		=> 't.topic_time',
60
		self::FORUMS_ORDER_LAST_POST		=> 't.topic_last_post_time',
61
		self::FORUMS_ORDER_LAST_READ		=> 't.topic_last_view_time'
62
	);
63
64
	/**
65
	 * Constructor
66
	 *
67
	 * @param \phpbb\auth\auth							$auth				Permission object
68
	 * @param \phpbb\content_visibility					$content_visibility	Content visibility object
69
	 * @param \phpbb\language\language					$translator			Language object
70
	 * @param \phpbb\user								$user				User object
71
	 * @param \Urodoz\Truncate\TruncateService			$truncator			Truncator service
72
	 * @param \blitze\sitemaker\services\date_range		$date_range			Date Range Object
73
	 * @param \blitze\sitemaker\services\forum\data		$forum_data			Forum Data object
74
	 * @param \blitze\sitemaker\services\forum\options	$forum_options		Forum Data object
75
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
76
	 * @param string									$php_ext			php file extension
77
	 */
78
	public function __construct(\phpbb\auth\auth $auth, \phpbb\content_visibility $content_visibility, \phpbb\language\language $translator, \phpbb\user $user, \Urodoz\Truncate\TruncateService $truncator, \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)
79
	{
80
		parent::__construct($forum_options);
81
82
		$this->auth = $auth;
83
		$this->content_visibility = $content_visibility;
84
		$this->translator = $translator;
85
		$this->user = $user;
86
		$this->truncator = $truncator;
87
		$this->date_range = $date_range;
88
		$this->forum_data = $forum_data;
89
		$this->phpbb_root_path = $phpbb_root_path;
90
		$this->php_ext = $php_ext;
91
	}
92
93
	/**
94
	 * {@inheritdoc}
95
	 */
96
	public function display(array $bdata, $edit_mode = false)
97
	{
98
		$this->settings = $bdata['settings'];
99
100
		$this->build_query();
101
		$this->forum_data->build();
102
103
		$topic_data = $this->get_topic_data();
104
105
		$data = null;
106
		if (sizeof($topic_data))
107
		{
108
			$this->topic_tracking_info = $this->forum_data->get_topic_tracking_info();
109
			$data = $this->get_block_content($topic_data);
110
		}
111
112
		return array(
113
			'title'	=> $this->get_block_title(),
114
			'data'	=> $data,
115
		);
116
	}
117
118
	/**
119
	 * @param array $topic_data
120
	 * @return array
121
	 */
122
	protected function get_block_content(array $topic_data)
123
	{
124
		$this->set_display_fields();
125
126
		$post_data = $this->get_post_data($topic_data);
127
		$topic_data = array_values($topic_data);
128
129
		return array(
130
			'CONTEXT'			=> $this->settings['context'],
131
			'TEMPLATE'			=> $this->settings['template'],
132
			'TOPICS'			=> $this->get_topics($topic_data, $post_data),
133
			'S_IS_BOT'			=> $this->user->data['is_bot'],
134
			'LAST_POST_IMG'		=> $this->user->img('icon_topic_latest'),
135
			'NEWEST_POST_IMG'	=> $this->user->img('icon_topic_newest'),
136
		);
137
	}
138
139
	/**
140
	 * @param array $topic_data
141
	 * @param array $post_data
142
	 * @return array
143
	 */
144
	protected function get_topics(array &$topic_data, array &$post_data)
145
	{
146
		$user_data = $this->forum_data->get_posters_info();
147
148
		$topics = [];
149
		for ($i = 0, $size = sizeof($topic_data); $i < $size; $i++)
150
		{
151
			$row = &$topic_data[$i];
152
			$topic_id = $row['topic_id'];
153
			$topics[] = $this->get_topics_template_data($row, $post_data[$topic_id], $user_data);
154
			unset($topic_data[$i], $post_data[$topic_id]);
155
		}
156
157
		return $topics;
158
	}
159
160
	/**
161
	 * @param array $topic_data
162
	 * @param array $post_data
163
	 * @param array $user_data
164
	 * return array
165
	 */
166
	protected function get_topics_template_data(array &$topic_data, array &$post_data, array $user_data)
167
	{
168
		$forum_id = $topic_data['forum_id'];
169
		$topic_id = $topic_data['topic_id'];
170
		$author = $user_data[$topic_data[$this->fields['user_id']]];
171
		$last_poster = $user_data[$topic_data['topic_last_poster_id']];
172
		$replies = $this->content_visibility->get_count('topic_posts', $topic_data, $forum_id) - 1;
173
		$unread_topic = $this->is_unread_topic($forum_id, $topic_id, $topic_data['topic_last_post_time']);
174
175
		return array(
176
			'USERNAME'			=> $author['username_full'],
177
			'AVATAR'			=> $author['avatar'],
178
			'LAST_POSTER'		=> $last_poster['username_full'],
179
			'LAST_AVATAR'		=> $last_poster['avatar'],
180
181
			'FORUM_TITLE'		=> $topic_data['forum_name'],
182
			'TOPIC_TITLE'		=> truncate_string(censor_text($topic_data['topic_title']), $this->settings['topic_title_limit'], 255, false, '...'),
183
			'TOPIC_PREVIEW'		=> $this->get_post_preview(array_pop($post_data)),
184
			'TOPIC_POST_TIME'	=> $this->user->format_date($topic_data[$this->fields['time']]),
185
			'ATTACH_ICON_IMG'	=> $this->get_attachment_icon($forum_id, $topic_data['topic_attachment']),
186
			'REPLIES'			=> $replies,
187
			'VIEWS'				=> (int) $topic_data['topic_views'],
188
			'S_UNREAD_TOPIC'	=> $unread_topic,
189
190
			'U_VIEWPROFILE'		=> $author['u_viewprofile'],
191
			'U_VIEWTOPIC'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id"),
192
			'U_VIEWFORUM'		=> append_sid($this->phpbb_root_path . 'viewforum.' . $this->php_ext, "f=$forum_id"),
193
			'U_NEW_POST'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
194
			'U_LAST_POST'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id&amp;p=" . $topic_data['topic_last_post_id']) . '#p' . $topic_data['topic_last_post_id'],
195
		);
196
	}
197
198
	/**
199
	 * @return string
200
	 */
201
	protected function get_block_title()
202
	{
203
		$types = array(
204
			POST_GLOBAL		=> 'FORUM_GLOBAL_ANNOUNCEMENTS',
205
			POST_ANNOUNCE	=> 'FORUM_ANNOUNCEMENTS',
206
			POST_STICKY		=> 'FORUM_STICKY_POSTS',
207
			POST_NORMAL		=> 'FORUM_RECENT_TOPICS',
208
		);
209
210
		// if more than one topic type is selected, we default to RECENT_TOPICS
211
		$topic_type = join(',', $this->settings['topic_type']);
212
213
		return ($this->settings['order_by'] !== self::FORUMS_ORDER_LAST_READ) ? (isset($types[$topic_type]) ? $types[$topic_type] : 'FORUM_RECENT_TOPICS') : 'TOPICS_LAST_READ';
214
	}
215
216
	/**
217
	 * @param array $row
218
	 * @return string
219
	 */
220
	protected function get_post_preview(array $row)
221
	{
222
		$preview = '';
223
		if ($this->settings['preview_chars'])
224
		{
225
			$method = ($this->settings['template'] === 'context') ? 'get_trimmed_text' : 'get_tooltip_text';
226
			$preview = call_user_func_array(array($this, $method), array($row));
227
		}
228
229
		return $preview;
230
	}
231
232
	/**
233
	 * @param array $row
234
	 */
235
	protected function get_trimmed_text(array $row)
236
	{
237
		$parse_flags = ($row['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
238
		$row['post_text'] = generate_text_for_display($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $parse_flags, true);
239
240
		return $this->truncator->truncate($row['post_text'], $this->settings['preview_chars']);
241
	}
242
243
	/**
244
	 * @param array $row
245
	 * @return string
246
	 */
247
	protected function get_tooltip_text(array $row)
248
	{
249
		strip_bbcode($row['post_text'], $row['bbcode_uid']);
250
251
		$row['post_text'] = truncate_string($row['post_text'], $this->settings['preview_chars'], 255, false, '...');
252
		return wordwrap($row['post_text'], 40, "\n");
253
	}
254
255
	/**
256
	 * @return array
257
	 */
258
	protected function get_topic_data()
259
	{
260
		return $this->forum_data->get_topic_data($this->settings['max_topics']);
261
	}
262
263
	/**
264
	 * @return []
0 ignored issues
show
Documentation Bug introduced by
The doc comment [] at position 0 could not be parsed: Unknown type name '[' at position 0 in [].
Loading history...
265
	 */
266
	protected function build_query()
267
	{
268
		$range_info = $this->date_range->get($this->settings['date_range']);
269
		$topic_types = isset($this->settings['topic_type']) ? $this->settings['topic_type'] : [];
270
271
		$this->forum_data->query($this->settings['enable_tracking'])
272
			->fetch_forum($this->settings['forum_ids'])
273
			->fetch_topic_type($topic_types)
274
			->fetch_date_range($range_info['start'], $range_info['stop'])
275
			->set_sorting($this->sort_order[$this->settings['order_by']]);
276
	}
277
278
	/**
279
	 * @param array $topic_data
280
	 * @return array
281
	 */
282
	private function get_post_data(array $topic_data)
283
	{
284
		if ($this->settings['context'] && $this->settings['preview_chars'])
285
		{
286
			$post_data = $this->forum_data->get_post_data($this->settings['context']);
287
		}
288
		else
289
		{
290
			$post_data = array_fill_keys(array_keys($topic_data), array(array('post_text' => '', 'bbcode_uid' => '', 'bbcode_bitfield' => '')));
291
		}
292
293
		return $post_data;
294
	}
295
296
	/**
297
	 * @return void
298
	 */
299
	private function set_display_fields()
300
	{
301
		if ($this->settings['context'] === 'last')
302
		{
303
			$this->fields['time'] = 'topic_last_post_time';
304
			$this->fields['user_id'] = 'topic_last_poster_id';
305
		}
306
		else
307
		{
308
			$this->fields['time'] = 'topic_time';
309
			$this->fields['user_id'] = 'topic_poster';
310
		}
311
	}
312
313
	/**
314
	 * @param int $forum_id
315
	 * @param int $topic_attachment
316
	 * @return string
317
	 */
318
	private function get_attachment_icon($forum_id, $topic_attachment)
319
	{
320
		return ($this->user_can_view_attachments($forum_id) && $topic_attachment) ? $this->user->img('icon_topic_attach', $this->translator->lang('TOTAL_ATTACHMENTS')) : '';
321
	}
322
323
	/**
324
	 * @param int $forum_id
325
	 * @return bool
326
	 */
327
	private function user_can_view_attachments($forum_id)
328
	{
329
		return ($this->auth->acl_get('u_download') && $this->auth->acl_get('f_download', $forum_id)) ? true : false;
330
	}
331
332
	/**
333
	 * @param int $forum_id
334
	 * @param int $topic_id
335
	 * @param int $topic_last_post_time
336
	 * @return bool
337
	 */
338
	private function is_unread_topic($forum_id, $topic_id, $topic_last_post_time)
339
	{
340
		return (isset($this->topic_tracking_info[$forum_id][$topic_id]) && $topic_last_post_time > $this->topic_tracking_info[$forum_id][$topic_id]) ? true : false;
341
	}
342
}
343