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

mybookmarks::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
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
* My Bookmarks Block
14
*/
15
class mybookmarks 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
			'max_topics'	=> array('lang' => 'MAX_TOPICS', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'explain' => false, 'default' => 5),
52
		);
53
	}
54
55
	/**
56
	 * {@inheritdoc}
57
	 */
58
	public function display(array $bdata, $edit_mode = false)
59
	{
60
		if ($this->user->data['is_registered'])
61
		{
62
			$topics_data = $this->_get_bookmarks($bdata['settings']);
63
			$this->_show_bookmarks($topics_data);
64
65
			return array(
66
				'title'     => 'MY_BOOKMARKS',
67
				'content'   => $this->ptemplate->render_view('blitze/sitemaker', 'blocks/topiclist.html', 'mybookmarks'),
68
			);
69
		}
70
71
		return array();
72
	}
73
74
	/**
75
	 * @param array $settings
76
	 */
77
	private function _get_bookmarks(array $settings)
78
	{
79
		$sql_array = $this->_get_bookmarks_sql();
80
		$this->forum_data->query()
81
			->set_sorting('t.topic_last_post_time')
82
			->fetch_custom($sql_array)
83
			->build(true, false);
84
		$topic_data = $this->forum_data->get_topic_data($settings['max_topics']);
85
86
		return array_values($topic_data);
87
	}
88
89
	/**
90
	 * @param array $topic_data
91
	 */
92
	private function _show_bookmarks(array $topic_data)
93
	{
94
		for ($i = 0, $size = sizeof($topic_data); $i < $size; $i++)
95
		{
96
			$row = $topic_data[$i];
97
			$forum_id = $row['forum_id'];
98
			$topic_id = $row['topic_id'];
99
100
			$this->ptemplate->assign_block_vars('topicrow', array(
101
				'TOPIC_TITLE'    => censor_text($row['topic_title']),
102
				'U_VIEWTOPIC'    => append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id"),
103
			));
104
			unset($topic_data[$i]);
105
		}
106
107
		$this->ptemplate->assign_var('NO_RECORDS', $this->user->lang('NO_BOOKMARKS'));
108
	}
109
110
	/**
111
	 * @return array
112
	 */
113
	private function _get_bookmarks_sql()
114
	{
115
		return array(
116
			'FROM'		=> array(
117
				BOOKMARKS_TABLE		=> 'b',
118
			),
119
			'WHERE'		=> array(
120
				'b.user_id = ' . $this->user->data['user_id'],
121
				'b.topic_id = t.topic_id',
122
			),
123
		);
124
	}
125
}
126