MTrackWeb/TicketEdit.php
[web.mtrack] / MTrackWeb / TicketEdit.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3  
4 //require_once 'MTrack/Captcha.php';
5
6  
7 require_once 'MTrackWeb.php';
8 class MTrackWeb_TicketEdit extends MTrackWeb 
9 {
10     var $id; // 0 = new
11     var $issue;
12     var $preview;
13     var $error;
14     var $editable;
15     var $tid = 0; // or the MD5 rep.
16     
17      
18     
19     function getAuth() 
20     {
21         parent::getAuth();
22         //require_once 'MTrack/ACL.php';
23      //   MTrackACL::requireAllRights('Browser', 'read');
24         return true;
25   
26     }
27     
28     function get($pi= 0)
29     {
30     
31         if (!isset($_REQUEST['ajax_body'])) {
32             return;
33         }
34         
35         
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();
43         
44         
45         return $ret;
46     }
47     
48     function loadIssue() { 
49         // -- load issue..
50
51         $this->issue = DB_DataObject::factory('mtrack_ticket');
52
53         if ($this->id) {
54             if (!$this->issue->get($this->id)) {
55                 throw new Exception("Invalid ticket $this->id");
56             }
57             if ($this->id && $this->currentProject() != $this->issue->project_id) {
58                 throw new Exception("Project and ticket do not match..");
59             }
60             
61         } 
62         
63         
64         
65         //$this->issue->augmentFormFields($this->fieldset());
66
67
68         $this->preview = false;
69         $this->error = array();
70        
71        
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');
75         }
76         
77         
78         if ($this->id &&  (
79                 !$this->hasPerm('MTrack.Issue','E')
80             )) {
81             $this->jerr('Permission denied = no rights to edit tickets', array('noperm' => true));
82             return HTML_FlexyFramework::run('Noperm');
83         }
84         
85         
86         
87         
88         
89         
90         
91         // new is always editable..????
92         $this->editable = true; //$this->id ?
93             //$this->ticket->hasPerm($this->authUser,'E')  : true;
94          
95         $this->issue->milestoneURL = $this->baseURL.'/Milestone'; // fix me later..
96     
97         $this->showEditBar = false;
98          
99         if ($this->editable && $this->id   && !$this->preview) {
100             $this->showEditBar = true;
101         }
102         
103          
104         
105  
106         
107       
108     }
109    
110     function post() // handle the post...
111     {
112        
113         $this->id = $_REQUEST['id'];
114         $ret = $this->loadIssue();
115         if (!empty($ret)) {
116             $this->jerr("load issue failed");
117             exit;
118         }
119         //echo '<PRE>';print_R($this);exit;
120         /// $this->preview = isset($_POST['preview']) ? true : false;
121
122         // hopefull get has sorted out permissions..
123          
124         //    $CS = MTrackChangeset::begin("ticket:X", $comment);
125         
126         $old = clone($this->issue);
127         
128         if (!$this->issue->id) {
129             // set some defaults.
130             $en = DB_DataObject::factory('core_enum');
131             $en->setFrom(array('etype' => 'ticketstate', 'name' => 'new'));
132             $en->find(true);
133             $this->issue->status = $en->id;
134         }
135         
136         
137         //  VALIDATE POST!??!
138         
139         
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;
146             $xx->act_now = 1;
147             $xx->resolution_id = 0;
148             $xx->whereAdd('id != ' . ((int) $this->issue->id);
149             if ($xx->count() > 2) {
150                 $this->jerr("To many act-now tasks assigned to that person, wait until the have completed some");
151             }
152             
153             
154             
155         }
156         
157         
158         
159         
160         $this->issue->setFrom($_POST); 
161         
162         /*
163         $kw = $this->issue->getKeywords();
164           $kill = array_values($kw);
165           foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
166             if (!strlen($w)) {
167               continue;
168             }
169             $x = array_search($w, $kw);
170             if ($x === false) {
171               $k = MTrackKeyword::loadByWord($w);
172               if ($k === null) {
173                 $k = new MTrackKeyword;
174                 $k->keyword = $w;
175                 $k->save($CS);
176               }
177               $this->issue->assocKeyword($k);
178             } else {
179               $w = array_search($w, $kill);
180               if ($w !== false) {
181                 unset($kill[$w]);
182               }
183             }
184           }
185           foreach ($kill as $w) {
186             $this->issue->dissocKeyword($w);
187           }
188      
189         
190           $ms = $this->issue->getMilestones();
191           $kill = $ms;
192           if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
193             foreach ($_POST['milestone'] as $mid) {
194               $this->issue->assocMilestone($mid);
195               unset($kill[$mid]);
196             }
197           }
198           foreach ($kill as $mid) {
199             $this->issue->dissocMilestone($mid);
200           }
201         
202           $ms = $this->issue->getComponents();
203           $kill = $ms;
204           if (isset($_POST['component']) && is_array($_POST['component'])) {
205             foreach ($_POST['component'] as $mid) {
206               $this->issue->assocComponent($mid);
207               unset($kill[$mid]);
208             }
209           }
210           foreach ($kill as $mid) {
211             $this->issue->dissocComponent($mid);
212           }
213           
214             if (!empty($_POST['comment'])) {
215                $this->issue->addComment($_POST['comment']);
216             }
217           
218           $this->issue->addEffort(
219             empty($_POST['spent']) ? 0 : $_POST['spent'], 
220             empty($_POST['estimate']) ? 0 : $_POST['estimate']
221         );
222         */
223         $this->issue->project_id = $this->currentProject();
224         
225         
226         $CS = DB_DataObject::factory('mtrack_change');
227         
228        
229         
230         if ($this->issue->id) {
231             $this->issue->update($old);
232         } else {
233             $this->issue->insert();
234             $old = false;
235         }
236         
237         $CS->begin($this->issue, $old ? 'Changed' : 'Created');
238         if (!$old) {
239             $this->issue->created = $CS->id;
240             $this->issue->update();
241         }
242         $CS->add($this->issue, $old);
243         
244         // issue ticket... - add watches etc... 
245         
246         
247         
248          
249         
250         $notify_query = "
251                 (ontable='mtrack_ticket' and onid = {$this->issue->id})
252                  OR
253                  (ontable='Projects' and onid = {$this->issue->project_id})
254                  ";
255          $w = DB_DataObject::factory('core_watch');
256         $w->ensureNotify(  'mtrack_ticket' ,
257                             $this->issue->id,
258                             $this->authUser->id,
259                         $notify_query
260             );
261         
262         $w->ensureNotify(  'mtrack_ticket' ,
263                             $this->issue->id,
264                             $this->issue->owner_id,
265                         $notify_query
266             );
267         
268          $w->ensureNotify(  'mtrack_ticket' ,
269                             $this->issue->id,
270                             $this->issue->developer_id,
271                         $notify_query
272             );
273         //DB_DataObject::debugLevel(1);
274         // who to notify.. -- originall did not send to issuer..
275         // we should probably make this configurable..
276        
277         $w->notify( 'mtrack_ticket' , $this->issue->id,
278                    $notify_query 
279                    /*
280                  " ( $notify_query )
281                     AND
282                     (person_id != {$this->authUser->id})   "  */
283         );
284         
285         
286         /*
287         //$this->issue->save($CS);
288         
289           if (!count($this->error)) {
290             try {
291               $this->issue->save($CS);
292               
293               // make sure everyone is watching it!!!!
294                 if($this->issue->owner && $this->issue->tid) {
295                   // make sure owner is tracking it...
296                     MTrackWatch::watch_object('ticket', $this->issue->tid,  $this->issue->owner);
297                 }
298                 
299                 if ($this->id == 'new') {
300                     MTrackWatch::watch_object('ticket', $this->issue->tid,  MTrackAuth::whoami());
301                 }
302               
303               
304               $CS->setObject("ticket:" . $this->issue->tid);
305             } catch (Exception $e) {
306               $this->error[] = $e->getMessage();
307             }
308         }
309         */
310         /*
311         if (!count($this->error)) {
312             if (!empty($_FILES['attachments'])) {
313                 require_once 'MTrack/Attachment.php';
314                 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
315                       
316                     MTrackAttachment::add("ticket:{$this->issue->tid}",
317                         $_FILES['attachments']['tmp_name'][$fileid],
318                         $_FILES['attachments']['name'][$fileid],
319                         $CS
320                     );
321                 }
322             }
323         }
324         if (!count($this->error) && $this->id != 'new') {
325             require_once 'MTrack/Attachment.php';
326             MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
327         }
328
329         if (isset($_POST['apply']) && !count($this->error)) {
330           $CS->commit();
331           header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
332           exit;
333         }
334         */
335         $this->jok($this->issue->id);
336     }
337       
338          
339     function initEditForm($params = array())
340     {
341         require_once 'HTML/Template/Flexy/Element.php';
342         require_once 'HTML/Template/Flexy/Factory.php';
343         
344         
345         
346         foreach(array( 'classification', 'priority', 'severity' ) as $c)  {
347             $d = DB_DataObject::factory('core_enum');
348             $d->etype = $c;
349             $d->orderBy('seqid ASC, name ASC');
350             if (!$d->count()) {
351                 $d->createBaseEntries();
352                 
353             }
354             $ar = $d->fetchAll('id','name');
355             
356             // mid point..
357             $ak = array_keys($ar);
358             $mid = floor(count($ak) / 2 );
359             $def = $ak[$mid];
360             
361             //var_dump($ar[$def]);
362             
363             $this->elements[$c.'_id'] = new HTML_Template_Flexy_Element('select');
364             $this->elements[$c.'_id']->setOptions($ar);
365             if (!$this->id) {
366                 $this->elements[$c.'_id']->setValue($def);
367             }
368             
369             
370         }
371        // DB_DAtaObject::DebugLevel(1);
372         
373         $pd = DB_DataObject::factory('ProjectDirectory');
374         $pd->project_id = $this->currentProject();
375         $pd->whereAdd("ProjectDirectory.role != ''");
376         $pd->joinAdd(DB_DataObject::factory('Person'), 'LEFT');
377         $pd->selectAdd();
378         $pd->selectAdd("distinct(Person.id) as id ,  CONCAT(Person.name , '<', Person.email , '>') as name");
379         $pd->groupBy('Person.id, Person.name');
380         $pd->orderBy('Person.name');
381         $users = $pd->fetchAll('id', 'name');
382         $users = array('' => '--select--') + $users;
383         
384         //$users = array();
385          
386         $this->elements['owner_id'] = new HTML_Template_Flexy_Element('select');
387         $this->elements['owner_id']->setOptions($users);
388         
389         $this->elements['developer_id'] = new HTML_Template_Flexy_Element('select');
390         $this->elements['developer_id']->setOptions($users);
391         
392           
393         if ($this->currentProject()) {
394             /*
395             $d = DB_DataObject::factory('mtrack_project_component');
396             $d->project_id = $this->currentProject();
397             $d->orderBy('name');
398             $d->whereAdd('deleted != 1');
399             $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
400             $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
401             $ar = $this->issue->components();
402             $this->elements['component[]']->setValue(array_keys($ar));
403             */
404             //DB_DataObject::debugLevel(1);
405             $d = DB_DataObject::factory('mtrack_milestone');
406             $d->project_id = $this->currentProject();
407             $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
408             $d->whereAdd('completed != 1');
409             $d->whereAdd('deleted != 1');
410             $this->elements['milestone_id'] = new HTML_Template_Flexy_Element('select');
411             $this->elements['milestone_id']->setOptions(array(''=>'--select--') + $d->fetchAll('id', 'name'));
412             //$ar = $this->issue->milestone();
413             //$this->elements['milestone_id']->setValue(array_keys($ar));
414         }
415         
416         // FIX ME - need to determine who the owner is..
417         // for a new issue it's the person who created it.
418         // later on it's an assignement???
419         
420        
421         
422         if ($this->id) {
423             $this->elements = HTML_Template_Flexy_Factory::fromArray($this->issue->toArray(), $this->elements);
424         }
425
426         
427         // keywords -- in toArray...
428         // milestone 
429           
430           
431         
432
433           
434     }
435    
436      
437     function eq($a,$b) {
438         return $a == $b;
439     }
440     
441 }