Completed
Push — master ( db7709...24ebcc )
by Daniel
10:24
created

mybookmarks::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

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