[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/phpbb/groupposition/ -> legend.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\groupposition;
  15  
  16  /**
  17  * Legend group position class
  18  *
  19  * group_legend is an ascending list 1, 2, ..., n for groups which are displayed. 1 is the first group, n the last.
  20  * If the value is 0 (self::GROUP_DISABLED) the group is not displayed.
  21  */
  22  class legend implements \phpbb\groupposition\groupposition_interface
  23  {
  24      /**
  25      * Group is not displayed
  26      */
  27      const GROUP_DISABLED = 0;
  28  
  29      /**
  30      * Database object
  31      * @var \phpbb\db\driver\driver_interface
  32      */
  33      protected $db;
  34  
  35      /**
  36      * User object
  37      * @var \phpbb\user
  38      */
  39      protected $user;
  40  
  41      /**
  42      * Constructor
  43      *
  44      * @param \phpbb\db\driver\driver_interface    $db        Database object
  45      * @param \phpbb\user            $user    User object
  46      */
  47  	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user)
  48      {
  49          $this->db = $db;
  50          $this->user = $user;
  51      }
  52  
  53      /**
  54      * Returns the group_legend for a given group, if the group exists.
  55      *
  56      * @param    int        $group_id    group_id of the group to be selected
  57      * @return    int            position of the group
  58      * @throws \phpbb\groupposition\exception
  59      */
  60  	public function get_group_value($group_id)
  61      {
  62          $sql = 'SELECT group_legend
  63              FROM ' . GROUPS_TABLE . '
  64              WHERE group_id = ' . (int) $group_id;
  65          $result = $this->db->sql_query($sql);
  66          $current_value = $this->db->sql_fetchfield('group_legend');
  67          $this->db->sql_freeresult($result);
  68  
  69          if ($current_value === false)
  70          {
  71              // Group not found.
  72              throw new \phpbb\groupposition\exception('NO_GROUP');
  73          }
  74  
  75          return (int) $current_value;
  76      }
  77  
  78      /**
  79      * Get number of groups, displayed on the legend
  80      *
  81      * @return    int        value of the last item displayed
  82      */
  83  	public function get_group_count()
  84      {
  85          $sql = 'SELECT group_legend
  86              FROM ' . GROUPS_TABLE . '
  87              ORDER BY group_legend DESC';
  88          $result = $this->db->sql_query_limit($sql, 1);
  89          $group_count = (int) $this->db->sql_fetchfield('group_legend');
  90          $this->db->sql_freeresult($result);
  91  
  92          return $group_count;
  93      }
  94  
  95      /**
  96      * {@inheritDoc}
  97      */
  98  	public function add_group($group_id)
  99      {
 100          $current_value = $this->get_group_value($group_id);
 101  
 102          if ($current_value == self::GROUP_DISABLED)
 103          {
 104              // Group is currently not displayed, add it at the end.
 105              $next_value = 1 + $this->get_group_count();
 106  
 107              $sql = 'UPDATE ' . GROUPS_TABLE . '
 108                  SET group_legend = ' . $next_value . '
 109                  WHERE group_legend = ' . self::GROUP_DISABLED . '
 110                      AND group_id = ' . (int) $group_id;
 111              $this->db->sql_query($sql);
 112              return true;
 113          }
 114  
 115          return false;
 116      }
 117  
 118      /**
 119      * Deletes a group by setting the field to self::GROUP_DISABLED and closing the gap in the list.
 120      *
 121      * @param    int        $group_id        group_id of the group to be deleted
 122      * @param    bool    $skip_group        Skip setting the value for this group, to save the query, when you need to update it anyway.
 123      * @return    bool        True if the group was deleted successfully
 124      */
 125  	public function delete_group($group_id, $skip_group = false)
 126      {
 127          $current_value = $this->get_group_value($group_id);
 128  
 129          if ($current_value != self::GROUP_DISABLED)
 130          {
 131              $this->db->sql_transaction('begin');
 132  
 133              $sql = 'UPDATE ' . GROUPS_TABLE . '
 134                  SET group_legend = group_legend - 1
 135                  WHERE group_legend > ' . $current_value;
 136              $this->db->sql_query($sql);
 137  
 138              if (!$skip_group)
 139              {
 140                  $sql = 'UPDATE ' . GROUPS_TABLE . '
 141                      SET group_legend = ' . self::GROUP_DISABLED . '
 142                      WHERE group_id = ' . (int) $group_id;
 143                  $this->db->sql_query($sql);
 144              }
 145  
 146              $this->db->sql_transaction('commit');
 147  
 148              return true;
 149          }
 150  
 151          return false;
 152      }
 153  
 154      /**
 155      * {@inheritDoc}
 156      */
 157  	public function move_up($group_id)
 158      {
 159          return $this->move($group_id, 1);
 160      }
 161  
 162      /**
 163      * {@inheritDoc}
 164      */
 165  	public function move_down($group_id)
 166      {
 167          return $this->move($group_id, -1);
 168      }
 169  
 170      /**
 171      * {@inheritDoc}
 172      */
 173  	public function move($group_id, $delta)
 174      {
 175          $delta = (int) $delta;
 176          if (!$delta)
 177          {
 178              return false;
 179          }
 180  
 181          $move_up = ($delta > 0) ? true : false;
 182          $current_value = $this->get_group_value($group_id);
 183  
 184          if ($current_value != self::GROUP_DISABLED)
 185          {
 186              $this->db->sql_transaction('begin');
 187  
 188              // First we move all groups between our current value and the target value up/down 1,
 189              // so we have a gap for our group to move.
 190              $sql = 'UPDATE ' . GROUPS_TABLE . '
 191                  SET group_legend = group_legend' . (($move_up) ? ' + 1' : ' - 1') . '
 192                  WHERE group_legend > ' . self::GROUP_DISABLED . '
 193                      AND group_legend' . (($move_up) ? ' >= ' : ' <= ') . ($current_value - $delta) . '
 194                      AND group_legend' . (($move_up) ? ' < ' : ' > ') . $current_value;
 195              $this->db->sql_query($sql);
 196  
 197              // Because there might be fewer groups above/below the group than we wanted to move,
 198              // we use the number of changed groups, to update the group.
 199              $delta = (int) $this->db->sql_affectedrows();
 200  
 201              if ($delta)
 202              {
 203                  // And now finally, when we moved some other groups and built a gap,
 204                  // we can move the desired group to it.
 205                  $sql = 'UPDATE ' . GROUPS_TABLE . '
 206                      SET group_legend = group_legend ' . (($move_up) ? ' - ' : ' + ') . $delta . '
 207                      WHERE group_id = ' . (int) $group_id;
 208                  $this->db->sql_query($sql);
 209  
 210                  $this->db->sql_transaction('commit');
 211  
 212                  return true;
 213              }
 214  
 215              $this->db->sql_transaction('commit');
 216          }
 217  
 218          return false;
 219      }
 220  
 221      /**
 222      * Get group type language var
 223      *
 224      * @param    int        $group_type    group_type from the groups-table
 225      * @return    string        name of the language variable for the given group-type.
 226      */
 227  	static public function group_type_language($group_type)
 228      {
 229          switch ($group_type)
 230          {
 231              case GROUP_OPEN:
 232                  return 'GROUP_REQUEST';
 233              case GROUP_CLOSED:
 234                  return 'GROUP_CLOSED';
 235              case GROUP_HIDDEN:
 236                  return 'GROUP_HIDDEN';
 237              case GROUP_SPECIAL:
 238                  return 'GROUP_SPECIAL';
 239              case GROUP_FREE:
 240                  return 'GROUP_OPEN';
 241          }
 242      }
 243  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1