[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/include/ -> class.group.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
   3  // +----------------------------------------------------------------------+
   4  // | Eventum - Issue Tracking System                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 2003, 2004, 2005, 2006, 2007 MySQL AB                  |
   7  // |                                                                      |
   8  // | This program is free software; you can redistribute it and/or modify |
   9  // | it under the terms of the GNU General Public License as published by |
  10  // | the Free Software Foundation; either version 2 of the License, or    |
  11  // | (at your option) any later version.                                  |
  12  // |                                                                      |
  13  // | This program is distributed in the hope that it will be useful,      |
  14  // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  15  // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
  16  // | GNU General Public License for more details.                         |
  17  // |                                                                      |
  18  // | You should have received a copy of the GNU General Public License    |
  19  // | along with this program; if not, write to:                           |
  20  // |                                                                      |
  21  // | Free Software Foundation, Inc.                                       |
  22  // | 59 Temple Place - Suite 330                                          |
  23  // | Boston, MA 02111-1307, USA.                                          |
  24  // +----------------------------------------------------------------------+
  25  // | Authors: Bryan Alsdorf <bryan@mysql.com>                             |
  26  // +----------------------------------------------------------------------+
  27  //
  28  // @(#) $Id: class.group.php 3246 2007-02-09 09:10:12Z glen $
  29  //
  30  
  31  require_once (APP_INC_PATH . "class.error_handler.php");
  32  require_once (APP_INC_PATH . "class.auth.php");
  33  require_once (APP_INC_PATH . "class.misc.php");
  34  require_once (APP_INC_PATH . "class.prefs.php");
  35  require_once (APP_INC_PATH . "class.date.php");
  36  require_once (APP_INC_PATH . "class.project.php");
  37  
  38  /**
  39   * Class to handle the business logic related to the administration
  40   * of groups.
  41   * Note! Any reference to the group table must use ` around the table name
  42   * due to "group" being a reserved word and some users don't use table prefixes.
  43   *
  44   * @version 1.0
  45   * @author Bryan Alsdorf <bryan@mysql.com>
  46   */
  47  
  48  class Group
  49  {
  50      /**
  51       * Inserts a new group into the database
  52       *
  53       * @access  public
  54       * @return integer 1 if successful, -1 or -2 otherwise
  55       */
  56      function insert()
  57      {
  58          $stmt = "INSERT INTO
  59                      " . APP_DEFAULT_DB . ".`" . APP_TABLE_PREFIX . "group`
  60                   (
  61                      grp_name,
  62                      grp_description,
  63                      grp_manager_usr_id
  64                   ) VALUES (
  65                      '" . Misc::escapeString($_POST["group_name"]) . "',
  66                      '" . Misc::escapeString($_POST["description"]) . "',
  67                      '" . Misc::escapeInteger($_POST["manager"]) . "'
  68                   )";
  69          $res = $GLOBALS["db_api"]->dbh->query($stmt);
  70          if (PEAR::isError($res)) {
  71              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
  72              return -1;
  73          } else {
  74              $grp_id = $GLOBALS["db_api"]->get_last_insert_id();
  75              
  76              Group::setProjects($grp_id, $_POST["projects"]);
  77              
  78              foreach ($_POST["users"] as $usr_id) {
  79                  User::setGroupID($usr_id, $grp_id);
  80              }
  81              return 1;
  82          }
  83      }
  84  
  85  
  86      /**
  87       * Updates a group
  88       *
  89       * @access  public
  90       * @return integer 1 if successful, -1 or -2 otherwise
  91       */
  92      function update()
  93      {
  94          $_POST['id'] = Misc::escapeInteger($_POST['id']);
  95          
  96          $stmt = "UPDATE
  97                      " . APP_DEFAULT_DB . ".`" . APP_TABLE_PREFIX . "group`
  98                   SET
  99                      grp_name = '" . Misc::escapeString($_POST["group_name"]) . "',
 100                      grp_description = '" . Misc::escapeString($_POST["description"]) . "',
 101                      grp_manager_usr_id = '" . Misc::escapeInteger($_POST["manager"]) . "'
 102                   WHERE
 103                      grp_id = " . $_POST["id"];
 104          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 105          if (PEAR::isError($res)) {
 106              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 107              return -1;
 108          } else {
 109              Group::setProjects($_POST["id"], $_POST["projects"]);
 110              // get old users so we can remove any ones that have been removed
 111              $existing_users = Group::getUsers($_POST["id"]);
 112              $diff = array_diff($existing_users, Misc::escapeInteger($_POST["users"]));
 113              if (count($diff) > 0) {
 114                  foreach ($diff as $usr_id) {
 115                      User::setGroupID($usr_id, false);
 116                  }
 117              }
 118              foreach ($_POST["users"] as $usr_id) {
 119                  User::setGroupID($usr_id, $_POST["id"]);
 120              }
 121              return 1;
 122          }
 123      }
 124  
 125  
 126      /**
 127       * Removes groups
 128       * 
 129       * @access  public
 130       */
 131      function remove()
 132      {
 133          foreach (Misc::escapeInteger(@$_POST["items"]) as $grp_id) {
 134              $users = Group::getUsers($grp_id);
 135              
 136              $stmt = "DELETE FROM
 137                          " . APP_DEFAULT_DB . ".`" . APP_TABLE_PREFIX . "group`
 138                       WHERE
 139                          grp_id = $grp_id";
 140              $res = $GLOBALS["db_api"]->dbh->query($stmt);
 141              if (PEAR::isError($res)) {
 142                  Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 143                  return -1;
 144              } else {
 145                  Group::removeProjectsByGroup($grp_id);
 146                  
 147                  foreach ($users as $usr_id) {
 148                      User::setGroupID($usr_id, false);
 149                  }
 150                  return 1;
 151              }
 152          }
 153      }
 154  
 155  
 156      /**
 157       * Sets projects for the group.
 158       * 
 159       * @access  public
 160       * @param   integer $grp_id The id of the group.
 161       * @param   array $projects An array of projects to associate with the group.
 162       */
 163      function setProjects($grp_id, $projects)
 164      {
 165          $grp_id = Misc::escapeInteger($grp_id);
 166          $projects = Misc::escapeInteger($projects);
 167          Group::removeProjectsByGroup($grp_id);
 168          
 169          // make new associations
 170          foreach ($projects as $prj_id) {
 171              $stmt = "INSERT INTO
 172                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_group
 173                       (
 174                          pgr_prj_id,
 175                          pgr_grp_id
 176                       ) VALUES (
 177                          $prj_id,
 178                          $grp_id
 179                       )";
 180              $res = $GLOBALS["db_api"]->dbh->query($stmt);
 181              if (PEAR::isError($res)) {
 182                  Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 183                  return -1;
 184              }
 185          }
 186          return 1;
 187      }
 188  
 189  
 190      /**
 191       * Removes all the projects for a group
 192       * 
 193       * @access  private
 194       * @param   integer $grp_id The ID of the group
 195       */
 196      function removeProjectsByGroup($grp_id)
 197      {
 198          // delete all current associations
 199          $stmt = "DELETE FROM
 200                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_group
 201                   WHERE
 202                      pgr_grp_id = " . Misc::escapeInteger($grp_id);
 203          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 204          if (PEAR::isError($res)) {
 205              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 206              return -1;
 207          }
 208          return 1;
 209      }
 210  
 211  
 212      /**
 213       * Removes specified projects from all groups.
 214       * 
 215       * @access  public
 216       * @param   array $projects An array of projects to remove from all groups.
 217       * @return  integer 1 if successful, -1 otherwise
 218       */
 219      function disassociateProjects($projects)
 220      {
 221          // delete all current associations
 222          $stmt = "DELETE FROM
 223                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_group
 224                   WHERE
 225                      pgr_prj_id IN(" . join(",", Misc::escapeInteger($projects)) . ")";
 226          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 227          if (PEAR::isError($res)) {
 228              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 229              return -1;
 230          }
 231          return 1;
 232      }
 233  
 234  
 235      /**
 236       * Returns details about a specific group
 237       * 
 238       * @access  public
 239       * @param   integer $grp_id The ID of the group.
 240       * @return  array An array of group information
 241       */
 242      function getDetails($grp_id)
 243      {
 244          static $returns;
 245          
 246          $grp_id = Misc::escapeInteger($grp_id);
 247          
 248          if (!empty($returns[$grp_id])) {
 249              return $returns[$grp_id];
 250          }
 251          
 252          $stmt = "SELECT
 253                      grp_name,
 254                      grp_description,
 255                      grp_manager_usr_id
 256                   FROM
 257                      " . APP_DEFAULT_DB . ".`" . APP_TABLE_PREFIX . "group`
 258                   WHERE
 259                      grp_id = $grp_id";
 260          $res = $GLOBALS["db_api"]->dbh->getRow($stmt, DB_FETCHMODE_ASSOC);
 261          if (PEAR::isError($res)) {
 262              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 263              return -1;
 264          } else {
 265              if (count($res) > 0) {
 266                  $res["users"] = Group::getUsers($grp_id);
 267                  $res["projects"] = Group::getProjects($grp_id);
 268                  $res["project_ids"] = array_keys($res["projects"]);
 269                  $res["manager"] = User::getFullName($res["grp_manager_usr_id"]);
 270              } else {
 271                  $res = array();
 272              }
 273              $returns[$grp_id] = $res;
 274              return $res;
 275          }
 276      }
 277  
 278  
 279      /**
 280       * Returns the name of the group
 281       * 
 282       * @access  public
 283       * @param   integer $grp_id The id of the group
 284       * @return  string The name of the group
 285       */
 286      function getName($grp_id)
 287      {
 288          $grp_id = Misc::escapeInteger($grp_id);
 289          if (empty($grp_id)) {
 290              return "";
 291          }
 292          $details = Group::getDetails($grp_id);
 293          if (count($details) < 1) {
 294              return "";
 295          } else {
 296              return $details["grp_name"];
 297          }
 298      }
 299  
 300  
 301      /**
 302       * Returns a list of groups
 303       * 
 304       * @access  public
 305       * @return  array An array of group information
 306       */
 307      function getList()
 308      {
 309          $stmt = "SELECT
 310                      grp_id,
 311                      grp_name,
 312                      grp_description,
 313                      grp_manager_usr_id
 314                   FROM
 315                      " . APP_DEFAULT_DB . ".`" . APP_TABLE_PREFIX . "group`
 316                   ORDER BY
 317                      grp_name";
 318          $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
 319          if (PEAR::isError($res)) {
 320              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 321              return -1;
 322          } else {
 323              for ($i = 0; $i < count($res); $i++) {
 324                  $res[$i]["users"] = Group::getUsers($res[$i]['grp_id']);
 325                  $res[$i]["projects"] = Group::getProjects($res[$i]['grp_id']);
 326                  $res[$i]["manager"] = User::getFullName($res[$i]["grp_manager_usr_id"]);
 327              }
 328              return $res;
 329          }
 330      }
 331  
 332  
 333      /**
 334       * Returns an associative array of groups
 335       * 
 336       * @access  public
 337       * @param   integer $prj_id The project ID
 338       * @return  array An associated array of groups
 339       */
 340      function getAssocList($prj_id)
 341      {
 342          static $list;
 343  
 344          $prj_id = Misc::escapeInteger($prj_id); 
 345          
 346          if (!empty($list[$prj_id])) {
 347              return $list[$prj_id];
 348          }
 349  
 350          $stmt = "SELECT
 351                      grp_id,
 352                      grp_name
 353                   FROM
 354                      " . APP_DEFAULT_DB . ".`" . APP_TABLE_PREFIX . "group`,
 355                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_group
 356                   WHERE
 357                      grp_id = pgr_grp_id AND
 358                      pgr_prj_id = $prj_id
 359                   ORDER BY
 360                      grp_name";
 361          $res = $GLOBALS["db_api"]->dbh->getAssoc($stmt);
 362          if (PEAR::isError($res)) {
 363              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 364              return -1;
 365          } else {
 366              $list[$prj_id] = $res;
 367              return $res;
 368          }
 369      }
 370  
 371  
 372      /**
 373       * Returns an array of users who belong to the current group.
 374       * 
 375       * @access  public
 376       * @param   integer $grp_id The ID of the group.
 377       * @return  array An array of usr ids
 378       */
 379      function getUsers($grp_id)
 380      {
 381          $stmt = "SELECT
 382                      usr_id
 383                   FROM
 384                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "user
 385                   WHERE
 386                      usr_grp_id = " . Misc::escapeInteger($grp_id);
 387          $res = $GLOBALS["db_api"]->dbh->getCol($stmt);
 388          if (PEAR::isError($res)) {
 389              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 390              return -1;
 391          }
 392          return $res;
 393      }
 394  
 395  
 396      /**
 397       * Returns an array of projects who belong to the current group.
 398       * 
 399       * @access  public
 400       * @param   integer $grp_id The ID of the group.
 401       * @return  array An array of project ids
 402       */
 403      function getProjects($grp_id)
 404      {
 405          $stmt = "SELECT
 406                      pgr_prj_id,
 407                      prj_title
 408                   FROM
 409                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_group,
 410                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project
 411                   WHERE
 412                      pgr_prj_id = prj_id AND
 413                      pgr_grp_id = " . Misc::escapeInteger($grp_id);
 414          $res = $GLOBALS["db_api"]->dbh->getAssoc($stmt);
 415          if (PEAR::isError($res)) {
 416              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 417              return -1;
 418          }
 419          return $res;
 420      }
 421  
 422  
 423      /**
 424       * Returns a group ID based on group name
 425       * 
 426       * @access  public
 427       * @param   string $name Name of the group
 428       * @return  integer The ID of the group, or -1 if no group by that name could be found.
 429       */
 430      function getGroupByName($name)
 431      {
 432          $stmt = "SELECT
 433                      grp_id
 434                   FROM
 435                      " . APP_DEFAULT_DB . ".`" . APP_TABLE_PREFIX . "group`,
 436                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_group
 437                   WHERE
 438                      grp_id = pgr_grp_id AND
 439                      pgr_prj_id = " . Auth::getCurrentProject() . " AND
 440                      grp_name = '" . Misc::escapeString($name) . "'";
 441          $res = $GLOBALS["db_api"]->dbh->getOne($stmt);
 442          if (PEAR::isError($res)) {
 443              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 444              return -1;
 445          }
 446          if (empty($res)) {
 447              return -2;
 448          } else {
 449              return $res;
 450          }
 451      }
 452  }
 453  


Generated: Wed Dec 19 21:21:33 2007 Cross-referenced by PHPXref 0.7