Completed
Push — master ( a0ceb6...422577 )
by Daniel
08:41
created

whats_new::get_config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

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