[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/include/ -> class.reminder.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.reminder.php 3355 2007-07-11 14:34:27Z balsdorf $
  29  //
  30  
  31  require_once (APP_INC_PATH . "class.error_handler.php");
  32  require_once (APP_INC_PATH . "class.reminder_action.php");
  33  require_once (APP_INC_PATH . "class.priority.php");
  34  
  35  /**
  36   * Class to handle the business logic related to the reminder emails
  37   * that the system sends out.
  38   *
  39   * @version 1.0
  40   * @author João Prado Maia <jpm@mysql.com>
  41   */
  42  
  43  class Reminder
  44  {
  45      /**
  46       * Returns whether we are in "debug mode" or not. Returning true
  47       * here will enable all sorts of helpful messages in the reminder
  48       * check script.
  49       *
  50       * @access  public
  51       * @return  boolean
  52       */
  53      function isDebug()
  54      {
  55          return false;
  56      }
  57  
  58  
  59      /**
  60       * Method used to quickly change the ranking of a reminder entry
  61       * from the administration screen.
  62       *
  63       * @access  public
  64       * @param   integer $rem_id The reminder entry ID
  65       * @param   string $rank_type Whether we should change the reminder ID down or up (options are 'asc' or 'desc')
  66       * @return  boolean
  67       */
  68      function changeRank($rem_id, $rank_type)
  69      {
  70          // check if the current rank is not already the first or last one
  71          $ranking = Reminder::_getRanking();
  72          $ranks = array_values($ranking);
  73          $ids = array_keys($ranking);
  74          $last = end($ids);
  75          $first = reset($ids);
  76          if ((($rank_type == 'asc') && ($rem_id == $first)) ||
  77                  (($rank_type == 'desc') && ($rem_id == $last))) {
  78              return false;
  79          }
  80  
  81          if ($rank_type == 'asc') {
  82              $diff = -1;
  83          } else {
  84              $diff = 1;
  85          }
  86          $new_rank = $ranking[$rem_id] + $diff;
  87          if (in_array($new_rank, $ranks)) {
  88              // switch the rankings here...
  89              $index = array_search($new_rank, $ranks);
  90              $replaced_rem_id = $ids[$index];
  91              $stmt = "UPDATE
  92                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_level
  93                       SET
  94                          rem_rank=" . Misc::escapeInteger($ranking[$rem_id]) . "
  95                       WHERE
  96                          rem_id=" . Misc::escapeInteger($replaced_rem_id);
  97              $GLOBALS["db_api"]->dbh->query($stmt);
  98          }
  99          $stmt = "UPDATE
 100                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_level
 101                   SET
 102                      rem_rank=" . Misc::escapeInteger($new_rank) . "
 103                   WHERE
 104                      rem_id=" . Misc::escapeInteger($rem_id);
 105          $GLOBALS["db_api"]->dbh->query($stmt);
 106          return true;
 107      }
 108  
 109  
 110      /**
 111       * Returns an associative array with the list of reminder IDs and
 112       * their respective ranking.
 113       *
 114       * @access  private
 115       * @return  array The list of reminders
 116       */
 117      function _getRanking()
 118      {
 119          $stmt = "SELECT
 120                      rem_id,
 121                      rem_rank
 122                   FROM
 123                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_level
 124                   ORDER BY
 125                      rem_rank ASC";
 126          $res = $GLOBALS["db_api"]->dbh->getAssoc($stmt);
 127          if (PEAR::isError($res)) {
 128              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 129              return array();
 130          } else {
 131              return $res;
 132          }
 133      }
 134  
 135  
 136      /**
 137       * Method used by the administration screen to list the available
 138       * issues in a project.
 139       *
 140       * @access  public
 141       * @param   integer $prj_id The project ID
 142       * @return  array The list of issues
 143       */
 144      function getIssueAssocListByProject($prj_id)
 145      {
 146          $issues = Issue::getAssocListByProject($prj_id);
 147          foreach ($issues as $iss_id => $iss_summary) {
 148              $issues[$iss_id] = $iss_id . ': ' . $iss_summary;
 149          }
 150          return $issues;
 151      }
 152  
 153  
 154      /**
 155       * Method used to get the title of a specific reminder.
 156       *
 157       * @access  public
 158       * @param   integer $rem_id The reminder ID
 159       * @return  string The title of the reminder
 160       */
 161      function getTitle($rem_id)
 162      {
 163          $stmt = "SELECT
 164                      rem_title
 165                   FROM
 166                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_level
 167                   WHERE
 168                      rem_id=" . Misc::escapeInteger($rem_id);
 169          $res = $GLOBALS["db_api"]->dbh->getOne($stmt);
 170          if (PEAR::isError($res)) {
 171              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 172              return '';
 173          } else {
 174              return $res;
 175          }
 176      }
 177  
 178  
 179      /**
 180       * Method used to get the project associated to a given reminder.
 181       *
 182       * @access  public
 183       * @param   integer $rem_id The reminder ID
 184       * @return  integer The project ID
 185       */
 186      function getProjectID($rem_id)
 187      {
 188          $stmt = "SELECT
 189                      rem_prj_id
 190                   FROM
 191                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_level
 192                   WHERE
 193                      rem_id=" . Misc::escapeInteger($rem_id);
 194          $res = $GLOBALS["db_api"]->dbh->getOne($stmt);
 195          if (PEAR::isError($res)) {
 196              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 197              return '';
 198          } else {
 199              return $res;
 200          }
 201      }
 202  
 203  
 204      /**
 205       * Method used to get the details for a specific reminder.
 206       *
 207       * @access  public
 208       * @param   integer $rem_id The reminder ID
 209       * @return  array The details for the specified reminder
 210       */
 211      function getDetails($rem_id)
 212      {
 213          $stmt = "SELECT
 214                      *
 215                   FROM
 216                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_level
 217                   WHERE
 218                      rem_id=" . Misc::escapeInteger($rem_id);
 219          $res = $GLOBALS["db_api"]->dbh->getRow($stmt, DB_FETCHMODE_ASSOC);
 220          if (PEAR::isError($res)) {
 221              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 222              return '';
 223          } else {
 224              $requirements = Reminder::getRequirements($rem_id);
 225              if (!empty($requirements)) {
 226                  $res['type'] = $requirements['type'];
 227                  if ($res['type'] == 'support_level') {
 228                      $res['rer_support_level_id'] = $requirements['values'];
 229                  } elseif ($res['type'] == 'customer') {
 230                      $res['rer_customer_id'] = $requirements['values'];
 231                  } elseif ($res['type'] == 'issue') {
 232                      $res['rer_iss_id'] = array_values($requirements['values']);
 233                  }
 234              }
 235              $priorities = Reminder::getAssociatedPriorities($rem_id);
 236              if (count($priorities) > 0) {
 237                  $res['check_priority'] = 'yes';
 238                  $res['rer_pri_id'] = $priorities;
 239              }
 240              return $res;
 241          }
 242      }
 243  
 244  
 245      /**
 246       * Method used to get a list of all priority IDs associated with the given
 247       * reminder.
 248       *
 249       * @access  public
 250       * @param   integer $rem_id The reminder ID
 251       * @return  array The list of associated priority IDs
 252       */
 253      function getAssociatedPriorities($rem_id)
 254      {
 255          $stmt = "SELECT
 256                      rep_pri_id
 257                   FROM
 258                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_priority
 259                   WHERE
 260                      rep_rem_id=" . Misc::escapeInteger($rem_id);
 261          $res = $GLOBALS["db_api"]->dbh->getCol($stmt);
 262          if (PEAR::isError($res)) {
 263              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 264              return array();
 265          } else {
 266              return $res;
 267          }
 268      }
 269  
 270  
 271      /**
 272       * Method used to associate a support level ID with a given
 273       * reminder entry ID.
 274       *
 275       * @access  public
 276       * @param   integer $rem_id The reminder ID
 277       * @param   integer $support_level_id The support level ID
 278       * @return  boolean
 279       */
 280      function addSupportLevelAssociation($rem_id, $support_level_id)
 281      {
 282          $stmt = "INSERT INTO
 283                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_requirement
 284                   (
 285                      rer_rem_id,
 286                      rer_support_level_id
 287                   ) VALUES (
 288                      " . Misc::escapeInteger($rem_id) . ",
 289                      " . Misc::escapeInteger($support_level_id) . "
 290                   )";
 291          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 292          if (PEAR::isError($res)) {
 293              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 294              return false;
 295          } else {
 296              return true;
 297          }
 298      }
 299  
 300  
 301      /**
 302       * Method used to associate an issue with a given reminder.
 303       *
 304       * @access  public
 305       * @param   integer $rem_id The reminder ID
 306       * @param   integer $issue_id The issue ID
 307       * @return  boolean
 308       */
 309      function addIssueAssociation($rem_id, $issue_id)
 310      {
 311          $stmt = "INSERT INTO
 312                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_requirement
 313                   (
 314                      rer_rem_id,
 315                      rer_iss_id
 316                   ) VALUES (
 317                      " . Misc::escapeInteger($rem_id) . ",
 318                      " . Misc::escapeInteger($issue_id) . "
 319                   )";
 320          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 321          if (PEAR::isError($res)) {
 322              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 323              return false;
 324          } else {
 325              return true;
 326          }
 327      }
 328  
 329  
 330      /**
 331       * Method used to associate a customer ID with a given reminder
 332       * entry ID.
 333       *
 334       * @access  public
 335       * @param   integer $rem_id The reminder ID
 336       * @param   integer $customer_id The customer ID
 337       * @return  boolean
 338       */
 339      function addCustomerAssociation($rem_id, $customer_id)
 340      {
 341          $stmt = "INSERT INTO
 342                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_requirement
 343                   (
 344                      rer_rem_id,
 345                      rer_customer_id
 346                   ) VALUES (
 347                      " . Misc::escapeInteger($rem_id) . ",
 348                      " . Misc::escapeInteger($customer_id) . "
 349                   )";
 350          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 351          if (PEAR::isError($res)) {
 352              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 353              return false;
 354          } else {
 355              return true;
 356          }
 357      }
 358  
 359  
 360      /**
 361       * Method used to associate a reminder with any issue.
 362       *
 363       * @access  public
 364       * @param   integer $rem_id The reminder ID
 365       * @return  boolean
 366       */
 367      function associateAllIssues($rem_id)
 368      {
 369          $stmt = "INSERT INTO
 370                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_requirement
 371                   (
 372                      rer_rem_id,
 373                      rer_trigger_all_issues
 374                   ) VALUES (
 375                      " . Misc::escapeInteger($rem_id) . ",
 376                      1
 377                   )";
 378          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 379          if (PEAR::isError($res)) {
 380              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 381              return false;
 382          } else {
 383              return true;
 384          }
 385      }
 386  
 387  
 388      /**
 389       * Method used to associate a priority with a given reminder.
 390       *
 391       * @access  public
 392       * @param   integer $rem_id The reminder ID
 393       * @param   integer $priority_id The priority ID
 394       * @return  boolean
 395       */
 396      function addPriorityAssociation($rem_id, $priority_id)
 397      {
 398          $stmt = "INSERT INTO
 399                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_priority
 400                   (
 401                      rep_rem_id,
 402                      rep_pri_id
 403                   ) VALUES (
 404                      " . Misc::escapeInteger($rem_id) . ",
 405                      " . Misc::escapeInteger($priority_id) . "
 406                   )";
 407          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 408          if (PEAR::isError($res)) {
 409              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 410              return false;
 411          } else {
 412              return true;
 413          }
 414      }
 415  
 416  
 417      /**
 418       * Method used to remove all requirements and priority associations for a
 419       * given reminder.
 420       *
 421       * @access  public
 422       * @param   integer $rem_id The reminder ID
 423       */
 424      function removeAllAssociations($rem_id)
 425      {
 426          $rem_id = Misc::escapeInteger($rem_id);
 427          if (!is_array($rem_id)) {
 428              $rem_id = array($rem_id);
 429          }
 430          $stmt = "DELETE FROM
 431                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_requirement
 432                   WHERE
 433                      rer_rem_id IN (" . implode(',', $rem_id) . ")";
 434          $GLOBALS["db_api"]->dbh->query($stmt);
 435          $stmt = "DELETE FROM
 436                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_priority
 437                   WHERE
 438                      rep_rem_id IN (" . implode(',', $rem_id) . ")";
 439          $GLOBALS["db_api"]->dbh->query($stmt);
 440      }
 441  
 442  
 443      /**
 444       * Method used to create a new reminder.
 445       *
 446       * @access  public
 447       * @return  integer 1 if the insert worked, -1 or -2 otherwise
 448       */
 449      function insert()
 450      {
 451          $stmt = "INSERT INTO
 452                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_level
 453                   (
 454                      rem_created_date,
 455                      rem_rank,
 456                      rem_title,
 457                      rem_prj_id,
 458                      rem_skip_weekend
 459                   ) VALUES (
 460                      '" . Date_API::getCurrentDateGMT() . "',
 461                      " . Misc::escapeInteger($_POST['rank']) . ",
 462                      '" . Misc::escapeString($_POST['title']) . "',
 463                      " . Misc::escapeInteger($_POST['project']) . ",
 464                      " . Misc::escapeInteger($_POST['skip_weekend']) . "
 465                   )";
 466          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 467          if (PEAR::isError($res)) {
 468              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 469              return -1;
 470          } else {
 471              $new_rem_id = $GLOBALS["db_api"]->get_last_insert_id();
 472              // map the reminder requirements now
 473              if ((@$_POST['reminder_type'] == 'support_level') && (count($_POST['support_levels']) > 0)) {
 474                  for ($i = 0; $i < count($_POST['support_levels']); $i++) {
 475                      Reminder::addSupportLevelAssociation($new_rem_id, $_POST['support_levels'][$i]);
 476                  }
 477              } elseif ((@$_POST['reminder_type'] == 'issue') && (count($_POST['issues']) > 0)) {
 478                  for ($i = 0; $i < count($_POST['issues']); $i++) {
 479                      Reminder::addIssueAssociation($new_rem_id, $_POST['issues'][$i]);
 480                  }
 481              } elseif ((@$_POST['reminder_type'] == 'customer') && (count($_POST['customers']) > 0)) {
 482                  for ($i = 0; $i < count($_POST['customers']); $i++) {
 483                      Reminder::addCustomerAssociation($new_rem_id, $_POST['customers'][$i]);
 484                  }
 485              } elseif (@$_POST['reminder_type'] == 'all_issues') {
 486                   Reminder::associateAllIssues($new_rem_id);
 487              }
 488              if ((@$_POST['check_priority'] == 'yes') && (count($_POST['priorities']) > 0)) {
 489                  for ($i = 0; $i < count($_POST['priorities']); $i++) {
 490                      Reminder::addPriorityAssociation($new_rem_id, $_POST['priorities'][$i]);
 491                  }
 492              }
 493              return 1;
 494          }
 495      }
 496  
 497  
 498      /**
 499       * Method used to update the details of a specific reminder.
 500       *
 501       * @access  public
 502       * @return  integer 1 if the update worked, -1 or -2 otherwise
 503       */
 504      function update()
 505      {
 506          $stmt = "UPDATE
 507                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_level
 508                   SET
 509                      rem_last_updated_date='" . Date_API::getCurrentDateGMT() . "',
 510                      rem_rank=" . Misc::escapeInteger($_POST['rank']) . ",
 511                      rem_title='" . Misc::escapeString($_POST['title']) . "',
 512                      rem_prj_id=" . Misc::escapeInteger($_POST['project']) . ",
 513                      rem_skip_weekend=" . Misc::escapeInteger($_POST['skip_weekend']) . "
 514                   WHERE
 515                      rem_id=" . Misc::escapeInteger($_POST['id']);
 516          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 517          if (PEAR::isError($res)) {
 518              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 519              return -1;
 520          } else {
 521              Reminder::removeAllAssociations($_POST['id']);
 522              // map the reminder requirements now
 523              if ((@$_POST['reminder_type'] == 'support_level') && (count($_POST['support_levels']) > 0)) {
 524                  for ($i = 0; $i < count($_POST['support_levels']); $i++) {
 525                      Reminder::addSupportLevelAssociation($_POST['id'], $_POST['support_levels'][$i]);
 526                  }
 527              } elseif ((@$_POST['reminder_type'] == 'issue') && (count($_POST['issues']) > 0)) {
 528                  for ($i = 0; $i < count($_POST['issues']); $i++) {
 529                      Reminder::addIssueAssociation($_POST['id'], $_POST['issues'][$i]);
 530                  }
 531              } elseif ((@$_POST['reminder_type'] == 'customer') && (count($_POST['customers']) > 0)) {
 532                  for ($i = 0; $i < count($_POST['customers']); $i++) {
 533                      Reminder::addCustomerAssociation($_POST['id'], $_POST['customers'][$i]);
 534                  }
 535              } elseif (@$_POST['reminder_type'] == 'all_issues') {
 536                   Reminder::associateAllIssues($_POST['id']);
 537              }
 538              if ((@$_POST['check_priority'] == 'yes') && (count($_POST['priorities']) > 0)) {
 539                  for ($i = 0; $i < count($_POST['priorities']); $i++) {
 540                      Reminder::addPriorityAssociation($_POST['id'], $_POST['priorities'][$i]);
 541                  }
 542              }
 543              return 1;
 544          }
 545      }
 546  
 547  
 548      /**
 549       * Method used to remove reminders by using the administrative
 550       * interface of the system.
 551       *
 552       * @access  public
 553       * @return  boolean
 554       */
 555      function remove()
 556      {
 557          $items = @implode(", ", Misc::escapeInteger($_POST["items"]));
 558          $stmt = "DELETE FROM
 559                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_level
 560                   WHERE
 561                      rem_id IN ($items)";
 562          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 563          if (PEAR::isError($res)) {
 564              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 565              return false;
 566          } else {
 567              Reminder::removeAllAssociations($_POST["items"]);
 568              $stmt = "SELECT
 569                          rma_id
 570                       FROM
 571                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action
 572                       WHERE
 573                          rma_rem_id IN ($items)";
 574              $actions = $GLOBALS["db_api"]->dbh->getCol($stmt);
 575              if (count($actions) > 0) {
 576                  Reminder_Action::remove($actions);
 577              }
 578              return true;
 579          }
 580      }
 581  
 582  
 583      /**
 584       * Method used to get the list of requirements associated with a given
 585       * reminder.
 586       *
 587       * @access  public
 588       * @param   integer $rem_id The reminder ID
 589       * @return  array The list of requirements
 590       */
 591      function getRequirements($rem_id)
 592      {
 593          $stmt = "SELECT
 594                      rer_customer_id,
 595                      rer_iss_id,
 596                      rer_support_level_id,
 597                      rer_trigger_all_issues
 598                   FROM
 599                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_requirement
 600                   WHERE
 601                      rer_rem_id=" . Misc::escapeInteger($rem_id);
 602          $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
 603          if (PEAR::isError($res)) {
 604              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 605              return '';
 606          } else {
 607              $type = '';
 608              $values = array();
 609              for ($i = 0; $i < count($res); $i++) {
 610                  if ($res[$i]['rer_trigger_all_issues'] == '1') {
 611                      return array('type' => 'ALL');
 612                  } elseif (!empty($res[$i]['rer_support_level_id'])) {
 613                      $type = 'support_level';
 614                      $values[] = $res[$i]['rer_support_level_id'];
 615                  } elseif (!empty($res[$i]['rer_customer_id'])) {
 616                      $type = 'customer';
 617                      $values[] = $res[$i]['rer_customer_id'];
 618                  } elseif (!empty($res[$i]['rer_iss_id'])) {
 619                      $type = 'issue';
 620                      $values[] = $res[$i]['rer_iss_id'];
 621                  }
 622              }
 623              return array(
 624                  'type'   => $type,
 625                  'values' => $values
 626              );
 627          }
 628      }
 629  
 630  
 631      /**
 632       * Method used to get the list of reminders to be displayed in the
 633       * administration section.
 634       *
 635       * @access  public
 636       * @return  array The list of reminders
 637       */
 638      function getAdminList()
 639      {
 640          $stmt = "SELECT
 641                      " . APP_TABLE_PREFIX . "reminder_level.*,
 642                      prj_title
 643                   FROM
 644                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_level,
 645                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project
 646                   WHERE
 647                      rem_prj_id=prj_id
 648                   ORDER BY
 649                      rem_rank ASC";
 650          $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
 651          if (PEAR::isError($res)) {
 652              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 653              return array();
 654          } else {
 655              for ($i = 0; $i < count($res); $i++) {
 656                  $res[$i]['rem_created_date'] = Date_API::getFormattedDate($res[$i]["rem_created_date"]);
 657                  $actions = Reminder_Action::getList($res[$i]['rem_id']);
 658                  $res[$i]['total_actions'] = count($actions);
 659                  $priorities = Reminder::getAssociatedPriorities($res[$i]['rem_id']);
 660                  $priority_titles = Priority::getAssocList($res[$i]['rem_prj_id']);
 661                  $res[$i]['priorities'] = array();
 662                  if (count($priorities) > 0) {
 663                      foreach ($priorities as $pri_id) {
 664                          $res[$i]['priorities'][] = $priority_titles[$pri_id];
 665                      }
 666                  } else {
 667                      $res[$i]['priorities'][] = 'Any';
 668                  }
 669                  $requirements = Reminder::getRequirements($res[$i]['rem_id']);
 670                  $res[$i]['type'] = $requirements['type'];
 671              }
 672              return $res;
 673          }
 674      }
 675  
 676  
 677      /**
 678       * Method used to get the full list of reminders.
 679       *
 680       * @access  public
 681       * @return  array The list of reminders
 682       */
 683      function getList()
 684      {
 685          $stmt = "SELECT
 686                      *
 687                   FROM
 688                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_level
 689                   ORDER BY
 690                      rem_rank ASC";
 691          $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
 692          if (PEAR::isError($res)) {
 693              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 694              return array();
 695          } else {
 696              if (empty($res)) {
 697                  return array();
 698              } else {
 699                  $t = array();
 700                  for ($i = 0; $i < count($res); $i++) {
 701                      // ignore reminders that have no actions set yet...
 702                      $actions = Reminder_Action::getList($res[$i]['rem_id']);
 703                      if (count($actions) == 0) {
 704                          continue;
 705                      }
 706                      $res[$i]['actions'] = $actions;
 707                      $t[] = $res[$i];
 708                  }
 709                  return $t;
 710              }
 711          }
 712      }
 713  
 714  
 715      /**
 716       * Method used to get the list of issue IDs that match the given conditions.
 717       *
 718       * @access  public
 719       * @param   integer $rem_id The reminder ID
 720       * @param   array $conditions The list of conditions
 721       * @return  array The list of issue IDs
 722       */
 723      function getTriggeredIssues($reminder, $conditions)
 724      {
 725          // - build the SQL query to check if we have an issue that matches these conditions...
 726          $stmt = "SELECT
 727                      iss_id
 728                   FROM
 729                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "issue";
 730          $stmt .= Reminder::getWhereClause($reminder, $conditions);
 731          $stmt .= ' AND iss_trigger_reminders=1 ';
 732          // can't rely on the mysql server's timezone setting, so let's use gmt dates throughout
 733          $stmt = str_replace('UNIX_TIMESTAMP()', "UNIX_TIMESTAMP('" . Date_API::getCurrentDateGMT() . "')", $stmt);
 734          $res = $GLOBALS["db_api"]->dbh->getCol($stmt);
 735          if (PEAR::isError($res)) {
 736              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 737              return array();
 738          } else {
 739              // - if query returns >= 1, then run the appropriate action
 740              if (empty($res)) {
 741                  return array();
 742              } else {
 743                  return $res;
 744              }
 745          }
 746      }
 747  
 748  
 749      /**
 750       * Method used to generate a where clause from the given list of conditions.
 751       *
 752       * @access  public
 753       * @param   array $reminder An array of reminder info.
 754       * @param   array $conditions The list of conditions
 755       * @return  string The where clause
 756       */
 757      function getWhereClause($reminder, $conditions)
 758      {
 759          $stmt = '
 760                    WHERE
 761                      iss_prj_id=' . $reminder['rem_prj_id'] . "\n";
 762          $requirement = Reminder::getRequirements($reminder['rem_id']);
 763          if ($requirement['type'] == 'issue') {
 764              $stmt .= ' AND iss_id IN (' . implode(', ', $requirement['values']) . ")\n";
 765          } else {
 766              if (Customer::hasCustomerIntegration($reminder['rem_prj_id'])) {
 767                  if ($requirement['type'] == 'customer') {
 768                      $stmt .= ' AND iss_customer_id IN (' . implode(', ', $requirement['values']) . ")\n";
 769                  } elseif ($requirement['type'] == 'support_level') {
 770                      if (Customer::doesBackendUseSupportLevels($reminder['rem_prj_id'])) {
 771                          $customer_ids = Customer::getListBySupportLevel($reminder['rem_prj_id'], $requirement['values'], CUSTOMER_EXCLUDE_EXPIRED);
 772                          // break the query on purpose if no customers could be found
 773                          if (count($customer_ids) == 0) {
 774                              $customer_ids = array(-1);
 775                          }
 776                          $stmt .= ' AND iss_customer_id IN (' . implode(', ', $customer_ids) . ")\n";
 777                      }
 778                  }
 779              }
 780          }
 781          $priorities = Reminder::getAssociatedPriorities($reminder['rem_id']);
 782          if (count($priorities) > 0) {
 783              $stmt .= ' AND iss_pri_id IN (' . implode(', ', $priorities) . ")\n";
 784          }
 785          // now for the interesting stuff
 786          for ($i = 0; $i < count($conditions); $i++) {
 787              // check for fields that compare to other fields
 788              if (!empty($conditions[$i]['rlc_comparison_rmf_id'])) {
 789                  $sql_field = Reminder_Condition::getSQLField($conditions[$i]['rlc_comparison_rmf_id']);
 790                  $stmt .= sprintf(" AND %s %s %s\n", $conditions[$i]['rmf_sql_field'],
 791                                                $conditions[$i]['rmo_sql_representation'],
 792                                                $sql_field);
 793              } else {
 794                  // date field values are always saved as number of hours, so let's calculate them now as seconds
 795                  if (stristr($conditions[$i]['rmf_title'], 'date')) {
 796                      // support NULL as values for a date field
 797                      if (strtoupper($conditions[$i]['rlc_value']) == 'NULL') {
 798                          $conditions[$i]['rmf_sql_representation'] = $conditions[$i]['rmf_sql_field'];
 799                      } else {
 800                          $conditions[$i]['rlc_value'] = $conditions[$i]['rlc_value'] * 60 * 60;
 801                          if (@$reminder["rem_skip_weekend"] == 1) {
 802                              $sql_field = Reminder_Condition::getSQLField($conditions[$i]['rlc_rmf_id']);
 803                              $conditions[$i]['rmf_sql_representation'] = DB_API::getNoWeekendDateDiffSQL($sql_field);
 804                          }
 805                      }
 806                  }
 807  
 808                  $stmt .= sprintf(" AND %s %s %s\n", $conditions[$i]['rmf_sql_representation'],
 809                                                    $conditions[$i]['rmo_sql_representation'],
 810                                                    $conditions[$i]['rlc_value']);
 811              }
 812          }
 813          return $stmt;
 814      }
 815  
 816  
 817      /**
 818       * Method used to generate an SQL query to be used in debugging the reminder
 819       * conditions.
 820       *
 821       * @access  public
 822       * @param   integer $rem_id The reminder ID
 823       * @param   integer $rma_id The reminder action ID
 824       * @return  string The SQL query
 825       */
 826      function getSQLQuery($rem_id, $rma_id)
 827      {
 828          $reminder = Reminder::getDetails($rem_id);
 829          $conditions = Reminder_Condition::getList($rma_id);
 830          $stmt = "SELECT
 831                      iss_id
 832                   FROM
 833                      " . APP_TABLE_PREFIX . "issue";
 834          $stmt .= Reminder::getWhereClause($reminder, $conditions);
 835          // can't rely on the mysql server's timezone setting, so let's use gmt dates throughout
 836          $stmt = str_replace('UNIX_TIMESTAMP()', "UNIX_TIMESTAMP('" . Date_API::getCurrentDateGMT() . "')", $stmt);
 837          return $stmt;
 838      }
 839  
 840  
 841      /**
 842       * Method used to list the history of triggered reminder actions
 843       * for a given issue.
 844       *
 845       * @access  public
 846       * @param   integer $iss_id The issue ID
 847       * @return  array The list of triggered reminder actions
 848       */
 849      function getHistoryList($iss_id)
 850      {
 851          $stmt = "SELECT
 852                      rmh_created_date,
 853                      rma_title
 854                   FROM
 855                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_history,
 856                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action
 857                   WHERE
 858                      rmh_iss_id=" . Misc::escapeInteger($iss_id) . " AND
 859                      rmh_rma_id=rma_id
 860                   ORDER BY
 861                      rmh_created_date DESC";
 862          $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
 863          if (PEAR::isError($res)) {
 864              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 865              return array();
 866          } else {
 867              for ($i = 0; $i < count($res); $i++) {
 868                  $res[$i]["rmh_created_date"] = Date_API::getFormattedDate($res[$i]["rmh_created_date"]);
 869              }
 870              return $res;
 871          }
 872      }
 873  
 874  
 875      /**
 876       * Method used to get the list of email addresses to use
 877       * to send diagnostic information about the reminder system.
 878       *
 879       * @access  private
 880       * @return  array The list of alert email addresses
 881       */
 882      function _getReminderAlertAddresses()
 883      {
 884          $emails = array();
 885          $setup = Setup::load();
 886          if ((@$setup['email_reminder']['status'] == 'enabled') &&
 887                  (!empty($setup['email_reminder']['addresses']))) {
 888              $addresses = $setup['email_reminder']['addresses'];
 889              $emails = explode(',', $addresses);
 890          }
 891          $emails = array_map('trim', $emails);
 892          return $emails;
 893      }
 894  }
 895  
 896  // benchmarking the included file (aka setup time)
 897  if (APP_BENCHMARK) {
 898      $GLOBALS['bench']->setMarker('Included Reminder Class');
 899  }


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