File: task.php

package info (click to toggle)
nag2 2.1.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,120 kB
  • ctags: 766
  • sloc: php: 3,066; xml: 304; sql: 132; makefile: 64; sh: 39
file content (319 lines) | stat: -rw-r--r-- 13,785 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/**
 * $Horde: nag/task.php,v 1.80.8.6 2006/01/01 21:29:06 jan Exp $
 *
 * Copyright 2001-2006 Jon Parise <jon@horde.org>
 *
 * See the enclosed file COPYING for license information (GPL).  If you
 * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
 */

@define('NAG_BASE', dirname(__FILE__));
require_once NAG_BASE . '/lib/base.php';
require_once 'Horde/Prefs/CategoryManager.php';
$cManager = &new Prefs_CategoryManager();

/* Redirect to the task list if no action has been requested. */
$actionID = Util::getFormData('actionID');
if (is_null($actionID)) {
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
}

$url = Util::getFormData('url');

/* Run through the action handlers. */
switch ($actionID) {
case 'add_task':
    /* Check permissions. */
    if (Nag::hasPermission('max_tasks') !== true &&
        Nag::hasPermission('max_tasks') <= Nag::countTasks()) {
        $message = @htmlspecialchars(sprintf(_("You are not allowed to create more than %d tasks."), Nag::hasPermission('max_tasks')), ENT_COMPAT, NLS::getCharset());
        if (!empty($conf['hooks']['permsdenied'])) {
            $message = Horde::callHook('_perms_hook_denied', array('nag:max_tasks'), 'horde', $message);
        }
        $notification->push($message, 'horde.error', array('content.raw'));
        header('Location: ' . Horde::applicationUrl('list.php', true));
        exit;
    }
    /* Set up the task attributes. */
    $tasklist_id = Nag::getDefaultTasklist(PERMS_EDIT);
    $task_id = null;
    $task_name = '';
    $task_due = 0;
    $task_desc = '';
    $task_priority = 3;
    $task_completed = 0;
    $task_category = '';

    $task_alarm = 0;
    $alarm_value = 15;
    $alarm_unit = 'min';
    $alarm_set = false;

    /* Set the initial due date to today. */
    require_once NAG_BASE . '/lib/Widgets.php';
    $initial_date = getdate();
    $javascript = 'onchange="document.task.due_type[1].checked = true;"';
    $day_widget = Widgets::buildDayWidget('due[day]', $initial_date['mday'], $javascript);
    $month_widget = Widgets::buildMonthWidget('due[month]', $initial_date['mon'], $javascript);
    $year_widget = Widgets::buildYearWidget('due[year]', 3, $initial_date['year'], $javascript);
    $hour_widget = Widgets::buildHourWidget('due_hour', 8, $javascript);
    $minute_widget = Widgets::buildMinuteWidget('due_minute', 15, null, $javascript);
    $am_pm_widget = Widgets::buildAmPmWidget('due_am_pm', $initial_date['hours'], $javascript, $javascript);

    /* Set up the radio buttons. */
    $none_checked = ($task_due == 0) ? 'checked="checked" ' : '';
    $specified_checked = ($task_due != 0) ? 'checked="checked" ' : '';

    $title = _("Adding A New Task");
    break;

case 'modify_task':
    $task_id = Util::getFormData('task');
    $tasklist_id = Util::getFormData('tasklist');
    $task = Nag::getTask($tasklist_id, $task_id);
    $alarm_value = 15;
    $alarm_unit = 'min';
    $alarm_set = false;

    if (isset($task) && isset($task['task_id'])) {
        /* Set up the task attributes. */
        $task_name = $task['name'];
        $task_due = $task['due'];
        $task_desc = $task['desc'];
        $task_priority = $task['priority'];
        $task_completed = $task['completed'];
        $task_category = $task['category'];
        $task_alarm = $task['alarm'];

        /* If the due date isn't set, set the widgets to the
         * default. */
        $due_date = getdate(($task_due > 0) ? $task_due : (time() + 604800));
        $javascript = 'onchange="document.task.due_type[1].checked = true;"';

        /* Set up alarm widget data. */
        if ($task_alarm) {
            $alarm_set = true;
            if ($task_alarm % 10080 == 0) {
                $alarm_value = $task_alarm / 10080;
                $alarm_unit = 'week';
            } elseif ($task_alarm % 1440 == 0) {
                $alarm_value = $task_alarm / 1440;
                $alarm_unit = 'day';
            } elseif ($task_alarm % 60 == 0) {
                $alarm_value = $task_alarm / 60;
                $alarm_unit = 'hour';
            } else {
                $alarm_value = $task_alarm;
                $alarm_unit = 'min';
            }
        }

        /* Set up the due date selection widgets. */
        require_once NAG_BASE . '/lib/Widgets.php';
        $day_widget = Widgets::buildDayWidget('due[day]', $due_date['mday'], $javascript);
        $month_widget = Widgets::buildMonthWidget('due[month]', $due_date['mon'], $javascript);
        $year_widget = Widgets::buildYearWidget('due[year]', 3, $due_date['year'], $javascript);
        $hour_widget = Widgets::buildHourWidget('due_hour', $due_date['hours'], $javascript);
        $minute_widget = Widgets::buildMinuteWidget('due_minute', 15, $due_date['minutes'], $javascript);
        $am_pm_widget = Widgets::buildAmPmWidget('due_am_pm', $due_date['hours'], $javascript, $javascript);

        /* Set up the radio buttons. */
        $none_checked = ($task_due == 0) ? 'checked="checked" ' : '';
        $specified_checked = ($task_due > 0) ? 'checked="checked" ' : '';

        $title = _("Modifying:") . ' ' . $task_name;
    } else {
        $notification->push(_("Task not found."), 'horde.error');
        header('Location: ' . Horde::applicationUrl('list.php', true));
        exit;
    }
    break;

case 'save_task':
    /* Get the form values. */
    $task_id = Util::getFormData('task');
    $tasklist_original = Util::getFormData('tasklist_original');
    $tasklist_target = Util::getFormData('tasklist_target');

    $share = $GLOBALS['nag_shares']->getShare($tasklist_target);
    if (is_a($share, 'PEAR_Error')) {
        $notification->push(sprintf(_("Access denied saving task: %s"), $share->getMessage()), 'horde.error');
    } elseif (!$share->hasPermission(Auth::getAuth(), PERMS_EDIT)) {
        $notification->push(sprintf(_("Access denied saving task to %s."), $share->get('name')), 'horde.error');
    } else {
        $task_name = Util::getFormData('task_name');
        $task_desc = Util::getFormData('task_desc');
        $task_priority = Util::getFormData('task_priority');
        $task_completed = Util::getFormData('task_completed') ? 1 : 0;
        $task_category = Util::getFormData('task_category');
        $due_type = Util::getFormData('due_type');
        $due = Util::getFormData('due');
        $due_day = !empty($due['day']) ? $due['day'] : null;
        $due_month = !empty($due['month']) ? $due['month'] : null;
        $due_year = !empty($due['year']) ? $due['year'] : null;
        $due_hour = Util::getFormData('due_hour');
        $due_minute = Util::getFormData('due_minute');
        if (!$prefs->getValue('twentyFour')) {
            $due_am_pm = Util::getFormData('due_am_pm');
            if ($due_am_pm == 'pm') {
                if ($due_hour < 12) {
                    $due_hour = $due_hour + 12;
                }
            } else {
                // Adjust 12:xx AM times.
                if ($due_hour == 12) {
                    $due_hour = 0;
                }
            }
        }
        $alarm_set = Util::getFormData('alarm');
        $alarm_unit = Util::getFormData('alarm_unit');
        $alarm_value = Util::getFormData('alarm_value');
        if ($alarm_set) {
            /* is_int() doesn't work here. */
            if ((string)(int)$alarm_value != $alarm_value) {
                $notification->push(_("The alarm field may only contain integers."), 'horde.warning');
                $task_alarm = 0;
            } else {
                $task_alarm = $alarm_value * $alarm_unit ;
            }
        } else {
            $task_alarm = 0;
        }

        if ($new_category = Util::getFormData('new_category')) {
            $new_category = $cManager->add($new_category);
            if ($new_category) {
                $task_category = $new_category;
            }
        }

        /* Convert the due date to Unix time. */
        $due_str = "$due_month/$due_day/$due_year $due_hour:$due_minute";

        /* Set the due date according to the $due_type toggle. */
        $task_due = strcasecmp($due_type, 'none') ? strtotime($due_str) : 0;

        /* If $task_id is set, we're modifying an existing task. Otherwise,
         * we're adding a new task with the provided attributes. */
        if ($task_id != null && !empty($tasklist_original)) {
            $storage = &Nag_Driver::singleton($tasklist_original);
            $result = $storage->modify($task_id, $task_name, $task_desc,
                                       $task_due, $task_priority,
                                       $task_completed, $task_category,
                                       $task_alarm);

            if (!is_a($result, 'PEAR_Error') &&
                $tasklist_original != $tasklist_target) {
                /* Moving the task to another tasklist. */
                $share = $GLOBALS['nag_shares']->getShare($tasklist_original);
                if (!is_a($share, 'PEAR_Error') &&
                    $share->hasPermission(Auth::getAuth(), PERMS_DELETE)) {
                    $share = $GLOBALS['nag_shares']->getShare($tasklist_original);
                    if (!is_a($share, 'PEAR_Error') &&
                        $share->hasPermission(Auth::getAuth(), PERMS_EDIT)) {
                        $result = $storage->move($task_id, $tasklist_target);
                    } else {
                        $notification->push(sprintf(_("Access denied moving the task to %s."), $share->get('name')), 'horde.error');
                    }
                } else {
                    $notification->push(sprintf(_("Access denied removing task from %s."), $share->get('name')), 'horde.error');
                }
            }
        } else {
            /* Check permissions. */
            if (Nag::hasPermission('max_tasks') !== true &&
                Nag::hasPermission('max_tasks') <= Nag::countTasks()) {
                header('Location: ' . Horde::applicationUrl('list.php', true));
                exit;
            }
            /* Creating a new task. */
            $storage = &Nag_Driver::singleton($tasklist_target);
            $result = $storage->add($task_name, $task_desc, $task_due,
                                    $task_priority, $task_completed,
                                    $task_category, $task_alarm);
        }

        // Check our results.
        if (is_a($result, 'PEAR_Error')) {
            $notification->push(sprintf(_("There was a problem saving the task: %s."), $result->getMessage()), 'horde.error');
        } else {
            $notification->push(sprintf(_("Saved %s."), $task_name), 'horde.success');
        }
    }

    /* Return to the task list. */
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
    break;

case 'delete_tasks':
    /* Delete the task if we're provided with a valid task ID. */
    $task_id = Util::getFormData('task');
    $tasklist_id = Util::getFormData('tasklist');
    if (isset($task_id)) {
        $share = $GLOBALS['nag_shares']->getShare($tasklist_id);
        $task = Nag::getTask($tasklist_id, $task_id);
        if (is_a($share, 'PEAR_Error') || !$share->hasPermission(Auth::getAuth(), PERMS_DELETE)) {
            $notification->push(sprintf(_("Access denied deleting %s."), $task['name']), 'horde.error');
        } else {
            $storage = &Nag_Driver::singleton($tasklist_id);
            $result = $storage->delete($task_id);
            if (is_a($result, 'PEAR_Error')) {
                $notification->push(sprintf(_("There was a problem deleting %s: %s"),
                                            $task['name'], $result->getMessage()), 'horde.error');
            } else {
                $notification->push(sprintf(_("Deleted %s."), $task['name']), 'horde.success');
            }
        }
    }

    /* Return to the task list. */
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
    break;

case 'complete_task':
    /* Complete the task if we're provided with a valid task ID. */
    $task_id = Util::getFormData('task');
    $tasklist_id = Util::getFormData('tasklist');
    if (isset($task_id)) {
        $share = $GLOBALS['nag_shares']->getShare($tasklist_id);
        $task = Nag::getTask($tasklist_id, $task_id);
        if (is_a($share, 'PEAR_Error') || !$share->hasPermission(Auth::getAuth(), PERMS_EDIT)) {
            $notification->push(sprintf(_("Access denied completing task %s."), $task['name']), 'horde.error');
        } else {
            $storage = &Nag_Driver::singleton($tasklist_id);
            $result = $storage->modify($task_id, $task['name'], $task['desc'], $task['due'],
                                       $task['priority'], 1, $task['category'], $task['alarm']);
            if (is_a($result, 'PEAR_Error')) {
                $notification->push(sprintf(_("There was a problem completing %s: %s"),
                                            $task['name'], $result->getMessage()), 'horde.error');
            } else {
                $notification->push(sprintf(_("Completed %s."), $task['name']), 'horde.success');
            }
        }
    }

    if (isset($url)) {
        header('Location: ' . $url);
    } else {
        header('Location: ' . Horde::applicationUrl('list.php', true));
    }
    exit;
    break;

default:
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
}

$notification->push('document.task.task_name.focus()', 'javascript');
$tasklists = Nag::listTasklists(false, PERMS_EDIT);
Horde::addScriptFile('stripe.js', 'horde', true);
require NAG_TEMPLATES . '/common-header.inc';
require NAG_TEMPLATES . '/menu.inc';
require NAG_TEMPLATES . '/task/task.inc';
require $registry->get('templates', 'horde') . '/common-footer.inc';