1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
4 //require_once 'MTrack/Captcha.php';
7 require_once 'MTrackWeb.php';
8 class MTrackWeb_Ticket extends MTrackWeb
15 var $tid = 0; // or the MD5 rep.
22 //require_once 'MTrack/ACL.php';
23 // MTrackACL::requireAllRights('Browser', 'read');
31 if (!isset($_REQUEST['ajax_body'])) {
34 $this->masterTempalte = 'ticket.html';
35 $this->id = $pi ? $pi: (isset($_GET['id']) ? $_GET['id'] : 0);
36 $this->id = $this->id == 'new' ? 0 : $this->id;
37 $this->id = (int) $this->id;
42 $this->issue = DB_DataObject::factory('mtrack_ticket');
45 if (!$this->issue->get($this->id)) {
46 throw new Exception("Invalid ticket $this->id");
54 //$this->issue->augmentFormFields($this->fieldset());
57 $this->preview = false;
58 $this->error = array();
61 if (!$this->id && !$this->hasPerm('MTrack.Issue','A')) {
62 return HTML_FlexyFramework::run('Noperm');
65 !$this->hasPerm('MTrack.Issue','S') || // general permission to view
66 !$this->ticket->hasPerm($this->authUser,'S') // specific permission on this bug.
69 return HTML_FlexyFramework::run('Noperm');
72 // new is always editable..????
73 $this->editable = $this->id ?
74 $this->ticket->hasPerm($this->authUser,'E') : true;
76 $this->issue->milestoneURL = $this->baseURL.'/Milestone'; // fix me later..
78 $this->showEditBar = false;
80 if ($this->editable && $this->id && !$this->preview) {
81 $this->showEditBar = true;
84 $this->initEditForm();
90 function post($pi=0) // handle the post...
95 if (isset($_POST['cancel'])) {
96 header("Location: {$$this->baseURL}/Ticket/$this->issue->nsident");
100 if (!MTrack_Captcha::check('ticket')) {
101 $this->error[] = "CAPTCHA failed, please try again";
103 $this->preview = isset($_POST['preview']) ? true : false;
108 MTrackACL::requireAllRights("Tickets", 'create');
110 MTrackACL::requireAllRights("ticket:" . $this->issue->tid, 'modify');
112 } catch (Exception $e) {
113 $this->error[] = $e->getMessage();
117 $comment = empty($_POST['comment']) ? '' : $_POST['comment'];
120 if (!strlen($comment)) {
121 $comment = $_POST['summary'];
124 $CS = MTrackChangeset::begin("ticket:X", $comment);
125 } catch (Exception $e) {
126 $this->error[] = $e->getMessage();
130 // compute next id number.
131 // We don't use auto-number, because we allow for importing multiple
132 // projects with their own ticket sequence.
133 // During "normal" user-driven operation, we do want plain old id numbers
134 // so we compute it here, under a transaction
135 $db = MTrackDB::get();
139 switch($db->getAttribute(PDO::ATTR_DRIVER_NAME)) {
141 // Some versions of postgres don't like that we have "abc123" for
142 // identifiers, so match on the bigest number nsident fields only
143 $max = "select max(cast(nsident as integer)) + 1 from tickets where nsident ~ '^\\\\d+$'";
147 $max = "select max(cast(nsident as UNSIGNED)) + 1 from tickets";
151 $max = 'select max(cast(nsident as integer)) + 1 from tickets';
157 list($this->issue->nsident) = MTrackDB::q($max)->fetchAll(PDO::FETCH_COLUMN, 0);
158 if ($this->issue->nsident === null) {
159 $this->issue->nsident = 1;
163 if (isset($_POST['action']) && !$this->preview) {
164 $act= explode('_', $_POST['action'] , 2);
165 //var_dump($act);exit;
170 $this->issue->reOpen();
173 $this->issue->resolution = 'fixed';
174 $this->issue->close();
175 $_POST['estimated'] = $this->issue->estimated;
180 // will be applied to the issue further down
181 $_POST['owner'] = MTrackAuth::whoami();
182 if ($this->issue->status == 'new') {
183 $this->issue->status = 'open';
189 //$this->issue->resolution = $_POST['resolution'];
190 $this->issue->resolution = $act[1];
191 $this->issue->close();
192 $_POST['estimated'] = $this->issue->estimated;
196 $this->issue->status = $act[1];
212 $this->issue->applyPOSTData($_POST);
216 foreach ($fields as $fieldname) {
217 if (isset($_POST[$fieldname]) && strlen($_POST[$fieldname])) {
218 $this->issue->$fieldname = $_POST[$fieldname];
220 $this->issue->$fieldname = null;
224 $kw = $this->issue->getKeywords();
225 $kill = array_values($kw);
226 foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
230 $x = array_search($w, $kw);
232 $k = MTrackKeyword::loadByWord($w);
234 $k = new MTrackKeyword;
238 $this->issue->assocKeyword($k);
240 $w = array_search($w, $kill);
246 foreach ($kill as $w) {
247 $this->issue->dissocKeyword($w);
250 $ms = $this->issue->getMilestones();
252 if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
253 foreach ($_POST['milestone'] as $mid) {
254 $this->issue->assocMilestone($mid);
258 foreach ($kill as $mid) {
259 $this->issue->dissocMilestone($mid);
262 $ms = $this->issue->getComponents();
264 if (isset($_POST['component']) && is_array($_POST['component'])) {
265 foreach ($_POST['component'] as $mid) {
266 $this->issue->assocComponent($mid);
270 foreach ($kill as $mid) {
271 $this->issue->dissocComponent($mid);
274 if (!empty($_POST['comment'])) {
275 $this->issue->addComment($_POST['comment']);
278 $this->issue->addEffort(
279 empty($_POST['spent']) ? 0 : $_POST['spent'],
280 empty($_POST['estimate']) ? 0 : $_POST['estimate']
283 if (!count($this->error)) {
285 $this->issue->save($CS);
287 // make sure everyone is watching it!!!!
288 if($this->issue->owner && $this->issue->tid) {
289 // make sure owner is tracking it...
290 MTrackWatch::watch_object('ticket', $this->issue->tid, $this->issue->owner);
293 if ($this->id == 'new') {
294 MTrackWatch::watch_object('ticket', $this->issue->tid, MTrackAuth::whoami());
298 $CS->setObject("ticket:" . $this->issue->tid);
299 } catch (Exception $e) {
300 $this->error[] = $e->getMessage();
304 if (!count($this->error)) {
305 if (!empty($_FILES['attachments'])) {
306 require_once 'MTrack/Attachment.php';
307 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
309 MTrackAttachment::add("ticket:{$this->issue->tid}",
310 $_FILES['attachments']['tmp_name'][$fileid],
311 $_FILES['attachments']['name'][$fileid],
317 if (!count($this->error) && $this->id != 'new') {
318 require_once 'MTrack/Attachment.php';
319 MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
322 if (isset($_POST['apply']) && !count($this->error)) {
324 header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
330 function initEditForm($params = array())
332 require_once 'HTML/Template/Flexy/Element.php';
333 require_once 'HTML/Template/Flexy/Factory.php';
334 $this->elements = array();
338 foreach(array( 'classification', 'priority', 'severity', 'resolution' ) as $c) {
339 $d = DB_DataObject::factory('core_enum');
341 $d->orderBy('seqid ASC, name ASC');
343 $d->createBaseEntries();
346 $this->elements[$c] = new HTML_Template_Flexy_Element('select');
347 $this->elements[$c]->setOptions($d->fetchAll('id','name'));
351 if ($this->issue->project_id) {
352 $d = DB_DataObject::factory('mtrack_project_component');
353 $d->project_id = $this->issue->project_id;
355 $d->whereAdd('deleted != 1');
356 $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
357 $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
358 $ar = $this->issue->components();
359 $this->elements['component[]']->setValue(array_keys($ar));
362 if ($this->issue->project_id) {
363 $d = DB_DataObject::factory('mtrack_milestone');
364 $d->project_id = $this->issue->project_id;
365 $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
366 $d->whereAdd('completed != 1');
367 $d->whereAdd('deleted != 1');
368 $this->elements['milestone[]'] = new HTML_Template_Flexy_Element('select');
369 $this->elements['milestone[]']->setOptions($d->fetchAll('id', 'name'));
370 $ar = $this->issue->milestones();
371 $this->elements['milestone[]']->setValue(array_keys($ar));
374 // FIX ME - need to determine who the owner is..
375 // for a new issue it's the person who created it.
376 // later on it's an assignement???
380 $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
381 $this->elements['owner']->setOptions($users);
385 // keywords -- in toArray...
389 $this->change_status = array();
390 $this->resolve_status = array();
393 // for coder's they can only change this ticke to certian states
396 // Nasty - I really do not like the acl code in this ...
397 require_once 'MTrack/TicketState.php';
399 $ST = new MTrackTicketState;
400 $ST = $ST->enumerate();
402 unset($ST['closed']);
403 unset($ST[$this->issue->status]);
405 $this->change_status = empty($ST) ? array() : array_keys($ST);
407 $ac = MTrackAuth::getUserClass($this->authUser->userid);
408 //var_dump($ac);exit;
409 // KLUDGE! - remove later...
410 if ($ac == 'admin') {
412 $this->resolve_status= array('fixed');
413 $R = new MTrackResolution;
414 $resolutions = $R->enumerate();
415 unset($resolutions['fixed']);
417 $this->resolve_status= array_keys($resolutions);
418 array_unshift($this->resolve_status, 'fixed');
419 // $html .= $this->mtrack_chg_status('action', 'resolve', 'Resolve as:', 'resolution', $resolutions, $this->issue );
423 // $html .= mtrack_radio('action', 'reopen', $_POST['action']);
424 // $html .= " <label for='reopen'>Reopen ticket</label><br>\n";
428 $this->elements = HTML_Template_Flexy_Factory::fromArray($this->issue->toArray(), $this->elements);
429 if (!empty($_POST)) {
430 $this->elements = HTML_Template_Flexy_Factory::fromArray($_POST, $this->elements);