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

mybookmarks::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
 * My Bookmarks Block
17
 */
18
class mybookmarks 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
			'max_topics'	=> array('lang' => 'MAX_TOPICS', 'validate' => 'int:0', 'type' => 'number:0', 'maxlength' => 2, 'explain' => false, 'default' => 5),
60
		);
61
	}
62
63
	/**
64
	 * {@inheritdoc}
65 3
	 */
66
	public function display(array $bdata, $edit_mode = false)
67 3
	{
68 3
		if ($this->user->data['is_registered'])
69 3
		{
70 2
			$topics_data = $this->get_bookmarks_data($bdata['settings']);
71 2
72
			return array(
73
				'title'	=> 'MY_BOOKMARKS',
74 2
				'data'	=> array(
75 2
					'NO_RECORDS'	=> $this->translator->lang('NO_BOOKMARKED_TOPICS'),
76 2
					'TOPICS'		=> $this->get_bookmarks($topics_data),
77 2
				),
78
			);
79 3
		}
80
81
		return [];
82
	}
83
84
	/**
85
	 * @param array $settings
86 2
	 * @return string[]
87
	 */
88 2
	private function get_bookmarks_data(array $settings)
89 2
	{
90 2
		$sql_array = $this->get_bookmarks_sql();
91 2
		$this->forum_data->query()
92 2
			->set_sorting('t.topic_last_post_time')
93 2
			->fetch_custom($sql_array)
94
			->build(true, false);
95 2
		$topic_data = $this->forum_data->get_topic_data($settings['max_topics']);
96
97
		return array_values($topic_data);
98
	}
99
100
	/**
101 2
	 * @param array $topic_data
102
	 */
103 2
	private function get_bookmarks(array $topic_data)
104
	{
105 1
		$bookmarks = [];
106 1
		for ($i = 0, $size = sizeof($topic_data); $i < $size; $i++)
107 1
		{
108
			$row = $topic_data[$i];
109 1
			$forum_id = $row['forum_id'];
110 1
			$topic_id = $row['topic_id'];
111 1
112 1
			$bookmarks[] = array(
113 1
				'TOPIC_TITLE'    => censor_text($row['topic_title']),
114 1
				'U_VIEWTOPIC'    => append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, "f=$forum_id&amp;t=$topic_id"),
115
			);
116 2
		}
117 2
118
		return $bookmarks;
119
	}
120
121
	/**
122 2
	 * @return array
123
	 */
124
	private function get_bookmarks_sql()
125
	{
126 2
		return array(
127 2
			'FROM'		=> array(
128
				BOOKMARKS_TABLE		=> 'b',
129 2
			),
130 2
			'WHERE'		=> array(
131 2
				'b.user_id = ' . (int) $this->user->data['user_id'],
132 2
				'b.topic_id = t.topic_id',
133
			),
134
		);
135
	}
136
137
	/**
138
	 * {@inheritdoc}
139
	 */
140
	public function get_template()
141
	{
142
		return '@blitze_sitemaker/blocks/topiclist.html';
143
	}
144
}
145