Passed
Push — renovate/configure ( c923d4...2c70da )
by
unknown
20:13
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
				'TOPIC_TITLE'		=> truncate_string(censor_text($row['topic_title']), $this->settings['topic_title_limit'], 255, false, '...'),
154
				'TOPIC_PREVIEW'		=> $this->get_post_preview(array_pop($post_data[$topic_id])),
155
				'TOPIC_POST_TIME'	=> $this->user->format_date($row[$this->fields['time']]),
156
				'ATTACH_ICON_IMG'	=> $this->get_attachment_icon($forum_id, $row['topic_attachment']),
157
				'REPLIES'			=> $this->content_visibility->get_count('topic_posts', $row, $forum_id) - 1,
158
				'VIEWS'				=> (int) $row['topic_views'],
159
				'S_UNREAD_TOPIC'	=> $this->is_unread_topic($forum_id, $topic_id, $row['topic_last_post_time']),
160
161
				'U_VIEWPROFILE'		=> $author['u_viewprofile'],
162
				'U_VIEWTOPIC'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id"),
163
				'U_VIEWFORUM'		=> append_sid($this->phpbb_root_path . 'viewforum.' . $this->php_ext, "f=$forum_id"),
164
				'U_NEW_POST'		=> append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id&amp;view=unread") . '#unread',
165
				'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'],
166
			);
167
			unset($topic_data[$i], $post_data[$topic_id]);
168
		}
169
170
		return $topics;
171
	}
172
173
	/**
174
	 * @return string
175
	 */
176
	protected function get_block_title()
177
	{
178
		$types = array(
179
			POST_GLOBAL		=> 'FORUM_GLOBAL_ANNOUNCEMENTS',
180
			POST_ANNOUNCE	=> 'FORUM_ANNOUNCEMENTS',
181
			POST_STICKY		=> 'FORUM_STICKY_POSTS',
182
			POST_NORMAL		=> 'FORUM_RECENT_TOPICS',
183
		);
184
185
		// if more than one topic type is selected, we default to RECENT_TOPICS
186
		$topic_type = join(',', $this->settings['topic_type']);
187
188
		return ($this->settings['order_by'] !== self::FORUMS_ORDER_LAST_READ) ? (isset($types[$topic_type]) ? $types[$topic_type] : 'FORUM_RECENT_TOPICS') : 'TOPICS_LAST_READ';
189
	}
190
191
	/**
192
	 * @param array $row
193
	 * @return string
194
	 */
195
	protected function get_post_preview(array $row)
196
	{
197
		$preview = '';
198
		if ($this->settings['preview_chars'])
199
		{
200
			$method = ($this->settings['template'] === 'context') ? 'get_trimmed_text' : 'get_tooltip_text';
201
			$preview = call_user_func_array(array($this, $method), array($row));
202
		}
203
204
		return $preview;
205
	}
206
207
	/**
208
	 * @param array $row
209
	 */
210
	protected function get_trimmed_text(array $row)
211
	{
212
		$parse_flags = ($row['bbcode_bitfield'] ? OPTION_FLAG_BBCODE : 0) | OPTION_FLAG_SMILIES;
213
		$row['post_text'] = generate_text_for_display($row['post_text'], $row['bbcode_uid'], $row['bbcode_bitfield'], $parse_flags, true);
214
215
		return $this->truncator->truncate($row['post_text'], $this->settings['preview_chars']);
216
	}
217
218
	/**
219
	 * @param array $row
220
	 * @return string
221
	 */
222
	protected function get_tooltip_text(array $row)
223
	{
224
		strip_bbcode($row['post_text'], $row['bbcode_uid']);
225
226
		$row['post_text'] = truncate_string($row['post_text'], $this->settings['preview_chars']);
227
		return wordwrap($row['post_text'], 40, "\n");
228
	}
229
230
	/**
231
	 * @return array
232
	 */
233
	private function get_topic_data()
234
	{
235
		$sort_order = array(
236
			self::FORUMS_ORDER_FIRST_POST		=> 't.topic_time',
237
			self::FORUMS_ORDER_LAST_POST		=> 't.topic_last_post_time',
238
			self::FORUMS_ORDER_LAST_READ		=> 't.topic_last_view_time'
239
		);
240
241
		$range_info = $this->date_range->get($this->settings['date_range']);
242
243
		$this->forum_data->query($this->settings['enable_tracking'])
244
			->fetch_forum($this->settings['forum_ids'])
245
			->fetch_topic_type($this->settings['topic_type'])
246
			->fetch_date_range($range_info['start'], $range_info['stop'])
247
			->set_sorting($sort_order[$this->settings['order_by']])
248
			->build();
249
250
		$topic_data = $this->forum_data->get_topic_data($this->settings['max_topics']);
251
		$this->topic_tracking_info = $this->forum_data->get_topic_tracking_info();
252
253
		return $topic_data;
254
	}
255
256
	/**
257
	 * @param array $topic_data
258
	 * @return array
259
	 */
260
	private function get_post_data(array $topic_data)
261
	{
262
		if ($this->settings['context'] && $this->settings['preview_chars'])
263
		{
264
			$post_data = $this->forum_data->get_post_data($this->settings['context']);
265
		}
266
		else
267
		{
268
			$post_data = array_fill_keys(array_keys($topic_data), array(array('post_text' => '', 'bbcode_uid' => '', 'bbcode_bitfield' => '')));
269
		}
270
271
		return $post_data;
272
	}
273
274
	/**
275
	 * @return void
276
	 */
277
	private function set_display_fields()
278
	{
279
		if ($this->settings['context'] === 'last')
280
		{
281
			$this->fields['time'] = 'topic_last_post_time';
282
			$this->fields['user_id'] = 'topic_last_poster_id';
283
		}
284
		else
285
		{
286
			$this->fields['time'] = 'topic_time';
287
			$this->fields['user_id'] = 'topic_poster';
288
		}
289
	}
290
291
	/**
292
	 * @param int $forum_id
293
	 * @param int $topic_attachment
294
	 * @return string
295
	 */
296
	private function get_attachment_icon($forum_id, $topic_attachment)
297
	{
298
		return ($this->user_can_view_attachments($forum_id) && $topic_attachment) ? $this->user->img('icon_topic_attach', $this->translator->lang('TOTAL_ATTACHMENTS')) : '';
299
	}
300
301
	/**
302
	 * @param int $forum_id
303
	 * @return bool
304
	 */
305
	private function user_can_view_attachments($forum_id)
306
	{
307
		return ($this->auth->acl_get('u_download') && $this->auth->acl_get('f_download', $forum_id)) ? true : false;
308
	}
309
310
	/**
311
	 * @param int $forum_id
312
	 * @param int $topic_id
313
	 * @param int $topic_last_post_time
314
	 * @return bool
315
	 */
316
	private function is_unread_topic($forum_id, $topic_id, $topic_last_post_time)
317
	{
318
		return (isset($this->topic_tracking_info[$forum_id][$topic_id]) && $topic_last_post_time > $this->topic_tracking_info[$forum_id][$topic_id]) ? true : false;
319
	}
320
}
321