Completed
Push — master ( 4bea8e...3e781a )
by Daniel
11:05
created

forum_topics::get_tooltip_text()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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