Passed
Push — develop ( f83164...45f12e )
by Daniel
11:29
created

whois::get_legend_sql()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 0
dl 0
loc 25
ccs 0
cts 0
cp 0
crap 12
rs 9.8666
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
 * Whois Block
17
 */
18
class whois extends block
19
{
20
	/** @var \phpbb\auth\auth */
21
	protected $auth;
22
23
	/** @var \phpbb\config\config */
24
	protected $config;
25
26
	/** @var \phpbb\db\driver\driver_interface */
27
	protected $db;
28
29
	/** \phpbb\group\helper */
30
	protected $group_helper;
31
32
	/** @var \phpbb\language\language */
33
	protected $translator;
34
35
	/** @var \phpbb\template\template */
36
	protected $template;
37
38
	/** @var \phpbb\user */
39
	protected $user;
40
41
	/** @var string */
42
	protected $phpbb_root_path;
43
44
	/** @var string */
45
	protected $php_ext;
46
47
	/**
48
	 * Constructor
49
	 *
50
	 * @param \phpbb\auth\auth					$auth				Permission object
51 3
	 * @param \phpbb\config\config				$config				phpBB configuration
52
	 * @param \phpbb\db\driver\driver_interface	$db     			Database connection
53 3
	 * @param \phpbb\group\helper				$group_helper		Group helper object
54 3
	 * @param \phpbb\language\language			$translator			Language object
55 3
	 * @param \phpbb\template\template			$template			Template object
56 3
	 * @param \phpbb\user						$user				User object
57 3
	 * @param string							$phpbb_root_path	Path to the phpbb includes directory.
58 3
	 * @param string							$php_ext			php file extension
59 3
	 */
60 3
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\group\helper $group_helper, \phpbb\language\language $translator, \phpbb\template\template $template, \phpbb\user $user, $phpbb_root_path, $php_ext)
61
	{
62
		$this->auth = $auth;
63
		$this->config = $config;
64
		$this->db = $db;
65 2
		$this->group_helper = $group_helper;
66
		$this->translator = $translator;
67 2
		$this->template = $template;
68
		$this->user = $user;
69 2
		$this->phpbb_root_path = $phpbb_root_path;
70 2
		$this->php_ext = $php_ext;
71 1
	}
72 1
73
	/**
74
	 * {@inheritdoc}
75 1
	 */
76 1
	public function display(array $bdata, $edit_mode = false)
77
	{
78 1
		$data = $this->template->retrieve_vars(array('TOTAL_USERS_ONLINE', 'LOGGED_IN_USER_LIST', 'RECORD_USERS', 'LEGEND'));
79 1
80
		if (!empty($data['TOTAL_USERS_ONLINE']))
81 1
		{
82 1
			list($l_online_users, $online_userlist, $l_online_record) = array_values($data);
83
		}
84 1
		else
85
		{
86
			$online_users = obtain_users_online();
87 2
			$user_online_strings = obtain_users_online_string($online_users);
88
89 2
			$l_online_users = $user_online_strings['l_online_users'];
90 2
			$online_userlist = $user_online_strings['online_userlist'];
91 2
92 2
			$l_online_record = $this->translator->lang('RECORD_ONLINE_USERS', (int) $this->config['record_online_users'], $this->user->format_date($this->config['record_online_date'], false, true));
93 2
		}
94 2
95 2
		$this->template->assign_var('S_DISPLAY_ONLINE_LIST', false);
96
97
		return array(
98 2
			'title'	=> 'WHO_IS_ONLINE',
99 2
			'data'	=> array(
100 2
				'TOTAL_USERS_ONLINE'	=> $l_online_users,
101
				'LOGGED_IN_USER_LIST'	=> $online_userlist,
102
				'RECORD_USERS'			=> $l_online_record,
103
				'LEGEND'				=> $data['LEGEND'] ? $data['LEGEND'] : $this->get_legend(),
104
				'U_VIEWONLINE'			=> $this->get_viewonline_url(),
105
			)
106 2
		);
107
	}
108 2
109
	/**
110
	 * @return string
111
	 */
112
	private function get_viewonline_url()
113
	{
114
		return ($this->auth->acl_gets('u_viewprofile', 'a_user', 'a_useradd', 'a_userdel')) ? append_sid("{$this->phpbb_root_path}viewonline." . $this->php_ext) : '';
115
	}
116
117
	/**
118
	 * @return string
119
	 */
120
	private function get_legend()
121
	{
122
		$sql = $this->get_legend_sql();
123
		$result = $this->db->sql_query($sql);
124
125
		$legend = array();
126
		while ($row = $this->db->sql_fetchrow($result))
127
		{
128
			$colour_text = ($row['group_colour']) ? ' style="color:#' . $row['group_colour'] . '"' : '';
129
			$group_name = $this->group_helper->get_name($row['group_name']);
130
131
			$legend[] = $this->get_legend_html($row, $group_name, $colour_text);
132
		}
133
		$this->db->sql_freeresult($result);
134
135
		return implode($this->translator->lang('COMMA_SEPARATOR'), $legend);
136
	}
137
138
	/**
139
	 * @param array $row
140
	 * @param string $group_name
141
	 * @param string $colour_text
142
	 * @return string
143
	 */
144
	protected function get_legend_html(array $row, $group_name, $colour_text)
145
	{
146
		if ($row['group_name'] == 'BOTS' || ($this->user->data['user_id'] != ANONYMOUS && !$this->auth->acl_get('u_viewprofile')))
147
		{
148
			return '<span' . $colour_text . '>' . $group_name . '</span>';
149
		}
150
		else
151
		{
152
			return '<a' . $colour_text . ' href="' . append_sid("{$this->phpbb_root_path}memberlist.{$this->php_ext}", 'mode=group&amp;g=' . $row['group_id']) . '">' . $group_name . '</a>';
153
		}
154
	}
155
156
	/**
157
	 * @return string
158
	 */
159
	protected function get_legend_sql()
160
	{
161
		$order_legend = ($this->config['legend_sort_groupname']) ? 'group_name' : 'group_legend';
162
163
		// Grab group details for legend display
164
		if ($this->auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel'))
165
		{
166
			return 'SELECT group_id, group_name, group_colour, group_type, group_legend
167
				FROM ' . GROUPS_TABLE . '
168
				WHERE group_legend > 0
169
				ORDER BY ' . $order_legend . ' ASC';
170
		}
171
		else
172
		{
173
			return 'SELECT g.group_id, g.group_name, g.group_colour, g.group_type, g.group_legend
174
				FROM ' . GROUPS_TABLE . ' g
175
				LEFT JOIN ' . USER_GROUP_TABLE . ' ug
176
					ON (
177
						g.group_id = ug.group_id
178
						AND ug.user_id = ' . $this->user->data['user_id'] . '
179
						AND ug.user_pending = 0
180
					)
181
				WHERE g.group_legend > 0
182
					AND (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $this->user->data['user_id'] . ')
183
				ORDER BY g.' . $order_legend . ' ASC';
184
		}
185
	}
186
187
	/**
188
	 * {@inheritdoc}
189
	 */
190
	public function get_template()
191
	{
192
		return '@blitze_sitemaker/blocks/whois.html';
193
	}
194
}
195