groups   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 125
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0
wmc 15

8 Methods

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