[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/include/ -> class.draft.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  
  29  require_once (APP_INC_PATH . "class.email_account.php");
  30  
  31  class Draft
  32  {
  33      /**
  34       * Method used to save the routed draft into a backup directory.
  35       *
  36       * @access  public
  37       * @param   string $message The full body of the draft
  38       */
  39      function saveRoutedMessage($message)
  40      {
  41          list($usec,) = explode(" ", microtime());
  42          $filename = date('Y-m-d_H-i-s_') . $usec . '.draft.txt';
  43          $path = APP_ROUTED_MAILS_SAVEDIR . 'routed_drafts/' . $filename;
  44          $fp = @fopen($file, 'w');
  45          @fwrite($fp, $message);
  46          @fclose($fp);
  47          @chmod($file, 0644);
  48      }
  49  
  50  
  51      /**
  52       * Method used to save the draft response in the database for
  53       * further use.
  54       *
  55       * @access  public
  56       * @param   integer $issue_id The issue ID
  57       * @param   string $to The primary recipient of the draft
  58       * @param   string $cc The secondary recipients of the draft
  59       * @param   string $subject The subject of the draft
  60       * @param   string $message The draft body
  61       * @param   integer $parent_id The ID of the email that this draft is replying to, if any
  62       * @param   string $unknown_user The sender of the draft, if not a real user
  63       * @param   boolean $add_history_entry Whether to add a history entry automatically or not
  64       * @return  integer 1 if the update worked, -1 otherwise
  65       */
  66      function saveEmail($issue_id, $to, $cc, $subject, $message, $parent_id = FALSE, $unknown_user = FALSE, $add_history_entry = TRUE)
  67      {
  68          $issue_id = Misc::escapeInteger($issue_id);
  69          $parent_id = Misc::escapeInteger($parent_id);
  70          if (empty($parent_id)) {
  71              $parent_id = 'NULL';
  72          }
  73          // if unknown_user is not empty, set the usr_id to be the system user.
  74          if (!empty($unknown_user)) {
  75              $usr_id = APP_SYSTEM_USER_ID;
  76          } else {
  77              $usr_id = Auth::getUserID();
  78          }
  79          $stmt = "INSERT INTO
  80                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "email_draft
  81                   (
  82                      emd_updated_date,
  83                      emd_usr_id,
  84                      emd_iss_id,
  85                      emd_sup_id,
  86                      emd_subject,
  87                      emd_body";
  88          if (!empty($unknown_user)) {
  89              $stmt .= ", emd_unknown_user";
  90          }
  91          $stmt .= ") VALUES (
  92                      '" . Date_API::getCurrentDateGMT() . "',
  93                      $usr_id,
  94                      $issue_id,
  95                      $parent_id,
  96                      '" . Misc::escapeString($subject) . "',
  97                      '" . Misc::escapeString($message) . "'";
  98          if (!empty($unknown_user)) {
  99              $stmt .= ", '" . Misc::escapeString($unknown_user) . "'";
 100          }
 101          $stmt .= ")";
 102          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 103          if (PEAR::isError($res)) {
 104              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 105              return -1;
 106          } else {
 107              $new_emd_id = $GLOBALS['db_api']->get_last_insert_id();
 108              Draft::addEmailRecipient($new_emd_id, $to, false);
 109              $cc = str_replace(',', ';', $cc);
 110              $ccs = explode(';', $cc);
 111              foreach ($ccs as $cc) {
 112                  Draft::addEmailRecipient($new_emd_id, $cc, true);
 113              }
 114              Issue::markAsUpdated($issue_id, "draft saved");
 115              if ($add_history_entry) {
 116                  History::add($issue_id, $usr_id, History::getTypeID('draft_added'), ev_gettext('Email message saved as a draft by %1$s', User::getFullName($usr_id)));
 117              }
 118              return 1;
 119          }
 120      }
 121  
 122  
 123      /**
 124       * Method used to update an existing draft response.
 125       *
 126       * @access  public
 127       * @param   integer $issue_id The issue ID
 128       * @param   integer $emd_id The email draft ID
 129       * @param   string $to The primary recipient of the draft
 130       * @param   string $cc The secondary recipients of the draft
 131       * @param   string $subject The subject of the draft
 132       * @param   string $message The draft body
 133       * @param   integer $parent_id The ID of the email that this draft is replying to, if any
 134       * @return  integer 1 if the update worked, -1 otherwise
 135       */
 136      function update($issue_id, $emd_id, $to, $cc, $subject, $message, $parent_id = FALSE)
 137      {
 138          $issue_id = Misc::escapeInteger($issue_id);
 139          $emd_id = Misc::escapeInteger($emd_id);
 140          $parent_id = Misc::escapeInteger($parent_id);
 141          if (empty($parent_id)) {
 142              $parent_id = 'NULL';
 143          }
 144          $usr_id = Auth::getUserID();
 145  
 146          // update previous draft and insert new record
 147          $stmt = "UPDATE
 148                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "email_draft
 149                   SET
 150                      emd_updated_date='" . Date_API::getCurrentDateGMT() . "',
 151                      emd_status = 'edited'
 152                   WHERE
 153                      emd_id=$emd_id";
 154          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 155          if (PEAR::isError($res)) {
 156              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 157              return -1;
 158          } else {
 159              Issue::markAsUpdated($issue_id, "draft saved");
 160              History::add($issue_id, $usr_id, History::getTypeID('draft_updated'), ev_gettext('Email message draft updated by %1$s', User::getFullName($usr_id)));
 161              Draft::saveEmail($issue_id, $to, $cc, $subject, $message, $parent_id, false, false);
 162              return 1;
 163          }
 164      }
 165  
 166  
 167      /**
 168       * Method used to remove a draft response.
 169       *
 170       * @access  public
 171       * @param   integer $emd_id The email draft ID
 172       * @return  boolean
 173       */
 174      function remove($emd_id)
 175      {
 176          $emd_id = Misc::escapeInteger($emd_id);
 177          $stmt = "UPDATE
 178                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "email_draft
 179                   SET
 180                      emd_status = 'sent'
 181                   WHERE
 182                      emd_id=$emd_id";
 183          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 184          if (PEAR::isError($res)) {
 185              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 186              return false;
 187          } else {
 188              return true;
 189          }
 190      }
 191  
 192  
 193      /**
 194       * Method used to remove the recipients associated with the given
 195       * email draft response.
 196       *
 197       * @access  public
 198       * @param   integer $emd_id The email draft ID
 199       * @return  boolean
 200       */
 201      function removeRecipients($emd_id)
 202      {
 203          $emd_id = Misc::escapeInteger($emd_id);
 204          $stmt = "DELETE FROM
 205                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "email_draft_recipient
 206                   WHERE
 207                      edr_emd_id=$emd_id";
 208          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 209          if (PEAR::isError($res)) {
 210              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 211              return false;
 212          } else {
 213              return true;
 214          }
 215      }
 216  
 217  
 218      /**
 219       * Method used to associate a recipient with a given email
 220       * draft response.
 221       *
 222       * @access  public
 223       * @param   integer $emd_id The email draft ID
 224       * @param   string $email The recipient's email address
 225       * @param   boolean $is_cc Whether this recipient is in the Cc list for the given draft
 226       * @return  boolean
 227       */
 228      function addEmailRecipient($emd_id, $email, $is_cc)
 229      {
 230          $emd_id = Misc::escapeInteger($emd_id);
 231          if (!$is_cc) {
 232              $is_cc = 0;
 233          } else {
 234              $is_cc = 1;
 235          }
 236          $email = trim($email);
 237          $stmt = "INSERT INTO
 238                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "email_draft_recipient
 239                   (
 240                      edr_emd_id,
 241                      edr_is_cc,
 242                      edr_email
 243                   ) VALUES (
 244                      $emd_id,
 245                      $is_cc,
 246                      '" . Misc::escapeString($email) . "'
 247                   )";
 248          $res = $GLOBALS["db_api"]->dbh->query($stmt);
 249          if (PEAR::isError($res)) {
 250              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 251              return false;
 252          } else {
 253              return true;
 254          }
 255      }
 256  
 257  
 258      /**
 259       * Method used to get the details on a given email draft response.
 260       *
 261       * @access  public
 262       * @param   integer $emd_id The email draft ID
 263       * @return  array The email draft details
 264       */
 265      function getDetails($emd_id)
 266      {
 267          $emd_id = Misc::escapeInteger($emd_id);
 268          $stmt = "SELECT
 269                      *
 270                   FROM
 271                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "email_draft
 272                   WHERE
 273                      emd_id=$emd_id";
 274          $res = $GLOBALS["db_api"]->dbh->getRow($stmt, DB_FETCHMODE_ASSOC);
 275          if (PEAR::isError($res)) {
 276              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 277              return '';
 278          } else {
 279              $res["emd_updated_date"] = Date_API::getFormattedDate($res["emd_updated_date"]);
 280              if (!empty($res['emd_unknown_user'])) {
 281                  $res['from'] = $res["emd_unknown_user"];
 282              } else {
 283                  $res['from'] = User::getFromHeader($res['emd_usr_id']);
 284              }
 285              list($res['to'], $res['cc']) = Draft::getEmailRecipients($emd_id);
 286              return $res;
 287          }
 288      }
 289  
 290  
 291      /**
 292       * Returns a list of drafts associated with an issue.
 293       *
 294       * @access  public
 295       * @param   integer $issue_id The ID of the issue.
 296       * @param   boolean $show_all If all draft statuses should be shown
 297       * @return  array An array of drafts.
 298       */
 299      function getList($issue_id, $show_all = false)
 300      {
 301          $issue_id = Misc::escapeInteger($issue_id);
 302          $stmt = "SELECT
 303                      emd_id,
 304                      emd_usr_id,
 305                      emd_subject,
 306                      emd_updated_date,
 307                      emd_unknown_user,
 308                      emd_status
 309                   FROM
 310                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "email_draft
 311                   WHERE
 312                      emd_iss_id=$issue_id\n";
 313          if ($show_all == false) {
 314              $stmt .= "AND emd_status = 'pending'\n";
 315          }
 316          $stmt .= "ORDER BY
 317                      emd_id";
 318          $res = $GLOBALS["db_api"]->dbh->getAll($stmt, DB_FETCHMODE_ASSOC);
 319          if (PEAR::isError($res)) {
 320              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 321              return '';
 322          } else {
 323              for ($i = 0; $i < count($res); $i++) {
 324                  $res[$i]["emd_updated_date"] = Date_API::getFormattedDate($res[$i]["emd_updated_date"]);
 325                  if (!empty($res[$i]['emd_unknown_user'])) {
 326                      $res[$i]['from'] = $res[$i]["emd_unknown_user"];
 327                  } else {
 328                      $res[$i]['from'] = User::getFromHeader($res[$i]['emd_usr_id']);
 329                  }
 330                  list($res[$i]['to'], ) = Draft::getEmailRecipients($res[$i]['emd_id']);
 331                  if (empty($res[$i]['to'])) {
 332                      $res[$i]['to'] = "Notification List";
 333                  }
 334              }
 335              return $res;
 336          }
 337      }
 338  
 339  
 340      /**
 341       * Method used to get the list of email recipients for a
 342       * given draft response.
 343       *
 344       * @access  public
 345       * @param   integer $emd_id The email draft ID
 346       * @return  array The list of email recipients
 347       */
 348      function getEmailRecipients($emd_id)
 349      {
 350          $emd_id = Misc::escapeInteger($emd_id);
 351          $stmt = "SELECT
 352                      edr_email,
 353                      edr_is_cc
 354                   FROM
 355                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "email_draft_recipient
 356                   WHERE
 357                      edr_emd_id=$emd_id";
 358          $res = $GLOBALS["db_api"]->dbh->getAssoc($stmt);
 359          if (PEAR::isError($res)) {
 360              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 361              return array('', '');
 362          } else {
 363              $to = '';
 364              $ccs = array();
 365              foreach ($res as $email => $is_cc) {
 366                  if ($is_cc) {
 367                      $ccs[] = $email;
 368                  } else {
 369                      $to = $email;
 370                  }
 371              }
 372              return array(
 373                  $to,
 374                  $ccs
 375              );
 376          }
 377      }
 378  
 379  
 380      /**
 381       * Returns the nth draft for the specific issue. Sequence starts at 1.
 382       *
 383       * @access  public
 384       * @param   integer $issue_id The id of the issue.
 385       * @param   integer $sequence The sequential number of the draft.
 386       * @return  array An array of data containing details about the draft.
 387       */
 388      function getDraftBySequence($issue_id, $sequence)
 389      {
 390          $issue_id = Misc::escapeInteger($issue_id);
 391          $sequence = Misc::escapeInteger($sequence);
 392          $stmt = "SELECT
 393                      emd_id
 394                  FROM
 395                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "email_draft
 396                  WHERE
 397                      emd_iss_id = $issue_id AND
 398                      emd_status = 'pending'
 399                  ORDER BY
 400                      emd_id ASC
 401                  LIMIT " . ($sequence - 1) . ", 1";
 402          $res = $GLOBALS["db_api"]->dbh->getOne($stmt);
 403          if (PEAR::isError($res)) {
 404              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 405              return array();
 406          } else {
 407              if (empty($res)) {
 408                  return array();
 409              } else {
 410                  return Draft::getDetails($res);
 411              }
 412          }
 413      }
 414  
 415  
 416      /**
 417       * Converts an email to a draft and sends it.
 418       *
 419       * @access  public
 420       * @param   integer $draft_id The id of the draft to send.
 421       */
 422      function send($draft_id)
 423      {
 424          $draft_id = Misc::escapeInteger($draft_id);
 425          $draft = Draft::getDetails($draft_id);
 426          $_POST["issue_id"] = $draft["emd_iss_id"];
 427          $_POST["subject"] = $draft["emd_subject"];
 428          $_POST["from"] = User::getFromHeader(Auth::getUserID());
 429          $_POST["to"] = $draft["to"];
 430          $_POST["cc"] = @join(";", $draft["cc"]);
 431          $_POST["message"] = $draft["emd_body"];
 432          $_POST["ema_id"] = Email_Account::getEmailAccount();
 433          $res = Support::sendEmail();
 434          if ($res == 1) {
 435             Draft::remove($draft_id);
 436          }
 437          return $res;
 438      }
 439  
 440  
 441      /**
 442       * Returns the number of drafts by a user in a time range.
 443       *
 444       * @access  public
 445       * @param   string $usr_id The ID of the user
 446       * @param   integer $start The timestamp of the start date
 447       * @param   integer $end The timestanp of the end date
 448       * @return  integer The number of note by the user.
 449       */
 450      function getCountByUser($usr_id, $start, $end)
 451      {
 452          $stmt = "SELECT
 453                      COUNT(emd_id)
 454                   FROM
 455                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "email_draft
 456                   WHERE
 457                      emd_updated_date BETWEEN '$start' AND '$end' AND
 458                      emd_usr_id = $usr_id";
 459          $res = $GLOBALS["db_api"]->dbh->getOne($stmt);
 460          if (PEAR::isError($res)) {
 461              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 462              return "";
 463          }
 464          return $res;
 465      }
 466  }
 467  
 468  // benchmarking the included file (aka setup time)
 469  if (APP_BENCHMARK) {
 470      $GLOBALS['bench']->setMarker('Included Draft Class');
 471  }


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