1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21: class Horde_Alarm_Sql extends Horde_Alarm
22: {
23: 24: 25: 26: 27:
28: protected $_db;
29:
30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:
42: public function __construct(array $params = array())
43: {
44: if (!isset($params['db'])) {
45: throw new Horde_Alarm_Exception('Missing db parameter.');
46: }
47: $this->_db = $params['db'];
48: unset($params['db']);
49:
50: $params = array_merge(array(
51: 'table' => 'horde_alarms'
52: ), $params);
53:
54: parent::__construct($params);
55: }
56:
57: 58: 59: 60: 61: 62: 63: 64: 65: 66:
67: protected function _list($user, Horde_Date $time)
68: {
69: $query = sprintf('SELECT alarm_id, alarm_uid, alarm_start, alarm_end, alarm_methods, alarm_params, alarm_title, alarm_text, alarm_snooze, alarm_internal FROM %s WHERE alarm_dismissed = 0 AND ((alarm_snooze IS NULL AND alarm_start <= ?) OR alarm_snooze <= ?) AND (alarm_end IS NULL OR alarm_end >= ?)%s ORDER BY alarm_start, alarm_end',
70: $this->_params['table'],
71: is_null($user) ? '' : ' AND (alarm_uid IS NULL OR alarm_uid = ? OR alarm_uid = ?)');
72: $dt = $time->setTimezone('UTC')->format('Y-m-d\TH:i:s');
73: $values = array($dt, $dt, $dt);
74: if (!is_null($user)) {
75: $values[] = '';
76: $values[] = (string)$user;
77: }
78:
79:
80: try {
81: $result = $this->_db->selectAll($query, $values);
82: } catch (Horde_Db_Exception $e) {
83: throw new Horde_Alarm_Exception($e);
84: }
85:
86: $alarms = array();
87: foreach ($result as $val) {
88: $alarms[] = $this->_getHash($val);
89: }
90:
91: return $alarms;
92: }
93:
94: 95: 96: 97: 98: 99:
100: protected function _global()
101: {
102: $query = sprintf('SELECT alarm_id, alarm_uid, alarm_start, alarm_end, alarm_methods, alarm_params, alarm_title, alarm_text, alarm_snooze, alarm_internal FROM %s WHERE alarm_uid IS NULL OR alarm_uid = \'\' ORDER BY alarm_start, alarm_end',
103: $this->_params['table']);
104:
105: try {
106: $result = $this->_db->selectAll($query);
107: } catch (Horde_Db_Exception $e) {
108: throw new Horde_Alarm_Exception($e);
109: }
110:
111: $alarms = array();
112: foreach ($result as $val) {
113: $alarms[] = $this->_getHash($val);
114: }
115:
116: return $alarms;
117: }
118:
119: 120:
121: protected function _getHash(array $alarm)
122: {
123: $params = base64_decode($alarm['alarm_params']);
124: if (!strlen($params) && strlen($alarm['alarm_params'])) {
125: $params = $alarm['alarm_params'];
126: }
127: return array(
128: 'id' => $alarm['alarm_id'],
129: 'user' => $alarm['alarm_uid'],
130: 'start' => new Horde_Date($alarm['alarm_start'], 'UTC'),
131: 'end' => empty($alarm['alarm_end']) ? null : new Horde_Date($alarm['alarm_end'], 'UTC'),
132: 'methods' => @unserialize($alarm['alarm_methods']),
133: 'params' => @unserialize($params),
134: 'title' => $this->_fromDriver($alarm['alarm_title']),
135: 'text' => $this->_fromDriver($alarm['alarm_text']),
136: 'snooze' => empty($alarm['alarm_snooze']) ? null : new Horde_Date($alarm['alarm_snooze'], 'UTC'),
137: 'internal' => empty($alarm['alarm_internal']) ? null : @unserialize($alarm['alarm_internal'])
138: );
139: }
140:
141: 142: 143: 144: 145: 146: 147: 148: 149:
150: protected function _get($id, $user)
151: {
152: $query = sprintf('SELECT alarm_id, alarm_uid, alarm_start, alarm_end, alarm_methods, alarm_params, alarm_title, alarm_text, alarm_snooze, alarm_internal FROM %s WHERE alarm_id = ? AND %s',
153: $this->_params['table'],
154: !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
155:
156: try {
157: $alarm = $this->_db->selectOne($query, array($id, $user));
158: } catch (Horde_Db_Exception $e) {
159: throw new Horde_Alarm_Exception($e);
160: }
161:
162: if (empty($alarm)) {
163: throw new Horde_Alarm_Exception('Alarm not found');
164: }
165:
166: return $this->_getHash($alarm);
167: }
168:
169: 170: 171: 172: 173: 174: 175:
176: protected function _add(array $alarm)
177: {
178: $query = sprintf('INSERT INTO %s (alarm_id, alarm_uid, alarm_start, alarm_end, alarm_methods, alarm_params, alarm_title, alarm_text, alarm_snooze) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', $this->_params['table']);
179: $values = array(
180: $alarm['id'],
181: isset($alarm['user']) ? $alarm['user'] : '',
182: (string)$alarm['start']->setTimezone('UTC'),
183: empty($alarm['end']) ? null : (string)$alarm['end']->setTimezone('UTC'),
184: serialize($alarm['methods']),
185: base64_encode(serialize($alarm['params'])),
186: $this->_toDriver($alarm['title']),
187: empty($alarm['text']) ? null : $this->_toDriver($alarm['text']),
188: null
189: );
190:
191: try {
192: $this->_db->insert($query, $values);
193: } catch (Horde_Db_Exception $e) {
194: throw new Horde_Alarm_Exception($e);
195: }
196: }
197:
198: 199: 200: 201: 202: 203: 204: 205:
206: protected function _update(array $alarm, $keepsnooze = false)
207: {
208: $query = sprintf('UPDATE %s set alarm_start = ?, alarm_end = ?, alarm_methods = ?, alarm_params = ?, alarm_title = ?, alarm_text = ?%s WHERE alarm_id = ? AND %s',
209: $this->_params['table'],
210: $keepsnooze ? '' : ', alarm_snooze = NULL, alarm_dismissed = 0',
211: isset($alarm['user']) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
212: $values = array((string)$alarm['start']->setTimezone('UTC'),
213: empty($alarm['end']) ? null : (string)$alarm['end']->setTimezone('UTC'),
214: serialize($alarm['methods']),
215: base64_encode(serialize($alarm['params'])),
216: $this->_toDriver($alarm['title']),
217: empty($alarm['text'])
218: ? null
219: : $this->_toDriver($alarm['text']),
220: $alarm['id'],
221: isset($alarm['user']) ? $alarm['user'] : '');
222:
223: try {
224: $this->_db->update($query, $values);
225: } catch (Horde_Db_Exception $e) {
226: throw new Horde_Alarm_Exception($e);
227: }
228: }
229:
230: 231: 232: 233: 234: 235: 236: 237: 238: 239:
240: public function internal($id, $user, array $internal)
241: {
242: $query = sprintf('UPDATE %s set alarm_internal = ? WHERE alarm_id = ? AND %s',
243: $this->_params['table'],
244: !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
245: $values = array(serialize($internal), $id, $user);
246:
247: try {
248: $this->_db->update($query, $values);
249: } catch (Horde_Db_Exception $e) {
250: throw new Horde_Alarm_Exception($e);
251: }
252: }
253:
254: 255: 256: 257: 258: 259: 260: 261: 262:
263: protected function _exists($id, $user)
264: {
265: $query = sprintf('SELECT 1 FROM %s WHERE alarm_id = ? AND %s',
266: $this->_params['table'],
267: !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
268:
269: try {
270: return ($this->_db->selectValue($query, array($id, $user)) == 1);
271: } catch (Horde_Db_Exception $e) {
272: throw new Horde_Alarm_Exception($e);
273: }
274: }
275:
276: 277: 278: 279: 280: 281: 282: 283: 284:
285: protected function _snooze($id, $user, Horde_Date $snooze)
286: {
287: $query = sprintf('UPDATE %s set alarm_snooze = ? WHERE alarm_id = ? AND %s',
288: $this->_params['table'],
289: !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
290: $values = array((string)$snooze->setTimezone('UTC'), $id, $user);
291:
292: try {
293: $this->_db->update($query, $values);
294: } catch (Horde_Db_Exception $e) {
295: throw new Horde_Alarm_Exception($e);
296: }
297: }
298:
299: 300: 301: 302: 303: 304: 305: 306: 307: 308:
309: protected function _isSnoozed($id, $user, Horde_Date $time)
310: {
311: $query = sprintf('SELECT 1 FROM %s WHERE alarm_id = ? AND %s AND (alarm_dismissed = 1 OR (alarm_snooze IS NOT NULL AND alarm_snooze >= ?))',
312: $this->_params['table'],
313: !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
314:
315: try {
316: return $this->_db->selectValue($query, array($id, $user, (string)$time->setTimezone('UTC')));
317: } catch (Horde_Db_Exception $e) {
318: throw new Horde_Alarm_Exception($e);
319: }
320: }
321:
322: 323: 324: 325: 326: 327: 328: 329:
330: protected function _dismiss($id, $user)
331: {
332: $query = sprintf('UPDATE %s set alarm_dismissed = 1 WHERE alarm_id = ? AND %s',
333: $this->_params['table'],
334: !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
335: $values = array($id, $user);
336:
337: try {
338: $this->_db->update($query, $values);
339: } catch (Horde_Db_Exception $e) {
340: throw new Horde_Alarm_Exception($e);
341: }
342: }
343:
344: 345: 346: 347: 348: 349: 350: 351:
352: protected function _delete($id, $user = null)
353: {
354: $query = sprintf('DELETE FROM %s WHERE alarm_id = ?', $this->_params['table']);
355: $values = array($id);
356: if (!is_null($user)) {
357: $query .= empty($user)
358: ? ' AND (alarm_uid IS NULL OR alarm_uid = ?)'
359: : ' AND alarm_uid = ?';
360: $values[] = $user;
361: }
362:
363: try {
364: $this->_db->delete($query, $values);
365: } catch (Horde_Db_Exception $e) {
366: throw new Horde_Alarm_Exception($e);
367: }
368: }
369:
370: 371: 372:
373: protected function _gc()
374: {
375: $query = sprintf('DELETE FROM %s WHERE alarm_end IS NOT NULL AND alarm_end < ?', $this->_params['table']);
376: $end = new Horde_Date(time());
377: $this->_db->delete($query, array((string)$end->setTimezone('UTC')));
378: }
379:
380: 381: 382: 383: 384:
385: public function initialize()
386: {
387:
388: switch ($this->_db->adapterName()) {
389: case 'PDO_Oci':
390: $query = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'";
391: $db->execute($query);
392: break;
393:
394: case 'PDO_PostrgreSQL':
395: $query = "SET datestyle TO 'iso'";
396: $db->execute($query);
397: break;
398: }
399: }
400:
401: 402: 403: 404: 405: 406: 407:
408: protected function _fromDriver($value)
409: {
410: return Horde_String::convertCharset($value, $this->_params['charset'], 'UTF-8');
411: }
412:
413: 414: 415: 416: 417: 418: 419:
420: protected function _toDriver($value)
421: {
422: return Horde_String::convertCharset($value, 'UTF-8', $this->_params['charset']);
423: }
424:
425: }
426: