File: alarms.php

package info (click to toggle)
horde3 3.3.8%2Bdebian0-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 34,220 kB
  • ctags: 28,224
  • sloc: php: 115,191; xml: 4,247; sql: 2,417; sh: 147; makefile: 140
file content (144 lines) | stat: -rw-r--r-- 5,310 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
 * $Horde: horde/admin/alarms.php,v 1.5.2.4 2009/01/06 15:22:10 jan Exp $
 *
 * Copyright 2007-2009 The Horde Project (http://www.horde.org/)
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author Jan Schneider <jan@horde.org>
 */

@define('HORDE_BASE', dirname(__FILE__) . '/..');
require_once HORDE_BASE . '/lib/base.php';
require_once 'Horde/Alarm.php';
require_once 'Horde/Form.php';
require_once 'Horde/Variables.php';

if (!Auth::isAdmin()) {
    Horde::authenticationFailureRedirect();
}

$horde_alarm = Horde_Alarm::factory();
$methods = array();
foreach ($horde_alarm->notificationMethods() as $name => $method) {
    $methods[$name] = $method['__desc'];
}

$vars = Variables::getDefaultVariables();

$form = new Horde_Form($vars, _("Add new alarm"));
$form->addHidden('', 'alarm', 'text', false);
$form->addVariable(_("Alarm title"), 'title', 'text', true);
$form->addVariable(_("Alarm start"), 'start', 'datetime', true);
$form->addVariable(_("Alarm end"), 'end', 'datetime', false);
$form->addVariable(_("Alarm text"), 'text', 'longtext', false);
$form->addVariable(_("Alarm methods"), 'methods', 'multienum', true, false, null, array($methods, min(5, count($methods))));
foreach ($horde_alarm->notificationMethods() as $name => $method) {
    if (count($method) < 2) {
        continue;
    }
    $form->addVariable($method['__desc'], '', 'header', false);
    foreach ($method as $param => $param_info) {
        if (substr($param, 0, 2) == '__') {
            continue;
        }
        $form->addVariable($param_info['desc'], $name . '_' . $param, $param_info['type'], false);
    }
}

if ($form->validate()) {
    $form->getInfo($vars, $info);
    if (empty($info['alarm'])) {
        $info['alarm'] = date('YmdHis') . '.'
            . substr(str_pad(base_convert(microtime(), 10, 36), 16, uniqid(mt_rand()), STR_PAD_LEFT), -16)
            . '@' . $GLOBALS['conf']['server']['name'];
    }

    $params = array();
    foreach ($info['methods'] as $method) {
        foreach ($info as $name => $value) {
            if (strpos($name, $method . '_') === 0) {
                $params[$method][substr($name, strlen($method) + 1)] = $value;
            }
        }
    }

    // Full path to any sound files.
    if (isset($params['notify']['sound'])) {
        $params['notify']['sound'] = $registry->get('themesuri', 'horde') . '/sounds/' . $params['notify']['sound'];
    }

    $result = $horde_alarm->set(
        array('id' => $info['alarm'],
              'title' => $info['title'],
              'text' => $info['text'],
              'start' => new Horde_Date($info['start']),
              'end' => empty($info['end']) ? null : new Horde_Date($info['end']),
              'methods' => $info['methods'],
              'params' => $params));
    if (is_a($result, 'PEAR_Error')) {
        $notification->push($result);
    } else {
        $notification->push(_("The alarm has been saved."), 'horde.success');
    }
}

$id = $vars->get('alarm');
if ($id) {
    if ($vars->get('delete')) {
        $deleted = $horde_alarm->delete($id, '');
        if (is_a($deleted, 'PEAR_Error')) {
            $notification->push($deleted);
            $id = null;
        } else {
            $notification->push(_("The alarm has been deleted."), 'horde.success');
        }
    } else {
        $alarm = $horde_alarm->get($id, '');
        if (is_a($alarm, 'PEAR_Error')) {
            $notification->push($alarm);
            $id = $alarm = null;
        } else {
            $form->setTitle(sprintf(_("Edit \"%s\""), $alarm['title']));
            $vars->set('title', $alarm['title']);
            $vars->set('text', $alarm['text']);
            $vars->set('start', $alarm['start']->timestamp());
            if (!empty($alarm['end'])) {
                $vars->set('end', $alarm['end']->timestamp());
            }
            $vars->set('methods', $alarm['methods']);
            foreach ($alarm['params'] as $method => $params) {
                foreach ($params as $name => $value) {
                    $vars->set($method . '_' . $name, $value);
                }
            }
        }
    }
}

$alarms = $horde_alarm->listAlarms('');

$title = _("Alarms");
require HORDE_TEMPLATES . '/common-header.inc';
require HORDE_TEMPLATES . '/admin/menu.inc';

echo '<h1 class="header">' . _("Current Alarms");
if (is_a($alarms, 'PEAR_Error')) {
    echo '</h1><p class="headerbox"><em>' . sprintf(_("Listing alarms failed: %s"), $alarms->getMessage()) . '</em></p>';
} else {
    echo ' (' . count($alarms) . ')</h1>';
    echo '<ul class="headerbox linedRow">';
    foreach ($alarms as $alarm_details) {
        $url = Util::addParameter(Horde::url('alarms.php'), 'alarm', $alarm_details['id']);
        echo '<li>' . Horde::link(Util::addParameter($url, 'delete', 1), sprintf(_("Delete \"%s\""), $alarm_details['title']), '', '', 'return confirm(\'' . addslashes(sprintf(_("Are you sure you want to delete '%s'?"), $alarm_details['title'])) . '\')') . Horde::img('delete.png') . '</a> ' . Horde::link($url) . htmlspecialchars($alarm_details['title']) . '</a></li>';
    }
    echo '</ul>';
}

echo '<br />';

$form->renderActive();

require HORDE_TEMPLATES . '/common-footer.inc';