1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
24: class IMP_Sentmail_Sql extends IMP_Sentmail
25: {
26: 27: 28: 29: 30:
31: protected $_db;
32:
33: 34: 35: 36: 37: 38:
39: public function __construct(array $params = array())
40: {
41: if (!isset($params['db'])) {
42: throw new IMP_Exception('Missing db parameter.');
43: }
44: $this->_db = $params['db'];
45: unset($params['db']);
46:
47: $params = array_merge(array(
48: 'table' => 'imp_sentmail'
49: ), $params);
50:
51: parent::__construct($params);
52: }
53:
54: 55:
56: protected function _log($action, $message_id, $recipient, $success)
57: {
58:
59: $query = sprintf('INSERT INTO %s (sentmail_who, sentmail_ts, sentmail_messageid, sentmail_action, sentmail_recipient, sentmail_success) VALUES (?, ?, ?, ?, ?, ?)', $this->_params['table']);
60: $values = array(
61: $GLOBALS['registry']->getAuth(),
62: time(),
63: $message_id,
64: $action,
65: $recipient,
66: intval($success)
67: );
68:
69:
70: try {
71: $this->_db->insert($query, $values);
72: } catch (Horde_Db_Exception $e) {}
73: }
74:
75: 76:
77: public function favouriteRecipients($limit, $filter = null)
78: {
79:
80: $where = '';
81: if (!empty($filter)) {
82: $filter = array_map(array($this->_db, 'quote'), $filter);
83: $where = sprintf(' AND sentmail_action in (%s)',
84: implode(', ', $filter));
85: }
86:
87: $query = sprintf('SELECT sentmail_recipient, count(*) AS sentmail_count FROM %s WHERE sentmail_who = %s AND sentmail_success = 1%s GROUP BY sentmail_recipient ORDER BY sentmail_count DESC',
88: $this->_params['table'],
89: $this->_db->quote($GLOBALS['registry']->getAuth()),
90: $where);
91:
92:
93: try {
94: $query = $this->_db->addLimitOffset($query, array('limit' => $limit));
95: return $this->_db->selectValues($query);
96: } catch (Horde_Db_Exception $e) {
97: return array();
98: }
99: }
100:
101: 102:
103: public function numberOfRecipients($hours, $user = false)
104: {
105:
106: $query = sprintf('SELECT COUNT(*) FROM %s WHERE sentmail_ts > ?',
107: $this->_params['table']);
108: if ($user) {
109: $query .= sprintf(' AND sentmail_who = %s', $this->_db->quote($GLOBALS['registry']->getAuth()));
110: }
111:
112:
113: try {
114: return $this->_db->selectValue($query, array(time() - $hours * 3600));
115: } catch (Horde_Db_Exception $e) {
116: return 0;
117: }
118: }
119:
120: 121:
122: protected function _deleteOldEntries($before)
123: {
124:
125: $query = sprintf('DELETE FROM %s WHERE sentmail_ts < ?',
126: $this->_params['table']);
127:
128:
129: try {
130: $this->_db->delete($query, array($before));
131: } catch (Horde_Db_Exception $e) {}
132: }
133:
134: }
135: