Completed
Push — master ( cbbc79...7e8448 )
by Daniel
08:03
created

mybookmarks::get_bookmarks_sql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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