Reorder methods.
authorJan Schneider <jan@horde.org>
Wed, 5 May 2010 21:47:54 +0000 (23:47 +0200)
committerJan Schneider <jan@horde.org>
Wed, 5 May 2010 21:47:54 +0000 (23:47 +0200)
framework/Alarm/lib/Horde/Alarm.php
framework/Alarm/lib/Horde/Alarm/Sql.php

index 7a3e98f..033ad89 100644 (file)
@@ -86,6 +86,110 @@ abstract class Horde_Alarm
     }
 
     /**
+     * Retrieves active alarms from all applications and stores them in the
+     * backend.
+     *
+     * The applications will only be called once in the configured time span,
+     * by default 5 minutes.
+     *
+     * @param string $user      Retrieve alarms for this user, or for all users
+     *                          if null.
+     * @param boolean $preload  Preload alarms that go off within the next
+     *                          ttl time span?
+     */
+    public function load($user = null, $preload = true)
+    {
+        if (isset($_SESSION['horde']['alarm']['loaded']) &&
+            (time() - $_SESSION['horde']['alarm']['loaded']) < $this->_params['ttl']) {
+            return;
+        }
+
+        foreach ($GLOBALS['registry']->listApps(null, false, Horde_Perms::READ) as $app) {
+            if (!$GLOBALS['registry']->hasMethod('listAlarms', $app)) {
+                continue;
+            }
+
+            /* Preload alarms that happen in the next ttl seconds. */
+            if ($preload) {
+                try {
+                    $alarms = $GLOBALS['registry']->callByPackage($app, 'listAlarms', array(time() + $this->_params['ttl'], $user), array('noperms' => true));
+                } catch (Horde_Exception $e) {
+                    continue;
+                }
+            } else {
+                $alarms = array();
+            }
+
+            /* Load current alarms if no preloading requested or if this
+             * is the first call in this session. */
+            if (!$preload ||
+                !isset($_SESSION['horde']['alarm']['loaded'])) {
+                try {
+                    $app_alarms = $GLOBALS['registry']->callByPackage($app, 'listAlarms', array(time(), $user), array('noperms' => true));
+                } catch (Horde_Exception $e) {
+                    if ($this->_logger) {
+                        $this->_logger->log($e, 'ERR');
+                    }
+                    $app_alarms = array();
+                }
+                $alarms = array_merge($alarms, $app_alarms);
+            }
+
+            foreach ($alarms as $alarm) {
+                $this->set($alarm, true);
+            }
+        }
+
+        $_SESSION['horde']['alarm']['loaded'] = time();
+    }
+
+    /**
+     * Returns a list of alarms from the backend.
+     *
+     * @param string $user      Return alarms for this user, all users if
+     *                          null, or global alarms if empty.
+     * @param Horde_Date $time  The time when the alarms should be active.
+     *                          Defaults to now.
+     * @param boolean $load     Update active alarms from all applications?
+     * @param boolean $preload  Preload alarms that go off within the next
+     *                          ttl time span?
+     *
+     * @return array  A list of alarm hashes.
+     * @throws Horde_Alarm_Exception
+     */
+    public function listAlarms($user = null, $time = null, $load = false,
+                               $preload = true)
+    {
+        if (empty($time)) {
+            $time = new Horde_Date(time());
+        }
+        if ($load) {
+            $this->load($user, $preload);
+        }
+
+        $alarms = $this->_list($user, $time);
+
+        foreach (array_keys($alarms) as $alarm) {
+            if (isset($alarms[$alarm]['mail']['body'])) {
+                $alarms[$alarm]['mail']['body'] = $this->_fromDriver($alarms[$alarm]['mail']['body']);
+            }
+        }
+        return $alarms;
+    }
+
+    /**
+     * Returns a list of alarms from the backend.
+     *
+     * @param Horde_Date $time  The time when the alarms should be active.
+     * @param string $user      Return alarms for this user, all users if
+     *                          null, or global alarms if empty.
+     *
+     * @return array  A list of alarm hashes.
+     * @throws Horde_Alarm_Exception
+     */
+    abstract protected function _list($user, $time);
+
+    /**
      * Returns an alarm hash from the backend.
      *
      * @param string $id    The alarm's unique id.
@@ -152,6 +256,15 @@ abstract class Horde_Alarm
     }
 
     /**
+     * Adds an alarm hash to the backend.
+     *
+     * @param array $alarm  An alarm hash.
+     *
+     * @throws Horde_Alarm_Exception
+     */
+    abstract protected function _add($alarm);
+
+    /**
      * Updates an alarm hash in the backend.
      *
      * @param array $alarm         An alarm hash.
@@ -162,13 +275,16 @@ abstract class Horde_Alarm
     abstract protected function _update($alarm, $keepsnooze = false);
 
     /**
-     * Adds an alarm hash to the backend.
+     * Updates internal alarm properties, i.e. properties not determined by
+     * the application setting the alarm.
      *
-     * @param array $alarm  An alarm hash.
+     * @param string $id       The alarm's unique id.
+     * @param string $user     The alarm's user
+     * @param array $internal  A hash with the internal data.
      *
      * @throws Horde_Alarm_Exception
      */
