1: <?php
2: /**
3: * @package Alarm
4: *
5: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: */
10:
11: /**
12: * The Horde_Alarm_Object class is a Horde_Alarm storage implementation using
13: * an object instance.
14: *
15: * @author Jan Schneider <jan@horde.org>
16: * @package Alarm
17: */
18: class Horde_Alarm_Object extends Horde_Alarm
19: {
20: protected $_alarms = array();
21:
22: /**
23: * Returns a certain alarm.
24: *
25: * @param string $id The alarm's unique id.
26: * @param string $user The alarm's user.
27: *
28: * @return array An alarm hash.
29: */
30: protected function &_findAlarm($id, $user)
31: {
32: foreach ($this->_alarms as &$alarm) {
33: if ($alarm['id'] == $id && $alarm['user'] === $user) {
34: return $alarm;
35: }
36: }
37: $result = null;
38: return $result;
39: }
40:
41: /**
42: * Sorts a number of alarms in chronological order.
43: */
44: protected function _sortAlarms($a, $b)
45: {
46: $cmp = $a['start']->compareDateTime($b['start']);
47: if ($cmp) {
48: return $cmp;
49: }
50: if (empty($a['end'])) {
51: return -1;
52: }
53: if (empty($b['end'])) {
54: return 1;
55: }
56: return $a['end']->compareDateTime($b['end']);
57: }
58:
59: /**
60: * Returns a list of alarms from the backend.
61: *
62: * @param Horde_Date $time The time when the alarms should be active.
63: * @param string $user Return alarms for this user, all users if
64: * null, or global alarms if empty.
65: *
66: * @return array A list of alarm hashes.
67: * @throws Horde_Alarm_Exception
68: */
69: protected function _list($user, Horde_Date $time)
70: {
71: $alarms = array();
72: foreach ($this->_alarms as $alarm) {
73: if (empty($alarm['dismissed']) &&
74: ((empty($alarm['snooze']) && $alarm['start']->compareDateTime($time) <= 0) ||
75: $alarm['snooze']->compareDateTime($time) <= 0) &&
76: (empty($alarm['end']) || $alarm['end']->compareDateTime($time) >= 0) &&
77: (is_null($user) || empty($alarm['uid']) || $alarm['uid'] = $user)) {
78: $alarms[] = $alarm;
79: }
80: }
81: usort($alarms, array($this, '_sortAlarms'));
82: return $alarms;
83: }
84:
85: /**
86: * Returns a list of all global alarms from the backend.
87: *
88: * @return array A list of alarm hashes.
89: */
90: protected function _global()
91: {
92: return array();
93: }
94:
95: /**
96: * Returns an alarm hash from the backend.
97: *
98: * @param string $id The alarm's unique id.
99: * @param string $user The alarm's user.
100: *
101: * @return array An alarm hash.
102: * @throws Horde_Alarm_Exception
103: */
104: protected function _get($id, $user)
105: {
106: $alarm = $this->_findAlarm($id, $user);
107: if (!$alarm) {
108: throw new Horde_Alarm_Exception('Alarm not found');
109: }
110: return $alarm;
111: }
112:
113: /**
114: * Adds an alarm hash to the backend.
115: *
116: * @param array $alarm An alarm hash.
117: */
118: protected function _add(array $alarm)
119: {
120: $alarm = array_merge(
121: array('user' => '',
122: 'end' => null,
123: 'text' => null,
124: 'snooze' => null,
125: 'internal' => null),
126: $alarm);
127: $this->_alarms[] = $alarm;
128: }
129:
130: /**
131: * Updates an alarm hash in the backend.
132: *
133: * @param array $alarm An alarm hash.
134: * @param boolean $keepsnooze Whether to keep the snooze value unchanged.
135: */
136: protected function _update(array $alarm, $keepsnooze = false)
137: {
138: $user = isset($alarm['user']) ? $alarm['user'] : null;
139: $al = &$this->_findAlarm($alarm['id'], $user);
140: foreach (array('start', 'end', 'methods', 'params', 'title', 'text') as $property) {
141: $al[$property] = isset($alarm[$property]) ? $alarm[$property] : null;
142: }
143: if (!$keepsnooze) {
144: $al['snooze'] = null;
145: }
146: }
147:
148: /**
149: * Updates internal alarm properties, i.e. properties not determined by
150: * the application setting the alarm.
151: *
152: * @param string $id The alarm's unique id.
153: * @param string $user The alarm's user
154: * @param array $internal A hash with the internal data.
155: *
156: * @throws Horde_Alarm_Exception
157: */
158: public function internal($id, $user, array $internal)
159: {
160: $alarm = &$this->_findAlarm($id, $user);
161: $alarm['internal'] = $internal;
162: }
163:
164: /**
165: * Returns whether an alarm with the given id exists already.
166: *
167: * @param string $id The alarm's unique id.
168: * @param string $user The alarm's user
169: *
170: * @return boolean True if the specified alarm exists.
171: * @throws Horde_Alarm_Exception
172: */
173: protected function _exists($id, $user)
174: {
175: return (bool)$this->_findAlarm($id, $user);
176: }
177:
178: /**
179: * Delays (snoozes) an alarm for a certain period.
180: *
181: * @param string $id The alarm's unique id.
182: * @param string $user The alarm's user
183: * @param Horde_Date $snooze The snooze time.
184: *
185: * @throws Horde_Alarm_Exception
186: */
187: protected function _snooze($id, $user, Horde_Date $snooze)
188: {
189: $alarm = &$this->_findAlarm($id, $user);
190: $alarm['snooze'] = $snooze;
191: }
192:
193: /**
194: * Returns whether an alarm is snoozed.
195: *
196: * @param string $id The alarm's unique id.
197: * @param string $user The alarm's user
198: * @param Horde_Date $time The time when the alarm may be snoozed.
199: *
200: * @return boolean True if the alarm is snoozed.
201: * @throws Horde_Alarm_Exception
202: */
203: protected function _isSnoozed($id, $user, Horde_Date $time)
204: {
205: $alarm = $this->_findAlarm($id, $user);
206: return !empty($alarm['dismissed']) ||
207: (isset($alarm['snooze']) &&
208: $alarm['snooze']->compareDateTime($time) >= 0);
209: }
210:
211: /**
212: * Dismisses an alarm.
213: *
214: * @param string $id The alarm's unique id.
215: * @param string $user The alarm's user
216: *
217: * @throws Horde_Alarm_Exception
218: */
219: protected function _dismiss($id, $user)
220: {
221: $alarm = &$this->_findAlarm($id, $user);
222: $alarm['dismissed'] = true;
223: }
224:
225: /**
226: * Deletes an alarm from the backend.
227: *
228: * @param string $id The alarm's unique id.
229: * @param string $user The alarm's user. All users' alarms if null.
230: *
231: * @throws Horde_Alarm_Exception
232: */
233: protected function _delete($id, $user = null)
234: {
235: $newAlarms = array();
236: foreach ($this->_alarms as &$alarm) {
237: if ($alarm['id'] != $id ||
238: (!is_null($user) && $alarm['user'] != $user)) {
239: $newAlarms[] = $alarm;
240: }
241: }
242: $this->_alarms = $newAlarms;
243: }
244:
245: /**
246: * Garbage collects old alarms in the backend.
247: *
248: * @throws Horde_Alarm_Exception
249: */
250: protected function _gc()
251: {
252: }
253:
254: /**
255: * Attempts to initialize the backend.
256: *
257: * @throws Horde_Alarm_Exception
258: */
259: public function initialize()
260: {
261: }
262:
263: /**
264: * Converts a value from the driver's charset.
265: *
266: * @param mixed $value Value to convert.
267: *
268: * @return mixed Converted value.
269: */
270: protected function _fromDriver($value)
271: {
272: return $value;
273: }
274:
275: /**
276: * Converts a value to the driver's charset.
277: *
278: * @param mixed $value Value to convert.
279: *
280: * @return mixed Converted value.
281: */
282: protected function _toDriver($value)
283: {
284: return $value;
285: }
286:
287: }
288: