Completed
Push — master ( a93112...75382e )
by Daniel
16:13
created

forum_topics::forum_topics_mini()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.8822

Importance

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