From a07b57e1c5c7c951321a6c89f06f2682d5cb08c6 Mon Sep 17 00:00:00 2001 From: Michael Slusarz Date: Fri, 12 Feb 2010 14:59:04 -0700 Subject: [PATCH] Convert Horde_Alarm to Horde 4 --- framework/Alarm/{ => lib/Horde}/Alarm.php | 407 ++++++++++----------- framework/Alarm/lib/Horde/Alarm/Exception.php | 16 + .../{Alarm/sql.php => lib/Horde/Alarm/Sql.php} | 246 +++++++------ framework/Alarm/package.xml | 63 +++- .../Alarm/{tests => test/Horde/Alarm}/001.phpt | 2 +- .../{tests => test/Horde/Alarm}/setup.inc.dist | 0 horde/config/conf.xml | 2 +- 7 files changed, 395 insertions(+), 341 deletions(-) rename framework/Alarm/{ => lib/Horde}/Alarm.php (67%) create mode 100644 framework/Alarm/lib/Horde/Alarm/Exception.php rename framework/Alarm/{Alarm/sql.php => lib/Horde/Alarm/Sql.php} (74%) rename framework/Alarm/{tests => test/Horde/Alarm}/001.phpt (99%) rename framework/Alarm/{tests => test/Horde/Alarm}/setup.inc.dist (100%) diff --git a/framework/Alarm/Alarm.php b/framework/Alarm/lib/Horde/Alarm.php similarity index 67% rename from framework/Alarm/Alarm.php rename to framework/Alarm/lib/Horde/Alarm.php index 8d9106bf9..99f8f66d9 100644 --- a/framework/Alarm/Alarm.php +++ b/framework/Alarm/lib/Horde/Alarm.php @@ -12,51 +12,65 @@ * The Horde_Alarm:: class provides an interface to deal with reminders, * alarms and notifications through a standardized API. * - * Alarm hashes have the following fields: - * - id: Unique alarm id. - * - user: The alarm's user. Empty if a global alarm. - * - start: The alarm start as a Horde_Date. - * - end: The alarm end as a Horde_Date. - * - methods: The notification methods for this alarm. - * - params: The paramters for the notification methods. - * - title: The alarm title. - * - text: An optional alarm description. - * - snooze: The snooze time (next time) of the alarm as a Horde_Date. - * - internal: Holds internally used data. - * * @author Jan Schneider * @package Horde_Alarm */ -class Horde_Alarm { - +class Horde_Alarm +{ /** * Hash containing connection parameters. * * @var array */ - var $_params = array('ttl' => 300); + protected $_params = array('ttl' => 300); /** - * An error message to throw when something is wrong. + * Attempts to return a concrete instance based on $driver. * - * @var string + * @param string $driver The type of concrete subclass to + * return. The class name is based on the storage + * driver ($driver). The code is dynamically + * included. + * @param array $params A hash containing any additional configuration + * or connection parameters a subclass might need. + * + * @return Horde_Alarm The newly created concrete instance. + * @throws Horde_Alarm_Exception */ - var $_errormsg; + static public function factory($driver = null, $params = null) + { + if (is_null($driver)) { + $driver = empty($GLOBALS['conf']['alarms']['driver']) + ? 'sql' + : $GLOBALS['conf']['alarms']['driver']; + } + + $driver = ucfirst(basename($driver)); + + if (is_null($params)) { + $params = Horde::getDriverConfig('alarms', $driver); + } + + $class = __CLASS__ . '_' . $driver; + + if (class_exists($class)) { + $alarm = new $class($params); + $alarm->initialize(); + $alarm->gc(); + return $alarm; + } + + throw new Horde_Alarm_Exception('Could not find driver.'); + } /** - * Constructor - just store the $params in our newly-created object. All - * other work is done by initialize(). + * Constructor. * * @param array $params Any parameters needed for this driver. */ - function Horde_Alarm($params = array(), $errormsg = null) + public function __construct($params = array()) { $this->_params = array_merge($this->_params, $params); - if ($errormsg === null) { - $this->_errormsg = _("The alarm backend is not currently available."); - } else { - $this->_errormsg = $errormsg; - } } /** @@ -65,37 +79,71 @@ class Horde_Alarm { * @param string $id The alarm's unique id. * @param string $user The alarm's user * - * @return array An alarm hash. + * @return array An alarm hash. Contains the following: + *
+     * id: Unique alarm id.
+     * user: The alarm's user. Empty if a global alarm.
+     * start: The alarm start as a Horde_Date.
+     * end: The alarm end as a Horde_Date.
+     * methods: The notification methods for this alarm.
+     * params: The paramters for the notification methods.
+     * title: The alarm title.
+     * text: An optional alarm description.
+     * snooze: The snooze time (next time) of the alarm as a Horde_Date.
+     * internal: Holds internally used data.
+     * 
+ * @throws Horde_Alarm_Exception */ - function get($id, $user) + public function get($id, $user) { $alarm = $this->_get($id, $user); - if (is_a($alarm, 'PEAR_Error')) { - return $alarm; - } + if (isset($alarm['mail']['body'])) { $alarm['mail']['body'] = $this->_fromDriver($alarm['mail']['body']); } + return $alarm; } /** + * @throws new Horde_Alarm_Exception + */ + protected function _get() + { + } + + /** * Stores an alarm hash in the backend. * * The alarm will be added if it doesn't exist, and updated otherwise. * - * @param array $alarm An alarm hash. + * @param array $alarm An alarm hash. See self::get() for format. + * + * @return TODO */ - function set($alarm) + public function set($alarm) { if (isset($alarm['mail']['body'])) { $alarm['mail']['body'] = $this->_toDriver($alarm['mail']['body']); } - if ($this->exists($alarm['id'], isset($alarm['user']) ? $alarm['user'] : '')) { - return $this->_update($alarm); - } else { - return $this->_add($alarm); - } + + return $this->exists($alarm['id'], isset($alarm['user']) ? $alarm['user'] : '') + ? $this->_update($alarm) + : $this->_add($alarm); + } + + /** + * @throws new Horde_Alarm_Exception + */ + protected function _update() + { + } + + /** + * @throws new Horde_Alarm_Exception + */ + protected function _add() + { } /** @@ -106,10 +154,20 @@ class Horde_Alarm { * * @return boolean True if the specified alarm exists. */ - function exists($id, $user) + public function exists($id, $user) + { + try { + return $this->_exists($id, $user); + } catch (Horde_Alarm_Exception $e) { + return false; + } + } + + /** + * @throws Horde_Alarm_Exception + */ + protected function _exists() { - $exists = $this->_exists($id, $user); - return $exists && !is_a($exists, 'PEAR_Error'); } /** @@ -119,28 +177,44 @@ class Horde_Alarm { * @param string $user The notified user. * @param integer $minutes The delay in minutes. A negative value * dismisses the alarm completely. + * + * @return TODO + * @throws Horde_Alarm_Exception */ - function snooze($id, $user, $minutes) + public function snooze($id, $user, $minutes) { - $alarm = $this->get($id, $user); - if (is_a($alarm, 'PEAR_Error')) { - return $alarm; - } if (empty($user)) { - return PEAR::raiseError(_("This alarm cannot be snoozed.")); + throw new Horde_Alarm_Exception('This alarm cannot be snoozed.'); } + + $alarm = $this->get($id, $user); + if ($alarm) { if ($minutes > 0) { $alarm['snooze'] = new Horde_Date(time()); $alarm['snooze']->min += $minutes; return $this->_snooze($id, $user, $alarm['snooze']); - } else { - return $this->_dismiss($id, $user); } + + return $this->_dismiss($id, $user); } } /** + * @throws new Horde_Alarm_Exception + */ + protected function _snooze() + { + } + + /** + * @throws new Horde_Alarm_Exception + */ + protected function _dismiss() + { + } + + /** * Returns whether an alarm is snoozed. * * @param string $id The alarm's unique id. @@ -150,7 +224,7 @@ class Horde_Alarm { * * @return boolean True if the alarm is snoozed. */ - function isSnoozed($id, $user, $time = null) + public function isSnoozed($id, $user, $time = null) { if (is_null($time)) { $time = new Horde_Date(time()); @@ -159,6 +233,13 @@ class Horde_Alarm { } /** + * @throws new Horde_Alarm_Exception + */ + protected function _isSnoozed() + { + } + + /** * Deletes an alarm from the backend. * * @param string $id The alarm's unique id. @@ -170,6 +251,13 @@ class Horde_Alarm { } /** + * @throws new Horde_Alarm_Exception + */ + protected function _delete() + { + } + + /** * Retrieves active alarms from all applications and stores them in the * backend. * @@ -181,10 +269,10 @@ class Horde_Alarm { * @param boolean $preload Preload alarms that go off within the next * ttl time span? */ - function load($user = null, $preload = true) + public function load($user = null, $preload = true) { if (isset($_SESSION['horde']['alarm']['loaded']) && - time() - $_SESSION['horde']['alarm']['loaded'] < $this->_params['ttl']) { + (time() - $_SESSION['horde']['alarm']['loaded']) < $this->_params['ttl']) { return; } @@ -237,9 +325,10 @@ class Horde_Alarm { * ttl time span? * * @return array A list of alarm hashes. + * @throws Horde_Alarm_Exception */ - function listAlarms($user = null, $time = null, $load = false, - $preload = true) + public function listAlarms($user = null, $time = null, $load = false, + $preload = true) { if (empty($time)) { $time = new Horde_Date(time()); @@ -249,9 +338,6 @@ class Horde_Alarm { } $alarms = $this->_list($user, $time); - if (is_a($alarms, 'PEAR_Error')) { - return $alarms; - } foreach (array_keys($alarms) as $alarm) { if (isset($alarms[$alarm]['mail']['body'])) { @@ -262,6 +348,13 @@ class Horde_Alarm { } /** + * @throws new Horde_Alarm_Exception + */ + protected function _list() + { + } + + /** * Notifies the user about any active alarms. * * @param string $user Notify this user, all users if null, or guest @@ -270,15 +363,19 @@ class Horde_Alarm { * @param boolean $preload Preload alarms that go off within the next * ttl time span? * @param array $exclude Don't notify with these methods. + * + * @throws Horde_Alarm_Exception */ - function notify($user = null, $load = true, $preload = true, - $exclude = array()) + public function notify($user = null, $load = true, $preload = true, + $exclude = array()) { - $alarms = $this->listAlarms($user, null, $load, $preload); - if (is_a($alarms, 'PEAR_Error')) { - Horde::logMessage($alarms, __FILE__, __LINE__, PEAR_LOG_ERR); - return $alarms; + try { + $alarms = $this->listAlarms($user, null, $load, $preload); + } catch (Horde_Alarm_Exception $e) { + Horde::logMessage($e, __FILE__, __LINE__, PEAR_LOG_ERR); + throw $e; } + if (empty($alarms)) { return; } @@ -288,9 +385,10 @@ class Horde_Alarm { foreach ($alarm['methods'] as $alarm_method) { if (in_array($alarm_method, $methods) && !in_array($alarm_method, $exclude)) { - $result = $this->{'_' . $alarm_method}($alarm); - if (is_a($result, 'PEAR_Error')) { - Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); + try { + $result = $this->{'_' . $alarm_method}($alarm); + } catch (Horde_Alarm_Exception $e) { + Horde::logMessage($e, __FILE__, __LINE__, PEAR_LOG_ERR); } } } @@ -302,14 +400,13 @@ class Horde_Alarm { * * @param array $alarm An alarm hash. */ - function _notify($alarm) + protected function _notify($alarm) { static $sound_played; $GLOBALS['notification']->push($alarm['title'], 'horde.alarm', array('alarm' => $alarm)); if (!empty($alarm['params']['notify']['sound']) && !isset($sound_played[$alarm['params']['notify']['sound']])) { - require_once 'Horde/Notification/Listener/Audio.php'; $GLOBALS['notification']->attach('audio'); $GLOBALS['notification']->push($alarm['params']['notify']['sound'], 'audio'); $sound_played[$alarm['params']['notify']['sound']] = true; @@ -320,8 +417,10 @@ class Horde_Alarm { * Notifies about an alarm by email. * * @param array $alarm An alarm hash. + * + * @throws Horde_Mime_Exception */ - function _mail($alarm) + protected function _mail($alarm) { if (!empty($alarm['internal']['mail']['sent'])) { return; @@ -342,24 +441,29 @@ class Horde_Alarm { 'body' => empty($alarm['params']['mail']['body']) ? $alarm['text'] : $alarm['params']['mail']['body'], 'to' => $email, 'from' => $email, - 'charset' => Horde_Nls::getCharset())); + 'charset' => Horde_Nls::getCharset() + )); $mail->addHeader('Auto-Submitted', 'auto-generated'); $mail->addHeader('X-Horde-Alarm', $alarm['title'], Horde_Nls::getCharset()); $sent = $mail->send(Horde::getMailerConfig()); - if (is_a($sent, 'PEAR_Error')) { - return $sent; - } $alarm['internal']['mail']['sent'] = true; $this->_internal($alarm['id'], $alarm['user'], $alarm['internal']); } /** + * @throws new Horde_Alarm_Exception + */ + protected function _internal() + { + } + + /** * Notifies about an alarm with an SMS through the sms/send API method. * * @param array $alarm An alarm hash. */ - function _sms($alarm) + protected function _sms($alarm) { } @@ -375,21 +479,29 @@ class Horde_Alarm { * * @return array List of methods and parameters. */ - function notificationMethods() + public function notificationMethods() { static $methods; if (!isset($methods)) { - $methods = array('notify' => array( - '__desc' => _("Inline Notification"), - 'sound' => array('type' => 'sound', - 'desc' => _("Play a sound?"), - 'required' => false)), - 'mail' => array( - '__desc' => _("Email Notification"), - 'email' => array('type' => 'text', - 'desc' => _("Email address (optional)"), - 'required' => false))); + $methods = array( + 'notify' => array( + '__desc' => _("Inline Notification"), + 'sound' => array( + 'type' => 'sound', + 'desc' => _("Play a sound?"), + 'required' => false + ) + ), + 'mail' => array( + '__desc' => _("Email Notification"), + 'email' => array( + 'type' => 'text', + 'desc' => _("Email address (optional)"), + 'required' => false + ) + ) + ); /* if ($GLOBALS['registry']->hasMethod('sms/send')) { $methods['sms'] = array( @@ -406,55 +518,12 @@ class Horde_Alarm { /** * Garbage collects old alarms in the backend. */ - function gc() + public function gc() { /* A 1% chance we will run garbage collection during a call. */ - if (rand(0, 99) != 0) { - return; - } - - return $this->_gc(); - } - - /** - * Attempts to return a concrete Horde_Alarm instance based on $driver. - * - * @param string $driver The type of concrete Horde_Alarm subclass to - * return. The class name is based on the storage - * driver ($driver). The code is dynamically - * included. - * @param array $params A hash containing any additional configuration - * or connection parameters a subclass might need. - * - * @return mixed The newly created concrete Horde_Alarm instance, or false - * on an error. - */ - static function factory($driver = null, $params = null) - { - if (is_null($driver)) { - $driver = empty($GLOBALS['conf']['alarms']['driver']) ? 'sql' : $GLOBALS['conf']['alarms']['driver']; - } - - $driver = basename($driver); - - if (is_null($params)) { - $params = Horde::getDriverConfig('alarms', $driver); + if (rand(0, 99) == 0) { + return $this->_gc(); } - - $class = 'Horde_Alarm_' . $driver; - if (class_exists($class)) { - $alarm = new $class($params); - $result = $alarm->initialize(); - if (is_a($result, 'PEAR_Error')) { - $alarm = new Horde_Alarm($params, sprintf(_("The alarm backend is not currently available: %s"), $result->getMessage())); - } else { - $alarm->gc(); - } - } else { - $alarm = new Horde_Alarm($params, sprintf(_("Unable to load the definition of %s."), $class)); - } - - return $alarm; } /** @@ -464,7 +533,7 @@ class Horde_Alarm { * * @return mixed Converted value. */ - function _fromDriver($value) + protected function _fromDriver($value) { return $value; } @@ -476,81 +545,9 @@ class Horde_Alarm { * * @return mixed Converted value. */ - function _toDriver($value) + protected function _toDriver($value) { return $value; } - /** - * @abstract - */ - function _get() - { - return PEAR::raiseError($this->_errormsg); - } - - /** - * @abstract - */ - function _list() - { - return PEAR::raiseError($this->_errormsg); - } - - /** - * @abstract - */ - function _add() - { - return PEAR::raiseError($this->_errormsg); - } - - /** - * @abstract - */ - function _update() - { - return PEAR::raiseError($this->_errormsg); - } - - /** - * @abstract - */ - function _internal() - { - return PEAR::raiseError($this->_errormsg); - } - - /** - * @abstract - */ - function _exists() - { - return PEAR::raiseError($this->_errormsg); - } - - /** - * @abstract - */ - function _snooze() - { - return PEAR::raiseError($this->_errormsg); - } - - /** - * @abstract - */ - function _isSnoozed() - { - return PEAR::raiseError($this->_errormsg); - } - - /** - * @abstract - */ - function _delete() - { - return PEAR::raiseError($this->_errormsg); - } - } diff --git a/framework/Alarm/lib/Horde/Alarm/Exception.php b/framework/Alarm/lib/Horde/Alarm/Exception.php new file mode 100644 index 000000000..0ead9bfe4 --- /dev/null +++ b/framework/Alarm/lib/Horde/Alarm/Exception.php @@ -0,0 +1,16 @@ + + * @category Horde + * @package Horde_Alarm + */ +class Horde_Alarm_Exception extends Horde_Exception_Prior +{ +} diff --git a/framework/Alarm/Alarm/sql.php b/framework/Alarm/lib/Horde/Alarm/Sql.php similarity index 74% rename from framework/Alarm/Alarm/sql.php rename to framework/Alarm/lib/Horde/Alarm/Sql.php index d565c9560..160045395 100644 --- a/framework/Alarm/Alarm/sql.php +++ b/framework/Alarm/lib/Horde/Alarm/Sql.php @@ -13,36 +13,36 @@ * the PEAR DB package. * * Required values for $params:
- *      'phptype'       The database type (e.g. 'pgsql', 'mysql', etc.).
- *      'charset'       The database's internal charset.
+ * 'phptype' - (string) The database type (e.g. 'pgsql', 'mysql', etc.). + * 'charset' - (string) The database's internal charset. * * Optional values for $params:
- *      'table'         The name of the foo table in 'database'.
+ * 'table' - (string) The name of the foo table in 'database'.
  *
  * Required by some database implementations:
- *      'database'      The name of the database.
- *      'hostspec'      The hostname of the database server.
- *      'protocol'      The communication protocol ('tcp', 'unix', etc.).
- *      'username'      The username with which to connect to the database.
- *      'password'      The password associated with 'username'.
- *      'options'       Additional options to pass to the database.
- *      'tty'           The TTY on which to connect to the database.
- *      'port'          The port on which to connect to the database.
+ * 'database' - The name of the database. + * 'hostspec' - The hostname of the database server. + * 'protocol' - The communication protocol ('tcp', 'unix', etc.). + * 'username' - The username with which to connect to the database. + * 'password' - The password associated with 'username'. + * 'options' - Additional options to pass to the database. + * 'tty' - The TTY on which to connect to the database. + * 'port' - The port on which to connect to the database.
* - * The table structure can be created by the scripts/sql/horde_alarm.sql - * script. + * The table structure can be created by the + * horde/scripts/sql/horde_alarm.sql script. * * @author Jan Schneider * @package Horde_Alarm */ -class Horde_Alarm_sql extends Horde_Alarm { - +class Horde_Alarm_Sql extends Horde_Alarm +{ /** * Handle for the current database connection. * * @var DB */ - var $_db; + protected $_db; /** * Handle for the current database connection, used for writing. Defaults @@ -50,17 +50,7 @@ class Horde_Alarm_sql extends Horde_Alarm { * * @var DB */ - var $_write_db; - - /** - * Constructs a new SQL storage object. - * - * @param array $params A hash containing connection parameters. - */ - function Horde_Alarm_sql($params = array()) - { - $this->_params = array_merge($this->_params, $params); - } + protected $_write_db; /** * Converts a value from the driver's charset. @@ -69,7 +59,7 @@ class Horde_Alarm_sql extends Horde_Alarm { * * @return mixed Converted value. */ - function _fromDriver($value) + protected function _fromDriver($value) { return Horde_String::convertCharset($value, $this->_params['charset']); } @@ -81,7 +71,7 @@ class Horde_Alarm_sql extends Horde_Alarm { * * @return mixed Converted value. */ - function _toDriver($value) + protected function _toDriver($value) { return Horde_String::convertCharset($value, Horde_Nls::getCharset(), $this->_params['charset']); } @@ -94,7 +84,7 @@ class Horde_Alarm_sql extends Horde_Alarm { * * @return array An alarm hash. */ - function _get($id, $user) + protected function _get($id, $user) { $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', $this->_params['table'], @@ -102,25 +92,16 @@ class Horde_Alarm_sql extends Horde_Alarm { Horde::logMessage('SQL query by Horde_Alarm_sql::_get(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); $alarm = $this->_db->getRow($query, array($id, $user), DB_FETCHMODE_ASSOC); - if (is_a($alarm, 'PEAR_Error')) { + if ($alarm instanceof PEAR_Error) { Horde::logMessage($alarm, __FILE__, __LINE__); - return $alarm; + throw new Horde_Alarm_Exception($alarm); } + if (empty($alarm)) { - return PEAR::raiseError(_("Alarm not found")); + throw new Horde_Alarm_Exception('Alarm not found'); } - $alarm = array( - 'id' => $alarm['alarm_id'], - 'user' => $alarm['alarm_uid'], - 'start' => new Horde_Date($alarm['alarm_start'], 'UTC'), - 'end' => empty($alarm['alarm_end']) ? null : new Horde_Date($alarm['alarm_end'], 'UTC'), - 'methods' => @unserialize($alarm['alarm_methods']), - 'params' => @unserialize($alarm['alarm_params']), - 'title' => $this->_fromDriver($alarm['alarm_title']), - 'text' => $this->_fromDriver($alarm['alarm_text']), - 'snooze' => empty($alarm['alarm_snooze']) ? null : new Horde_Date($alarm['alarm_snooze'], 'UTC'), - 'internal' => empty($alarm['alarm_internal']) ? null : @unserialize($alarm['alarm_internal'])); - return $alarm; + + return $this->_getHash($alarm); } /** @@ -132,7 +113,7 @@ class Horde_Alarm_sql extends Horde_Alarm { * * @return array A list of alarm hashes. */ - function _list($user, $time) + protected function _list($user, $time) { $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', $this->_params['table'], @@ -148,54 +129,69 @@ class Horde_Alarm_sql extends Horde_Alarm { $alarms = array(); $result = $this->_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, __FILE__, __LINE__); - return $result; + throw new Horde_Alarm_Exception($result); } + while ($alarm = $result->fetchRow(DB_FETCHMODE_ASSOC)) { - if (is_a($alarm, 'PEAR_Error')) { + if ($alarm instanceof PEAR_Error) { Horde::logMessage($alarm, __FILE__, __LINE__); - return $alarm; + throw new Horde_Alarm_Exception($alarm); } - $alarms[] = array( - 'id' => $alarm['alarm_id'], - 'user' => $alarm['alarm_uid'], - 'start' => new Horde_Date($alarm['alarm_start'], 'UTC'), - 'end' => empty($alarm['alarm_end']) ? null : new Horde_Date($alarm['alarm_end'], 'UTC'), - 'methods' => @unserialize($alarm['alarm_methods']), - 'params' => @unserialize($alarm['alarm_params']), - 'title' => $this->_fromDriver($alarm['alarm_title']), - 'text' => $this->_fromDriver($alarm['alarm_text']), - 'snooze' => empty($alarm['alarm_snooze']) ? null : new Horde_Date($alarm['alarm_snooze'], 'UTC'), - 'internal' => empty($alarm['alarm_internal']) ? null : @unserialize($alarm['alarm_internal'])); + + $alarms[] = $this->_getHash($alarm); } return $alarms; } /** + */ + protected function _getHash($alarm) + { + return array( + 'id' => $alarm['alarm_id'], + 'user' => $alarm['alarm_uid'], + 'start' => new Horde_Date($alarm['alarm_start'], 'UTC'), + 'end' => empty($alarm['alarm_end']) ? null : new Horde_Date($alarm['alarm_end'], 'UTC'), + 'methods' => @unserialize($alarm['alarm_methods']), + 'params' => @unserialize($alarm['alarm_params']), + 'title' => $this->_fromDriver($alarm['alarm_title']), + 'text' => $this->_fromDriver($alarm['alarm_text']), + 'snooze' => empty($alarm['alarm_snooze']) ? null : new Horde_Date($alarm['alarm_snooze'], 'UTC'), + 'internal' => empty($alarm['alarm_internal']) ? null : @unserialize($alarm['alarm_internal']) + ); + } + + /** * Adds an alarm hash to the backend. * * @param array $alarm An alarm hash. */ - function _add($alarm) + protected function _add($alarm) { $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']); - $values = array($alarm['id'], - isset($alarm['user']) ? $alarm['user'] : '', - (string)$alarm['start']->setTimezone('UTC'), - empty($alarm['end']) ? null : (string)$alarm['end']->setTimezone('UTC'), - serialize($alarm['methods']), - serialize($alarm['params']), - $this->_toDriver($alarm['title']), - empty($alarm['text']) ? null : $this->_toDriver($alarm['text']), - null); + $values = array( + $alarm['id'], + isset($alarm['user']) ? $alarm['user'] : '', + (string)$alarm['start']->setTimezone('UTC'), + empty($alarm['end']) ? null : (string)$alarm['end']->setTimezone('UTC'), + serialize($alarm['methods']), + serialize($alarm['params']), + $this->_toDriver($alarm['title']), + empty($alarm['text']) ? null : $this->_toDriver($alarm['text']), + null + ); Horde::logMessage('SQL query by Horde_Alarm_sql::_add(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, __FILE__, __LINE__); + throw new Horde_Alarm_Exception($result); } + return $result; } @@ -204,7 +200,7 @@ class Horde_Alarm_sql extends Horde_Alarm { * * @param array $alarm An alarm hash. */ - function _update($alarm) + protected function _update($alarm) { $query = sprintf('UPDATE %s set alarm_start = ?, alarm_end = ?, alarm_methods = ?, alarm_params = ?, alarm_title = ?, alarm_text = ? WHERE alarm_id = ? AND %s', $this->_params['table'], @@ -221,10 +217,13 @@ class Horde_Alarm_sql extends Horde_Alarm { isset($alarm['user']) ? $alarm['user'] : ''); Horde::logMessage('SQL query by Horde_Alarm_sql::_update(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, __FILE__, __LINE__); + throw new Horde_Alarm_Exception($result); } + return $result; } @@ -236,7 +235,7 @@ class Horde_Alarm_sql extends Horde_Alarm { * @param string $user The alarm's user * @param array $internal A hash with the internal data. */ - function _internal($id, $user, $internal) + protected function _internal($id, $user, $internal) { $query = sprintf('UPDATE %s set alarm_internal = ? WHERE alarm_id = ? AND %s', $this->_params['table'], @@ -244,10 +243,13 @@ class Horde_Alarm_sql extends Horde_Alarm { $values = array(serialize($internal), $id, $user); Horde::logMessage('SQL query by Horde_Alarm_sql::_internal(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, __FILE__, __LINE__); + throw new Horde_Alarm_Exception($result); } + return $result; } @@ -259,17 +261,20 @@ class Horde_Alarm_sql extends Horde_Alarm { * * @return boolean True if the specified alarm exists. */ - function _exists($id, $user) + protected function _exists($id, $user) { $query = sprintf('SELECT 1 FROM %s WHERE alarm_id = ? AND %s', $this->_params['table'], !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)'); Horde::logMessage('SQL query by Horde_Alarm_sql::_exists(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_db->getOne($query, array($id, $user)); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, __FILE__, __LINE__); + throw new Horde_Alarm_Exception($result); } + return $result; } @@ -280,7 +285,7 @@ class Horde_Alarm_sql extends Horde_Alarm { * @param string $user The alarm's user * @param Horde_Date $snooze The snooze time. */ - function _snooze($id, $user, $snooze) + protected function _snooze($id, $user, $snooze) { $query = sprintf('UPDATE %s set alarm_snooze = ? WHERE alarm_id = ? AND %s', $this->_params['table'], @@ -288,10 +293,13 @@ class Horde_Alarm_sql extends Horde_Alarm { $values = array((string)$snooze->setTimezone('UTC'), $id, $user); Horde::logMessage('SQL query by Horde_Alarm_sql::_snooze(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, __FILE__, __LINE__); + throw new Horde_Alarm_Exception($result); } + return $result; } @@ -301,7 +309,7 @@ class Horde_Alarm_sql extends Horde_Alarm { * @param string $id The alarm's unique id. * @param string $user The alarm's user */ - function _dismiss($id, $user) + protected function _dismiss($id, $user) { $query = sprintf('UPDATE %s set alarm_dismissed = 1 WHERE alarm_id = ? AND %s', $this->_params['table'], @@ -309,10 +317,13 @@ class Horde_Alarm_sql extends Horde_Alarm { $values = array($id, $user); Horde::logMessage('SQL query by Horde_Alarm_sql::_dismiss(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, __FILE__, __LINE__); + throw new Horde_Alarm_Exception($result); } + return $result; } @@ -325,17 +336,20 @@ class Horde_Alarm_sql extends Horde_Alarm { * * @return boolean True if the alarm is snoozed. */ - function _isSnoozed($id, $user, $time) + protected function _isSnoozed($id, $user, $time) { $query = sprintf('SELECT 1 FROM %s WHERE alarm_id = ? AND %s AND (alarm_dismissed = 1 OR (alarm_snooze IS NOT NULL AND alarm_snooze >= ?))', $this->_params['table'], !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)'); Horde::logMessage('SQL query by Horde_Alarm_sql::_isSnoozed(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_db->getOne($query, array($id, $user, (string)$time->setTimezone('UTC'))); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, __FILE__, __LINE__); + throw new Horde_Alarm_Exception($result); } + return $result; } @@ -345,7 +359,7 @@ class Horde_Alarm_sql extends Horde_Alarm { * @param string $id The alarm's unique id. * @param string $user The alarm's user. All users' alarms if null. */ - function _delete($id, $user = null) + protected function _delete($id, $user = null) { $query = sprintf('DELETE FROM %s WHERE alarm_id = ?', $this->_params['table']); $values = array($id); @@ -357,85 +371,80 @@ class Horde_Alarm_sql extends Horde_Alarm { } Horde::logMessage('SQL query by Horde_Alarm_sql::_delete(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, __FILE__, __LINE__); + throw new Horde_Alarm_Exception($result); } + return $result; } /** * Garbage collects old alarms in the backend. */ - function _gc() + protected function _gc() { $query = sprintf('DELETE FROM %s WHERE alarm_end IS NOT NULL AND alarm_end < ?', $this->_params['table']); Horde::logMessage('SQL query by Horde_Alarm_sql::_gc(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); $end = new Horde_Date(time()); + $result = $this->_write_db->query($query, (string)$end->setTimezone('UTC')); - if (is_a($result, 'PEAR_Error')) { + if ($result instanceof PEAR_Error) { Horde::logMessage($result, __FILE__, __LINE__); + throw new Horde_Alarm_Exception($result); } + return $result; } /** * Attempts to open a connection to the SQL server. - * - * @return boolean True on success, PEAR_Error on failure. */ - function initialize() + public function initialize() { Horde::assertDriverConfig($this->_params, 'sql', array('phptype', 'charset')); - if (!isset($this->_params['database'])) { - $this->_params['database'] = ''; - } - if (!isset($this->_params['username'])) { - $this->_params['username'] = ''; - } - if (!isset($this->_params['hostspec'])) { - $this->_params['hostspec'] = ''; - } - if (!isset($this->_params['table'])) { - $this->_params['table'] = 'horde_alarms'; - } + $this->_params = array_merge(array( + 'database' => '', + 'username' => '', + 'hostspec' => '', + 'table' => 'horde_alarms' + ), $this->_params); /* Connect to the SQL server using the supplied parameters. */ - require_once 'DB.php'; - $this->_write_db = &DB::connect($this->_params, - array('persistent' => !empty($this->_params['persistent']), - 'ssl' => !empty($this->_params['ssl']))); - if (is_a($this->_write_db, 'PEAR_Error')) { + $this->_write_db = DB::connect($this->_params, + array('persistent' => !empty($this->_params['persistent']), + 'ssl' => !empty($this->_params['ssl']))); + if ($this->_write_db instanceof PEAR_Error) { Horde::logMessage($this->_write_db, __FILE__, __LINE__); - return $this->_write_db; + throw new Horde_Alarm_Exception($this->_write_db); } $this->_initConn($this->_write_db); /* Check if we need to set up the read DB connection seperately. */ if (!empty($this->_params['splitread'])) { $params = array_merge($this->_params, $this->_params['read']); - $this->_db = &DB::connect($params, - array('persistent' => !empty($params['persistent']), - 'ssl' => !empty($params['ssl']))); - if (is_a($this->_db, 'PEAR_Error')) { + $this->_db = DB::connect($params, + array('persistent' => !empty($params['persistent']), + 'ssl' => !empty($params['ssl']))); + if ($this->_db instanceof PEAR_Error) { Horde::logMessage($this->_db, __FILE__, __LINE__); - return $this->_db; + throw new Horde_Alarm_Exception($this->_db); } $this->_initConn($this->_db); } else { /* Default to the same DB handle for the writer too. */ - $this->_db = &$this->_write_db; + $this->_db = $this->_write_db; } - - return true; } /** */ - function _initConn(&$db) + protected function _initConn($db) { // Set DB portability options. switch ($db->phptype) { @@ -445,6 +454,7 @@ class Horde_Alarm_sql extends Horde_Alarm { default: $db->setOption('portability', DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_ERRORS); + break; } /* Handle any database specific initialization code to run. */ diff --git a/framework/Alarm/package.xml b/framework/Alarm/package.xml index 79c5968dc..de323e8c4 100644 --- a/framework/Alarm/package.xml +++ b/framework/Alarm/package.xml @@ -3,7 +3,7 @@ http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd"> - Horde_Alarm + Alarm pear.horde.org Horde alarm libraries This package provides an interface to deal with reminders, @@ -16,35 +16,45 @@ http://pear.php.net/dtd/package-2.0.xsd"> jan@horde.org yes - 2007-02-01 + 2009-02-11 - 0.1.0 - 0.1.0 + 0.2.0 + 0.2.0 beta beta LGPL - * Initial release. + * Initial Horde 4 package. - - - - - - - - + + + + + + + + + + + + + + + + + + - 4.3.0 + 5.2.0 - 1.4.0b1 + 1.5.0 Date @@ -52,5 +62,26 @@ http://pear.php.net/dtd/package-2.0.xsd"> - + + + + + + + + + + 2007-02-01 + + 0.1.0 + 0.1.0 + + + beta + beta + + LGPL + * Initial release. + + diff --git a/framework/Alarm/tests/001.phpt b/framework/Alarm/test/Horde/Alarm/001.phpt similarity index 99% rename from framework/Alarm/tests/001.phpt rename to framework/Alarm/test/Horde/Alarm/001.phpt index ed10754e9..993b0fff1 100644 --- a/framework/Alarm/tests/001.phpt +++ b/framework/Alarm/test/Horde/Alarm/001.phpt @@ -11,7 +11,7 @@ if (!$setup || empty($params)) { sql - +