1<?php
2/**
3 * Basic Ticket Editing form.
4 *
5 * @author  Chuck Hagenbuch <chuck@horde.org>
6 * @package Whups
7 */
8class Whups_Form_Ticket_Edit extends Horde_Form
9{
10    public function __construct(&$vars, &$ticket, $title = '')
11    {
12        global $whups_driver;
13
14        parent::__construct($vars, $title);
15        $type = $vars->get('type');
16
17        $start_year = date('Y');
18        if (is_numeric($d = $vars->get('due'))) {
19            $start_year = min($start_year, date('Y', $d));
20        }
21
22        $fields = array('summary');
23
24        $qinfo = $whups_driver->getQueue($vars->get('queue'));
25        if (!empty($qinfo['versioned'])) {
26            $fields[] = 'version';
27        }
28
29        $fields = array_merge($fields, array('state', 'priority', 'due'));
30        try {
31            $attributes = $ticket->addAttributes();
32        } catch (Whups_Exception $e) {
33            $attributes = array();
34        }
35        foreach ($attributes as $attribute) {
36            $fields[] = 'attribute_' . $attribute['id'];
37        }
38        $fields = array_merge($fields, array('owner', 'attachments', 'comment'));
39
40        $grouped_fields = array($fields);
41        $grouped_hook = false;
42        try {
43            $grouped_fields = Horde::callHook(
44                'group_fields',
45                array($ticket->get('type'), $fields),
46                'whups');
47            $grouped_hook = true;
48        } catch (Horde_Exception_HookNotSet $e) {
49        } catch (Horde_Exception $e) {
50            Horde::log($e, 'ERR');
51        }
52
53        $this->addHidden('', 'id', 'int', true, true);
54        $this->addHidden('', 'type', 'int', true, true);
55
56        foreach ($grouped_fields as $header => $fields) {
57            if ($grouped_hook) {
58                $this->addVariable($header, null, 'header', false);
59            }
60            foreach ($fields as $field) {
61                switch ($field) {
62                case 'summary':
63                    $this->addVariable(_("Summary"), 'summary', 'text', true);
64                    break;
65
66                case 'version':
67                    $versions = $whups_driver->getVersions($vars->get('queue'));
68                    if (count($versions) == 0) {
69                        $vtype = 'invalid';
70                        $v_params = array(_("This queue requires that you specify a version, but there are no versions associated with it. Until versions are created for this queue, you will not be able to create tickets."));
71                    } else {
72                        $vtype = 'enum';
73                        $v_params = array($versions);
74                    }
75                    $this->addVariable(_("Queue Version"), 'version', $vtype, true, false, null, $v_params);
76                    break;
77
78                case 'state':
79                    $this->addVariable(
80                        _("State"), 'state', 'enum', true, false, null,
81                        array($whups_driver->getStates($type)));
82                    break;
83
84                case 'priority':
85                    $this->addVariable(
86                        _("Priority"), 'priority', 'enum', true, false, null,
87                        array($whups_driver->getPriorities($type)));
88                    break;
89
90                case 'due':
91                    $this->addVariable(
92                        _("Due Date"), 'due', 'datetime', false, false, null,
93                        array($start_year));
94                    break;
95
96                case 'owner':
97                    if (Whups::hasPermission($vars->get('queue'), 'queue', 'assign')) {
98                        $groups = $GLOBALS['injector']->getInstance('Horde_Group');
99                        $mygroups = $groups->listAll($GLOBALS['conf']['prefs']['assign_all_groups'] ? null : $GLOBALS['registry']->getAuth());
100                        asort($mygroups);
101
102                        $f_users = array();
103                        $users = $whups_driver->getQueueUsers($vars->get('queue'));
104                        foreach ($users as $user) {
105                            $f_users['user:' . $user] = Whups::formatUser($user);
106                        }
107
108                        $f_groups = array();
109                        if ($mygroups) {
110                            foreach (array_keys($mygroups) as $id) {
111                                $f_groups['group:' . $id] = $groups->getName($id);
112                            }
113                        }
114
115                        if (count($f_users)) {
116                            asort($f_users);
117                            $this->addVariable(
118                                _("Owners"),
119                               'owners',
120                               'multienum',
121                               false, false, null,
122                               array($f_users));
123                        }
124
125                        if (count($f_groups)) {
126                            asort($f_groups);
127                            $this->addVariable(
128                                _("Group Owners"),
129                                'group_owners',
130                                'multienum',
131                                false, false,
132                                null,
133                                array($f_groups));
134                        }
135                    }
136                    break;
137
138                case 'attachments':
139                    $this->addVariable(
140                        _("Attachment"), 'newattachment', 'file', false);
141                    break;
142
143                case 'comment':
144                    $cvar = $this->addVariable(
145                        _("Comment"), 'newcomment', 'longtext', false);
146
147                    /* Form replies. */
148                    try {
149                        $replies = Whups::permissionsFilter(
150                            $whups_driver->getReplies($type), 'reply');
151                    } catch (Whups_Exception $e) {
152                        $replies = array();
153                    }
154                    if (count($replies)) {
155                        $params = array();
156                        foreach ($replies as $key => $reply) {
157                            $params[$key] = $reply['reply_name'];
158                        }
159                        $rvar = $this->addVariable(
160                            _("Form Reply:"), 'reply', 'enum', false, false,
161                             null, array($params, true));
162                        $rvar->setAction(Horde_Form_Action::factory('reload'));
163                        if ($vars->get('reply')) {
164                            $reply = $vars->get('newcomment');
165                            if (strlen($reply)) {
166                                $reply .= "\n\n";
167                            }
168                            $reply .= $replies[$vars->get('reply')]['reply_text'];
169                            $vars->set('newcomment', $reply);
170                            $vars->remove('reply');
171                        }
172                    }
173
174                    /* Comment permissions. */
175                    $groups = $GLOBALS['injector']->getInstance('Horde_Group');
176                    $mygroups = $groups->listGroups($GLOBALS['registry']->getAuth());
177                    if ($mygroups) {
178                        foreach (array_keys($mygroups) as $gid) {
179                            $grouplist[$gid] = $groups->getName($gid, true);
180                        }
181                        asort($grouplist);
182                        $grouplist = array(0 => _("This comment is visible to everyone")) + $grouplist;
183                        $this->addVariable(
184                            _("Make this comment visible only to members of a group?"), 'group',
185                            'enum', false, false, null, array($grouplist));
186                    }
187                    break;
188
189                default:
190                    /* Ticket attributes. */
191                    if ($ticket &&
192                        substr($field, 0, 10) == 'attribute_' &&
193                        isset($attributes[substr($field, 10)])) {
194                        $attribute = $attributes[substr($field, 10)];
195                        $var = $this->addVariable(
196                            $attribute['human_name'],
197                            'attribute_' . $attribute['id'],
198                            $attribute['type'],
199                            $attribute['required'],
200                            $attribute['readonly'],
201                            $attribute['desc'],
202                            $attribute['params']);
203                        $var->setDefault($attribute['value']);
204                    }
205                }
206            }
207        }
208    }
209
210    public function validate(&$vars)
211    {
212        if (!$GLOBALS['registry']->getAuth()) {
213            $this->setError('_auth', _("Permission Denied."));
214        }
215
216        return parent::validate($vars);
217    }
218
219}
220