Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
EmailLogDAO.inc.php
1 <?php
2 
18 import ('lib.pkp.classes.log.EmailLogEntry');
19 
20 class EmailLogDAO extends DAO {
24  function EmailLogDAO() {
25  parent::DAO();
26  }
27 
35  function getById($logId, $assocType = null, $assocId = null) {
36  $params = array((int) $logId);
37  if (isset($assocType)) {
38  $params[] = (int) $assocType;
39  $params[] = (int) $assocId;
40  }
41 
42  $result = $this->retrieve(
43  'SELECT * FROM email_log WHERE log_id = ?' .
44  (isset($assocType)?' AND assoc_type = ? AND assoc_id = ?':''),
45  $params
46  );
47 
48  $returner = null;
49  if ($result->RecordCount() != 0) {
50  $returner =& $this->build($result->GetRowAssoc(false));
51  }
52 
53  $result->Close();
54  return $returner;
55  }
56 
66  function getByEventType($assocType, $assocId, $eventType, $userId = null, $rangeInfo = null) {
67  $params = array(
68  (int) $assocType,
69  (int) $assocId,
70  (int) $eventType);
71  if ($userId) $params[] = $userId;
72 
73  $result = $this->retrieveRange(
74  'SELECT e.*
75  FROM email_log e' .
76  ($userId ? ' LEFT JOIN email_log_users u ON e.log_id = u.email_log_id' : '') .
77  ' WHERE e.assoc_type = ? AND
78  e.assoc_id = ? AND
79  e.event_type = ?' .
80  ($userId ? ' AND u.user_id = ?' : ''),
81  $params,
82  $rangeInfo
83  );
84 
85  return new DAOResultFactory($result, $this, 'build');
86  }
87 
95  function getByAssoc($assocType = null, $assocId = null, $rangeInfo = null) {
96  $result = $this->retrieveRange(
97  'SELECT *
98  FROM email_log
99  WHERE assoc_type = ?
100  AND assoc_id = ?
101  ORDER BY log_id DESC',
102  array((int) $assocType, (int) $assocId),
103  $rangeInfo
104  );
105 
106  return new DAOResultFactory($result, $this, 'build');
107  }
108 
114  function &build($row) {
115  $entry = $this->newDataObject();
116  $entry->setId($row['log_id']);
117  $entry->setAssocType($row['assoc_type']);
118  $entry->setAssocId($row['assoc_id']);
119  $entry->setSenderId($row['sender_id']);
120  $entry->setDateSent($this->datetimeFromDB($row['date_sent']));
121  $entry->setIPAddress($row['ip_address']);
122  $entry->setEventType($row['event_type']);
123  $entry->setFrom($row['from_address']);
124  $entry->setRecipients($row['recipients']);
125  $entry->setCcs($row['cc_recipients']);
126  $entry->setBccs($row['bcc_recipients']);
127  $entry->setSubject($row['subject']);
128  $entry->setBody($row['body']);
129 
130  HookRegistry::call('EmailLogDAO::build', array(&$entry, &$row));
131 
132  return $entry;
133  }
134 
139  function insertObject(&$entry) {
140  $this->update(
141  sprintf('INSERT INTO email_log
142  (sender_id, date_sent, ip_address, event_type, assoc_type, assoc_id, from_address, recipients, cc_recipients, bcc_recipients, subject, body)
143  VALUES
144  (?, %s, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
145  $this->datetimeToDB($entry->getDateSent())),
146  array(
147  $entry->getSenderId(),
148  $entry->getIPAddress(),
149  $entry->getEventType(),
150  $entry->getAssocType(),
151  $entry->getAssocId(),
152  $entry->getFrom(),
153  $entry->getRecipients(),
154  $entry->getCcs(),
155  $entry->getBccs(),
156  $entry->getSubject(),
157  $entry->getBody()
158  )
159  );
160 
161  $entry->setId($this->getInsertId());
162  $this->_insertLogUserIds($entry);
163 
164  return $entry->getId();
165  }
166 
173  function deleteObject($logId, $assocType = null, $assocId = null) {
174  $params = array((int) $logId);
175  if (isset($assocType)) {
176  $params[] = (int) $assocType;
177  $params[] = (int) $assocId;
178  }
179  return $this->update(
180  'DELETE FROM email_log WHERE log_id = ?' .
181  (isset($assocType)?' AND assoc_type = ? AND assoc_id = ?':''),
182  $params
183  );
184  }
185 
191  function deleteByAssoc($assocType, $assocId) {
192  return $this->update(
193  'DELETE FROM email_log WHERE assoc_type = ? AND assoc_id = ?',
194  array((int) $assocType, (int) $assocId)
195  );
196  }
197 
203  function changeUser($oldUserId, $newUserId) {
204  return $this->update(
205  'UPDATE email_log SET sender_id = ? WHERE sender_id = ?',
206  array((int) $newUserId, (int) $oldUserId)
207  );
208  }
209 
214  function getInsertId() {
215  return $this->_getInsertId('email_log', 'log_id');
216  }
217 
218 
219  //
220  // Private helper methods.
221  //
226  function _insertLogUserIds($entry) {
227  $recipients = $entry->getRecipients();
228 
229  // We can use a simple regex to get emails, since we don't want to validate it.
230  $pattern = '/(?<=<)[^>]*(?=>)/';
231  preg_match_all($pattern, $recipients, $matches);
232  if (!isset($matches[0])) return;
233 
234  $userDao = DAORegistry::getDAO('UserDAO');
235  foreach ($matches[0] as $emailAddress) {
236  $user = $userDao->getUserByEmail($emailAddress);
237  if (is_a($user, 'User')) {
238  // We use replace here to avoid inserting duplicated entries
239  // in table (sometimes the recipients can have the same email twice).
240  $this->replace('email_log_users',
241  array('email_log_id' => $entry->getId(), 'user_id' => $user->getId()),
242  array('email_log_id', 'user_id'));
243  }
244  }
245  }
246 }
247 
248 ?>
static & getDAO($name, $dbconn=null)
getById($logId, $assocType=null, $assocId=null)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
getByEventType($assocType, $assocId, $eventType, $userId=null, $rangeInfo=null)
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
getByAssoc($assocType=null, $assocId=null, $rangeInfo=null)
datetimeFromDB($dt)
Definition: DAO.inc.php:313
insertObject(&$entry)
static call($hookName, $args=null)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
changeUser($oldUserId, $newUserId)
_insertLogUserIds($entry)
Class for inserting/accessing email log entries.
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:237
deleteObject($logId, $assocType=null, $assocId=null)
deleteByAssoc($assocType, $assocId)
datetimeToDB($dt)
Definition: DAO.inc.php:293
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208