From b7d3d57b700ecfc3c845085e0f1809ee0acbde33 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 9 Feb 2011 21:02:18 +0100 Subject: [ticket/9549] Enhance teampage functionality with a new class, group_positions. PHPBB3-9549 --- phpBB/includes/group_positions.php | 220 +++++++++++++++++++++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 phpBB/includes/group_positions.php (limited to 'phpBB/includes/group_positions.php') diff --git a/phpBB/includes/group_positions.php b/phpBB/includes/group_positions.php new file mode 100644 index 0000000000..1749ca0578 --- /dev/null +++ b/phpBB/includes/group_positions.php @@ -0,0 +1,220 @@ +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'; + } + } +} -- cgit v1.2.1 From 8d12838aedeaa23cccf128e98e93d05507edda4d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 14 Feb 2011 16:09:09 +0100 Subject: [ticket/9549] Make the class non static and extend delete_group function. delete_group() can now be used, so it does not update the actual group. This can save a query, when you update the group anyway. PHPBB3-9549 --- phpBB/includes/group_positions.php | 173 ++++++++++++++++++++----------------- 1 file changed, 94 insertions(+), 79 deletions(-) (limited to 'phpBB/includes/group_positions.php') diff --git a/phpBB/includes/group_positions.php b/phpBB/includes/group_positions.php index 1749ca0578..e63e17c384 100644 --- a/phpBB/includes/group_positions.php +++ b/phpBB/includes/group_positions.php @@ -31,21 +31,41 @@ class phpbb_group_positions 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 + * phpbb-database object */ - static function get_group_value($field, $group_id) + public $db = null; + + /** + * Name of the field we want to handle: either 'teampage' or 'legend' + */ + private $field = ''; + + /** + * Constructor + */ + public function __construct ($db, $field) { - global $db; + if (!in_array($field, array('teampage', 'legend'))) + { + + } + $this->db = $db; + $this->field = $field; + } - $sql = 'SELECT group_' . $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 = $db->sql_query($sql); - $current_value = $db->sql_fetchfield('group_' . $field); - $db->sql_freeresult($result); + $result = $this->db->sql_query($sql); + $current_value = $this->db->sql_fetchfield('group_' . $this->field); + $this->db->sql_freeresult($result); if ($current_value === false) { @@ -59,149 +79,144 @@ class phpbb_group_positions /** * 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 + * + * @return int value of the last group displayed */ - static function get_group_count($field) + public function get_group_count() { - global $db; - - $sql = 'SELECT group_' . $field . ' + $sql = 'SELECT group_' . $this->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); + 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 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 + * + * @param int $group_id group_id of the group to be added + * @return void */ - static function add_group($field, $group_id) + public function add_group($group_id) { - $current_value = self::get_group_value($field, $group_id); + $current_value = $this->get_group_value($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); + $next_value = 1 + $this->get_group_count(); $sql = 'UPDATE ' . GROUPS_TABLE . ' - SET group_' . $field . ' = ' . $next_value . ' - WHERE group_' . $field . ' = ' . self::GROUP_DISABLED . ' + SET group_' . $this->field . ' = ' . $next_value . ' + WHERE group_' . $this->field . ' = ' . self::GROUP_DISABLED . ' AND group_id = ' . (int) $group_id; - $db->sql_query($sql); + $this->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 + * + * @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 */ - static function delete_group($field, $group_id) + public function delete_group($group_id, $skip_group = false) { - $current_value = self::get_group_value($field, $group_id); + $current_value = $this->get_group_value($group_id); if ($current_value != self::GROUP_DISABLED) { - global $db; - - $db->sql_transaction('begin'); + $this->db->sql_transaction('begin'); $sql = 'UPDATE ' . GROUPS_TABLE . ' - SET group_' . $field . ' = group_' . $field . ' - 1 - WHERE group_' . $field . ' > ' . $current_value; - $db->sql_query($sql); + SET group_' . $this->field . ' = group_' . $this->field . ' - 1 + WHERE group_' . $this->field . ' > ' . $current_value; + $this->db->sql_query($sql); - $sql = 'UPDATE ' . GROUPS_TABLE . ' - SET group_' . $field . ' = ' . self::GROUP_DISABLED . ' - WHERE group_id = ' . (int) $group_id; - $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); + } - $db->sql_transaction('commit'); + $this->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 + * + * @param int $group_id group_id of the group to be moved + * @return void */ - static function move_up($field, $group_id) + public function move_up($group_id) { - $current_value = self::get_group_value($field, $group_id); + $current_value = $this->get_group_value($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'); + $this->db->sql_transaction('begin'); $sql = 'UPDATE ' . GROUPS_TABLE . ' - SET group_' . $field . ' = group_' . $field . ' + 1 - WHERE group_' . $field . ' = ' . ($current_value - 1); - $db->sql_query($sql); + SET group_' . $this->field . ' = group_' . $this->field . ' + 1 + WHERE group_' . $this->field . ' = ' . ($current_value - 1); + $this->db->sql_query($sql); $sql = 'UPDATE ' . GROUPS_TABLE . ' - SET group_' . $field . ' = ' . ($current_value - 1) . ' + SET group_' . $this->field . ' = ' . ($current_value - 1) . ' WHERE group_id = ' . (int) $group_id; - $db->sql_query($sql); + $this->db->sql_query($sql); - $db->sql_transaction('commit'); + $this->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 + * + * @param int $group_id group_id of the group to be moved + * @return void */ - static function move_down($field, $group_id) + public function move_down($group_id) { - $current_value = self::get_group_value($field, $group_id); + $current_value = $this->get_group_value($group_id); if ($current_value != self::GROUP_DISABLED) { - global $db; - - $db->sql_transaction('begin'); + $this->db->sql_transaction('begin'); $sql = 'UPDATE ' . GROUPS_TABLE . ' - SET group_' . $field . ' = group_' . $field . ' - 1 - WHERE group_' . $field . ' = ' . ($current_value + 1); - $db->sql_query($sql); + SET group_' . $this->field . ' = group_' . $this->field . ' - 1 + WHERE group_' . $this->field . ' = ' . ($current_value + 1); + $this->db->sql_query($sql); - if ($db->sql_affectedrows() == 1) + if ($this->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) . ' + SET group_' . $this->field . ' = ' . ($current_value + 1) . ' WHERE group_id = ' . (int) $group_id; - $db->sql_query($sql); + $this->db->sql_query($sql); } - $db->sql_transaction('commit'); + $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. + * + * @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) + static public function group_type_language($group_type) { switch ($group_type) { -- cgit v1.2.1 From ad05f32c494d6622eca97c028cbde53e44a54647 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 14 Feb 2011 16:43:56 +0100 Subject: [ticket/9549] Throw an error when the given field-name is invalid. Also the code now only appends an adm_back_link, when we are in the ACP. PHPBB3-9549 --- phpBB/includes/group_positions.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'phpBB/includes/group_positions.php') diff --git a/phpBB/includes/group_positions.php b/phpBB/includes/group_positions.php index e63e17c384..a697d96f66 100644 --- a/phpBB/includes/group_positions.php +++ b/phpBB/includes/group_positions.php @@ -40,15 +40,23 @@ class phpbb_group_positions */ private $field = ''; + /** + * URI for the adm_back_link when there was an error. + */ + private $adm_back_link = ''; + /** * Constructor */ - public function __construct ($db, $field) + 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; } @@ -70,8 +78,7 @@ class phpbb_group_positions if ($current_value === false) { // Group not found. - global $user; - trigger_error($user->lang['NO_GROUP'] . adm_back_link($this->u_action), E_USER_WARNING); + $this->error('NO_GROUP'); } return (int) $current_value; @@ -232,4 +239,13 @@ class phpbb_group_positions 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); + } } -- cgit v1.2.1 From d2e2ccf8a49a3d62d3de3145e114d3ed0b255465 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 16 Feb 2011 10:47:41 +0100 Subject: [ticket/9549] Fix some minor issues with descriptions and coding-guidelines. PHPBB3-9549 --- phpBB/includes/group_positions.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'phpBB/includes/group_positions.php') diff --git a/phpBB/includes/group_positions.php b/phpBB/includes/group_positions.php index a697d96f66..951014c0c0 100644 --- a/phpBB/includes/group_positions.php +++ b/phpBB/includes/group_positions.php @@ -2,8 +2,7 @@ /** * * @package phpBB3 -* @version $Id$ -* @copyright (c) 2005 phpBB Group +* @copyright (c) 2011 phpBB Group * @license http://opensource.org/licenses/gpl-license.php GNU Public License * */ @@ -125,10 +124,10 @@ class phpbb_group_positions } /** - * Deletes a group by group_id + * 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 the group itself, to save the query, when you need to update it anyway. + * @param bool $skip_group Skip setting the group to GROUP_DISABLED, to save the query, when you need to update it anyway. * @return void */ public function delete_group($group_id, $skip_group = false) @@ -177,7 +176,7 @@ class phpbb_group_positions $this->db->sql_query($sql); $sql = 'UPDATE ' . GROUPS_TABLE . ' - SET group_' . $this->field . ' = ' . ($current_value - 1) . ' + SET group_' . $this->field . ' = group_' . $this->field . ' - 1 WHERE group_id = ' . (int) $group_id; $this->db->sql_query($sql); @@ -208,7 +207,7 @@ class phpbb_group_positions { // Only update when we move another one up, otherwise it was the last. $sql = 'UPDATE ' . GROUPS_TABLE . ' - SET group_' . $this->field . ' = ' . ($current_value + 1) . ' + SET group_' . $this->field . ' = group_' . $this->field . ' + 1 WHERE group_id = ' . (int) $group_id; $this->db->sql_query($sql); } -- cgit v1.2.1 From f908a016612c47542971d829b9aeed95db829b77 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 16 Feb 2011 18:15:52 +0100 Subject: [ticket/9549] New method move() to move a group more than 1 up/down. PHPBB3-9549 --- phpBB/includes/group_positions.php | 59 ++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 24 deletions(-) (limited to 'phpBB/includes/group_positions.php') diff --git a/phpBB/includes/group_positions.php b/phpBB/includes/group_positions.php index 951014c0c0..fc44e3249d 100644 --- a/phpBB/includes/group_positions.php +++ b/phpBB/includes/group_positions.php @@ -163,25 +163,7 @@ class phpbb_group_positions */ public function move_up($group_id) { - $current_value = $this->get_group_value($group_id); - - // Only move the group, if it is in the list and not already on top. - if ($current_value > 1) - { - $this->db->sql_transaction('begin'); - - $sql = 'UPDATE ' . GROUPS_TABLE . ' - SET group_' . $this->field . ' = group_' . $this->field . ' + 1 - WHERE group_' . $this->field . ' = ' . ($current_value - 1); - $this->db->sql_query($sql); - - $sql = 'UPDATE ' . GROUPS_TABLE . ' - SET group_' . $this->field . ' = group_' . $this->field . ' - 1 - WHERE group_id = ' . (int) $group_id; - $this->db->sql_query($sql); - - $this->db->sql_transaction('commit'); - } + $this->move($group_id, 1); } /** @@ -192,22 +174,51 @@ class phpbb_group_positions */ 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 void + */ + 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 . ' - 1 - WHERE group_' . $this->field . ' = ' . ($current_value + 1); + 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); - if ($this->db->sql_affectedrows() == 1) + // 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) { - // Only update when we move another one up, otherwise it was the last. + // 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 . ' + 1 + SET group_' . $this->field . ' = group_' . $this->field . (($move_up) ? ' - ' : ' + ') . $delta . ' WHERE group_id = ' . (int) $group_id; $this->db->sql_query($sql); } -- cgit v1.2.1