Completed
Push — master ( 9866a1...02e5e5 )
by Daniel
08:18
created

whats_new   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 11
c 4
b 1
f 0
lcom 1
cbo 3
dl 0
loc 142
ccs 0
cts 88
cp 0
rs 10

7 Methods

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