1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16: 17:
18: class Horde_Alarm_Handler_Desktop extends Horde_Alarm_Handler
19: {
20: 21: 22: 23: 24:
25: protected $_jsNotify;
26:
27: 28: 29: 30: 31:
32: protected $_icon;
33:
34: 35: 36: 37: 38: 39: 40: 41: 42: 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: 60: 61: 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: 74: 75: 76:
77: public function getDescription()
78: {
79: return Horde_Alarm_Translation::t("Desktop notification (with certain browsers)");
80: }
81: }
82: