Open Journal Systems  2.4.4
 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  unset($result);
55 
56  return $returner;
57  }
58 
68  function &getByEventType($assocType, $assocId, $eventType, $userId = null, $rangeInfo = null) {
69  $params = array(
70  (int) $assocType,
71  (int) $assocId,
72  (int) $eventType);
73  if ($userId) $params[] = $userId;
74 
75  $result =& $this->retrieveRange(
76  'SELECT e.*
77  FROM email_log e' .
78  ($userId ? ' LEFT JOIN email_log_users u ON e.log_id = u.email_log_id' : '') .
79  ' WHERE e.assoc_type = ? AND
80  e.assoc_id = ? AND
81  e.event_type = ?' .
82  ($userId ? ' AND u.user_id = ?' : ''),
83  $params,
84  $rangeInfo
85  );
86 
87  $returner = new DAOResultFactory($result, $this, 'build');
88  return $returner;
89  }
90 
98  function &getByAssoc($assocType = null, $assocId = null, $rangeInfo = null) {
99  $result =& $this->retrieveRange(
100  'SELECT *
101  FROM email_log
102  WHERE assoc_type = ?
103  AND assoc_id = ?
104  ORDER BY log_id DESC',
105  array((int) $assocType, (int) $assocId),
106  $rangeInfo
107  );
108 
109  $returner = new DAOResultFactory($result, $this, 'build');
110  return $returner;
111  }
112 
118  function &build(&$row) {
119  $entry = $this->newDataObject();
120  $entry->setId($row['log_id']);
121  $entry->setAssocType($row['assoc_type']);
122  $entry->setAssocId($row['assoc_id']);
123  $entry->setSenderId($row['sender_id']);
124  $entry->setDateSent($this->datetimeFromDB($row['date_sent']));
125  $entry->setIPAddress($row['ip_address']);
126  $entry->setEventType($row['event_type']);
127  $entry->setFrom($row['from_address']);
128  $entry->setRecipients($row['recipients']);
129  $entry->setCcs($row['cc_recipients']);
130  $entry->setBccs($row['bcc_recipients']);
131  $entry->setSubject($row['subject']);
132  $entry->setBody($row['body']);
133 
134  HookRegistry::call('EmailLogDAO::build', array(&$entry, &$row));
135 
136  return $entry;
137  }
138 
143  function insertObject(&$entry) {
144  $this->update(
145  sprintf('INSERT INTO email_log
146  (sender_id, date_sent, ip_address, event_type, assoc_type, assoc_id, from_address, recipients, cc_recipients, bcc_recipients, subject, body)
147  VALUES
148  (?, %s, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
149  $this->datetimeToDB($entry->getDateSent())),
150  array(
151  $entry->getSenderId(),
152  $entry->getIPAddress(),
153  $entry->getEventType(),
154  $entry->getAssocType(),
155  $entry->getAssocId(),
156  $entry->getFrom(),
157  $entry->getRecipients(),
158  $entry->getCcs(),
159  $entry->getBccs(),
160  $entry->getSubject(),
161  $entry->getBody()
162  )
163  );
164 
165  $entry->setId($this->getInsertLogId());
166  $this->_insertLogUserIds($entry);
167 
168  return $entry->getId();
169  }
170 
177  function deleteObject($logId, $assocType = null, $assocId = null) {
178  $params = array((int) $logId);
179  if (isset($assocType)) {
180  $params[] = (int) $assocType;
181  $params[] = (int) $assocId;
182  }
183  return $this->update(
184  'DELETE FROM email_log WHERE log_id = ?' .
185  (isset($assocType)?' AND assoc_type = ? AND assoc_id = ?':''),
186  $params
187  );
188  }
189 
195  function deleteByAssoc($assocType, $assocId) {
196  return $this->update(
197  'DELETE FROM email_log WHERE assoc_type = ? AND assoc_id = ?',
198  array((int) $assocType, (int) $assocId)
199  );
200  }
201 
207  function changeUser($oldUserId, $newUserId) {
208  return $this->update(
209  'UPDATE email_log SET sender_id = ? WHERE sender_id = ?',
210  array((int) $newUserId, (int) $oldUserId)
211  );
212  }
213 
218  function getInsertLogId() {
219  return $this->getInsertId('email_log', 'log_id');
220  }
221 
222 
223  //
224  // Private helper methods.
225  //
230  function _insertLogUserIds($entry) {
231  $recipients = $entry->getRecipients();
232 
233  // We can use a simple regex to get emails, since we don't want to validate it.
234  $pattern = '/(?<=<)[^>]*(?=>)/';
235  preg_match_all($pattern, $recipients, $matches);
236  if (!isset($matches[0])) return;
237 
238  $userDao = DAORegistry::getDAO('UserDAO');
239  foreach ($matches[0] as $emailAddress) {
240  $user =& $userDao->getUserByEmail($emailAddress);
241  if (is_a($user, 'User')) {
242  // We use replace here to avoid inserting duplicated entries
243  // in table (sometimes the recipients can have the same email twice).
244  $this->replace('email_log_users',
245  array('email_log_id' => $entry->getId(), 'user_id' => $user->getId()),
246  array('email_log_id', 'user_id'));
247  }
248  }
249  }
250 }
251 
252 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& getByAssoc($assocType=null, $assocId=null, $rangeInfo=null)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
getInsertId($table= '', $id= '', $callHooks=true)
Definition: DAO.inc.php:252
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
datetimeFromDB($dt)
Definition: DAO.inc.php:316
insertObject(&$entry)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
& getDAO($name, $dbconn=null)
changeUser($oldUserId, $newUserId)
call($hookName, $args=null)
_insertLogUserIds($entry)
Class for inserting/accessing email log entries.
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:240
deleteObject($logId, $assocType=null, $assocId=null)
deleteByAssoc($assocType, $assocId)
datetimeToDB($dt)
Definition: DAO.inc.php:296
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
& getById($logId, $assocType=null, $assocId=null)
& getByEventType($assocType, $assocId, $eventType, $userId=null, $rangeInfo=null)