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_TicketEdit 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_edit.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");
48 if ($this->id && $this->currentProject() != $this->issue->project_id) {
49 throw new Exception("Project and ticket do not match..");
56 //$this->issue->augmentFormFields($this->fieldset());
59 $this->preview = false;
60 $this->error = array();
63 if (!$this->id && !$this->hasPerm('MTrack.Issue','A')) {
64 return HTML_FlexyFramework::run('Noperm');
69 !$this->hasPerm('MTrack.Issue','S') || // general permission to view
70 !$this->ticket->hasPerm($this->authUser,'S') // specific permission on this bug.
73 return HTML_FlexyFramework::run('Noperm');
81 // new is always editable..????
82 $this->editable = $this->id ?
83 $this->ticket->hasPerm($this->authUser,'E') : true;
85 $this->issue->milestoneURL = $this->baseURL.'/Milestone'; // fix me later..
87 $this->showEditBar = false;
89 if ($this->editable && $this->id && !$this->preview) {
90 $this->showEditBar = true;
93 $this->initEditForm();
99 function post($pi=0) // handle the post...
104 if (isset($_POST['cancel'])) {
105 header("Location: {$$this->baseURL}/Ticket/$this->issue->nsident");
109 if (!MTrack_Captcha::check('ticket')) {
110 $this->error[] = "CAPTCHA failed, please try again";
112 $this->preview = isset($_POST['preview']) ? true : false;
117 MTrackACL::requireAllRights("Tickets", 'create');
119 MTrackACL::requireAllRights("ticket:" . $this->issue->tid, 'modify');
121 } catch (Exception $e) {
122 $this->error[] = $e->getMessage();
126 $comment = empty($_POST['comment']) ? '' : $_POST['comment'];
129 if (!strlen($comment)) {
130 $comment = $_POST['summary'];
133 $CS = MTrackChangeset::begin("ticket:X", $comment);
134 } catch (Exception $e) {
135 $this->error[] = $e->getMessage();
139 // compute next id number.
140 // We don't use auto-number, because we allow for importing multiple
141 // projects with their own ticket sequence.
142 // During "normal" user-driven operation, we do want plain old id numbers
143 // so we compute it here, under a transaction
144 $db = MTrackDB::get();
148 switch($db->getAttribute(PDO::ATTR_DRIVER_NAME)) {
150 // Some versions of postgres don't like that we have "abc123" for
151 // identifiers, so match on the bigest number nsident fields only
152 $max = "select max(cast(nsident as integer)) + 1 from tickets where nsident ~ '^\\\\d+$'";
156 $max = "select max(cast(nsident as UNSIGNED)) + 1 from tickets";
160 $max = 'select max(cast(nsident as integer)) + 1 from tickets';
166 list($this->issue->nsident) = MTrackDB::q($max)->fetchAll(PDO::FETCH_COLUMN, 0);
167 if ($this->issue->nsident === null) {
168 $this->issue->nsident = 1;
172 if (isset($_POST['action']) && !$this->preview) {
173 $act= explode('_', $_POST['action'] , 2);
174 //var_dump($act);exit;
179 $this->issue->reOpen();
182 $this->issue->resolution = 'fixed';
183 $this->issue->close();
184 $_POST['estimated'] = $this->issue->estimated;
189 // will be applied to the issue further down
190 $_POST['owner'] = MTrackAuth::whoami();
191 if ($this->issue->status == 'new') {
192 $this->issue->status = 'open';
198 //$this->issue->resolution = $_POST['resolution'];
199 $this->issue->resolution = $act[1];
200 $this->issue->close();
201 $_POST['estimated'] = $this->issue->estimated;
205 $this->issue->status = $act[1];
221 $this->issue->applyPOSTData($_POST);
225 foreach ($fields as $fieldname) {
226 if (isset($_POST[$fieldname]) && strlen($_POST[$fieldname])) {
227 $this->issue->$fieldname = $_POST[$fieldname];
229 $this->issue->$fieldname = null;
233 $kw = $this->issue->getKeywords();
234 $kill = array_values($kw);
235 foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
239 $x = array_search($w, $kw);
241 $k = MTrackKeyword::loadByWord($w);
243 $k = new MTrackKeyword;
247 $this->issue->assocKeyword($k);
249 $w = array_search($w, $kill);
255 foreach ($kill as $w) {
256 $this->issue->dissocKeyword($w);
259 $ms = $this->issue->getMilestones();
261 if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
262 foreach ($_POST['milestone'] as $mid) {
263 $this->issue->assocMilestone($mid);
267 foreach ($kill as $mid) {
268 $this->issue->dissocMilestone($mid);
271 $ms = $this->issue->getComponents();
273 if (isset($_POST['component']) && is_array($_POST['component'])) {
274 foreach ($_POST['component'] as $mid) {
275 $this->issue->assocComponent($mid);
279 foreach ($kill as $mid) {
280 $this->issue->dissocComponent($mid);
283 if (!empty($_POST['comment'])) {
284 $this->issue->addComment($_POST['comment']);
287 $this->issue->addEffort(
288 empty($_POST['spent']) ? 0 : $_POST['spent'],
289 empty($_POST['estimate']) ? 0 : $_POST['estimate']
292 if (!count($this->error)) {
294 $this->issue->save($CS);
296 // make sure everyone is watching it!!!!
297 if($this->issue->owner && $this->issue->tid) {
298 // make sure owner is tracking it...
299 MTrackWatch::watch_object('ticket', $this->issue->tid, $this->issue->owner);
302 if ($this->id == 'new') {
303 MTrackWatch::watch_object('ticket', $this->issue->tid, MTrackAuth::whoami());
307 $CS->setObject("ticket:" . $this->issue->tid);
308 } catch (Exception $e) {
309 $this->error[] = $e->getMessage();
313 if (!count($this->error)) {
314 if (!empty($_FILES['attachments'])) {
315 require_once 'MTrack/Attachment.php';
316 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
318 MTrackAttachment::add("ticket:{$this->issue->tid}",
319 $_FILES['attachments']['tmp_name'][$fileid],
320 $_FILES['attachments']['name'][$fileid],
326 if (!count($this->error) && $this->id != 'new') {
327 require_once 'MTrack/Attachment.php';
328 MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
331 if (isset($_POST['apply']) && !count($this->error)) {
333 header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
339 function initEditForm($params = array())
341 require_once 'HTML/Template/Flexy/Element.php';
342 require_once 'HTML/Template/Flexy/Factory.php';
343 $this->elements = array();
347 foreach(array( 'classification', 'priority', 'severity', 'resolution' ) as $c) {
348 $d = DB_DataObject::factory('core_enum');
350 $d->orderBy('seqid ASC, name ASC');
352 $d->createBaseEntries();
355 $this->elements[$c] = new HTML_Template_Flexy_Element('select');
356 $this->elements[$c]->setOptions($d->fetchAll('id','name'));
360 if ($this->issue->project_id) {
361 $d = DB_DataObject::factory('mtrack_project_component');
362 $d->project_id = $this->issue->project_id;
364 $d->whereAdd('deleted != 1');
365 $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
366 $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
367 $ar = $this->issue->components();
368 $this->elements['component[]']->setValue(array_keys($ar));
371 if ($this->issue->project_id) {
372 $d = DB_DataObject::factory('mtrack_milestone');
373 $d->project_id = $this->issue->project_id;
374 $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
375 $d->whereAdd('completed != 1');
376 $d->whereAdd('deleted != 1');
377 $this->elements['milestone[]'] = new HTML_Template_Flexy_Element('select');
378 $this->elements['milestone[]']->setOptions($d->fetchAll('id', 'name'));
379 $ar = $this->issue->milestones();
380 $this->elements['milestone[]']->setValue(array_keys($ar));
383 // FIX ME - need to determine who the owner is..
384 // for a new issue it's the person who created it.
385 // later on it's an assignement???
389 $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
390 $this->elements['owner']->setOptions($users);
394 // keywords -- in toArray...
398 $this->change_status = array();
399 $this->resolve_status = array();
402 // for coder's they can only change this ticke to certian states
405 // Nasty - I really do not like the acl code in this ...
406 require_once 'MTrack/TicketState.php';
408 $ST = new MTrackTicketState;
409 $ST = $ST->enumerate();
411 unset($ST['closed']);
412 unset($ST[$this->issue->status]);
414 $this->change_status = empty($ST) ? array() : array_keys($ST);
416 $ac = MTrackAuth::getUserClass($this->authUser->userid);
417 //var_dump($ac);exit;
418 // KLUDGE! - remove later...
419 if ($ac == 'admin') {
421 $this->resolve_status= array('fixed');
422 $R = new MTrackResolution;
423 $resolutions = $R->enumerate();
424 unset($resolutions['fixed']);
426 $this->resolve_status= array_keys($resolutions);
427 array_unshift($this->resolve_status, 'fixed');
428 // $html .= $this->mtrack_chg_status('action', 'resolve', 'Resolve as:', 'resolution', $resolutions, $this->issue );
432 // $html .= mtrack_radio('action', 'reopen', $_POST['action']);
433 // $html .= " <label for='reopen'>Reopen ticket</label><br>\n";
437 $this->elements = HTML_Template_Flexy_Factory::fromArray($this->issue->toArray(), $this->elements);
438 if (!empty($_POST)) {
439 $this->elements = HTML_Template_Flexy_Factory::fromArray($_POST, $this->elements);