Re-send mails if alarm has been changed (Bug #8717).
authorJan Schneider <jan@horde.org>
Mon, 10 May 2010 10:22:56 +0000 (12:22 +0200)
committerJan Schneider <jan@horde.org>
Mon, 10 May 2010 12:04:32 +0000 (14:04 +0200)
framework/Alarm/lib/Horde/Alarm.php
framework/Alarm/lib/Horde/Alarm/Handler.php
framework/Alarm/lib/Horde/Alarm/Handler/Mail.php
framework/Alarm/test/Horde/Alarm/HandlerTest.php

index eac2533..c3bc11e 100644 (file)
@@ -247,19 +247,27 @@ abstract class Horde_Alarm
      *
      * The alarm will be added if it doesn't exist, and updated otherwise.
      *
-     * @param array $alarm         An alarm hash. See self::get() for format.
-     * @param boolean $keepsnooze  Whether to keep the snooze value unchanged.
+     * @param array $alarm   An alarm hash. See self::get() for format.
+     * @param boolean $keep  Whether to keep the snooze value and notification
+     *                       status unchanged. If true, the alarm will get
+     *                       "un-snoozed", and notifications (like mails) are
+     *                       sent again.
      *
      * @throws Horde_Alarm_Exception
      */
-    public function set(array $alarm, $keepsnooze = false)
+    public function set(array $alarm, $keep = false)
     {
         if (isset($alarm['mail']['body'])) {
             $alarm['mail']['body'] = $this->_toDriver($alarm['mail']['body']);
         }
 
         if ($this->exists($alarm['id'], isset($alarm['user']) ? $alarm['user'] : '')) {
-            $this->_update($alarm, $keepsnooze);
+            $this->_update($alarm, $keep);
+            if (!$keep) {
+                foreach ($this->_handlers as &$handler) {
+                    $handler->reset($alarm);
+                }
+            }
         } else {
             $this->_add($alarm);
         }
index ecf52c1..7e7a5ca 100644 (file)
@@ -39,6 +39,16 @@ abstract class Horde_Alarm_Handler
     abstract public function notify(array $alarm);
 
     /**
+     * Resets the internal status of the handler, so that alarm notifications
+     * are sent again.
+     *
+     * @param array $alarm  An alarm hash.
+     */
+    public function reset(array $alarm)
+    {
+    }
+
+    /**
      * Returns a human readable description of the handler.
      *
      * @return string
index bff6ef8..30854b7 100644 (file)
@@ -105,6 +105,18 @@ class Horde_Alarm_Handler_Mail extends Horde_Alarm_Handler
     }
 
     /**
+     * Resets the internal status of the handler, so that alarm notifications
+     * are sent again.
+     *
+     * @param array $alarm  An alarm hash.
+     */
+    public function reset(array $alarm)
+    {
+        $alarm['internal']['mail']['sent'] = false;
+        $this->alarm->internal($alarm['id'], $alarm['user'], $alarm['internal']);
+    }
+
+    /**
      * Returns a human readable description of the handler.
      *
      * @return string
index f863806..17df329 100644 (file)
@@ -83,11 +83,17 @@ MIME-Version: 1\.0
 Action is required\.
 
 EOR;
+        $regexp = trim(str_replace("\r\n", "\n", $regexp));
 
-        $this->assertRegExp('/' . trim(str_replace("\r\n", "\n", $regexp)) . '/', trim(str_replace("\r\n", "\n", self::$mail->sentOutput)));
+        $this->assertRegExp('/' . $regexp . '/', trim(str_replace("\r\n", "\n", self::$mail->sentOutput)));
         self::$mail->sentOutput = null;
         self::$alarm->notify('john', false);
         $this->assertNull(self::$mail->sentOutput);
+
+        /* Test re-sending mails after changing the alarm. */
+        self::$alarm->set(self::$alarm->get('personalalarm', 'john'));
+        self::$alarm->notify('john', false);
+        $this->assertRegExp('/' . $regexp . '/', trim(str_replace("\r\n", "\n", self::$mail->sentOutput)));
     }
 }