1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Group Position class, containing all functions to manage the groups in the teampage and legend.
*
* group_teampage/group_legend is an ascending list 1, 2, ..., n for groups which are displayed. 1 is the first group, n the last.
* If the value is 0 (self::GROUP_DISABLED) the group is not displayed.
* @package phpBB3
*/
class phpbb_group_positions
{
/**
* Group is not displayed
*/
const GROUP_DISABLED = 0;
/**
* Returns the group_{$field} for a given group, if the group exists.
* @param string $field name of the field to be selected
* @param int $group_id group_id of the group to be selected
* @return int position of the group
*/
static function get_group_value($field, $group_id)
{
global $db;
$sql = 'SELECT group_' . $field . '
FROM ' . GROUPS_TABLE . '
WHERE group_id = ' . (int) $group_id;
$result = $db->sql_query($sql);
$current_value = $db->sql_fetchfield('group_' . $field);
$db->sql_freeresult($result);
if ($current_value === false)
{
// Group not found.
global $user;
trigger_error($user->lang['NO_GROUP'] . adm_back_link($this->u_action), E_USER_WARNING);
}
return (int) $current_value;
}
/**
* Get number of groups, displayed on the teampage/legend
* @param string $field name of the field to be counted
* @return int value of the last group displayed
*/
static function get_group_count($field)
{
global $db;
$sql = 'SELECT group_' . $field . '
FROM ' . GROUPS_TABLE . '
ORDER BY group_' . $field . ' DESC';
$result = $db->sql_query_limit($sql, 1);
$group_count = (int) $db->sql_fetchfield('group_' . $field);
$db->sql_freeresult($result);
return $group_count;
}
/**
* Addes a group by group_id
* @param string $field name of the field the group is added to
* @param int $group_id group_id of the group to be added
* @return void
*/
static function add_group($field, $group_id)
{
$current_value = self::get_group_value($field, $group_id);
if ($current_value == self::GROUP_DISABLED)
{
global $db;
// Group is currently not displayed, add it at the end.
$next_value = 1 + self::get_group_count($field, $field);
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = ' . $next_value . '
WHERE group_' . $field . ' = ' . self::GROUP_DISABLED . '
AND group_id = ' . (int) $group_id;
$db->sql_query($sql);
}
}
/**
* Deletes a group by group_id
* @param string $field name of the field the group is deleted from
* @param int $group_id group_id of the group to be deleted
* @return void
*/
static function delete_group($field, $group_id)
{
$current_value = self::get_group_value($field, $group_id);
if ($current_value != self::GROUP_DISABLED)
{
global $db;
$db->sql_transaction('begin');
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = group_' . $field . ' - 1
WHERE group_' . $field . ' > ' . $current_value;
$db->sql_query($sql);
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = ' . self::GROUP_DISABLED . '
WHERE group_id = ' . (int) $group_id;
$db->sql_query($sql);
$db->sql_transaction('commit');
}
}
/**
* Moves a group up by group_id
* @param string $field name of the field the group is moved by
* @param int $group_id group_id of the group to be moved
* @return void
*/
static function move_up($field, $group_id)
{
$current_value = self::get_group_value($field, $group_id);
// Only move the group, if it is in the list and not already on top.
if ($current_value > 1)
{
global $db;
$db->sql_transaction('begin');
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = group_' . $field . ' + 1
WHERE group_' . $field . ' = ' . ($current_value - 1);
$db->sql_query($sql);
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = ' . ($current_value - 1) . '
WHERE group_id = ' . (int) $group_id;
$db->sql_query($sql);
$db->sql_transaction('commit');
}
}
/**
* Moves a group down by group_id
* @param string $field name of the field the group is moved by
* @param int $group_id group_id of the group to be moved
* @return void
*/
static function move_down($field, $group_id)
{
$current_value = self::get_group_value($field, $group_id);
if ($current_value != self::GROUP_DISABLED)
{
global $db;
$db->sql_transaction('begin');
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = group_' . $field . ' - 1
WHERE group_' . $field . ' = ' . ($current_value + 1);
$db->sql_query($sql);
if ($db->sql_affectedrows() == 1)
{
// Only update when we move another one up, otherwise it was the last.
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = ' . ($current_value + 1) . '
WHERE group_id = ' . (int) $group_id;
$db->sql_query($sql);
}
$db->sql_transaction('commit');
}
}
/**
* Get group type language var
* @param int $group_type group_type from the groups-table
* @return string name of the language variable for the given group-type.
*/
static function group_type_language($group_type)
{
switch ($group_type)
{
case GROUP_OPEN:
return 'GROUP_REQUEST';
case GROUP_CLOSED:
return 'GROUP_CLOSED';
case GROUP_HIDDEN:
return 'GROUP_HIDDEN';
case GROUP_SPECIAL:
return 'GROUP_SPECIAL';
case GROUP_FREE:
return 'GROUP_OPEN';
}
}
}
|