Overview

Packages

  • Alarm

Classes

  • Horde_Alarm
  • Horde_Alarm_Exception
  • Horde_Alarm_Handler
  • Horde_Alarm_Handler_Desktop
  • Horde_Alarm_Handler_Mail
  • Horde_Alarm_Handler_Notify
  • Horde_Alarm_Null
  • Horde_Alarm_Object
  • Horde_Alarm_Sql
  • Horde_Alarm_Translation
  • Overview
  • Package
  • Class
  • Tree
 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_Handler_Mail class is a Horde_Alarm handler that notifies
13:  * of active alarms by desktop notification through webkit browsers.
14:  *
15:  * @author  Jan Schneider <jan@horde.org>
16:  * @package Alarm
17:  */
18: class Horde_Alarm_Handler_Desktop extends Horde_Alarm_Handler
19: {
20:     /**
21:      * A notification callback.
22:      *
23:      * @var callback
24:      */
25:     protected $_jsNotify;
26: 
27:     /**
28:      * An icon URL.
29:      *
30:      * @var string
31:      */
32:     protected $_icon;
33: 
34:     /**
35:      * Constructor.
36:      *
37:      * @param array $params  Any parameters that the handler might need.
38:      *                       Required parameter:
39:      *                       - js_notify: A Horde_Notification_Handler
40:      *                         instance.
41:      *                       Optional parameter:
42:      *                       - icon: URL of an icon to display.
43:      */
44:     public function __construct(array $params = null)
45:     {
46:         if (!isset($params['js_notify'])) {
47:             throw new InvalidArgumentException('Parameter \'js_notify\' missing.');
48:         }
49:         if (!is_callable($params['js_notify'])) {
50:             throw new Horde_Alarm_Exception('Parameter \'js_notify\' is not a valid callback.');
51:         }
52:         $this->_jsNotify = $params['js_notify'];
53:         if (isset($params['icon'])) {
54:             $this->_icon = $params['icon'];
55:         }
56:     }
57: 
58:     /**
59:      * Notifies about an alarm through javscript.
60:      *
61:      * @param array $alarm  An alarm hash.
62:      */
63:     public function notify(array $alarm)
64:     {
65:         $js = sprintf('if(window.webkitNotifications)(function(){function show(){switch(window.webkitNotifications.checkPermission()){case 0:var notify=window.webkitNotifications.createNotification("%s",%s,%s);notify.show();(function(){notify.cancel()}).delay(5);break;case 1:window.webkitNotifications.requestPermission(function(){});break}}show()})()',
66:                       $this->_icon,
67:                       Horde_Serialize::serialize($alarm['title'], Horde_Serialize::JSON),
68:                       isset($alarm['text']) ? Horde_Serialize::serialize($alarm['text'], Horde_Serialize::JSON) : "''");
69:         call_user_func($this->_jsNotify, $js);
70:     }
71: 
72:     /**
73:      * Returns a human readable description of the handler.
74:      *
75:      * @return string
76:      */
77:     public function getDescription()
78:     {
79:         return Horde_Alarm_Translation::t("Desktop notification (with certain browsers)");
80:     }
81: }
82: 
API documentation generated by ApiGen