forum_topics   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 325
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 121
c 2
b 0
f 0
dl 0
loc 325
ccs 128
cts 128
cp 1
rs 9.76
wmc 33

16 Methods

Rating   Name   Duplication   Size   Complexity  
A get_post_data() 0 12 3
A is_unread_topic() 0 3 3
A get_post_preview() 0 10 3
A get_trimmed_text() 0 6 2
A user_can_view_attachments() 0 3 3
A get_topics_template_data() 0 29 1
A get_block_content() 0 14 1
A get_tooltip_text() 0 6 1
A get_attachment_icon() 0 3 3
A get_topic_data() 0 3 1
A display() 0 19 2
A set_display_fields() 0 11 2
A get_topics() 0 14 2
A __construct() 0 13 1
A get_block_title() 0 13 3
A build_query() 0 10 2
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 7
	 * @param \phpbb\content_visibility					$content_visibility	Content visibility object
69
	 * @param \phpbb\language\language					$translator			Language object
70 7
	 * @param \phpbb\user								$user				User object
71
	 * @param \Urodoz\Truncate\TruncateService			$truncator			Truncator service
72 7
	 * @param \blitze\sitemaker\services\date_range		$date_range			Date Range Object
73 7
	 * @param \blitze\sitemaker\services\forum\data		$forum_data			Forum Data object
74 7
	 * @param \blitze\sitemaker\services\forum\options	$forum_options		Forum Data object
75 7
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
76 7
	 * @param string									$php_ext			php file extension
77 7
	 */
78 7
	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 7
	{
80 7
		parent::__construct($forum_options);
81
82
		$this->auth = $auth;
83
		$this->content_visibility = $content_visibility;
84
		$this->translator = $translator;
85 6
		$this->user = $user;
86
		$this->truncator = $truncator;
87 6
		$this->date_range = $date_range;
88
		$this->forum_data = $forum_data;
89 6
		$this->phpbb_root_path = $phpbb_root_path;
90
		$this->php_ext = $php_ext;
91 6
	}
92 6
93 6
	/**
94 6
	 * {@inheritdoc}
95 6
	 */
96
	public function display(array $bdata, $edit_mode = false)
97
	{
98 6
		$this->settings = $bdata['settings'];
99 6
100 6
		$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 6
		{
108
			$this->topic_tracking_info = $this->forum_data->get_topic_tracking_info();
109 6
			$data = $this->get_block_content($topic_data);
110
		}
111 6
112 6
		return array(
113 6
			'title'	=> $this->get_block_title(),
114
			'data'	=> $data,
115 6
		);
116 6
	}
117
118 6
	/**
119 6
	 * @param array $topic_data
120 6
	 * @return array
121 6
	 */
122 6
	protected function get_block_content(array $topic_data)
123 6
	{
124
		$this->set_display_fields();
125 6
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 6
			'TOPICS'			=> $this->get_topics($topic_data, $post_data),
133
			'S_IS_BOT'			=> $this->user->data['is_bot'],
134 6
			'LAST_POST_IMG'		=> $this->user->img('icon_topic_latest'),
135
			'NEWEST_POST_IMG'	=> $this->user->img('icon_topic_newest'),
136 6
		);
137
	}
138 6
139 6
	/**
140 6
	 * @param array $topic_data
141 6
	 * @param array $post_data
142 6
	 * @return array
143
	 */
144 6
	protected function get_topics(array &$topic_data, array &$post_data)
145 6
	{
146 6
		$user_data = $this->forum_data->get_posters_info();
147 6
148 6
		$topics = [];
149
		for ($i = 0, $size = sizeof($topic_data); $i < $size; $i++)
150 6
		{
151 6
			$row = &$topic_data[$i];
152 6
			$topic_id = $row['topic_id'];
153 6
			$topics[] = $this->get_topics_template_data($row, $post_data[$topic_id], $user_data);
154 6
			unset($topic_data[$i], $post_data[$topic_id]);
155 6
		}
156 6
157 6
		return $topics;
158
	}
159 6
160 6
	/**
161 6
	 * @param array $topic_data
162 6
	 * @param array $post_data
163 6
	 * @param array $user_data
164 6
	 * return array
165 6
	 */
166 6
	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 6
		$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 6
175 6
		return array(
176 6
			'USERNAME'			=> $author['username_full'],
177 6
			'AVATAR'			=> $author['avatar'],
178 6
			'LAST_POSTER'		=> $last_poster['username_full'],
179
			'LAST_AVATAR'		=> $last_poster['avatar'],
180
181 6
			'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 6
			'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 6
			'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 6
			'U_VIEWFORUM'		=> append_sid($this->phpbb_root_path . 'viewforum.' . $this->php_ext, "f=$forum_id"),
193 6
			'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 6
			'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 3
		);
196 3
	}
197 3
198
	/**
199 6
	 * @return string
200
	 */
201
	protected function get_block_title()
202
	{
203
		$types = array(
204
			POST_GLOBAL		=> 'FORUM_GLOBAL_ANNOUNCEMENTS',
205 1
			POST_ANNOUNCE	=> 'FORUM_ANNOUNCEMENTS',
206
			POST_STICKY		=> 'FORUM_STICKY_POSTS',
207 1
			POST_NORMAL		=> 'FORUM_RECENT_TOPICS',
208 1
		);
209
210 1
		// if more than one topic type is selected, we default to RECENT_TOPICS
211 1
		$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 2
	 * @return string
219
	 */
220 2
	protected function get_post_preview(array $row)
221
	{
222 2
		$preview = '';
223 2
		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 6
		return $preview;
230
	}
231
232 6
	/**
233 6
	 * @param array $row
234 6
	 */
235 6
	protected function get_trimmed_text(array $row)
236
	{
237 6
		$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 6
240 6
		return $this->truncator->truncate($row['post_text'], $this->settings['preview_chars']);
241 6
	}
242 6
243 6
	/**
244 6
	 * @param array $row
245
	 * @return string
246 6
	 */
247 6
	protected function get_tooltip_text(array $row)
248
	{
249 6
		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 6
	 * @return array
257
	 */
258 6
	protected function get_topic_data()
259 6
	{
260 3
		return $this->forum_data->get_topic_data($this->settings['max_topics']);
261 3
	}
262
263
	/**
264 3
	 * @return array
265
	 */
266
	protected function build_query()
267 6
	{
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 6
			->fetch_topic_type($topic_types)
274
			->fetch_date_range($range_info['start'], $range_info['stop'])
275 6
			->set_sorting($this->sort_order[$this->settings['order_by']]);
276 6
	}
277 3
278 3
	/**
279
	 * @param array $topic_data
280 3
	 * @return array
281 3
	 */
282
	private function get_post_data(array $topic_data)
283
	{
284 3
		if ($this->settings['context'] && $this->settings['preview_chars'])
285 3
		{
286
			$post_data = $this->forum_data->get_post_data($this->settings['context']);
287 6
		}
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 6
	}
295
296 6
	/**
297
	 * @return void
298
	 */
299
	private function set_display_fields()
300
	{
301
		if ($this->settings['context'] === 'last')
302
		{
303 6
			$this->fields['time'] = 'topic_last_post_time';
304
			$this->fields['user_id'] = 'topic_last_poster_id';
305 6
		}
306
		else
307
		{
308
			$this->fields['time'] = 'topic_time';
309
			$this->fields['user_id'] = 'topic_poster';
310
		}
311
	}
312
313
	/**
314 6
	 * @param int $forum_id
315
	 * @param int $topic_attachment
316 6
	 * @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