-    abstract protected function _add($alarm);
+    abstract protected function _internal($id, $user, $internal);
 
     /**
      * Returns whether an alarm with the given id exists already.
@@ -240,16 +356,6 @@ abstract class Horde_Alarm
     abstract protected function _snooze($id, $user, $snooze);
 
     /**
-     * Dismisses an alarm.
-     *
-     * @param string $id          The alarm's unique id.
-     * @param string $user        The alarm's user
-     *
-     * @throws Horde_Alarm_Exception
-     */
-    abstract protected function _dismiss($id, $user);
-
-    /**
      * Returns whether an alarm is snoozed.
      *
      * @param string $id        The alarm's unique id.
@@ -282,17 +388,14 @@ abstract class Horde_Alarm
     abstract protected function _isSnoozed($id, $user, $time);
 
     /**
-     * Deletes an alarm from the backend.
+     * Dismisses an alarm.
      *
-     * @param string $id    The alarm's unique id.
-     * @param string $user  The alarm's user. All users' alarms if null.
+     * @param string $id          The alarm's unique id.
+     * @param string $user        The alarm's user
      *
      * @throws Horde_Alarm_Exception
      */
-    function delete($id, $user = null)
-    {
-        $this->_delete($id, $user);
-    }
+    abstract protected function _dismiss($id, $user);
 
     /**
      * Deletes an alarm from the backend.
@@ -302,111 +405,20 @@ abstract class Horde_Alarm
      *
      * @throws Horde_Alarm_Exception
      */
-    abstract protected function _delete($id, $user = null);
-
-    /**
-     * Retrieves active alarms from all applications and stores them in the
-     * backend.
-     *
-     * The applications will only be called once in the configured time span,
-     * by default 5 minutes.
-     *
-     * @param string $user      Retrieve alarms for this user, or for all users
-     *                          if null.
-     * @param boolean $preload  Preload alarms that go off within the next
-     *                          ttl time span?
-     */
-    public function load($user = null, $preload = true)
-    {
-        if (isset($_SESSION['horde']['alarm']['loaded']) &&
-            (time() - $_SESSION['horde']['alarm']['loaded']) < $this->_params['ttl']) {
-            return;
-        }
-
-        foreach ($GLOBALS['registry']->listApps(null, false, Horde_Perms::READ) as $app) {
-            if (!$GLOBALS['registry']->hasMethod('listAlarms', $app)) {
-                continue;
-            }
-
-            /* Preload alarms that happen in the next ttl seconds. */
-            if ($preload) {
-                try {
-                    $alarms = $GLOBALS['registry']->callByPackage($app, 'listAlarms', array(time() + $this->_params['ttl'], $user), array('noperms' => true));
-                } catch (Horde_Exception $e) {
-                    continue;
-                }
-            } else {
-                $alarms = array();
-            }
-
-            /* Load current alarms if no preloading requested or if this
-             * is the first call in this session. */
-            if (!$preload ||
-                !isset($_SESSION['horde']['alarm']['loaded'])) {
-                try {
-                    $app_alarms = $GLOBALS['registry']->callByPackage($app, 'listAlarms', array(time(), $user), array('noperms' => true));
-                } catch (Horde_Exception $e) {
-                    if ($this->_logger) {
-                        $this->_logger->log($e, 'ERR');
-                    }
-                    $app_alarms = array();
-                }
-                $alarms = array_merge($alarms, $app_alarms);
-            }
-
-            foreach ($alarms as $alarm) {
-                $this->set($alarm, true);
-            }
-        }
-
-        $_SESSION['horde']['alarm']['loaded'] = time();
-    }
-
-    /**
-     * Returns a list of alarms from the backend.
-     *
-     * @param string $user      Return alarms for this user, all users if
-     *                          null, or global alarms if empty.
-     * @param Horde_Date $time  The time when the alarms should be active.
-     *                          Defaults to now.
-     * @param boolean $load     Update active alarms from all applications?
-     * @param boolean $preload  Preload alarms that go off within the next
-     *                          ttl time span?
-     *
-     * @return array  A list of alarm hashes.
-     * @throws Horde_Alarm_Exception
-     */
-    public function listAlarms($user = null, $time = null, $load = false,
-                               $preload = true)
+    function delete($id, $user = null)
     {
-        if (empty($time)) {
-            $time = new Horde_Date(time());
-        }
-        if ($load) {
-            $this->load($user, $preload);
-        }
-
-        $alarms = $this->_list($user, $time);
-
-        foreach (array_keys($alarms) as $alarm) {
-            if (isset($alarms[$alarm]['mail']['body'])) {
-                $alarms[$alarm]['mail']['body'] = $this->_fromDriver($alarms[$alarm]['mail']['body']);
-            }
-        }
-        return $alarms;
+        $this->_delete($id, $user);
     }
 
     /**
-     * Returns a list of alarms from the backend.
+     * Deletes an alarm from the backend.
      *
-     * @param Horde_Date $time  The time when the alarms should be active.
-     * @param string $user      Return alarms for this user, all users if
-     *                          null, or global alarms if empty.
+     * @param string $id    The alarm's unique id.
+     * @param string $user  The alarm's user. All users' alarms if null.
      *
-     * @return array  A list of alarm hashes.
      * @throws Horde_Alarm_Exception
      */
