[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/include/ -> class.reminder_action.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_action.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_condition.php");
  33  require_once (APP_INC_PATH . "class.notification.php");
  34  require_once (APP_INC_PATH . "class.user.php");
  35  require_once (APP_INC_PATH . "class.group.php");
  36  require_once (APP_INC_PATH . "class.mail.php");
  37  require_once (APP_INC_PATH . "class.issue.php");
  38  require_once (APP_INC_PATH . "class.validation.php");
  39  
  40  /**
  41   * Class to handle the business logic related to the reminder emails
  42   * that the system sends out.
  43   *
  44   * @version 1.0
  45   * @author João Prado Maia <jpm@mysql.com>
  46   */
  47  
  48  class Reminder_Action
  49  {
  50      /**
  51       * Method used to quickly change the ranking of a reminder action
  52       * from the administration screen.
  53       *
  54       * @access  public
  55       * @param   integer $rem_id The reminder ID
  56       * @param   integer $rma_id The reminder action ID
  57       * @param   string $rank_type Whether we should change the entry down or up (options are 'asc' or 'desc')
  58       * @return  boolean
  59       */
  60      function changeRank($rem_id, $rma_id, $rank_type)
  61      {
  62          // check if the current rank is not already the first or last one
  63          $ranking = Reminder_Action::_getRanking($rem_id);
  64          $ranks = array_values($ranking);
  65          $ids = array_keys($ranking);
  66          $last = end($ids);
  67          $first = reset($ids);
  68          if ((($rank_type == 'asc') && ($rma_id == $first)) ||
  69                  (($rank_type == 'desc') && ($rma_id == $last))) {
  70              return false;
  71          }
  72  
  73          if ($rank_type == 'asc') {
  74              $diff = -1;
  75          } else {
  76              $diff = 1;
  77          }
  78          $new_rank = $ranking[$rma_id] + $diff;
  79          if (in_array($new_rank, $ranks)) {
  80              // switch the rankings here...
  81              $index = array_search($new_rank, $ranks);
  82              $replaced_rma_id = $ids[$index];
  83              $stmt = "UPDATE
  84                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action
  85                       SET
  86                          rma_rank=" . Misc::escapeInteger($ranking[$rma_id]) . "
  87                       WHERE
  88                          rma_id=" . Misc::escapeInteger($replaced_rma_id);
  89              $GLOBALS["db_api"]->dbh->query($stmt);
  90          }
  91          $stmt = "UPDATE
  92                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action
  93                   SET
  94                      rma_rank=" . Misc::escapeInteger($new_rank) . "
  95                   WHERE
  96                      rma_id=" . Misc::escapeInteger($rma_id);
  97          $GLOBALS["db_api"]->dbh->query($stmt);
  98          return true;
  99      }
 100  
 101  
 102      /**
 103       * Returns an associative array with the list of reminder action
 104       * IDs and their respective ranking.
 105       *
 106       * @access  private
 107       * @param   integer $rem_id The reminder ID
 108       * @return  array The list of reminder actions
 109       */
 110      function _getRanking($rem_id)
 111      {
 112          $stmt = "SELECT
 113                      rma_id,
 114                      rma_rank
 115                   FROM
 116                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action
 117                   WHERE
 118                      rma_rem_id = " . Misc::escapeInteger($rem_id) . "
 119                   ORDER BY
 120                      rma_rank ASC";
 121          $res = $GLOBALS["db_api"]->dbh->getAssoc($stmt);
 122          if (PEAR::isError($res)) {
 123              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 124              return array();
 125          } else {
 126              return $res;
 127          }
 128      }
 129  
 130  
 131      /**
 132       * Method used to get the title of a specific reminder action.
 133       *
 134       * @access  public
 135       * @param   integer $rma_id The reminder action ID
 136       * @return  string The title of the reminder action
 137       */
 138      function getTitle($rma_id)
 139      {
 140          $stmt = "SELECT
 141                      rma_title
 142                   FROM
 143                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action
 144                   WHERE
 145                      rma_id=" . Misc::escapeInteger($rma_id);
 146          $res = $GLOBALS["db_api"]->dbh->getOne($stmt);
 147          if (PEAR::isError($res)) {
 148              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 149              return '';
 150          } else {
 151              return $res;
 152          }
 153      }
 154  
 155  
 156      /**
 157       * Method used to get the details for a specific reminder action.
 158       *
 159       * @access  public
 160       * @param   integer $rma_id The reminder action ID
 161       * @return  array The details for the specified reminder action
 162       */
 163      function getDetails($rma_id)
 164      {
 165          $stmt = "SELECT
 166                      *
 167                   FROM
 168                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action
 169                   WHERE
 170                      rma_id=" . Misc::escapeInteger($rma_id);
 171          $res = $GLOBALS["db_api"]->dbh->getRow($stmt, DB_FETCHMODE_ASSOC);
 172          if (PEAR::isError($res)) {
 173              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 174              return '';
 175          } else {
 176              // get the user list, if appropriate
 177              if (Reminder_Action::isUserList($res['rma_rmt_id'])) {
 178                  $res['user_list'] = Reminder_Action::getUserList($res['rma_id']);
 179              }
 180              return $res;
 181          }
 182      }
 183  
 184  
 185      /**
 186       * Method used to create a new reminder action.
 187       *
 188       * @access  public
 189       * @return  integer 1 if the insert worked, -1 or -2 otherwise
 190       */
 191      function insert()
 192      {
 193          $stmt = "INSERT INTO
 194                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action
 195                   (
 196                      rma_rem_id,
 197                      rma_rmt_id,
 198                      rma_created_date,
 199                      rma_title,
 200                      rma_rank,
 201                      rma_alert_irc,
 202                      rma_alert_group_leader,
 203                      rma_boilerplate
 204                   ) VALUES (
 205                      " . Misc::escapeInteger($_POST['rem_id']) . ",
 206                      " . Misc::escapeInteger($_POST['type']) . ",
 207                      '" . Date_API::getCurrentDateGMT() . "',
 208                      '" . Misc::escapeString($_POST['title']) . "',
 209                      '" . Misc::escapeInteger($_POST['rank']) . "',
 210                      " . Misc::escapeInteger($_POST['alert_irc']) . ",
 211                      " . Misc::escapeInteger($_POST['alert_group_leader']) . ",
 212                      '" . Misc::escapeString($_POST['boilerplate']) . "'
 213                   )";
 214          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 215          if (PEAR::isError($res)) {
 216              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 217              return -1;
 218          } else {
 219              $new_rma_id = $GLOBALS["db_api"]->get_last_insert_id();
 220              // add the user list, if appropriate
 221              if (Reminder_Action::isUserList($_POST['type'])) {
 222                  Reminder_Action::associateUserList($new_rma_id, $_POST['user_list']);
 223              }
 224              return 1;
 225          }
 226      }
 227  
 228  
 229      /**
 230       * Returns the list of users associated with a given reminder
 231       * action ID
 232       *
 233       * @access  public
 234       * @param   integer $rma_id The reminder action ID
 235       * @return  array The list of associated users
 236       */
 237      function getUserList($rma_id)
 238      {
 239          $stmt = "SELECT
 240                      ral_usr_id,
 241                      ral_email
 242                   FROM
 243                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action_list
 244                   WHERE
 245                      ral_rma_id=" . Misc::escapeInteger($rma_id);
 246          $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
 247          if (PEAR::isError($res)) {
 248              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 249              return array();
 250          } else {
 251              $t = array();
 252              for ($i = 0; $i < count($res); $i++) {
 253                  if (Validation::isEmail($res[$i]['ral_email'])) {
 254                      $t[$res[$i]['ral_email']] = $res[$i]['ral_email'];
 255                  } else {
 256                      $t[$res[$i]['ral_usr_id']] = User::getFullName($res[$i]['ral_usr_id']);
 257                  }
 258              }
 259              return $t;
 260          }
 261      }
 262  
 263  
 264      /**
 265       * Method used to associate a list of users with a given reminder
 266       * action ID
 267       *
 268       * @access  public
 269       * @param   integer $rma_id The reminder action ID
 270       * @param   array $user_list The list of users
 271       * @return  void
 272       */
 273      function associateUserList($rma_id, $user_list)
 274      {
 275          for ($i = 0; $i < count($user_list); $i++) {
 276              $usr_id = 0;
 277              $email = '';
 278              if (!Validation::isEmail($user_list[$i])) {
 279                  $usr_id = $user_list[$i];
 280              } else {
 281                  $email = $user_list[$i];
 282              }
 283              $stmt = "INSERT INTO
 284                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action_list
 285                       (
 286                          ral_rma_id,
 287                          ral_usr_id,
 288                          ral_email
 289                       ) VALUES (
 290                          " . Misc::escapeInteger($rma_id) . ",
 291                          " . Misc::escapeInteger($usr_id) . ",
 292                          '" . Misc::escapeString($email) . "'
 293                       )";
 294              $GLOBALS["db_api"]->dbh->query($stmt);
 295          }
 296      }
 297  
 298  
 299      /**
 300       * Method used to update the details of a specific reminder action.
 301       *
 302       * @access  public
 303       * @return  integer 1 if the update worked, -1 or -2 otherwise
 304       */
 305      function update()
 306      {
 307          $stmt = "UPDATE
 308                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action
 309                   SET
 310                      rma_last_updated_date='" . Date_API::getCurrentDateGMT() . "',
 311                      rma_rank='" . Misc::escapeInteger($_POST['rank']) . "',
 312                      rma_title='" . Misc::escapeString($_POST['title']) . "',
 313                      rma_rmt_id=" . Misc::escapeInteger($_POST['type']) . ",
 314                      rma_alert_irc=" . Misc::escapeInteger($_POST['alert_irc']) . ",
 315                      rma_alert_group_leader=" . Misc::escapeInteger($_POST['alert_group_leader']) . ",
 316                      rma_boilerplate='" . Misc::escapeString($_POST['boilerplate']) . "'
 317                   WHERE
 318                      rma_id=" . Misc::escapeInteger($_POST['id']);
 319          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 320          if (PEAR::isError($res)) {
 321              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 322              return -1;
 323          } else {
 324              // remove any user list associated with this reminder action
 325              Reminder_Action::clearActionUserList($_POST['id']);
 326              // add the user list back in, if appropriate
 327              if (Reminder_Action::isUserList($_POST['type'])) {
 328                  Reminder_Action::associateUserList($_POST['id'], $_POST['user_list']);
 329              }
 330              return 1;
 331          }
 332      }
 333  
 334  
 335      /**
 336       * Checks whether the given reminder action type is one where a
 337       * list of users is used or not.
 338       *
 339       * @access  public
 340       * @param   integer $rmt_id The reminder action type ID
 341       * @return  boolean
 342       */
 343      function isUserList($rmt_id)
 344      {
 345          $stmt = "SELECT
 346                      rmt_type
 347                   FROM
 348                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action_type
 349                   WHERE
 350                      rmt_id=" . Misc::escapeInteger($rmt_id);
 351          $res = $GLOBALS["db_api"]->dbh->getOne($stmt);
 352          if (PEAR::isError($res)) {
 353              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 354              return false;
 355          } else {
 356              $user_list_types = array(
 357                  'sms_list',
 358                  'email_list'
 359              );
 360              if (!in_array($res, $user_list_types)) {
 361                  return false;
 362              } else {
 363                  return true;
 364              }
 365          }
 366      }
 367  
 368  
 369      /**
 370       * Removes the full user list for a given reminder action ID.
 371       *
 372       * @access  public
 373       * @param   integer $rma_id The reminder action ID
 374       * @return  void
 375       */
 376      function clearActionUserList($rma_id)
 377      {
 378          if (!is_array($rma_id)) {
 379              $rma_id = array($rma_id);
 380          }
 381          $items = @implode(", ", Misc::escapeInteger($rma_id));
 382          $stmt = "DELETE FROM
 383                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action_list
 384                   WHERE
 385                      ral_rma_id IN ($items)";
 386          $GLOBALS["db_api"]->dbh->query($stmt);
 387      }
 388  
 389  
 390      /**
 391       * Method used to remove reminder actions by using the administrative
 392       * interface of the system.
 393       *
 394       * @access  public
 395       * @return  boolean
 396       */
 397      function remove($action_ids)
 398      {
 399          $items = @implode(", ", Misc::escapeInteger($action_ids));
 400          $stmt = "DELETE FROM
 401                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action
 402                   WHERE
 403                      rma_id IN ($items)";
 404          $GLOBALS["db_api"]->dbh->query($stmt);
 405          $stmt = "DELETE FROM
 406                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_history
 407                   WHERE
 408                      rmh_rma_id IN ($items)";
 409          $GLOBALS["db_api"]->dbh->query($stmt);
 410          $stmt = "DELETE FROM
 411                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_level_condition
 412                   WHERE
 413                      rlc_rma_id IN ($items)";
 414          $GLOBALS["db_api"]->dbh->query($stmt);
 415          Reminder_Action::clearActionUserList($action_ids);
 416      }
 417  
 418  
 419      /**
 420       * Method used to get an associative array of action types.
 421       *
 422       * @access  public
 423       * @return  array The list of action types
 424       */
 425      function getActionTypeList()
 426      {
 427          $stmt = "SELECT
 428                      rmt_id,
 429                      rmt_title
 430                   FROM
 431                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action_type
 432                   ORDER BY
 433                      rmt_title ASC";
 434          $res = $GLOBALS["db_api"]->dbh->getAssoc($stmt);
 435          if (PEAR::isError($res)) {
 436              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 437              return array();
 438          } else {
 439              return $res;
 440          }
 441      }
 442  
 443  
 444      /**
 445       * Method used to get the list of reminder actions to be displayed in the
 446       * administration section.
 447       *
 448       * @access  public
 449       * @param   integer $rem_id The reminder ID
 450       * @return  array The list of reminder actions
 451       */
 452      function getAdminList($rem_id)
 453      {
 454          $stmt = "SELECT
 455                      rma_rem_id,
 456                      rma_id,
 457                      rma_title,
 458                      rmt_title,
 459                      rma_rank,
 460                      rma_alert_irc,
 461                      rma_alert_group_leader
 462                   FROM
 463                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action,
 464                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action_type
 465                   WHERE
 466                      rma_rmt_id=rmt_id AND
 467                      rma_rem_id=" . Misc::escapeInteger($rem_id) . "
 468                   ORDER BY
 469                      rma_rank ASC";
 470          $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
 471          if (PEAR::isError($res)) {
 472              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 473              return array();
 474          } else {
 475              for ($i = 0; $i < count($res); $i++) {
 476                  $conditions = Reminder_Condition::getList($res[$i]['rma_id']);
 477                  $res[$i]['total_conditions'] = count($conditions);
 478                  foreach ($conditions as $condition) {
 479                      if ($condition['rmf_sql_field'] == 'iss_sta_id') {
 480                          $res[$i]['status'] = Status::getStatusTitle($condition['rlc_value']);
 481                      }
 482                  }
 483              }
 484              return $res;
 485          }
 486      }
 487  
 488  
 489      /**
 490       * Method used to get the list of reminder actions associated with a given
 491       * reminder ID.
 492       *
 493       * @access  public
 494       * @param   integer $reminder_id The reminder ID
 495       * @return  array The list of reminder actions
 496       */
 497      function getList($reminder_id)
 498      {
 499          $stmt = "SELECT
 500                      *
 501                   FROM
 502                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action
 503                   WHERE
 504                      rma_rem_id=" . Misc::escapeInteger($reminder_id) . "
 505                   ORDER BY
 506                      rma_rank ASC";
 507          $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
 508          if (PEAR::isError($res)) {
 509              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 510              return array();
 511          } else {
 512              if (empty($res)) {
 513                  return array();
 514              } else {
 515                  return $res;
 516              }
 517          }
 518      }
 519  
 520  
 521      /**
 522       * Method used to get the title of a reminder action type.
 523       *
 524       * @access  public
 525       * @param   integer $rmt_id The reminder action type
 526       * @return  string The action type title
 527       */
 528      function getActionType($rmt_id)
 529      {
 530          $stmt = "SELECT
 531                      rmt_type
 532                   FROM
 533                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_action_type
 534                   WHERE
 535                      rmt_id=" . Misc::escapeInteger($rmt_id);
 536          $res = $GLOBALS["db_api"]->dbh->getOne($stmt);
 537          if (PEAR::isError($res)) {
 538              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 539              return '';
 540          } else {
 541              return $res;
 542          }
 543      }
 544  
 545  
 546      /**
 547       * Method used to save a history entry about the execution of the current
 548       * reminder.
 549       *
 550       * @access  public
 551       * @param   integer $issue_id The issue ID
 552       * @param   integer $rma_id The reminder action ID
 553       * @return  boolean
 554       */
 555      function saveHistory($issue_id, $rma_id)
 556      {
 557          $stmt = "INSERT INTO
 558                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_history
 559                   (
 560                      rmh_iss_id,
 561                      rmh_rma_id,
 562                      rmh_created_date
 563                   ) VALUES (
 564                      " . Misc::escapeInteger($issue_id) . ",
 565                      " . Misc::escapeInteger($rma_id) . ",
 566                      '" . Date_API::getCurrentDateGMT() . "'
 567                   )";
 568          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 569          if (PEAR::isError($res)) {
 570              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 571              return false;
 572          } else {
 573              return true;
 574          }
 575      }
 576  
 577  
 578      /**
 579       * Method used to perform a specific action to an issue.
 580       *
 581       * @access  public
 582       * @param   integer $issue_id The issue ID
 583       * @param   array $reminder The reminder details
 584       * @param   array $action The action details
 585       * @return  boolean
 586       */
 587      function perform($issue_id, $reminder, $action)
 588      {
 589          $type = '';
 590          // - see which action type we're talking about here...
 591          $action_type = Reminder_Action::getActionType($action['rma_rmt_id']);
 592          // - do we also need to alert the group leader about this?
 593          $group_leader_usr_id = 0;
 594          if ($action['rma_alert_group_leader']) {
 595              if (Reminder::isDebug()) {
 596                  echo "  - " . ev_gettext("Processing Group Leader notification") . "\n";
 597              }
 598              $group_id = Issue::getGroupID($issue_id);
 599              // check if there's even a group associated with this issue
 600              if (empty($group_id)) {
 601                  if (Reminder::isDebug()) {
 602                      echo "  - " . ev_gettext('No group associated with issue %1$s', $issue_id) . "\n";
 603                  }
 604              } else {
 605                  $group_details = Group::getDetails($group_id);
 606                  if (!empty($group_details['grp_manager_usr_id'])) {
 607                      $group_leader_usr_id = $group_details['grp_manager_usr_id'];
 608                  }
 609              }
 610          }
 611          if (Reminder::isDebug()) {
 612             echo "  - " . ev_gettext('Performing action %1$s for issue # %2$s', $action_type, $issue_id) . "\n";
 613          }
 614          switch ($action_type) {
 615              case 'email_assignee':
 616                  $type = 'email';
 617                  $assignees = Issue::getAssignedUserIDs($issue_id);
 618                  $to = array();
 619                  foreach ($assignees as $assignee) {
 620                      $to[] = User::getFromHeader($assignee);
 621                  }
 622                  // add the group leader to the recipient list, if needed
 623                  if (!empty($group_leader_usr_id)) {
 624                      $leader_email = User::getFromHeader($group_leader_usr_id);
 625                      if ((!empty($leader_email)) && (!in_array($leader_email, $to))) {
 626                          $to[] = $leader_email;
 627                      }
 628                  }
 629                  break;
 630              case 'email_list':
 631                  $type = 'email';
 632                  $list = Reminder_Action::getUserList($action['rma_id']);
 633                  $to = array();
 634                  foreach ($list as $key => $value) {
 635                      // add the recipient to the list if it's a simple email address
 636                      if (Validation::isEmail($key)) {
 637                          $to[] = $key;
 638                      } else {
 639                          $to[] = User::getFromHeader($key);
 640                      }
 641                  }
 642                  // add the group leader to the recipient list, if needed
 643                  if (!empty($group_leader_usr_id)) {
 644                      $leader_email = User::getFromHeader($group_leader_usr_id);
 645                      if ((!empty($leader_email)) && (!in_array($leader_email, $to))) {
 646                          $to[] = $leader_email;
 647                      }
 648                  }
 649                  break;
 650              case 'sms_assignee':
 651                  $type = 'sms';
 652                  $assignees = Issue::getAssignedUserIDs($issue_id);
 653                  $to = array();
 654                  foreach ($assignees as $assignee) {
 655                      if (User::isClockedIn($assignee)) {
 656                          $sms_email = User::getSMS($assignee);
 657                          if (!empty($sms_email)) {
 658                              $to[] = $sms_email;
 659                          }
 660                      }
 661                  }
 662                  // add the group leader to the recipient list, if needed
 663                  if ((!empty($group_leader_usr_id)) && (User::isClockedIn($group_leader_usr_id))) {
 664                      $leader_sms_email = User::getSMS($group_leader_usr_id);
 665                      if ((!empty($leader_sms_email)) && (!in_array($leader_sms_email, $to))) {
 666                          $to[] = $leader_sms_email;
 667                      }
 668                  }
 669                  break;
 670              case 'sms_list':
 671                  $type = 'sms';
 672                  $list = Reminder_Action::getUserList($action['rma_id']);
 673                  $to = array();
 674                  foreach ($list as $key => $value) {
 675                      // add the recipient to the list if it's a simple email address
 676                      if (Validation::isEmail($key)) {
 677                          $to[] = $key;
 678                      } else {
 679                          // otherwise, check for the clocked-in status
 680                          if (User::isClockedIn($key)) {
 681                              $sms_email = User::getSMS($key);
 682                              if (!empty($sms_email)) {
 683                                  $to[] = $sms_email;
 684                              }
 685                          }
 686                      }
 687                  }
 688                  // add the group leader to the recipient list, if needed
 689                  if ((!empty($group_leader_usr_id)) && (User::isClockedIn($group_leader_usr_id))) {
 690                      $leader_sms_email = User::getSMS($group_leader_usr_id);
 691                      if ((!empty($leader_sms_email)) && (!in_array($leader_sms_email, $to))) {
 692                          $to[] = $leader_sms_email;
 693                      }
 694                  }
 695                  break;
 696          }
 697          $data = Notification::getIssueDetails($issue_id);
 698          $conditions = Reminder_Condition::getAdminList($action['rma_id']);
 699          // alert IRC if needed
 700          if ($action['rma_alert_irc']) {
 701              if (Reminder::isDebug()) {
 702                  echo "  - Processing IRC notification\n";
 703              }
 704              $irc_notice = "Issue #$issue_id (Priority: " . $data['pri_title'];
 705              // also add information about the assignee, if any
 706              $assignment = Issue::getAssignedUsers($issue_id);
 707              if (count($assignment) > 0) {
 708                  $irc_notice .= "; Assignment: " . implode(', ', $assignment);
 709              }
 710              if (!empty($data['iss_grp_id'])) {
 711                  $irc_notice .= "; Group: " . Group::getName($data['iss_grp_id']);
 712              }
 713              $irc_notice .= "), Reminder action '" . $action['rma_title'] . "' was just triggered";
 714              Notification::notifyIRC(Issue::getProjectID($issue_id), $irc_notice, $issue_id);
 715          }
 716          $setup = Setup::load();
 717          // if there are no recipients, then just skip to the next action
 718          if (count($to) == 0) {
 719              if (Reminder::isDebug()) {
 720                  echo "  - No recipients could be found\n";
 721              }
 722              // if not even an irc alert was sent, then save
 723              // a notice about this on reminder_sent@, if needed
 724              if (!$action['rma_alert_irc']) {
 725                  if (@$setup['email_reminder']['status'] == 'enabled') {
 726                      Reminder_Action::_recordNoRecipientError($issue_id, $type, $reminder, $action, $data, $conditions);
 727                  }
 728                  return false;
 729              }
 730          }
 731          // - save a history entry about this action
 732          Reminder_Action::saveHistory($issue_id, $action['rma_id']);
 733          // - save this action as the latest triggered one for the given issue ID
 734          Reminder_Action::recordLastTriggered($issue_id, $action['rma_id']);
 735  
 736          // - perform the action
 737          if (count($to) > 0) {
 738              // send a copy of this reminder to reminder_sent@, if needed
 739              if ((@$setup['email_reminder']['status'] == 'enabled') &&
 740                      (!empty($setup['email_reminder']['addresses']))) {
 741                  $addresses = Reminder::_getReminderAlertAddresses();
 742                  if (count($addresses) > 0) {
 743                      $to = array_merge($to, $addresses);
 744                  }
 745              }
 746              $tpl = new Template_API;
 747              $tpl->setTemplate('reminders/' . $type . '_alert.tpl.text');
 748              $tpl->bulkAssign(array(
 749                  "data"                     => $data,
 750                  "reminder"                 => $reminder,
 751                  "action"                   => $action,
 752                  "conditions"               => $conditions,
 753                  "has_customer_integration" => Customer::hasCustomerIntegration(Issue::getProjectID($issue_id))
 754              ));
 755              $text_message = $tpl->getTemplateContents();
 756              foreach ($to as $address) {
 757                  // send email (use PEAR's classes)
 758                  $mail = new Mail_API;
 759                  $mail->setTextBody($text_message);
 760                  $setup = $mail->getSMTPSettings();
 761                  $mail->send($setup["from"], $address, "[#$issue_id] " . ev_gettext("Reminder") . ": " . $action['rma_title'], 0, $issue_id, 'reminder');
 762              }
 763          }
 764          // - eventum saves the day once again
 765          return true;
 766      }
 767  
 768  
 769      /**
 770       * Method used to send an alert to a set of email addresses when
 771       * a reminder action was triggered, but no action was really
 772       * taken because no recipients could be found.
 773       *
 774       * @access  private
 775       * @param   integer $issue_id The issue ID
 776       * @param   string $type Which reminder are we trying to send, email or sms
 777       * @param   array $reminder The reminder details
 778       * @param   array $action The action details
 779       * @return  void
 780       */
 781      function _recordNoRecipientError($issue_id, $type, $reminder, $action, $data, $conditions)
 782      {
 783          $to = Reminder::_getReminderAlertAddresses();
 784          if (count($to) > 0) {
 785              $tpl = new Template_API;
 786              $tpl->setTemplate('reminders/alert_no_recipients.tpl.text');
 787              $tpl->bulkAssign(array(
 788                  "type"                     => $type,
 789                  "data"                     => $data,
 790                  "reminder"                 => $reminder,
 791                  "action"                   => $action,
 792                  "conditions"               => $conditions,
 793                  "has_customer_integration" => Customer::hasCustomerIntegration(Issue::getProjectID($issue_id))
 794              ));
 795              $text_message = $tpl->getTemplateContents();
 796              foreach ($to as $address) {
 797                  // send email (use PEAR's classes)
 798                  $mail = new Mail_API;
 799                  $mail->setTextBody($text_message);
 800                  $setup = $mail->getSMTPSettings();
 801                  $mail->send($setup["from"], $address, "[#$issue_id] " . ev_gettext("Reminder Not Triggered") . ": " . $action['rma_title'], 0, $issue_id);
 802              }
 803          }
 804      }
 805  
 806  
 807      /**
 808       * Returns the given list of issues with only the issues that
 809       * were last triggered for the given reminder action ID.
 810       *
 811       * @access  public
 812       * @param   array $issues The list of issue IDs
 813       * @param   integer $rma_id The reminder action ID
 814       * @return  array The list of issue IDs
 815       */
 816      function getRepeatActions($issues, $rma_id)
 817      {
 818          if (count($issues) == 0) {
 819              return $issues;
 820          }
 821  
 822          $stmt = "SELECT
 823                      rta_iss_id,
 824                      rta_rma_id
 825                   FROM
 826                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_triggered_action
 827                   WHERE
 828                      rta_iss_id IN (" . implode(', ', Misc::escapeInteger($issues)) . ")";
 829          $triggered_actions = $GLOBALS["db_api"]->dbh->getAssoc($stmt);
 830          if (PEAR::isError($triggered_actions)) {
 831              Error_Handler::logError(array($triggered_actions->getMessage(), $triggered_actions->getDebugInfo()), __FILE__, __LINE__);
 832              return $issues;
 833          } else {
 834              $repeat_issues = array();
 835              foreach ($issues as $issue_id) {
 836                  // if the issue was already triggered and the last triggered
 837                  // action was the given one, then add it to the list of repeat issues
 838                  if ((in_array($issue_id, array_keys($triggered_actions))) && ($triggered_actions[$issue_id] == $rma_id)) {
 839                      $repeat_issues[] = $issue_id;
 840                  }
 841              }
 842              return $repeat_issues;
 843          }
 844      }
 845  
 846  
 847      /**
 848       * Records the last triggered reminder action for a given
 849       * issue ID.
 850       *
 851       * @access  public
 852       * @param   integer $issue_id The issue ID
 853       * @param   integer $rma_id The reminder action ID
 854       * @return  boolean
 855       */
 856      function recordLastTriggered($issue_id, $rma_id)
 857      {
 858          $issue_id = Misc::escapeInteger($issue_id);
 859          $rma_id = Misc::escapeInteger($rma_id);
 860          $stmt = "SELECT
 861                      COUNT(*)
 862                   FROM
 863                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_triggered_action
 864                   WHERE
 865                      rta_iss_id=$issue_id";
 866          $total = $GLOBALS["db_api"]->dbh->getOne($stmt);
 867          if ($total == 1) {
 868              $stmt = "UPDATE
 869                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_triggered_action
 870                       SET
 871                          rta_rma_id=$rma_id
 872                       WHERE
 873                          rta_iss_id=$issue_id";
 874          } else {
 875              $stmt = "INSERT INTO
 876                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_triggered_action
 877                       (
 878                          rta_iss_id,
 879                          rta_rma_id
 880                       ) VALUES (
 881                          $issue_id,
 882                          $rma_id
 883                       )";
 884          }
 885          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 886          if (PEAR::isError($res)) {
 887              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 888              return false;
 889          } else {
 890              return true;
 891          }
 892      }
 893  
 894  
 895      /**
 896       * Clears the last triggered reminder for a given issue ID.
 897       *
 898       * @access  public
 899       * @param   integer $issue_id The issue ID
 900       * @return  boolean
 901       */
 902      function clearLastTriggered($issue_id)
 903      {
 904          $stmt = "DELETE FROM
 905                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "reminder_triggered_action
 906                   WHERE
 907                      rta_iss_id=" . Misc::escapeInteger($issue_id);
 908          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 909          if (PEAR::isError($res)) {
 910              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 911              return false;
 912          } else {
 913              return true;
 914          }
 915      }
 916  }
 917  
 918  // benchmarking the included file (aka setup time)
 919  if (APP_BENCHMARK) {
 920      $GLOBALS['bench']->setMarker('Included Reminder_Action Class');
 921  }


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