friends::install()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
*
4
* @package Board3 Portal v2.1
5
* @copyright (c) 2013 Board3 Group ( www.board3.de )
6
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
*
8
*/
9
10
namespace board3\portal\modules;
11
12
/**
13
* @package Friends
14
*/
15
class friends extends module_base
16
{
17
	/**
18
	* Allowed columns: Just sum up your options (Exp: left + right = 10)
19
	* top		1
20
	* left		2
21
	* center	4
22
	* right		8
23
	* bottom	16
24
	*/
25
	public $columns = 10;
26
27
	/**
28
	* Default modulename
29
	*/
30
	public $name = 'FRIENDS';
31
32
	/**
33
	* Default module-image:
34
	* file must be in "{T_THEME_PATH}/images/portal/"
35
	*/
36
	public $image_src = 'portal_friends.png';
37
38
	/**
39
	* module-language file
40
	* file must be in "language/{$user->lang}/mods/portal/"
41
	*/
42
	public $language = 'portal_friends_module';
43
44
	/** @var \phpbb\auth\auth */
45
	protected $auth;
46
47
	/** @var \phpbb\config\config */
48
	protected $config;
49
50
	/** @var \phpbb\db\driver */
51
	protected $db;
52
53
	/** @var \phpbb\template */
54
	protected $template;
55
56
	/** @var \phpbb\user */
57
	protected $user;
58
59
	/**
60
	* Construct a friends object
61
	*
62
	* @param \phpbb\auth\auth $auth phpBB auth service
63
	* @param \phpbb\config\config $config phpBB config
64
	* @param \phpbb\db\driver $db phpBB db driver
65
	* @param \phpbb\template $template phpBB template
66
	* @param \phpbb\user $user phpBB user object
67
	*/
68 View Code Duplication
	public function __construct($auth, $config, $db, $template, $user)
69
	{
70
		$this->auth = $auth;
71
		$this->config = $config;
72
		$this->db = $db;
73
		$this->template = $template;
74
		$this->user = $user;
75
	}
76
77
	/**
78
	* {@inheritdoc}
79
	*/
80
	public function get_template_side($module_id)
81
	{
82
		$s_display_friends = false;
83
84
		// Output listing of friends online
85
		$update_time = $this->config['load_online_time'] * 60;
86
87
		$sql = $this->db->sql_build_query('SELECT_DISTINCT', array(
88
			'SELECT'	=> 'u.user_id, u.username, u.username_clean, u.user_colour, u.user_allow_viewonline, MAX(s.session_time) as online_time, MIN(s.session_viewonline) AS viewonline',
89
			'FROM'		=> array(
90
				USERS_TABLE	=> 'u',
91
				ZEBRA_TABLE	=> 'z'
92
			),
93
94
			'LEFT_JOIN'	=> array(
95
				array(
96
					'FROM'	=> array(SESSIONS_TABLE => 's'),
97
					'ON'	=> 's.session_user_id = z.zebra_id'
98
				)
99
			),
100
101
			'WHERE'		=> 'z.user_id = ' . $this->user->data['user_id'] . '
102
				AND z.friend = 1
103
				AND u.user_id = z.zebra_id',
104
			'GROUP_BY'	=> 'z.zebra_id, u.user_id, u.username, u.username_clean, u.user_allow_viewonline, u.user_colour',
105
			'ORDER_BY'   => 'u.username_clean ASC',
106
		));
107
108
		$result = $this->db->sql_query_limit($sql, $this->config['board3_max_online_friends_' . $module_id]);
109
110
		while ($row = $this->db->sql_fetchrow($result))
111
		{
112
			$which = (time() - $update_time < $row['online_time'] && ($row['viewonline'] || $this->auth->acl_get('u_viewonline'))) ? 'online' : 'offline';
113
			$s_display_friends = ($row['user_id']) ? true : false;
114
115
			$this->template->assign_block_vars("b3p_friends_{$which}", array(
116
				'USER_ID'		=> $row['user_id'],
117
				'U_PROFILE'		=> get_username_string('profile', $row['user_id'], $row['username'], $row['user_colour']),
118
				'USER_COLOUR'	=> get_username_string('colour', $row['user_id'], $row['username'], $row['user_colour']),
119
				'USERNAME'		=> get_username_string('username', $row['user_id'], $row['username'], $row['user_colour']),
120
				'USERNAME_FULL'	=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']))
121
			);
122
		}
123
		$this->db->sql_freeresult($result);
124
125
		// Assign specific vars
126
		$this->template->assign_vars(array(
127
			'S_DISPLAY_FRIENDS'	=> $s_display_friends,
128
		));
129
130
		return 'friends_side.html';
131
	}
132
133
	/**
134
	* {@inheritdoc}
135
	*/
136 View Code Duplication
	public function get_template_acp($module_id)
137
	{
138
		return array(
139
			'title'	=> 'ACP_PORTAL_FRIENDS_SETTINGS',
140
			'vars'	=> array(
141
				'legend1'					=> 'ACP_PORTAL_FRIENDS_SETTINGS',
142
				'board3_max_online_friends_' . $module_id	=> array('lang' => 'PORTAL_MAX_ONLINE_FRIENDS',	'validate' => 'int',	'type' => 'text:3:3', 'explain' => true),
143
			)
144
		);
145
	}
146
147
	/**
148
	* {@inheritdoc}
149
	*/
150
	public function install($module_id)
151
	{
152
		$this->config->set('board3_max_online_friends_' . $module_id, 8);
153
		return true;
154
	}
155
156
	/**
157
	* {@inheritdoc}
158
	*/
159 View Code Duplication
	public function uninstall($module_id, $db)
160
	{
161
		$del_config = array(
162
			'board3_max_online_friends_' . $module_id,
163
		);
164
		$sql = 'DELETE FROM ' . CONFIG_TABLE . '
165
			WHERE ' . $db->sql_in_set('config_name', $del_config);
166
		return $db->sql_query($sql);
167
	}
168
}
169