aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/group_positions.php
blob: 60352ed97dcea385f9b3a16695a02931721c5440 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
*
* @package phpBB3
* @copyright (c) 2011 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/

/**
* @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;

	/**
	* phpbb-database object
	*/
	public $db = null;

	/**
	* Name of the field we want to handle: either 'teampage' or 'legend'
	*/
	private $field = '';

	/**
	* URI for the adm_back_link when there was an error.
	*/
	private $adm_back_link = '';

	/**
	* Constructor
	*/
	public function __construct ($db, $field, $adm_back_link = '')
	{
		$this->adm_back_link = $adm_back_link;

		if (!in_array($field, array('teampage', 'legend')))
		{
			$this->error('NO_MODE');
		}

		$this->db = $db;
		$this->field = $field;
	}

	/**
	* Returns the group_{$this->field} for a given group, if the group exists.
	* @param	int		$group_id	group_id of the group to be selected
	* @return	int					position of the group
	*/
	public function get_group_value($group_id)
	{
		$sql = 'SELECT group_' . $this->field . '
			FROM ' . GROUPS_TABLE . '
			WHERE group_id = ' . (int) $group_id;
		$result = $this->db->sql_query($sql);
		$current_value = $this->db->sql_fetchfield('group_' . $this->field);
		$this->db->sql_freeresult($result);

		if ($current_value === false)
		{
			// Group not found.
			$this->error('NO_GROUP');
		}

		return (int) $current_value;
	}

	/**
	* Get number of groups, displayed on the teampage/legend
	*
	* @return	int		value of the last group displayed
	*/
	public function get_group_count()
	{
		$sql = 'SELECT group_' . $this->field . '
			FROM ' . GROUPS_TABLE . '
			ORDER BY group_' . $this->field . ' DESC';
		$result = $this->db->sql_query_limit($sql, 1);
		$group_count = (int) $this->db->sql_fetchfield('group_' . $this->field);
		$this->db->sql_freeresult($result);

		return $group_count;
	}

	/**
	* Addes a group by group_id
	*
	* @param	int		$group_id	group_id of the group to be added
	* @return	null
	*/
	public function add_group($group_id)
	{
		$current_value = $this->get_group_value($group_id);

		if ($current_value == self::GROUP_DISABLED)
		{
			// Group is currently not displayed, add it at the end.
			$next_value = 1 + $this->get_group_count();

			$sql = 'UPDATE ' . GROUPS_TABLE . '
				SET group_' . $this->field . ' = ' . $next_value . '
				WHERE group_' . $this->field . ' = ' . self::GROUP_DISABLED . '
					AND group_id = ' . (int) $group_id;
			$this->db->sql_query($sql);
		}
	}

	/**
	* Deletes a group by setting the field to self::GROUP_DISABLED and closing the gap in the list.
	*
	* @param	int		$group_id		group_id of the group to be deleted
	* @param	bool	$skip_group		Skip setting the group to GROUP_DISABLED, to save the query, when you need to update it anyway.
	* @return	null
	*/
	public function delete_group($group_id, $skip_group = false)
	{
		$current_value = $this->get_group_value($group_id);

		if ($current_value != self::GROUP_DISABLED)
		{
			$this->db->sql_transaction('begin');

			$sql = 'UPDATE ' . GROUPS_TABLE . '
				SET group_' . $this->field . ' = group_' . $this->field . ' - 1
				WHERE group_' . $this->field . ' > ' . $current_value;
			$this->db->sql_query($sql);

			if (!$skip_group)
			{
				$sql = 'UPDATE ' . GROUPS_TABLE . '
					SET group_' . $this->field . ' = ' . self::GROUP_DISABLED . '
					WHERE group_id = ' . (int) $group_id;
				$this->db->sql_query($sql);
			}

			$this->db->sql_transaction('commit');
		}
	}

	/**
	* Moves a group up by group_id
	*
	* @param	int		$group_id	group_id of the group to be moved
	* @return	null
	*/
	public function move_up($group_id)
	{
		$this->move($group_id, 1);
	}

	/**
	* Moves a group down by group_id
	*
	* @param	int		$group_id	group_id of the group to be moved
	* @return	null
	*/
	public function move_down($group_id)
	{
		$this->move($group_id, -1);
	}

	/**
	* Moves a group up/down
	*
	* @param	int		$group_id	group_id of the group to be moved
	* @param	int		$delta		number of steps:
	*								- positive = move up
	*								- negative = move down
	* @return	null
	*/
	public function move($group_id, $delta)
	{
		if (!is_int($delta) || !$delta)
		{
			return;
		}

		$move_up = ($delta > 0) ? true : false;
		$current_value = $this->get_group_value($group_id);

		if ($current_value != self::GROUP_DISABLED)
		{
			$this->db->sql_transaction('begin');

			// First we move all groups between our current value and the target value up/down 1,
			// so we have a gap for our group to move.
			$sql = 'UPDATE ' . GROUPS_TABLE . '
				SET group_' . $this->field . ' = group_' . $this->field . (($move_up) ? ' + 1' : ' - 1') . '
				WHERE group_' . $this->field . ' > ' . self::GROUP_DISABLED . '
					AND group_' . $this->field . (($move_up) ? ' >= ' : ' <= ') . ($current_value - $delta) . '
					AND group_' . $this->field . (($move_up) ? ' < ' : ' > ') . $current_value;
			$this->db->sql_query($sql);

			// Because there might be fewer groups above/below the group than we wanted to move,
			// we use the number of changed groups, to update the group.
			$delta = (int) $this->db->sql_affectedrows();

			if ($delta)
			{
				// And now finally, when we moved some other groups and built a gap,
				// we can move the desired group to it.
				$sql = 'UPDATE ' . GROUPS_TABLE . '
					SET group_' . $this->field . ' = group_' . $this->field . (($move_up) ? ' - ' : ' + ') . $delta . '
					WHERE group_id = ' . (int) $group_id;
				$this->db->sql_query($sql);
			}

			$this->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 public 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';
		}
	}

	/**
	* Error
	*/
	public function error($message)
	{
		global $user;
		trigger_error($user->lang[$message] . (($this->adm_back_link) ? adm_back_link($this->adm_back_link) : ''), E_USER_WARNING);
	}
}