1<?php
2/**
3 * Copyright 2001-2002 Robert E. Coyle <robertecoyle@hotmail.com>
4 * Copyright 2001-2017 Horde LLC (http://www.horde.org/)
5 *
6 * See the enclosed file LICENSE for license information (BSD). If you
7 * did not receive this file, see http://www.horde.org/licenses/bsdl.php.
8 */
9
10require_once __DIR__ . '/../lib/Application.php';
11Horde_Registry::appInit('whups');
12
13$ticket = Whups::getCurrentTicket();
14$page_output->addLinkTag($ticket->feedLink());
15
16if (!Whups::hasPermission($ticket->get('queue'), 'queue', 'update')) {
17    $notification->push(_("Permission Denied"), 'horde.error');
18    Horde::url($prefs->getValue('whups_default_view') . '.php', true)
19        ->redirect();
20}
21
22Whups::addTopbarSearch();
23
24$vars = Horde_Variables::getDefaultVariables();
25$ticket->setDetails($vars, true);
26$id = $vars->id;
27if ($tid = $vars->get('transaction')) {
28    $history = Whups::permissionsFilter($whups_driver->getHistory($ticket->getId()),
29                                        'comment', Horde_Perms::READ);
30    if (!empty($history[$tid]['comment'])) {
31        // If this was a restricted comment, load the group_id it was
32        // restricted to and default to keeping that restriction on
33        // the reply.
34        foreach ($history[$tid]['changes'] as $change) {
35            if (!empty($change['private'])) {
36                $permission = $GLOBALS['injector']
37                    ->getInstance('Horde_Perms')
38                    ->getPermission('whups:comments:' . $change['value']);
39                $group_id = array_shift(array_keys($permission->getGroupPermissions()));
40                $vars->set('group', $group_id);
41                break;
42            }
43        }
44
45        $flowed = new Horde_Text_Flowed(preg_replace("/\s*\n/U", "\n", $history[$tid]['comment']), 'UTF-8');
46        $vars->set('newcomment', $flowed->toFlowed(true));
47    }
48}
49
50// Edit action.
51$title = '[#' . $id . '] ' . $ticket->get('summary');
52$editform = new Whups_Form_Ticket_Edit($vars, $ticket, sprintf(_("Update %s"), $title));
53if ($vars->get('formname') == 'whups_form_ticket_edit') {
54    if ($editform->validate($vars)) {
55        $editform->getInfo($vars, $info);
56
57        $ticket->change('summary', $info['summary']);
58        $ticket->change('state', $info['state']);
59        $ticket->change('priority', $info['priority']);
60        $ticket->change('due', $info['due']);
61        if (!empty($info['version'])) {
62            $ticket->change('version', $info['version']);
63        }
64        if (!empty($info['newcomment'])) {
65            $ticket->change('comment', $info['newcomment']);
66        }
67
68        // Update user and group assignments.
69        if (Whups::hasPermission($vars->get('queue'), 'queue', 'assign')) {
70            $ticket->change('owners', array_merge(isset($info['owners']) ? $info['owners'] : array(),
71                                                  isset($info['group_owners']) ? $info['group_owners'] : array()));
72        }
73
74        // Update attributes.
75        $whups_driver->setAttributes($info, $ticket);
76
77        // Add attachment if one was uploaded.
78        if (!empty($info['newattachment']['name'])) {
79            $ticket->change('attachment', array('name' => $info['newattachment']['name'],
80                                                'tmp_name' => $info['newattachment']['tmp_name']));
81        }
82
83        // If there was a new comment and permissions were specified
84        // on it, set them.
85        if (!empty($info['group'])) {
86            $ticket->change('comment-perms', $info['group']);
87        }
88        try {
89            $ticket->commit();
90            $notification->push(_("Ticket Updated"), 'horde.success');
91            $ticket->show();
92        } catch (Whups_Exception $e) {
93            $notification->push($e, 'horde.error');
94        }
95    }
96}
97
98$page_output->header(array(
99    'title' => $title
100));
101$notification->notify(array('listeners' => 'status'));
102require WHUPS_TEMPLATES . '/prevnext.inc';
103
104$tabs = Whups::getTicketTabs($vars, $id);
105echo $tabs->render('update');
106
107$editform->renderActive($editform->getRenderer(), $vars, Horde::url('ticket/update.php'), 'post');
108echo '<br class="spacer" />';
109
110$form = new Whups_Form_TicketDetails($vars, $ticket, $title);
111$ticket->setDetails($vars);
112$form->renderInactive($form->getRenderer(), $vars);
113
114$page_output->footer();
115