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