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;
97 $this->initEditForm();
103 function post() // handle the post...
106 $this->get($_REQUEST['id']);
108 if (isset($_POST['cancel'])) {
109 header("Location: {$$this->baseURL}/Ticket/$this->issue->nsident");
113 if (!MTrack_Captcha::check('ticket')) {
114 $this->error[] = "CAPTCHA failed, please try again";
116 $this->preview = isset($_POST['preview']) ? true : false;
121 MTrackACL::requireAllRights("Tickets", 'create');
123 MTrackACL::requireAllRights("ticket:" . $this->issue->tid, 'modify');
125 } catch (Exception $e) {
126 $this->error[] = $e->getMessage();
130 $comment = empty($_POST['comment']) ? '' : $_POST['comment'];
133 if (!strlen($comment)) {
134 $comment = $_POST['summary'];
137 $CS = MTrackChangeset::begin("ticket:X", $comment);
138 } catch (Exception $e) {
139 $this->error[] = $e->getMessage();
143 // compute next id number.
144 // We don't use auto-number, because we allow for importing multiple
145 // projects with their own ticket sequence.
146 // During "normal" user-driven operation, we do want plain old id numbers
147 // so we compute it here, under a transaction
148 $db = MTrackDB::get();
152 switch($db->getAttribute(PDO::ATTR_DRIVER_NAME)) {
154 // Some versions of postgres don't like that we have "abc123" for
155 // identifiers, so match on the bigest number nsident fields only
156 $max = "select max(cast(nsident as integer)) + 1 from tickets where nsident ~ '^\\\\d+$'";
160 $max = "select max(cast(nsident as UNSIGNED)) + 1 from tickets";
164 $max = 'select max(cast(nsident as integer)) + 1 from tickets';
170 list($this->issue->nsident) = MTrackDB::q($max)->fetchAll(PDO::FETCH_COLUMN, 0);
171 if ($this->issue->nsident === null) {
172 $this->issue->nsident = 1;
176 if (isset($_POST['action']) && !$this->preview) {
177 $act= explode('_', $_POST['action'] , 2);
178 //var_dump($act);exit;
183 $this->issue->reOpen();
186 $this->issue->resolution = 'fixed';
187 $this->issue->close();
188 $_POST['estimated'] = $this->issue->estimated;
193 // will be applied to the issue further down
194 $_POST['owner'] = MTrackAuth::whoami();
195 if ($this->issue->status == 'new') {
196 $this->issue->status = 'open';
202 //$this->issue->resolution = $_POST['resolution'];
203 $this->issue->resolution = $act[1];
204 $this->issue->close();
205 $_POST['estimated'] = $this->issue->estimated;
209 $this->issue->status = $act[1];
225 $this->issue->applyPOSTData($_POST);
229 foreach ($fields as $fieldname) {
230 if (isset($_POST[$fieldname]) && strlen($_POST[$fieldname])) {
231 $this->issue->$fieldname = $_POST[$fieldname];
233 $this->issue->$fieldname = null;
237 $kw = $this->issue->getKeywords();
238 $kill = array_values($kw);
239 foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
243 $x = array_search($w, $kw);
245 $k = MTrackKeyword::loadByWord($w);
247 $k = new MTrackKeyword;
251 $this->issue->assocKeyword($k);
253 $w = array_search($w, $kill);
259 foreach ($kill as $w) {
260 $this->issue->dissocKeyword($w);
263 $ms = $this->issue->getMilestones();
265 if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
266 foreach ($_POST['milestone'] as $mid) {
267 $this->issue->assocMilestone($mid);
271 foreach ($kill as $mid) {
272 $this->issue->dissocMilestone($mid);
275 $ms = $this->issue->getComponents();
277 if (isset($_POST['component']) && is_array($_POST['component'])) {
278 foreach ($_POST['component'] as $mid) {
279 $this->issue->assocComponent($mid);
283 foreach ($kill as $mid) {
284 $this->issue->dissocComponent($mid);
287 if (!empty($_POST['comment'])) {
288 $this->issue->addComment($_POST['comment']);
291 $this->issue->addEffort(
292 empty($_POST['spent']) ? 0 : $_POST['spent'],
293 empty($_POST['estimate']) ? 0 : $_POST['estimate']
296 if (!count($this->error)) {
298 $this->issue->save($CS);
300 // make sure everyone is watching it!!!!
301 if($this->issue->owner && $this->issue->tid) {
302 // make sure owner is tracking it...
303 MTrackWatch::watch_object('ticket', $this->issue->tid, $this->issue->owner);
306 if ($this->id == 'new') {
307 MTrackWatch::watch_object('ticket', $this->issue->tid, MTrackAuth::whoami());
311 $CS->setObject("ticket:" . $this->issue->tid);
312 } catch (Exception $e) {
313 $this->error[] = $e->getMessage();
317 if (!count($this->error)) {
318 if (!empty($_FILES['attachments'])) {
319 require_once 'MTrack/Attachment.php';
320 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
322 MTrackAttachment::add("ticket:{$this->issue->tid}",
323 $_FILES['attachments']['tmp_name'][$fileid],
324 $_FILES['attachments']['name'][$fileid],
330 if (!count($this->error) && $this->id != 'new') {
331 require_once 'MTrack/Attachment.php';
332 MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
335 if (isset($_POST['apply']) && !count($this->error)) {
337 header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
343 function initEditForm($params = array())
345 require_once 'HTML/Template/Flexy/Element.php';
346 require_once 'HTML/Template/Flexy/Factory.php';
347 $this->elements = array();
351 foreach(array( 'classification', 'priority', 'severity', 'resolution' ) as $c) {
352 $d = DB_DataObject::factory('core_enum');
354 $d->orderBy('seqid ASC, name ASC');
356 $d->createBaseEntries();
359 $ar = $d->fetchAll('id','name');
361 $ar = array_merge( array(0 => '-- Select --'),$ar);
363 $this->elements[$c] = new HTML_Template_Flexy_Element('select');
367 $this->elements[$c]->setOptions($ar);
371 if (false && $this->currentProject()) {
372 $d = DB_DataObject::factory('mtrack_project_component');
373 $d->project_id = $this->currentProject();
375 $d->whereAdd('deleted != 1');
376 $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
377 $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
378 $ar = $this->issue->components();
379 $this->elements['component[]']->setValue(array_keys($ar));
382 $d = DB_DataObject::factory('mtrack_milestone');
383 $d->project_id = $this->currentProject();
384 $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
385 $d->whereAdd('completed != 1');
386 $d->whereAdd('deleted != 1');
387 $this->elements['milestone'] = new HTML_Template_Flexy_Element('select');
388 $this->elements['milestone']->setOptions($d->fetchAll('id', 'name'));
389 $ar = $this->issue->milestone();
390 $this->elements['milestone']->setValue(array_keys($ar));
393 // FIX ME - need to determine who the owner is..
394 // for a new issue it's the person who created it.
395 // later on it's an assignement???
399 $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
400 $this->elements['owner']->setOptions($users);
404 // keywords -- in toArray...