Completed
Push — master ( 24ebcc...799b0c )
by Daniel
10:31
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 10
CRAP Score 2

Importance

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