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

whats_new::display()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 19
ccs 0
cts 15
cp 0
rs 9.4286
cc 2
eloc 10
nc 2
nop 2
crap 6
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
/**
13
* Menu Block
14
* @package phpBB Sitemaker Menu
15
*/
16
class whats_new extends \blitze\sitemaker\services\blocks\driver\block
17
{
18
	/** @var \phpbb\user */
19
	protected $user;
20
21
	/** @var \blitze\sitemaker\services\forum\data */
22
	protected $forum;
23
24
	/** @var string */
25
	protected $phpbb_root_path;
26
27
	/** @var string */
28
	protected $php_ext;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param \phpbb\user							$user				User object
34
	 * @param \blitze\sitemaker\services\forum\data	$forum				Forum Data object
35
	 * @param string								$phpbb_root_path	Path to the phpbb includes directory.
36
	 * @param string								$php_ext			php file extension
37
	 */
38
	public function __construct(\phpbb\user $user, \blitze\sitemaker\services\forum\data $forum, $phpbb_root_path, $php_ext)
39
	{
40
		$this->user = $user;
41
		$this->forum = $forum;
42
		$this->phpbb_root_path = $phpbb_root_path;
43
		$this->php_ext = $php_ext;
44
	}
45
46
	/**
47
	 * {@inheritdoc}
48
	 */
49
	public function get_config(array $settings)
50
	{
51
		return array(
52
			'legend1'		=> $this->user->lang('SETTINGS'),
53
			'topics_only'	=> array('lang' => 'TOPICS_ONLY', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false, 'default' => 0),
54
			'max_topics'	=> array('lang' => 'MAX_TOPICS', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'explain' => false, 'default' => 5),
55
		);
56
	}
57
58
	/**
59
	 * {@inheritdoc}
60
	 */
61
	public function display(array $bdata, $edit_mode = false)
62
	{
63
		if (!$this->user->data['is_registered'])
64
		{
65
			return array(
66
				'title'		=> '',
67
				'content'	=> '',
68
			);
69
		}
70
		else
71
		{
72
			$this->_fetch_new($bdata['settings']);
73
74
			return array(
75
				'title'     => 'WHATS_NEW',
76
				'content'   => $this->ptemplate->render_view('blitze/sitemaker', 'blocks/topiclist.html', 'whats_new'),
77
			);
78
		}
79
	}
80
81
	/**
82
	 * @param array $settings
83
	 */
84
	private function _fetch_new(array $settings)
85
	{
86
		$topic_data = $this->_get_topics($settings);
87
88
		for ($i = 0, $size = sizeof($topic_data); $i < $size; $i++)
89
		{
90
			$row = $topic_data[$i];
91
			$forum_id = $row['forum_id'];
92
			$topic_id = $row['topic_id'];
93
94
			$this->ptemplate->assign_block_vars('topicrow', array(
95
				'TOPIC_TITLE'    => censor_text($row['topic_title']),
96
				'U_VIEWTOPIC'    => append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id"),
97
			));
98
			unset($topic_data[$i]);
99
		}
100
101
		$this->ptemplate->assign_var('NO_RECORDS', ($settings['topics_only']) ? $this->user->lang('NO_NEW_TOPICS') : $this->user->lang('NO_NEW_POSTS'));
102
	}
103
104
	/**
105
	 * @param array $settings
106
	 * @return array
107
	 */
108
	private function _get_topics(array $settings)
109
	{
110
		if ($settings['topics_only'])
111
		{
112
			$sorting = 't.topic_last_post_time';
113
			$sql_array = $this->_get_topics_sql();
114
		}
115
		else
116
		{
117
			$sorting = 'p.post_time';
118
			$sql_array = $this->_get_posts_sql();
119
		}
120
121
		$this->forum->query()
122
			->set_sorting($sorting)
123
			->fetch_custom($sql_array)
124
			->build(true, false);
125
		$topic_data = $this->forum->get_topic_data($settings['max_topics']);
126
127
		return array_values($topic_data);
128
	}
129
130
	/**
131
	 * @return array
132
	 */
133
	private function _get_topics_sql()
134
	{
135
		return array(
136
			'WHERE'		=> array(
137
				't.topic_last_post_time > ' . $this->user->data['user_lastvisit'],
138
				't.topic_moved_id = 0',
139
			),
140
		);
141
	}
142
143
	/**
144
	 * @return array
145
	 */
146
	private function _get_posts_sql()
147
	{
148
		return array(
149
			'FROM'		=> array(
150
				POSTS_TABLE		=> 'p',
151
			),
152
			'WHERE'		=> array(
153
				't.topic_id = p.topic_id AND p.post_time > ' . $this->user->data['user_lastvisit'],
154
			),
155
		);
156
	}
157
}
158