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->masterTemplate = 'ticket.html';
35 $this->id = $pi ? $pi: (isset($_GET['id']) ? $_GET['id'] : 0);
36 $this->id = (int) $this->id;
41 $this->issue = DB_DataObject::factory('mtrack_ticket');
43 if (!$this->id || !$this->issue->get($this->id)) {
44 $this->jerr("no such ticket");
47 if ($this->issue->project_id != $this->currentProject()) {
48 $this->jerr("not in current project");
55 //$this->issue->augmentFormFields($this->fieldset());
58 $this->preview = false;
59 $this->error = array();
62 if (!$this->id && !$this->hasPerm('MTrack.Issue','A')) {
63 return HTML_FlexyFramework::run('Noperm');
66 !$this->hasPerm('MTrack.Issue','S')
68 return HTML_FlexyFramework::run('Noperm');
71 $this->editable = !$this->hasPerm('MTrack.Issue','E') ;
73 $this->issue->milestoneURL = $this->baseURL.'/Milestone'; // fix me later..
75 $this->showEditBar = false;
77 if ($this->editable && $this->id && !$this->preview) {
78 $this->showEditBar = true;
81 $this->initEditForm();
87 function post($pi=0) // handle the post...
92 if (isset($_POST['cancel'])) {
93 header("Location: {$$this->baseURL}/Ticket/$this->issue->nsident");
97 if (!MTrack_Captcha::check('ticket')) {
98 $this->error[] = "CAPTCHA failed, please try again";
100 $this->preview = isset($_POST['preview']) ? true : false;
105 MTrackACL::requireAllRights("Tickets", 'create');
107 MTrackACL::requireAllRights("ticket:" . $this->issue->tid, 'modify');
109 } catch (Exception $e) {
110 $this->error[] = $e->getMessage();
114 $comment = empty($_POST['comment']) ? '' : $_POST['comment'];
117 if (!strlen($comment)) {
118 $comment = $_POST['summary'];
121 $CS = MTrackChangeset::begin("ticket:X", $comment);
122 } catch (Exception $e) {
123 $this->error[] = $e->getMessage();
127 // compute next id number.
128 // We don't use auto-number, because we allow for importing multiple
129 // projects with their own ticket sequence.
130 // During "normal" user-driven operation, we do want plain old id numbers
131 // so we compute it here, under a transaction
132 $db = MTrackDB::get();
136 switch($db->getAttribute(PDO::ATTR_DRIVER_NAME)) {
138 // Some versions of postgres don't like that we have "abc123" for
139 // identifiers, so match on the bigest number nsident fields only
140 $max = "select max(cast(nsident as integer)) + 1 from tickets where nsident ~ '^\\\\d+$'";
144 $max = "select max(cast(nsident as UNSIGNED)) + 1 from tickets";
148 $max = 'select max(cast(nsident as integer)) + 1 from tickets';
154 list($this->issue->nsident) = MTrackDB::q($max)->fetchAll(PDO::FETCH_COLUMN, 0);
155 if ($this->issue->nsident === null) {
156 $this->issue->nsident = 1;
160 if (isset($_POST['action']) && !$this->preview) {
161 $act= explode('_', $_POST['action'] , 2);
162 //var_dump($act);exit;
167 $this->issue->reOpen();
170 $this->issue->resolution = 'fixed';
171 $this->issue->close();
172 $_POST['estimated'] = $this->issue->estimated;
177 // will be applied to the issue further down
178 $_POST['owner'] = MTrackAuth::whoami();
179 if ($this->issue->status == 'new') {
180 $this->issue->status = 'open';
186 //$this->issue->resolution = $_POST['resolution'];
187 $this->issue->resolution = $act[1];
188 $this->issue->close();
189 $_POST['estimated'] = $this->issue->estimated;
193 $this->issue->status = $act[1];
209 $this->issue->applyPOSTData($_POST);
213 foreach ($fields as $fieldname) {
214 if (isset($_POST[$fieldname]) && strlen($_POST[$fieldname])) {
215 $this->issue->$fieldname = $_POST[$fieldname];
217 $this->issue->$fieldname = null;
221 $kw = $this->issue->getKeywords();
222 $kill = array_values($kw);
223 foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
227 $x = array_search($w, $kw);
229 $k = MTrackKeyword::loadByWord($w);
231 $k = new MTrackKeyword;
235 $this->issue->assocKeyword($k);
237 $w = array_search($w, $kill);
243 foreach ($kill as $w) {
244 $this->issue->dissocKeyword($w);
247 $ms = $this->issue->getMilestones();
249 if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
250 foreach ($_POST['milestone'] as $mid) {
251 $this->issue->assocMilestone($mid);
255 foreach ($kill as $mid) {
256 $this->issue->dissocMilestone($mid);
259 $ms = $this->issue->getComponents();
261 if (isset($_POST['component']) && is_array($_POST['component'])) {
262 foreach ($_POST['component'] as $mid) {
263 $this->issue->assocComponent($mid);
267 foreach ($kill as $mid) {
268 $this->issue->dissocComponent($mid);
271 if (!empty($_POST['comment'])) {
272 $this->issue->addComment($_POST['comment']);
275 $this->issue->addEffort(
276 empty($_POST['spent']) ? 0 : $_POST['spent'],
277 empty($_POST['estimate']) ? 0 : $_POST['estimate']
280 if (!count($this->error)) {
282 $this->issue->save($CS);
284 // make sure everyone is watching it!!!!
285 if($this->issue->owner && $this->issue->tid) {
286 // make sure owner is tracking it...
287 MTrackWatch::watch_object('ticket', $this->issue->tid, $this->issue->owner);
290 if ($this->id == 'new') {
291 MTrackWatch::watch_object('ticket', $this->issue->tid, MTrackAuth::whoami());
295 $CS->setObject("ticket:" . $this->issue->tid);
296 } catch (Exception $e) {
297 $this->error[] = $e->getMessage();
301 if (!count($this->error)) {
302 if (!empty($_FILES['attachments'])) {
303 require_once 'MTrack/Attachment.php';
304 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
306 MTrackAttachment::add("ticket:{$this->issue->tid}",
307 $_FILES['attachments']['tmp_name'][$fileid],
308 $_FILES['attachments']['name'][$fileid],
314 if (!count($this->error) && $this->id != 'new') {
315 require_once 'MTrack/Attachment.php';
316 MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
319 if (isset($_POST['apply']) && !count($this->error)) {
321 header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
327 function initEditForm($params = array())
329 require_once 'HTML/Template/Flexy/Element.php';
330 require_once 'HTML/Template/Flexy/Factory.php';
331 $this->elements = array();
335 foreach(array( 'classification', 'priority', 'severity', 'resolution' ) as $c) {
336 $d = DB_DataObject::factory('core_enum');
338 $d->orderBy('seqid ASC, name ASC');
340 $d->createBaseEntries();
343 $this->elements[$c] = new HTML_Template_Flexy_Element('select');
344 $this->elements[$c]->setOptions($d->fetchAll('id','name'));
348 if ($this->issue->project_id) {
349 $d = DB_DataObject::factory('mtrack_project_component');
350 $d->project_id = $this->issue->project_id;
352 $d->whereAdd('deleted != 1');
353 $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
354 $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
355 $ar = $this->issue->components();
356 $this->elements['component[]']->setValue(array_keys($ar));
359 if ($this->issue->project_id) {
360 $d = DB_DataObject::factory('mtrack_milestone');
361 $d->project_id = $this->issue->project_id;
362 $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
363 $d->whereAdd('completed != 1');
364 $d->whereAdd('deleted != 1');
365 $this->elements['milestone[]'] = new HTML_Template_Flexy_Element('select');
366 $this->elements['milestone[]']->setOptions($d->fetchAll('id', 'name'));
367 $ar = $this->issue->milestones();
368 $this->elements['milestone[]']->setValue(array_keys($ar));
371 // FIX ME - need to determine who the owner is..
372 // for a new issue it's the person who created it.
373 // later on it's an assignement???
377 $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
378 $this->elements['owner']->setOptions($users);
382 // keywords -- in toArray...
386 $this->change_status = array();
387 $this->resolve_status = array();
390 // for coder's they can only change this ticke to certian states
393 // Nasty - I really do not like the acl code in this ...
394 require_once 'MTrack/TicketState.php';
396 $ST = new MTrackTicketState;
397 $ST = $ST->enumerate();
399 unset($ST['closed']);
400 unset($ST[$this->issue->status]);
402 $this->change_status = empty($ST) ? array() : array_keys($ST);
404 $ac = MTrackAuth::getUserClass($this->authUser->userid);
405 //var_dump($ac);exit;
406 // KLUDGE! - remove later...
407 if ($ac == 'admin') {
409 $this->resolve_status= array('fixed');
410 $R = new MTrackResolution;
411 $resolutions = $R->enumerate();
412 unset($resolutions['fixed']);
414 $this->resolve_status= array_keys($resolutions);
415 array_unshift($this->resolve_status, 'fixed');
416 // $html .= $this->mtrack_chg_status('action', 'resolve', 'Resolve as:', 'resolution', $resolutions, $this->issue );
420 // $html .= mtrack_radio('action', 'reopen', $_POST['action']);
421 // $html .= " <label for='reopen'>Reopen ticket</label><br>\n";
425 $this->elements = HTML_Template_Flexy_Factory::fromArray($this->issue->toArray(), $this->elements);
426 if (!empty($_POST)) {
427 $this->elements = HTML_Template_Flexy_Factory::fromArray($_POST, $this->elements);