Open Monograph Press  3.3.0
EmailLogDAO.inc.php
1 <?php
2 
18 import ('lib.pkp.classes.log.EmailLogEntry');
19 
20 class EmailLogDAO extends DAO {
21 
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 
36  $result = $this->retrieve(
37  'SELECT * FROM email_log WHERE log_id = ?' .
38  (isset($assocType)?' AND assoc_type = ? AND assoc_id = ?':''),
39  $params
40  );
41 
42  $returner = null;
43  if ($result->RecordCount() != 0) {
44  $returner =& $this->build($result->GetRowAssoc(false));
45  }
46 
47  $result->Close();
48  return $returner;
49  }
50 
60  function _getByEventType($assocType, $assocId, $eventType, $userId = null, $rangeInfo = null) {
61  $params = array(
62  (int) $assocType,
63  (int) $assocId,
64  (int) $eventType);
65  if ($userId) $params[] = $userId;
66 
67  $result = $this->retrieveRange(
68  'SELECT e.*
69  FROM email_log e' .
70  ($userId ? ' LEFT JOIN email_log_users u ON e.log_id = u.email_log_id' : '') .
71  ' WHERE e.assoc_type = ? AND
72  e.assoc_id = ? AND
73  e.event_type = ?' .
74  ($userId ? ' AND u.user_id = ?' : ''),
75  $params,
76  $rangeInfo
77  );
78 
79  return new DAOResultFactory($result, $this, 'build');
80  }
81 
89  function getByAssoc($assocType = null, $assocId = null, $rangeInfo = null) {
90  $result = $this->retrieveRange(
91  'SELECT *
92  FROM email_log
93  WHERE assoc_type = ?
94  AND assoc_id = ?
95  ORDER BY log_id DESC',
96  array((int) $assocType, (int) $assocId),
97  $rangeInfo
98  );
99 
100  return new DAOResultFactory($result, $this, 'build');
101  }
102 
108  function &build($row) {
109  $entry = $this->newDataObject();
110  $entry->setId($row['log_id']);
111  $entry->setAssocType($row['assoc_type']);
112  $entry->setAssocId($row['assoc_id']);
113  $entry->setSenderId($row['sender_id']);
114  $entry->setDateSent($this->datetimeFromDB($row['date_sent']));
115  $entry->setEventType($row['event_type']);
116  $entry->setFrom($row['from_address']);
117  $entry->setRecipients($row['recipients']);
118  $entry->setCcs($row['cc_recipients']);
119  $entry->setBccs($row['bcc_recipients']);
120  $entry->setSubject($row['subject']);
121  $entry->setBody($row['body']);
122 
123  HookRegistry::call('EmailLogDAO::build', array(&$entry, &$row));
124 
125  return $entry;
126  }
127 
132  function insertObject(&$entry) {
133  $this->update(
134  sprintf('INSERT INTO email_log
135  (sender_id, date_sent, event_type, assoc_type, assoc_id, from_address, recipients, cc_recipients, bcc_recipients, subject, body)
136  VALUES
137  (?, %s, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
138  $this->datetimeToDB($entry->getDateSent())),
139  array(
140  $entry->getSenderId(),
141  $entry->getEventType(),
142  $entry->getAssocType(),
143  $entry->getAssocId(),
144  $entry->getFrom(),
145  $entry->getRecipients(),
146  $entry->getCcs(),
147  $entry->getBccs(),
148  $entry->getSubject(),
149  $entry->getBody()
150  )
151  );
152 
153  $entry->setId($this->getInsertId());
154  $this->_insertLogUserIds($entry);
155 
156  return $entry->getId();
157  }
158 
165  function deleteObject($logId, $assocType = null, $assocId = null) {
166  $params = array((int) $logId);
167  if (isset($assocType)) {
168  $params[] = (int) $assocType;
169  $params[] = (int) $assocId;
170  }
171  return $this->update(
172  'DELETE FROM email_log WHERE log_id = ?' .
173  (isset($assocType)?' AND assoc_type = ? AND assoc_id = ?':''),
174  $params
175  );
176  }
177 
183  function deleteByAssoc($assocType, $assocId) {
184  return $this->update(
185  'DELETE FROM email_log WHERE assoc_type = ? AND assoc_id = ?',
186  array((int) $assocType, (int) $assocId)
187  );
188  }
189 
195  function changeUser($oldUserId, $newUserId) {
196  return $this->update(
197  'UPDATE email_log SET sender_id = ? WHERE sender_id = ?',
198  array((int) $newUserId, (int) $oldUserId)
199  );
200  }
201 
206  function getInsertId() {
207  return $this->_getInsertId('email_log', 'log_id');
208  }
209 
210 
211  //
212  // Private helper methods.
213  //
218  function _insertLogUserIds($entry) {
219  $recipients = $entry->getRecipients();
220 
221  // We can use a simple regex to get emails, since we don't want to validate it.
222  $pattern = '/(?<=<)[^>]*(?=>)/';
223  preg_match_all($pattern, $recipients, $matches);
224  if (!isset($matches[0])) return;
225 
226  $userDao = DAORegistry::getDAO('UserDAO'); /* @var $userDao UserDAO */
227  foreach ($matches[0] as $emailAddress) {
228  $user = $userDao->getUserByEmail($emailAddress);
229  if (is_a($user, 'User')) {
230  // We use replace here to avoid inserting duplicated entries
231  // in table (sometimes the recipients can have the same email twice).
232  $this->replace('email_log_users',
233  array('email_log_id' => $entry->getId(), 'user_id' => $user->getId()),
234  array('email_log_id', 'user_id'));
235  }
236  }
237  }
238 }
239 
240 
DAOResultFactory
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
Definition: DAOResultFactory.inc.php:21
EmailLogDAO\getById
getById($logId, $assocType=null, $assocId=null)
Definition: EmailLogDAO.inc.php:29
DAO\retrieveRange
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
EmailLogDAO\build
& build($row)
Definition: EmailLogDAO.inc.php:108
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
EmailLogDAO\getByAssoc
getByAssoc($assocType=null, $assocId=null, $rangeInfo=null)
Definition: EmailLogDAO.inc.php:89
DAO\datetimeFromDB
datetimeFromDB($dt)
Definition: DAO.inc.php:319
EmailLogDAO\insertObject
insertObject(&$entry)
Definition: EmailLogDAO.inc.php:132
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
DAO\datetimeToDB
datetimeToDB($dt)
Definition: DAO.inc.php:299
DAO\_getInsertId
_getInsertId($table='', $id='')
Definition: DAO.inc.php:255
EmailLogDAO\_insertLogUserIds
_insertLogUserIds($entry)
Definition: EmailLogDAO.inc.php:218
EmailLogDAO\changeUser
changeUser($oldUserId, $newUserId)
Definition: EmailLogDAO.inc.php:195
DAO\replace
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:243
EmailLogDAO\_getByEventType
_getByEventType($assocType, $assocId, $eventType, $userId=null, $rangeInfo=null)
Definition: EmailLogDAO.inc.php:60
EmailLogDAO\deleteObject
deleteObject($logId, $assocType=null, $assocId=null)
Definition: EmailLogDAO.inc.php:165
EmailLogDAO\deleteByAssoc
deleteByAssoc($assocType, $assocId)
Definition: EmailLogDAO.inc.php:183
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
EmailLogDAO
Class for inserting/accessing email log entries.
Definition: EmailLogDAO.inc.php:20
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31
EmailLogDAO\getInsertId
getInsertId()
Definition: EmailLogDAO.inc.php:206