From 153b99e11c4bf60fb72079cd61e3d27ab7d28055 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 13 Nov 2012 11:33:22 +0100 Subject: [ticket/10411] Add new classes for legend and teampage handling PHPBB3-10411 --- phpBB/includes/groupposition/legend.php | 251 ++++++++++++++++++++++++++++++++ 1 file changed, 251 insertions(+) create mode 100644 phpBB/includes/groupposition/legend.php (limited to 'phpBB/includes/groupposition/legend.php') diff --git a/phpBB/includes/groupposition/legend.php b/phpBB/includes/groupposition/legend.php new file mode 100644 index 0000000000..4bd4d43ece --- /dev/null +++ b/phpBB/includes/groupposition/legend.php @@ -0,0 +1,251 @@ +adm_back_link = $adm_back_link; + $this->db = $db; + $this->user = $user; + } + + /** + * Returns the group_legend for a given group, if the group exists. + * + * {@inheritDoc} + */ + public function get_group_value($group_id) + { + $sql = 'SELECT group_legend + FROM ' . GROUPS_TABLE . ' + WHERE group_id = ' . (int) $group_id; + $result = $this->db->sql_query($sql); + $current_value = $this->db->sql_fetchfield('group_legend'); + $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 legend + * + * {@inheritDoc} + */ + public function get_group_count() + { + $sql = 'SELECT group_legend + FROM ' . GROUPS_TABLE . ' + ORDER BY group_legend DESC'; + $result = $this->db->sql_query_limit($sql, 1); + $group_count = (int) $this->db->sql_fetchfield('group_legend'); + $this->db->sql_freeresult($result); + + return $group_count; + } + + /** + * Adds a group by group_id + * + * {@inheritDoc} + */ + 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_legend = ' . $next_value . ' + WHERE group_legend = ' . 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. + * + * {@inheritDoc} + */ + 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_legend = group_legend - 1 + WHERE group_legend > ' . $current_value; + $this->db->sql_query($sql); + + if (!$skip_group) + { + $sql = 'UPDATE ' . GROUPS_TABLE . ' + SET group_legend = ' . 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 + * + * {@inheritDoc} + */ + public function move_up($group_id) + { + $this->move($group_id, 1); + } + + /** + * Moves a group down by group_id + * + * {@inheritDoc} + */ + public function move_down($group_id) + { + $this->move($group_id, -1); + } + + /** + * Moves a group up/down + * + * {@inheritDoc} + */ + 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_legend = group_legend' . (($move_up) ? ' + 1' : ' - 1') . ' + WHERE group_legend > ' . self::GROUP_DISABLED . ' + AND group_legend' . (($move_up) ? ' >= ' : ' <= ') . ($current_value - $delta) . ' + AND group_legend' . (($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_legend = group_legend ' . (($move_up) ? ' - ' : ' + ') . $delta . ' + WHERE group_id = ' . (int) $group_id; + $this->db->sql_query($sql); + } + + $this->db->sql_transaction('commit'); + } + } + + /** + * Error + * + * {@inheritDoc} + */ + public function error($message) + { + trigger_error($this->user->lang[$message] . (($this->adm_back_link) ? adm_back_link($this->adm_back_link) : ''), E_USER_WARNING); + } + + /** + * 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'; + } + } +} -- cgit v1.2.1 From 79eea0ccac1bc6dd5d39b4d47e973ef522cf7781 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 14 Nov 2012 15:31:16 +0100 Subject: [ticket/10411] Use DIC to get the groupposition classes PHPBB3-10411 --- phpBB/includes/groupposition/legend.php | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'phpBB/includes/groupposition/legend.php') diff --git a/phpBB/includes/groupposition/legend.php b/phpBB/includes/groupposition/legend.php index 4bd4d43ece..4dcf31ff06 100644 --- a/phpBB/includes/groupposition/legend.php +++ b/phpBB/includes/groupposition/legend.php @@ -31,12 +31,14 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface const GROUP_DISABLED = 0; /** - * phpbb-database object + * Database object + * @var dbal */ private $db = null; /** - * phpbb-user object + * User object + * @var phpbb_user */ private $user = null; @@ -48,16 +50,25 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface /** * Constructor * - * @param phpbb_dbal $db Database object - * @param string $adm_back_link Return URL to use after an error occured + * @param dbal $db Database object + * @param phpbb_user $user User object */ - public function __construct($db, phpbb_user $user, $adm_back_link = '') + public function __construct(dbal $db, phpbb_user $user) { - $this->adm_back_link = $adm_back_link; $this->db = $db; $this->user = $user; } + /** + * Set the back link for error messages + * + * @param string $adm_back_link Return URL to use after an error occured + */ + public function set_admin_back_link($adm_back_link) + { + $this->adm_back_link = $adm_back_link; + } + /** * Returns the group_legend for a given group, if the group exists. * @@ -221,7 +232,7 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface * * {@inheritDoc} */ - public function error($message) + private function error($message) { trigger_error($this->user->lang[$message] . (($this->adm_back_link) ? adm_back_link($this->adm_back_link) : ''), E_USER_WARNING); } -- cgit v1.2.1 From b0dc5925b91cb0447046e8a7f089c6675e4be95c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 25 Feb 2013 20:29:04 +0100 Subject: [ticket/10411] Fix typehinting and change private to protected PHPBB3-10411 --- phpBB/includes/groupposition/legend.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'phpBB/includes/groupposition/legend.php') diff --git a/phpBB/includes/groupposition/legend.php b/phpBB/includes/groupposition/legend.php index 4dcf31ff06..51f2510e85 100644 --- a/phpBB/includes/groupposition/legend.php +++ b/phpBB/includes/groupposition/legend.php @@ -32,28 +32,28 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface /** * Database object - * @var dbal + * @var phpbb_db_driver */ - private $db = null; + protected $db; /** * User object * @var phpbb_user */ - private $user = null; + protected $user; /** * URI for the adm_back_link when there was an error. */ - private $adm_back_link = ''; + protected $adm_back_link = ''; /** * Constructor * - * @param dbal $db Database object - * @param phpbb_user $user User object + * @param phpbb_db_driver $db Database object + * @param phpbb_user $user User object */ - public function __construct(dbal $db, phpbb_user $user) + public function __construct(phpbb_db_driver $db, phpbb_user $user) { $this->db = $db; $this->user = $user; -- cgit v1.2.1 From 1d7b082a6fa05d5760ef15ef8bf78cd3a1d204cf Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 25 Feb 2013 20:58:12 +0100 Subject: [ticket/10411] Add return value to move functions PHPBB3-10411 --- phpBB/includes/groupposition/legend.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'phpBB/includes/groupposition/legend.php') diff --git a/phpBB/includes/groupposition/legend.php b/phpBB/includes/groupposition/legend.php index 51f2510e85..9b69f9d2b3 100644 --- a/phpBB/includes/groupposition/legend.php +++ b/phpBB/includes/groupposition/legend.php @@ -168,7 +168,7 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface */ public function move_up($group_id) { - $this->move($group_id, 1); + return $this->move($group_id, 1); } /** @@ -178,7 +178,7 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface */ public function move_down($group_id) { - $this->move($group_id, -1); + return $this->move($group_id, -1); } /** @@ -188,9 +188,10 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface */ public function move($group_id, $delta) { - if (!is_int($delta) || !$delta) + $delta = (int) $delta; + if (!$delta) { - return; + return false; } $move_up = ($delta > 0) ? true : false; @@ -221,10 +222,16 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface SET group_legend = group_legend ' . (($move_up) ? ' - ' : ' + ') . $delta . ' WHERE group_id = ' . (int) $group_id; $this->db->sql_query($sql); + + $this->db->sql_transaction('commit'); + + return true; } $this->db->sql_transaction('commit'); } + + return false; } /** -- cgit v1.2.1 From 41eea66da975a3b4140d8f4dc3f6931ea84916db Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 25 Feb 2013 21:24:52 +0100 Subject: [ticket/10411] Add return values to add/delete function PHPBB3-10411 --- phpBB/includes/groupposition/legend.php | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'phpBB/includes/groupposition/legend.php') diff --git a/phpBB/includes/groupposition/legend.php b/phpBB/includes/groupposition/legend.php index 9b69f9d2b3..8f115a8b1e 100644 --- a/phpBB/includes/groupposition/legend.php +++ b/phpBB/includes/groupposition/legend.php @@ -128,7 +128,10 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface WHERE group_legend = ' . self::GROUP_DISABLED . ' AND group_id = ' . (int) $group_id; $this->db->sql_query($sql); + return true; } + + return false; } /** @@ -158,7 +161,11 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface } $this->db->sql_transaction('commit'); + + return true; } + + return false; } /** -- cgit v1.2.1 From e0df5934485df0eee22141d1c5ded3e432119b20 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 26 Feb 2013 16:52:53 +0100 Subject: [ticket/10411] Throw exceptions instead of using trigger_error() PHPBB3-10411 --- phpBB/includes/groupposition/legend.php | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) (limited to 'phpBB/includes/groupposition/legend.php') diff --git a/phpBB/includes/groupposition/legend.php b/phpBB/includes/groupposition/legend.php index 8f115a8b1e..7fddadde99 100644 --- a/phpBB/includes/groupposition/legend.php +++ b/phpBB/includes/groupposition/legend.php @@ -42,11 +42,6 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface */ protected $user; - /** - * URI for the adm_back_link when there was an error. - */ - protected $adm_back_link = ''; - /** * Constructor * @@ -59,16 +54,6 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface $this->user = $user; } - /** - * Set the back link for error messages - * - * @param string $adm_back_link Return URL to use after an error occured - */ - public function set_admin_back_link($adm_back_link) - { - $this->adm_back_link = $adm_back_link; - } - /** * Returns the group_legend for a given group, if the group exists. * @@ -86,7 +71,7 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface if ($current_value === false) { // Group not found. - $this->error('NO_GROUP'); + throw new phpbb_groupposition_exception('NO_GROUP'); } return (int) $current_value; @@ -241,16 +226,6 @@ class phpbb_groupposition_legend implements phpbb_groupposition_interface return false; } - /** - * Error - * - * {@inheritDoc} - */ - private function error($message) - { - trigger_error($this->user->lang[$message] . (($this->adm_back_link) ? adm_back_link($this->adm_back_link) : ''), E_USER_WARNING); - } - /** * Get group type language var * -- cgit v1.2.1