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'])) {
36 $this->masterTemplate = 'ticket_edit.html';
37 $this->id = $pi ? $pi : (isset($_GET['id']) ? $_GET['id'] : 0);
38 $this->id = $this->id == 'new' ? 0 : $this->id;
39 $this->id = (int) $this->id;
40 // var_dump($this->id);
41 $ret = $this->loadIssue();
42 $this->initEditForm();
48 function loadIssue() {
51 $this->issue = DB_DataObject::factory('mtrack_ticket');
54 if (!$this->issue->get($this->id)) {
55 throw new Exception("Invalid ticket $this->id");
57 if ($this->id && $this->currentProject() != $this->issue->project_id) {
58 throw new Exception("Project and ticket do not match..");
65 //$this->issue->augmentFormFields($this->fieldset());
68 $this->preview = false;
69 $this->error = array();
72 if (!$this->id && !$this->hasPerm('MTrack.Issue','A')) {
73 $this->jerr('Permission denied = no rights to create tickets', array('noperm' => true));
74 //return HTML_FlexyFramework::run('Noperm');
79 !$this->hasPerm('MTrack.Issue','E')
81 $this->jerr('Permission denied = no rights to edit tickets', array('noperm' => true));
82 return HTML_FlexyFramework::run('Noperm');
91 // new is always editable..????
92 $this->editable = true; //$this->id ?
93 //$this->ticket->hasPerm($this->authUser,'E') : true;
95 $this->issue->milestoneURL = $this->baseURL.'/Milestone'; // fix me later..
97 $this->showEditBar = false;
99 if ($this->editable && $this->id && !$this->preview) {
100 $this->showEditBar = true;
110 function post() // handle the post...
113 $this->id = $_REQUEST['id'];
114 $ret = $this->loadIssue();
116 $this->jerr("load issue failed");
119 //echo '<PRE>';print_R($this);exit;
120 /// $this->preview = isset($_POST['preview']) ? true : false;
122 // hopefull get has sorted out permissions..
124 // $CS = MTrackChangeset::begin("ticket:X", $comment);
126 $old = clone($this->issue);
128 if (!$this->issue->id) {
129 // set some defaults.
130 $en = DB_DataObject::factory('core_enum');
131 $en->setFrom(array('etype' => 'ticketstate', 'name' => 'new'));
133 $this->issue->status = $en->id;
140 if (!empty($_REQUEST['act_now'])) { //=+ adding act now..
141 // check project / assigned person / !closed for more than 3 'act_now'
142 //DB_DataObject::DebugLevel(1);
143 $xx = DB_DataObject::factory('mtrack_ticket');
144 $xx->owner_id = $this->issue->owner_id;
145 $xx->project_id = $this->issue->project_id;
147 $xx->resolution_id = 0;
148 $xx->whereAdd('id != ' . ((int) $this->issue->id)); // just in case we are double setting...
149 if ($xx->count() > 2) {
150 $this->jerr("To many act-now tasks assigned to that person, wait until the have completed some");
160 $this->issue->setFrom($_POST);
163 $kw = $this->issue->getKeywords();
164 $kill = array_values($kw);
165 foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
169 $x = array_search($w, $kw);
171 $k = MTrackKeyword::loadByWord($w);
173 $k = new MTrackKeyword;
177 $this->issue->assocKeyword($k);
179 $w = array_search($w, $kill);
185 foreach ($kill as $w) {
186 $this->issue->dissocKeyword($w);
190 $ms = $this->issue->getMilestones();
192 if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
193 foreach ($_POST['milestone'] as $mid) {
194 $this->issue->assocMilestone($mid);
198 foreach ($kill as $mid) {
199 $this->issue->dissocMilestone($mid);
202 $ms = $this->issue->getComponents();
204 if (isset($_POST['component']) && is_array($_POST['component'])) {
205 foreach ($_POST['component'] as $mid) {
206 $this->issue->assocComponent($mid);
210 foreach ($kill as $mid) {
211 $this->issue->dissocComponent($mid);
214 if (!empty($_POST['comment'])) {
215 $this->issue->addComment($_POST['comment']);
218 $this->issue->addEffort(
219 empty($_POST['spent']) ? 0 : $_POST['spent'],
220 empty($_POST['estimate']) ? 0 : $_POST['estimate']
227 $this->issue->project_id = $this->currentProject();
230 $CS = DB_DataObject::factory('mtrack_change');
234 if ($this->issue->id) {
235 if (0 === $this->issue->update($old)) {
236 $this->jerr("nothing changed?");
240 $this->issue->insert();
244 $CS->begin($this->issue, $old ? 'Changed' : 'Created');
246 $this->issue->created = $CS->id;
247 $this->issue->update();
249 $CS->add($this->issue, $old);
251 // issue ticket... - add watches etc...
258 (ontable='mtrack_ticket' and onid = {$this->issue->id})
260 (ontable='Projects' and onid = {$this->issue->project_id})
262 $w = DB_DataObject::factory('core_watch');
263 $w->ensureNotify( 'mtrack_ticket' ,
269 $w->ensureNotify( 'mtrack_ticket' ,
271 $this->issue->owner_id,
275 $w->ensureNotify( 'mtrack_ticket' ,
277 $this->issue->developer_id,
280 //DB_DataObject::debugLevel(1);
281 // who to notify.. -- originall did not send to issuer..
282 // we should probably make this configurable..
284 $w->notify( 'mtrack_ticket' , $this->issue->id,
289 (person_id != {$this->authUser->id}) " */
294 //$this->issue->save($CS);
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();
318 if (!count($this->error)) {
319 if (!empty($_FILES['attachments'])) {
320 require_once 'MTrack/Attachment.php';
321 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
323 MTrackAttachment::add("ticket:{$this->issue->tid}",
324 $_FILES['attachments']['tmp_name'][$fileid],
325 $_FILES['attachments']['name'][$fileid],
331 if (!count($this->error) && $this->id != 'new') {
332 require_once 'MTrack/Attachment.php';
333 MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
336 if (isset($_POST['apply']) && !count($this->error)) {
338 header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
342 $this->jok($this->issue->id);
346 function initEditForm($params = array())
348 require_once 'HTML/Template/Flexy/Element.php';
349 require_once 'HTML/Template/Flexy/Factory.php';
353 foreach(array( 'classification', 'priority', 'severity' ) as $c) {
354 $d = DB_DataObject::factory('core_enum');
356 $d->orderBy('seqid ASC, name ASC');
358 $d->createBaseEntries();
361 $ar = $d->fetchAll('id','name');
364 $ak = array_keys($ar);
365 $mid = floor(count($ak) / 2 );
368 //var_dump($ar[$def]);
370 $this->elements[$c.'_id'] = new HTML_Template_Flexy_Element('select');
371 $this->elements[$c.'_id']->setOptions($ar);
373 $this->elements[$c.'_id']->setValue($def);
378 // DB_DAtaObject::DebugLevel(1);
380 $pd = DB_DataObject::factory('ProjectDirectory');
381 $pd->project_id = $this->currentProject();
382 $pd->whereAdd("ProjectDirectory.role != ''");
383 $pd->joinAdd(DB_DataObject::factory('core_person'), 'LEFT');
385 $pd->selectAdd("distinct(Person.id) as id , CONCAT(Person.name , '<', Person.email , '>') as name");
386 $pd->groupBy('Person.id, Person.name');
387 $pd->orderBy('Person.name');
388 $users = $pd->fetchAll('id', 'name');
389 $users = array('' => '--select--') + $users;
393 $this->elements['owner_id'] = new HTML_Template_Flexy_Element('select');
394 $this->elements['owner_id']->setOptions($users);
396 $this->elements['developer_id'] = new HTML_Template_Flexy_Element('select');
397 $this->elements['developer_id']->setOptions($users);
400 if ($this->currentProject()) {
402 $d = DB_DataObject::factory('mtrack_project_component');
403 $d->project_id = $this->currentProject();
405 $d->whereAdd('deleted != 1');
406 $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
407 $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
408 $ar = $this->issue->components();
409 $this->elements['component[]']->setValue(array_keys($ar));
411 //DB_DataObject::debugLevel(1);
412 $d = DB_DataObject::factory('mtrack_milestone');
413 $d->project_id = $this->currentProject();
414 $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
415 $d->whereAdd('completed != 1');
416 $d->whereAdd('deleted != 1');
417 $this->elements['milestone_id'] = new HTML_Template_Flexy_Element('select');
418 $this->elements['milestone_id']->setOptions(array(''=>'--select--') + $d->fetchAll('id', 'name'));
419 //$ar = $this->issue->milestone();
420 //$this->elements['milestone_id']->setValue(array_keys($ar));
423 // FIX ME - need to determine who the owner is..
424 // for a new issue it's the person who created it.
425 // later on it's an assignement???
430 $this->elements = HTML_Template_Flexy_Factory::fromArray($this->issue->toArray(), $this->elements);
434 // keywords -- in toArray...