/classes/log/EmailLogDAO.inc.php

https://github.com/ojsde/pkp-lib · PHP · 251 lines · 145 code · 29 blank · 77 comment · 7 complexity · ef064640878479c0cd8aa0b4c562e9b3 MD5 · raw file

  1. <?php
  2. /**
  3. * @file classes/log/EmailLogDAO.inc.php
  4. *
  5. * Copyright (c) 2003-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class EmailLogDAO
  9. * @ingroup log
  10. * @see EmailLogEntry, Log
  11. *
  12. * @brief Class for inserting/accessing email log entries.
  13. */
  14. import ('lib.pkp.classes.log.EmailLogEntry');
  15. class EmailLogDAO extends DAO {
  16. /**
  17. * Constructor
  18. */
  19. function EmailLogDAO() {
  20. parent::DAO();
  21. }
  22. /**
  23. * Retrieve a log entry by ID.
  24. * @param $logId int
  25. * @param $assocType int optional
  26. * @param $assocId int optional
  27. * @return EmailLogEntry
  28. */
  29. function &getById($logId, $assocType = null, $assocId = null) {
  30. $params = array((int) $logId);
  31. if (isset($assocType)) {
  32. $params[] = (int) $assocType;
  33. $params[] = (int) $assocId;
  34. }
  35. $result =& $this->retrieve(
  36. 'SELECT * FROM email_log WHERE log_id = ?' .
  37. (isset($assocType)?' AND assoc_type = ? AND assoc_id = ?':''),
  38. $params
  39. );
  40. $returner = null;
  41. if ($result->RecordCount() != 0) {
  42. $returner =& $this->build($result->GetRowAssoc(false));
  43. }
  44. $result->Close();
  45. unset($result);
  46. return $returner;
  47. }
  48. /**
  49. * Retrieve a log entry by event type.
  50. * @param $assocType int
  51. * @param $assocId int
  52. * @param $eventType int
  53. * @param $userId int optional
  54. * @param $rangeInfo object optional
  55. * @return EmailLogEntry
  56. */
  57. function &getByEventType($assocType, $assocId, $eventType, $userId = null, $rangeInfo = null) {
  58. $params = array(
  59. (int) $assocType,
  60. (int) $assocId,
  61. (int) $eventType);
  62. if ($userId) $params[] = $userId;
  63. $result =& $this->retrieveRange(
  64. 'SELECT e.*
  65. FROM email_log e' .
  66. ($userId ? ' LEFT JOIN email_log_users u ON e.log_id = u.email_log_id' : '') .
  67. ' WHERE e.assoc_type = ? AND
  68. e.assoc_id = ? AND
  69. e.event_type = ?' .
  70. ($userId ? ' AND u.user_id = ?' : ''),
  71. $params,
  72. $rangeInfo
  73. );
  74. $returner = new DAOResultFactory($result, $this, 'build');
  75. return $returner;
  76. }
  77. /**
  78. * Retrieve all log entries for an object matching the specified association.
  79. * @param $assocType int
  80. * @param $assocId int
  81. * @param $rangeInfo object optional
  82. * @return DAOResultFactory containing matching EventLogEntry ordered by sequence
  83. */
  84. function &getByAssoc($assocType = null, $assocId = null, $rangeInfo = null) {
  85. $result =& $this->retrieveRange(
  86. 'SELECT *
  87. FROM email_log
  88. WHERE assoc_type = ?
  89. AND assoc_id = ?
  90. ORDER BY log_id DESC',
  91. array((int) $assocType, (int) $assocId),
  92. $rangeInfo
  93. );
  94. $returner = new DAOResultFactory($result, $this, 'build');
  95. return $returner;
  96. }
  97. /**
  98. * Internal function to return an EmailLogEntry object from a row.
  99. * @param $row array
  100. * @return EmailLogEntry
  101. */
  102. function &build($row) {
  103. $entry = $this->newDataObject();
  104. $entry->setId($row['log_id']);
  105. $entry->setAssocType($row['assoc_type']);
  106. $entry->setAssocId($row['assoc_id']);
  107. $entry->setSenderId($row['sender_id']);
  108. $entry->setDateSent($this->datetimeFromDB($row['date_sent']));
  109. $entry->setIPAddress($row['ip_address']);
  110. $entry->setEventType($row['event_type']);
  111. $entry->setFrom($row['from_address']);
  112. $entry->setRecipients($row['recipients']);
  113. $entry->setCcs($row['cc_recipients']);
  114. $entry->setBccs($row['bcc_recipients']);
  115. $entry->setSubject($row['subject']);
  116. $entry->setBody($row['body']);
  117. HookRegistry::call('EmailLogDAO::build', array(&$entry, &$row));
  118. return $entry;
  119. }
  120. /**
  121. * Insert a new log entry.
  122. * @param $entry EmailLogEntry
  123. */
  124. function insertObject(&$entry) {
  125. $this->update(
  126. sprintf('INSERT INTO email_log
  127. (sender_id, date_sent, ip_address, event_type, assoc_type, assoc_id, from_address, recipients, cc_recipients, bcc_recipients, subject, body)
  128. VALUES
  129. (?, %s, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
  130. $this->datetimeToDB($entry->getDateSent())),
  131. array(
  132. $entry->getSenderId(),
  133. $entry->getIPAddress(),
  134. $entry->getEventType(),
  135. $entry->getAssocType(),
  136. $entry->getAssocId(),
  137. $entry->getFrom(),
  138. $entry->getRecipients(),
  139. $entry->getCcs(),
  140. $entry->getBccs(),
  141. $entry->getSubject(),
  142. $entry->getBody()
  143. )
  144. );
  145. $entry->setId($this->getInsertLogId());
  146. $this->_insertLogUserIds($entry);
  147. return $entry->getId();
  148. }
  149. /**
  150. * Delete a single log entry for an object.
  151. * @param $logId int
  152. * @param $assocType int optional
  153. * @param $assocId int optional
  154. */
  155. function deleteObject($logId, $assocType = null, $assocId = null) {
  156. $params = array((int) $logId);
  157. if (isset($assocType)) {
  158. $params[] = (int) $assocType;
  159. $params[] = (int) $assocId;
  160. }
  161. return $this->update(
  162. 'DELETE FROM email_log WHERE log_id = ?' .
  163. (isset($assocType)?' AND assoc_type = ? AND assoc_id = ?':''),
  164. $params
  165. );
  166. }
  167. /**
  168. * Delete all log entries for an object.
  169. * @param $assocType int
  170. * @praam $assocId int
  171. */
  172. function deleteByAssoc($assocType, $assocId) {
  173. return $this->update(
  174. 'DELETE FROM email_log WHERE assoc_type = ? AND assoc_id = ?',
  175. array((int) $assocType, (int) $assocId)
  176. );
  177. }
  178. /**
  179. * Transfer all log entries to another user.
  180. * @param $oldUserId int
  181. * @param $newUserId int
  182. */
  183. function changeUser($oldUserId, $newUserId) {
  184. return $this->update(
  185. 'UPDATE email_log SET sender_id = ? WHERE sender_id = ?',
  186. array((int) $newUserId, (int) $oldUserId)
  187. );
  188. }
  189. /**
  190. * Get the ID of the last inserted log entry.
  191. * @return int
  192. */
  193. function getInsertLogId() {
  194. return $this->_getInsertId('email_log', 'log_id');
  195. }
  196. //
  197. // Private helper methods.
  198. //
  199. /**
  200. * Stores the correspondent user ids of the all recipient emails.
  201. * @param $entry EmailLogEntry
  202. */
  203. function _insertLogUserIds($entry) {
  204. $recipients = $entry->getRecipients();
  205. // We can use a simple regex to get emails, since we don't want to validate it.
  206. $pattern = '/(?<=\<)[^\>]*(?=\>)/';
  207. preg_match_all($pattern, $recipients, $matches);
  208. if (!isset($matches[0])) return;
  209. $userDao = DAORegistry::getDAO('UserDAO');
  210. foreach ($matches[0] as $emailAddress) {
  211. $user =& $userDao->getUserByEmail($emailAddress);
  212. if (is_a($user, 'User')) {
  213. // We use replace here to avoid inserting duplicated entries
  214. // in table (sometimes the recipients can have the same email twice).
  215. $this->replace('email_log_users',
  216. array('email_log_id' => $entry->getId(), 'user_id' => $user->getId()),
  217. array('email_log_id', 'user_id'));
  218. }
  219. }
  220. }
  221. }
  222. ?>