-    abstract protected function _list($user, $time);
+    abstract protected function _delete($id, $user = null);
 
     /**
      * Notifies the user about any active alarms.
@@ -511,18 +523,6 @@ abstract class Horde_Alarm
     }
 
     /**
-     * Updates internal alarm properties, i.e. properties not determined by
-     * the application setting the alarm.
-     *
-     * @param string $id       The alarm's unique id.
-     * @param string $user     The alarm's user
-     * @param array $internal  A hash with the internal data.
-     *
-     * @throws Horde_Alarm_Exception
-     */
-    abstract protected function _internal($id, $user, $internal);
-
-    /**
      * Notifies about an alarm with an SMS through the sms/send API method.
      *
      * @param array $alarm  An alarm hash.
@@ -600,6 +600,13 @@ abstract class Horde_Alarm
     abstract protected function _gc();
 
     /**
+     * Attempts to initialize the backend.
+     *
+     * @throws Horde_Alarm_Exception
+     */
+    abstract public function initialize();
+
+    /**
      * Converts a value from the driver's charset.
      *
      * @param mixed $value  Value to convert.
index d32cd3e..4c38c19 100644 (file)
@@ -53,64 +53,6 @@ class Horde_Alarm_Sql extends Horde_Alarm
     protected $_write_db;
 
     /**
-     * Converts a value from the driver's charset.
-     *
-     * @param mixed $value  Value to convert.
-     *
-     * @return mixed  Converted value.
-     */
-    protected function _fromDriver($value)
-    {
-        return Horde_String::convertCharset($value, $this->_params['charset']);
-    }
-
-    /**
-     * Converts a value to the driver's charset.
-     *
-     * @param mixed $value  Value to convert.
-     *
-     * @return mixed  Converted value.
-     */
-    protected function _toDriver($value)
-    {
-        return Horde_String::convertCharset($value, Horde_Nls::getCharset(), $this->_params['charset']);
-    }
-
-    /**
-     * Returns an alarm hash from the backend.
-     *
-     * @param string $id    The alarm's unique id.
-     * @param string $user  The alarm's user
-     *
-     * @return array  An alarm hash.
-     * @throws Horde_Alarm_Exception
-     */
-    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'],
-                         !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
-
-        if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_get(): ' . $query, 'DEBUG');
-        }
-
-        $alarm = $this->_db->getRow($query, array($id, $user), DB_FETCHMODE_ASSOC);
-        if ($alarm instanceof PEAR_Error) {
-            if ($this->_logger) {
-                $this->_logger->log($alarm, 'INFO');
-            }
-            throw new Horde_Alarm_Exception($alarm);
-        }
-
-        if (empty($alarm)) {
-            throw new Horde_Alarm_Exception('Alarm not found');
-        }
-
-        return $this->_getHash($alarm);
-    }
-
-    /**
      * Returns a list of alarms from the backend.
      *
      * @param Horde_Date $time  The time when the alarms should be active.
@@ -178,6 +120,40 @@ class Horde_Alarm_Sql extends Horde_Alarm
     }
 
     /**
+     * Returns an alarm hash from the backend.
+     *
+     * @param string $id    The alarm's unique id.
+     * @param string $user  The alarm's user
+     *
+     * @return array  An alarm hash.
+     * @throws Horde_Alarm_Exception
+     */
+    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'],
+                         !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
+
+        if ($this->_logger) {
+            $this->_logger->log('SQL query by Horde_Alarm_sql::_get(): ' . $query, 'DEBUG');
+        }
+
+        $alarm = $this->_db->getRow($query, array($id, $user), DB_FETCHMODE_ASSOC);
+        if ($alarm instanceof PEAR_Error) {
+            if ($this->_logger) {
+                $this->_logger->log($alarm, 'INFO');
+            }
+            throw new Horde_Alarm_Exception($alarm);
+        }
+
+        if (empty($alarm)) {
+            throw new Horde_Alarm_Exception('Alarm not found');
+        }
+
+        return $this->_getHash($alarm);
+    }
+
+    /**
      * Adds an alarm hash to the backend.
      *
      * @param array $alarm  An alarm hash.
@@ -339,62 +315,62 @@ class Horde_Alarm_Sql extends Horde_Alarm
     }
 
     /**
-     * Dismisses an alarm.
+     * Returns whether an alarm is snoozed.
      *
-     * @param string $id          The alarm's unique id.
-     * @param string $user        The alarm's user
+     * @param string $id        The alarm's unique id.
+     * @param string $user      The alarm's user
+     * @param Horde_Date $time  The time when the alarm may be snoozed.
      *
+     * @return boolean  True if the alarm is snoozed.
      * @throws Horde_Alarm_Exception
      */
