[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/include/ -> class.faq.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: João Prado Maia <jpm@mysql.com>                             |
  26  // +----------------------------------------------------------------------+
  27  //
  28  // @(#) $Id: class.faq.php 3246 2007-02-09 09:10:12Z glen $
  29  //
  30  
  31  
  32  class FAQ
  33  {
  34      /**
  35       * Returns the list of FAQ entries associated to a given support level.
  36       *
  37       * @access  public
  38       * @param   integer $support_level_id The support level ID
  39       * @return  array The list of FAQ entries
  40       */
  41      function getListBySupportLevel($support_level_id)
  42      {
  43          $support_level_id = Misc::escapeInteger($support_level_id);
  44          $prj_id = Auth::getCurrentProject();
  45  
  46          if ($support_level_id == -1) {
  47              $stmt = "SELECT
  48                          *
  49                       FROM
  50                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq
  51                       WHERE
  52                          faq_prj_id = $prj_id
  53                       ORDER BY
  54                          faq_rank ASC";
  55          } else {
  56              $stmt = "SELECT
  57                          *
  58                       FROM
  59                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq,
  60                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq_support_level
  61                       WHERE
  62                          faq_id=fsl_faq_id AND
  63                          fsl_support_level_id=$support_level_id AND
  64                          faq_prj_id = $prj_id
  65                       ORDER BY
  66                          faq_rank ASC";
  67          }
  68          $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
  69          if (PEAR::isError($res)) {
  70              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
  71              return "";
  72          } else {
  73              for ($i = 0; $i < count($res); $i++) {
  74                  if (empty($res[$i]['faq_updated_date'])) {
  75                      $res[$i]['faq_updated_date'] = $res[$i]['faq_created_date'];
  76                  }
  77                  $res[$i]['faq_updated_date'] = Date_API::getSimpleDate($res[$i]["faq_updated_date"]);
  78              }
  79              return $res;
  80          }
  81      }
  82  
  83  
  84      /**
  85       * Method used to remove a FAQ entry from the system.
  86       *
  87       * @access  public
  88       * @return  boolean
  89       */
  90      function remove()
  91      {
  92          $items = @implode(", ", Misc::escapeInteger($_POST["items"]));
  93          $stmt = "DELETE FROM
  94                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq
  95                   WHERE
  96                      faq_id IN ($items)";
  97          $res = $GLOBALS["db_api"]->dbh->query($stmt);
  98          if (PEAR::isError($res)) {
  99              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 100              return false;
 101          } else {
 102              FAQ::removeSupportLevelAssociations($_POST['items']);
 103              return true;
 104          }
 105      }
 106  
 107  
 108      /**
 109       * Method used to remove the support level associations for a given
 110       * FAQ entry.
 111       *
 112       * @access  public
 113       * @param   integer $faq_id The FAQ ID
 114       * @return  boolean
 115       */
 116      function removeSupportLevelAssociations($faq_id)
 117      {
 118          $faq_id = Misc::escapeInteger($faq_id);
 119          if (!is_array($faq_id)) {
 120              $faq_id = array($faq_id);
 121          }
 122          $items = @implode(", ", $faq_id);
 123          $stmt = "DELETE FROM
 124                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq_support_level
 125                   WHERE
 126                      fsl_faq_id IN ($items)";
 127          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 128          if (PEAR::isError($res)) {
 129              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 130              return false;
 131          } else {
 132              return true;
 133          }
 134      }
 135  
 136  
 137      /**
 138       * Method used to update a FAQ entry in the system.
 139       *
 140       * @access  public
 141       * @return  integer 1 if the update worked, -1 otherwise
 142       */
 143      function update()
 144      {
 145          $_POST['id'] = Misc::escapeInteger($_POST['id']);
 146          
 147          if (Validation::isWhitespace($_POST["title"])) {
 148              return -2;
 149          }
 150          if (Validation::isWhitespace($_POST["message"])) {
 151              return -3;
 152          }
 153          $stmt = "UPDATE
 154                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq
 155                   SET
 156                      faq_prj_id=" . $_POST['project'] . ",
 157                      faq_updated_date='" . Date_API::getCurrentDateGMT() . "',
 158                      faq_title='" . Misc::escapeString($_POST["title"]) . "',
 159                      faq_message='" . Misc::escapeString($_POST["message"]) . "',
 160                      faq_rank=" . $_POST['rank'] . "
 161                   WHERE
 162                      faq_id=" . $_POST["id"];
 163          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 164          if (PEAR::isError($res)) {
 165              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 166              return -1;
 167          } else {
 168              // remove all of the associations with support levels, then add them all again
 169              FAQ::removeSupportLevelAssociations($_POST['id']);
 170              if (Customer::doesBackendUseSupportLevels($_POST['project'])) {
 171                  foreach ($_POST['support_levels'] as $support_level_id) {
 172                      FAQ::addSupportLevelAssociation($_POST['id'], $support_level_id);
 173                  }
 174              }
 175              return 1;
 176          }
 177      }
 178  
 179  
 180      /**
 181       * Method used to add a FAQ entry to the system.
 182       *
 183       * @access  public
 184       * @return  integer 1 if the insert worked, -1 otherwise
 185       */
 186      function insert()
 187      {
 188          if (Validation::isWhitespace($_POST["title"])) {
 189              return -2;
 190          }
 191          if (Validation::isWhitespace($_POST["message"])) {
 192              return -3;
 193          }
 194          $stmt = "INSERT INTO
 195                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq
 196                   (
 197                      faq_prj_id,
 198                      faq_usr_id,
 199                      faq_created_date,
 200                      faq_title,
 201                      faq_message,
 202                      faq_rank
 203                   ) VALUES (
 204                      " . $_POST['project'] . ",
 205                      " . Auth::getUserID() . ",
 206                      '" . Date_API::getCurrentDateGMT() . "',
 207                      '" . Misc::escapeString($_POST["title"]) . "',
 208                      '" . Misc::escapeString($_POST["message"]) . "',
 209                      " . $_POST['rank'] . "
 210                   )";
 211          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 212          if (PEAR::isError($res)) {
 213              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 214              return -1;
 215          } else {
 216              $new_faq_id = $GLOBALS["db_api"]->get_last_insert_id();
 217              if (Customer::doesBackendUseSupportLevels(Misc::escapeInteger($_POST['project']))) {
 218                  // now populate the faq-support level mapping table
 219                  foreach ($_POST['support_levels'] as $support_level_id) {
 220                      FAQ::addSupportLevelAssociation($new_faq_id, $support_level_id);
 221                  }
 222              }
 223              return 1;
 224          }
 225      }
 226  
 227  
 228      /**
 229       * Method used to add a support level association to a FAQ entry.
 230       *
 231       * @access  public
 232       * @param   integer $faq_id The FAQ ID
 233       * @param   integer $support_level_id The support level ID
 234       * @return  void
 235       */
 236      function addSupportLevelAssociation($faq_id, $support_level_id)
 237      {
 238          $stmt = "INSERT INTO
 239                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq_support_level
 240                   (
 241                      fsl_faq_id,
 242                      fsl_support_level_id
 243                   ) VALUES (
 244                      " . Misc::escapeInteger($faq_id) . ",
 245                      " . Misc::escapeInteger($support_level_id) . "
 246                   )";
 247          $GLOBALS["db_api"]->dbh->query($stmt);
 248      }
 249  
 250  
 251      /**
 252       * Method used to get the details of a FAQ entry for a given FAQ ID.
 253       *
 254       * @access  public
 255       * @param   integer $faq_id The FAQ entry ID
 256       * @return  array The FAQ entry details
 257       */
 258      function getDetails($faq_id)
 259      {
 260          $stmt = "SELECT
 261                      *
 262                   FROM
 263                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq
 264                   WHERE
 265                      faq_id=" . Misc::escapeInteger($faq_id);
 266          $res = $GLOBALS["db_api"]->dbh->getRow($stmt, DB_FETCHMODE_ASSOC);
 267          if (PEAR::isError($res)) {
 268              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 269              return "";
 270          } else {
 271              if (Customer::doesBackendUseSupportLevels($res['faq_prj_id'])) {
 272                  // get all of the support level associations here as well
 273                  $res['support_levels'] = array_keys(FAQ::getAssociatedSupportLevels($res['faq_prj_id'], $res['faq_id']));
 274              }
 275              if (empty($res['faq_updated_date'])) {
 276                  $res['faq_updated_date'] = $res['faq_created_date'];
 277              }
 278              $res['faq_updated_date'] = Date_API::getFormattedDate($res['faq_updated_date']);
 279              $res['message'] = Misc::activateLinks(nl2br(htmlspecialchars($res['faq_message'])));
 280              return $res;
 281          }
 282      }
 283  
 284  
 285      /**
 286       * Method used to get the list of FAQ entries available in the system.
 287       *
 288       * @access  public
 289       * @return  array The list of news entries
 290       */
 291      function getList()
 292      {
 293          $stmt = "SELECT
 294                      faq_id,
 295                      faq_prj_id,
 296                      faq_title,
 297                      faq_rank
 298                   FROM
 299                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq
 300                   ORDER BY
 301                      faq_rank ASC";
 302          $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
 303          if (PEAR::isError($res)) {
 304              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 305              return "";
 306          } else {
 307              // get the list of associated support levels
 308              for ($i = 0; $i < count($res); $i++) {
 309                  if (Customer::doesBackendUseSupportLevels($res[$i]['faq_prj_id'])) {
 310                      $res[$i]['support_levels'] = implode(", ", array_values(FAQ::getAssociatedSupportLevels($res[$i]['faq_prj_id'], $res[$i]['faq_id'])));
 311                  }
 312              }
 313              return $res;
 314          }
 315      }
 316  
 317  
 318      /**
 319       * Method used to get the list of associated support levels for a given
 320       * FAQ entry.
 321       *
 322       * @access  public
 323       * @param   integer $prj_id The project ID
 324       * @param   integer $faq_id The FAQ ID
 325       * @return  array The list of projects
 326       */
 327      function getAssociatedSupportLevels($prj_id, $faq_id)
 328      {
 329          $stmt = "SELECT
 330                      fsl_support_level_id
 331                   FROM
 332                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq_support_level
 333                   WHERE
 334                      fsl_faq_id=" . Misc::escapeInteger($faq_id);
 335          $ids = $GLOBALS["db_api"]->dbh->getCol($stmt);
 336  
 337          $t = array();
 338          $levels = Customer::getSupportLevelAssocList(Misc::escapeInteger($prj_id));
 339          foreach ($levels as $support_level_id => $support_level) {
 340              if (in_array($support_level_id, $ids)) {
 341                  $t[$support_level_id] = $support_level;
 342              }
 343          }
 344          return $t;
 345      }
 346  
 347  
 348      /**
 349       * Method used to quickly change the ranking of a faq entry
 350       * from the administration screen.
 351       *
 352       * @access  public
 353       * @param   integer $faq_id The faq entry ID
 354       * @param   string $rank_type Whether we should change the entry down or up (options are 'asc' or 'desc')
 355       * @return  boolean
 356       */
 357      function changeRank($faq_id, $rank_type)
 358      {
 359          // check if the current rank is not already the first or last one
 360          $ranking = FAQ::_getRanking();
 361          $ranks = array_values($ranking);
 362          $ids = array_keys($ranking);
 363          $last = end($ids);
 364          $first = reset($ids);
 365          if ((($rank_type == 'asc') && ($faq_id == $first)) ||
 366                  (($rank_type == 'desc') && ($faq_id == $last))) {
 367              return false;
 368          }
 369  
 370          if ($rank_type == 'asc') {
 371              $diff = -1;
 372          } else {
 373              $diff = 1;
 374          }
 375          $new_rank = $ranking[$faq_id] + $diff;
 376          if (in_array($new_rank, $ranks)) {
 377              // switch the rankings here...
 378              $index = array_search($new_rank, $ranks);
 379              $replaced_faq_id = $ids[$index];
 380              $stmt = "UPDATE
 381                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq
 382                       SET
 383                          faq_rank=" . $ranking[$faq_id] . "
 384                       WHERE
 385                          faq_id=" . $replaced_faq_id;
 386              $GLOBALS["db_api"]->dbh->query($stmt);
 387          }
 388          $stmt = "UPDATE
 389                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq
 390                   SET
 391                      faq_rank=" . $new_rank . "
 392                   WHERE
 393                      faq_id=" . $faq_id;
 394          $GLOBALS["db_api"]->dbh->query($stmt);
 395          return true;
 396      }
 397  
 398  
 399      /**
 400       * Returns an associative array with the list of faq entry 
 401       * IDs and their respective ranking.
 402       *
 403       * @access  private
 404       * @return  array The list of faq entries
 405       */
 406      function _getRanking()
 407      {
 408          $stmt = "SELECT
 409                      faq_id,
 410                      faq_rank
 411                   FROM
 412                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "faq
 413                   ORDER BY
 414                      faq_rank ASC";
 415          $res = $GLOBALS["db_api"]->dbh->getAssoc($stmt);
 416          if (PEAR::isError($res)) {
 417              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 418              return array();
 419          } else {
 420              return $res;
 421          }
 422      }
 423  }
 424  
 425  // benchmarking the included file (aka setup time)
 426  if (APP_BENCHMARK) {
 427      $GLOBALS['bench']->setMarker('Included FAQ Class');
 428  }


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