Completed
Push — master ( c96b51...1deced )
by Daniel
08:48
created

groups::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
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\services;
11
12
class groups
13
{
14
	/** @var \phpbb\db\driver\driver_interface */
15
	protected $db;
16
17
	/** @var \phpbb\user */
18
	protected $user;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param \phpbb\db\driver\driver_interface		$db	 		Database connection
24
	 * @param \phpbb\user							$user		User object
25
	 */
26 8
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user)
27
	{
28 8
		$this->db = $db;
29 8
		$this->user = $user;
30 8
	}
31
32
	/**
33
	 * @return array
34
	 */
35 3
	public function get_users_groups()
36
	{
37
		$sql = 'SELECT group_id
38 3
			FROM ' . USER_GROUP_TABLE . '
39 3
			WHERE user_id = ' . (int) $this->user->data['user_id'];
40 3
		$result = $this->db->sql_query($sql);
41
42 3
		$groups = array();
43 3
		while ($row = $this->db->sql_fetchrow($result))
44
		{
45 3
			$groups[$row['group_id']] = (int) $row['group_id'];
46 3
		}
47 3
		$this->db->sql_freeresult($result);
48
49 3
		return $groups;
50
	}
51
52
	/**
53
	 * @param string $mode (all|special)
54
	 * @return array
55
	 */
56 2
	public function get_data($mode = 'all')
57
	{
58 2
		$sql = $this->_get_group_sql($mode);
59 2
		$result = $this->db->sql_query($sql);
60
61 2
		$group_data = array('' => 'ALL');
62 2
		while ($row = $this->db->sql_fetchrow($result))
63
		{
64 2
			$group_data[$row['group_id']] = $this->_get_group_name($row);
65 2
		}
66 2
		$this->db->sql_freeresult($result);
67
68 2
		return $group_data;
69
	}
70
71
	/**
72
	 * @param string $mode (all|special)
73
	 * @param array $selected
74
	 * @return string
75
	 */
76 3
	public function get_options($mode = 'all', array $selected = array())
77
	{
78 3
		$sql = $this->_get_group_sql($mode);
79 3
		$result = $this->db->sql_query($sql);
80
81 3
		$options = '<option value="0">' . $this->user->lang('ALL') . '</option>';
82 3
		while ($row = $this->db->sql_fetchrow($result))
83
		{
84 3
			$group_name = $this->_get_group_name($row);
85 3
			$group_class = $this->_get_group_class($row['group_type']);
86 3
			$selected_option = $this->_get_selected_option($row['group_id'], $selected);
87 3
			$options .= '<option' . $group_class . ' value="' . $row['group_id'] . '"' . $selected_option . '>' . $group_name . '</option>';
88 3
		}
89 3
		$this->db->sql_freeresult($result);
90
91 3
		return $options;
92
	}
93
94
	/**
95
	 * @param string $mode (all|special)
96
	 * @return string
97
	 */
98 5
	private function _get_group_sql($mode)
99
	{
100
		return 'SELECT group_id, group_name, group_type 
101 5
			FROM ' . GROUPS_TABLE .
102 5
			(($mode === 'special') ? ' WHERE group_type = ' . GROUP_SPECIAL : '') . '
103 5
			ORDER BY group_name ASC';
104
	}
105
106
	/**
107
	 * @param array $row
108
	 * @return mixed|string
109
	 */
110 5
	private function _get_group_name(array $row)
111
	{
112 5
		return ($row['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $row['group_name']) : ucfirst($row['group_name']);
113
	}
114
115
	/**
116
	 * @param int $group_type
117
	 * @return string
118
	 */
119 3
	private function _get_group_class($group_type)
120
	{
121 3
		return ($group_type == GROUP_SPECIAL) ? ' class="sep"' : '';
122
	}
123
124
	/**
125
	 * @param int $group_id
126
	 * @param array $selected_options
127
	 * @return string
128
	 */
129 3
	private function _get_selected_option($group_id, $selected_options)
130
	{
131 3
		return (in_array($group_id, $selected_options)) ? ' selected="selected"' : '';
132
	}
133
}
134