Passed
Push — develop ( b3eda6...9f2d35 )
by Daniel
03:59 queued 40s
created

whats_new::get_template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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