2011-02-09 21:02:18 +01:00
|
|
|
<?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;
|
|
|
|
|
|
|
|
/**
|
2011-02-14 16:09:09 +01:00
|
|
|
* phpbb-database object
|
2011-02-09 21:02:18 +01:00
|
|
|
*/
|
2011-02-14 16:09:09 +01:00
|
|
|
public $db = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the field we want to handle: either 'teampage' or 'legend'
|
|
|
|
*/
|
|
|
|
private $field = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
public function __construct ($db, $field)
|
2011-02-09 21:02:18 +01:00
|
|
|
{
|
2011-02-14 16:09:09 +01:00
|
|
|
if (!in_array($field, array('teampage', 'legend')))
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
$this->db = $db;
|
|
|
|
$this->field = $field;
|
|
|
|
}
|
2011-02-09 21:02:18 +01:00
|
|
|
|
2011-02-14 16:09:09 +01:00
|
|
|
/**
|
|
|
|
* 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 . '
|
2011-02-09 21:02:18 +01:00
|
|
|
FROM ' . GROUPS_TABLE . '
|
|
|
|
WHERE group_id = ' . (int) $group_id;
|
2011-02-14 16:09:09 +01:00
|
|
|
$result = $this->db->sql_query($sql);
|
|
|
|
$current_value = $this->db->sql_fetchfield('group_' . $this->field);
|
|
|
|
$this->db->sql_freeresult($result);
|
2011-02-09 21:02:18 +01:00
|
|
|
|
|
|
|
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
|
2011-02-14 16:09:09 +01:00
|
|
|
*
|
|
|
|
* @return int value of the last group displayed
|
2011-02-09 21:02:18 +01:00
|
|
|
*/
|
2011-02-14 16:09:09 +01:00
|
|
|
public function get_group_count()
|
2011-02-09 21:02:18 +01:00
|
|
|
{
|
2011-02-14 16:09:09 +01:00
|
|
|
$sql = 'SELECT group_' . $this->field . '
|
2011-02-09 21:02:18 +01:00
|
|
|
FROM ' . GROUPS_TABLE . '
|
2011-02-14 16:09:09 +01:00
|
|
|
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);
|
2011-02-09 21:02:18 +01:00
|
|
|
|
|
|
|
return $group_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Addes a group by group_id
|
2011-02-14 16:09:09 +01:00
|
|
|
*
|
|
|
|
* @param int $group_id group_id of the group to be added
|
|
|
|
* @return void
|
2011-02-09 21:02:18 +01:00
|
|
|
*/
|
2011-02-14 16:09:09 +01:00
|
|
|
public function add_group($group_id)
|
2011-02-09 21:02:18 +01:00
|
|
|
{
|
2011-02-14 16:09:09 +01:00
|
|
|
$current_value = $this->get_group_value($group_id);
|
2011-02-09 21:02:18 +01:00
|
|
|
|
|
|
|
if ($current_value == self::GROUP_DISABLED)
|
|
|
|
{
|
|
|
|
// Group is currently not displayed, add it at the end.
|
2011-02-14 16:09:09 +01:00
|
|
|
$next_value = 1 + $this->get_group_count();
|
2011-02-09 21:02:18 +01:00
|
|
|
|
|
|
|
$sql = 'UPDATE ' . GROUPS_TABLE . '
|
2011-02-14 16:09:09 +01:00
|
|
|
SET group_' . $this->field . ' = ' . $next_value . '
|
|
|
|
WHERE group_' . $this->field . ' = ' . self::GROUP_DISABLED . '
|
2011-02-09 21:02:18 +01:00
|
|
|
AND group_id = ' . (int) $group_id;
|
2011-02-14 16:09:09 +01:00
|
|
|
$this->db->sql_query($sql);
|
2011-02-09 21:02:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a group by group_id
|
2011-02-14 16:09:09 +01:00
|
|
|
*
|
|
|
|
* @param int $group_id group_id of the group to be deleted
|
|
|
|
* @param bool $skip_group Skip the group itself, to save the query, when you need to update it anyway.
|
|
|
|
* @return void
|
2011-02-09 21:02:18 +01:00
|
|
|
*/
|
2011-02-14 16:09:09 +01:00
|
|
|
public function delete_group($group_id, $skip_group = false)
|
2011-02-09 21:02:18 +01:00
|
|
|
{
|
2011-02-14 16:09:09 +01:00
|
|
|
$current_value = $this->get_group_value($group_id);
|
2011-02-09 21:02:18 +01:00
|
|
|
|
|
|
|
if ($current_value != self::GROUP_DISABLED)
|
|
|
|
{
|
2011-02-14 16:09:09 +01:00
|
|
|
$this->db->sql_transaction('begin');
|
2011-02-09 21:02:18 +01:00
|
|
|
|
|
|
|
$sql = 'UPDATE ' . GROUPS_TABLE . '
|
2011-02-14 16:09:09 +01:00
|
|
|
SET group_' . $this->field . ' = group_' . $this->field . ' - 1
|
|
|
|
WHERE group_' . $this->field . ' > ' . $current_value;
|
|
|
|
$this->db->sql_query($sql);
|
2011-02-09 21:02:18 +01:00
|
|
|
|
2011-02-14 16:09:09 +01:00
|
|
|
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);
|
|
|
|
}
|
2011-02-09 21:02:18 +01:00
|
|
|
|
2011-02-14 16:09:09 +01:00
|
|
|
$this->db->sql_transaction('commit');
|
2011-02-09 21:02:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves a group up by group_id
|
2011-02-14 16:09:09 +01:00
|
|
|
*
|
|
|
|
* @param int $group_id group_id of the group to be moved
|
|
|
|
* @return void
|
2011-02-09 21:02:18 +01:00
|
|
|
*/
|
2011-02-14 16:09:09 +01:00
|
|
|
public function move_up($group_id)
|
2011-02-09 21:02:18 +01:00
|
|
|
{
|
2011-02-14 16:09:09 +01:00
|
|
|
$current_value = $this->get_group_value($group_id);
|
2011-02-09 21:02:18 +01:00
|
|
|
|
|
|
|
// Only move the group, if it is in the list and not already on top.
|
|
|
|
if ($current_value > 1)
|
|
|
|
{
|
2011-02-14 16:09:09 +01:00
|
|
|
$this->db->sql_transaction('begin');
|
2011-02-09 21:02:18 +01:00
|
|
|
|
|
|
|
$sql = 'UPDATE ' . GROUPS_TABLE . '
|
2011-02-14 16:09:09 +01:00
|
|
|
SET group_' . $this->field . ' = group_' . $this->field . ' + 1
|
|
|
|
WHERE group_' . $this->field . ' = ' . ($current_value - 1);
|
|
|
|
$this->db->sql_query($sql);
|
2011-02-09 21:02:18 +01:00
|
|
|
|
|
|
|
$sql = 'UPDATE ' . GROUPS_TABLE . '
|
2011-02-14 16:09:09 +01:00
|
|
|
SET group_' . $this->field . ' = ' . ($current_value - 1) . '
|
2011-02-09 21:02:18 +01:00
|
|
|
WHERE group_id = ' . (int) $group_id;
|
2011-02-14 16:09:09 +01:00
|
|
|
$this->db->sql_query($sql);
|
2011-02-09 21:02:18 +01:00
|
|
|
|
2011-02-14 16:09:09 +01:00
|
|
|
$this->db->sql_transaction('commit');
|
2011-02-09 21:02:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves a group down by group_id
|
2011-02-14 16:09:09 +01:00
|
|
|
*
|
|
|
|
* @param int $group_id group_id of the group to be moved
|
|
|
|
* @return void
|
2011-02-09 21:02:18 +01:00
|
|
|
*/
|
2011-02-14 16:09:09 +01:00
|
|
|
public function move_down($group_id)
|
2011-02-09 21:02:18 +01:00
|
|
|
{
|
2011-02-14 16:09:09 +01:00
|
|
|
$current_value = $this->get_group_value($group_id);
|
2011-02-09 21:02:18 +01:00
|
|
|
|
|
|
|
if ($current_value != self::GROUP_DISABLED)
|
|
|
|
{
|
2011-02-14 16:09:09 +01:00
|
|
|
$this->db->sql_transaction('begin');
|
2011-02-09 21:02:18 +01:00
|
|
|
|
|
|
|
$sql = 'UPDATE ' . GROUPS_TABLE . '
|
2011-02-14 16:09:09 +01:00
|
|
|
SET group_' . $this->field . ' = group_' . $this->field . ' - 1
|
|
|
|
WHERE group_' . $this->field . ' = ' . ($current_value + 1);
|
|
|
|
$this->db->sql_query($sql);
|
2011-02-09 21:02:18 +01:00
|
|
|
|
2011-02-14 16:09:09 +01:00
|
|
|
if ($this->db->sql_affectedrows() == 1)
|
2011-02-09 21:02:18 +01:00
|
|
|
{
|
|
|
|
// Only update when we move another one up, otherwise it was the last.
|
|
|
|
$sql = 'UPDATE ' . GROUPS_TABLE . '
|
2011-02-14 16:09:09 +01:00
|
|
|
SET group_' . $this->field . ' = ' . ($current_value + 1) . '
|
2011-02-09 21:02:18 +01:00
|
|
|
WHERE group_id = ' . (int) $group_id;
|
2011-02-14 16:09:09 +01:00
|
|
|
$this->db->sql_query($sql);
|
2011-02-09 21:02:18 +01:00
|
|
|
}
|
|
|
|
|
2011-02-14 16:09:09 +01:00
|
|
|
$this->db->sql_transaction('commit');
|
2011-02-09 21:02:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get group type language var
|
2011-02-14 16:09:09 +01:00
|
|
|
*
|
|
|
|
* @param int $group_type group_type from the groups-table
|
|
|
|
* @return string name of the language variable for the given group-type.
|
2011-02-09 21:02:18 +01:00
|
|
|
*/
|
2011-02-14 16:09:09 +01:00
|
|
|
static public function group_type_language($group_type)
|
2011-02-09 21:02:18 +01:00
|
|
|
{
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|