Completed
Branch master (a182de)
by Daniel
03:00
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

Importance

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