-    protected function _dismiss($id, $user)
+    protected function _isSnoozed($id, $user, $time)
     {
-        $query = sprintf('UPDATE %s set alarm_dismissed = 1 WHERE alarm_id = ? AND %s',
+        $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)');
-        $values = array($id, $user);
 
         if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_dismiss(): ' . $query, 'DEBUG');
+            $this->_logger->log('SQL query by Horde_Alarm_sql::_isSnoozed(): ' . $query, 'DEBUG');
         }
 
-        $result = $this->_write_db->query($query, $values);
+        $result = $this->_db->getOne($query, array($id, $user, (string)$time->setTimezone('UTC')));
         if ($result instanceof PEAR_Error) {
             if ($this->_logger) {
                 $this->_logger->log($result, 'INFO');
             }
             throw new Horde_Alarm_Exception($result);
         }
+
+        return $result;
     }
 
     /**
-     * Returns whether an alarm is snoozed.
+     * Dismisses an alarm.
      *
-     * @param string $id        The alarm's unique id.
-     * @param string $user      The alarm's user
-     * @param Horde_Date $time  The time when the alarm may be snoozed.
+     * @param string $id          The alarm's unique id.
+     * @param string $user        The alarm's user
      *
-     * @return boolean  True if the alarm is snoozed.
      * @throws Horde_Alarm_Exception
      */
-    protected function _isSnoozed($id, $user, $time)
+    protected function _dismiss($id, $user)
     {
-        $query = sprintf('SELECT 1 FROM %s WHERE alarm_id = ? AND %s AND (alarm_dismissed = 1 OR (alarm_snooze IS NOT NULL AND alarm_snooze >= ?))',
+        $query = sprintf('UPDATE %s set alarm_dismissed = 1 WHERE alarm_id = ? AND %s',
                          $this->_params['table'],
                          !empty($user) ? 'alarm_uid = ?' : '(alarm_uid = ? OR alarm_uid IS NULL)');
+        $values = array($id, $user);
 
         if ($this->_logger) {
-            $this->_logger->log('SQL query by Horde_Alarm_sql::_isSnoozed(): ' . $query, 'DEBUG');
+            $this->_logger->log('SQL query by Horde_Alarm_sql::_dismiss(): ' . $query, 'DEBUG');
         }
 
-        $result = $this->_db->getOne($query, array($id, $user, (string)$time->setTimezone('UTC')));
+        $result = $this->_write_db->query($query, $values);
         if ($result instanceof PEAR_Error) {
             if ($this->_logger) {
                 $this->_logger->log($result, 'INFO');
             }
             throw new Horde_Alarm_Exception($result);
         }
-
-        return $result;
     }
 
     /**
@@ -542,4 +518,28 @@ class Horde_Alarm_Sql extends Horde_Alarm
         }
     }
 
+    /**
+     * Converts a value from the driver's charset.
+     *
+     * @param mixed $value  Value to convert.
+     *
+     * @return mixed  Converted value.
+     */
+    protected function _fromDriver($value)
+    {
+        return Horde_String::convertCharset($value, $this->_params['charset']);
+    }
+
+    /**
+     * Converts a value to the driver's charset.
+     *
+     * @param mixed $value  Value to convert.
+     *
+     * @return mixed  Converted value.
+     */
+    protected function _toDriver($value)
+    {
+        return Horde_String::convertCharset($value, Horde_Nls::getCharset(), $this->_params['charset']);
+    }
+
 }