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             $xx = DB_DataObject::factroy('mtrack_ticket');
143             $xx->owner_id = $this->issue->owner_id;
144             $xx->project_id  = $this->issue->project_id;
145             $xx->resolution_id = 0;
146             if ($xx->count() > 3) {
147                 $this->jerr("To many act-now tasks assigned to that person, wait until the have completed some");
148             }
149             
150             
151             
152         }
153         
154         
155         
156         
157         $this->issue->setFrom($_POST); 
158         
159         /*
160         $kw = $this->issue->getKeywords();
161           $kill = array_values($kw);
162           foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
163             if (!strlen($w)) {
164               continue;
165             }
166             $x = array_search($w, $kw);
167             if ($x === false) {
168               $k = MTrackKeyword::loadByWord($w);
169               if ($k === null) {
170                 $k = new MTrackKeyword;
171                 $k->keyword = $w;
172                 $k->save($CS);
173               }
174               $this->issue->assocKeyword($k);
175             } else {
176               $w = array_search($w, $kill);
177               if ($w !== false) {
178                 unset($kill[$w]);
179               }
180             }
181           }
182           foreach ($kill as $w) {
183             $this->issue->dissocKeyword($w);
184           }
185      
186         
187           $ms = $this->issue->getMilestones();
188           $kill = $ms;
189           if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
190             foreach ($_POST['milestone'] as $mid) {
191               $this->issue->assocMilestone($mid);
192               unset($kill[$mid]);
193             }
194           }
195           foreach ($kill as $mid) {
196             $this->issue->dissocMilestone($mid);
197           }
198         
199           $ms = $this->issue->getComponents();
200           $kill = $ms;
201           if (isset($_POST['component']) && is_array($_POST['component'])) {
202             foreach ($_POST['component'] as $mid) {
203               $this->issue->assocComponent($mid);
204               unset($kill[$mid]);
205             }
206           }
207           foreach ($kill as $mid) {
208             $this->issue->dissocComponent($mid);
209           }
210           
211             if (!empty($_POST['comment'])) {
212                $this->issue->addComment($_POST['comment']);
213             }
214           
215           $this->issue->addEffort(
216             empty($_POST['spent']) ? 0 : $_POST['spent'], 
217             empty($_POST['estimate']) ? 0 : $_POST['estimate']
218         );
219         */
220         $this->issue->project_id = $this->currentProject();
221         
222         
223         $CS = DB_DataObject::factory('mtrack_change');
224         
225        
226         
227         if ($this->issue->id) {
228             $this->issue->update($old);
229         } else {
230             $this->issue->insert();
231             $old = false;
232         }
233         
234         $CS->begin($this->issue, $old ? 'Changed' : 'Created');
235         if (!$old) {
236             $this->issue->created = $CS->id;
237             $this->issue->update();
238         }
239         $CS->add($this->issue, $old);
240         
241         // issue ticket... - add watches etc... 
242         
243         
244         
245          
246         
247         $notify_query = "
248                 (ontable='mtrack_ticket' and onid = {$this->issue->id})
249                  OR
250                  (ontable='Projects' and onid = {$this->issue->project_id})
251                  ";
252          $w = DB_DataObject::factory('core_watch');
253         $w->ensureNotify(  'mtrack_ticket' ,
254                             $this->issue->id,
255                             $this->authUser->id,
256                         $notify_query
257             );
258         
259         $w->ensureNotify(  'mtrack_ticket' ,
260                             $this->issue->id,
261                             $this->issue->owner_id,
262                         $notify_query
263             );
264         
265          $w->ensureNotify(  'mtrack_ticket' ,
266                             $this->issue->id,
267                             $this->issue->developer_id,
268                         $notify_query
269             );
270         //DB_DataObject::debugLevel(1);
271         // who to notify.. -- originall did not send to issuer..
272         // we should probably make this configurable..
273        
274         $w->notify( 'mtrack_ticket' , $this->issue->id,
275                    $notify_query 
276                    /*
277                  " ( $notify_query )
278                     AND
279                     (person_id != {$this->authUser->id})   "  */
280         );
281         
282         
283         /*
284         //$this->issue->save($CS);
285         
286           if (!count($this->error)) {
287             try {
288               $this->issue->save($CS);
289               
290               // make sure everyone is watching it!!!!
291                 if($this->issue->owner && $this->issue->tid) {
292                   // make sure owner is tracking it...
293                     MTrackWatch::watch_object('ticket', $this->issue->tid,  $this->issue->owner);
294                 }
295                 
296                 if ($this->id == 'new') {
297                     MTrackWatch::watch_object('ticket', $this->issue->tid,  MTrackAuth::whoami());
298                 }
299               
300               
301               $CS->setObject("ticket:" . $this->issue->tid);
302             } catch (Exception $e) {
303               $this->error[] = $e->getMessage();
304             }
305         }
306         */
307         /*
308         if (!count($this->error)) {
309             if (!empty($_FILES['attachments'])) {
310                 require_once 'MTrack/Attachment.php';
311                 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
312                       
313                     MTrackAttachment::add("ticket:{$this->issue->tid}",
314                         $_FILES['attachments']['tmp_name'][$fileid],
315                         $_FILES['attachments']['name'][$fileid],
316                         $CS
317                     );
318                 }
319             }
320         }
321         if (!count($this->error) && $this->id != 'new') {
322             require_once 'MTrack/Attachment.php';
323             MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
324         }
325
326         if (isset($_POST['apply']) && !count($this->error)) {
327           $CS->commit();
328           header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
329           exit;
330         }
331         */
332         $this->jok($this->issue->id);
333     }
334       
335          
336     function initEditForm($params = array())
337     {
338         require_once 'HTML/Template/Flexy/Element.php';
339         require_once 'HTML/Template/Flexy/Factory.php';
340         
341         
342         
343         foreach(array( 'classification', 'priority', 'severity' ) as $c)  {
344             $d = DB_DataObject::factory('core_enum');
345             $d->etype = $c;
346             $d->orderBy('seqid ASC, name ASC');
347             if (!$d->count()) {
348                 $d->createBaseEntries();
349                 
350             }
351             $ar = $d->fetchAll('id','name');
352             
353             // mid point..
354             $ak = array_keys($ar);
355             $mid = floor(count($ak) / 2 );
356             $def = $ak[$mid];
357             
358             //var_dump($ar[$def]);
359             
360             $this->elements[$c.'_id'] = new HTML_Template_Flexy_Element('select');
361             $this->elements[$c.'_id']->setOptions($ar);
362             if (!$this->id) {
363                 $this->elements[$c.'_id']->setValue($def);
364             }
365             
366             
367         }
368        // DB_DAtaObject::DebugLevel(1);
369         
370         $pd = DB_DataObject::factory('ProjectDirectory');
371         $pd->project_id = $this->currentProject();
372         $pd->whereAdd("ProjectDirectory.role != ''");
373         $pd->joinAdd(DB_DataObject::factory('Person'), 'LEFT');
374         $pd->selectAdd();
375         $pd->selectAdd("distinct(Person.id) as id ,  CONCAT(Person.name , '<', Person.email , '>') as name");
376         $pd->groupBy('Person.id, Person.name');
377         $pd->orderBy('Person.name');
378         $users = $pd->fetchAll('id', 'name');
379         $users = array('' => '--select--') + $users;
380         
381         //$users = array();
382          
383         $this->elements['owner_id'] = new HTML_Template_Flexy_Element('select');
384         $this->elements['owner_id']->setOptions($users);
385         
386         $this->elements['developer_id'] = new HTML_Template_Flexy_Element('select');
387         $this->elements['developer_id']->setOptions($users);
388         
389           
390         if ($this->currentProject()) {
391             /*
392             $d = DB_DataObject::factory('mtrack_project_component');
393             $d->project_id = $this->currentProject();
394             $d->orderBy('name');
395             $d->whereAdd('deleted != 1');
396             $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
397             $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
398             $ar = $this->issue->components();
399             $this->elements['component[]']->setValue(array_keys($ar));
400             */
401             //DB_DataObject::debugLevel(1);
402             $d = DB_DataObject::factory('mtrack_milestone');
403             $d->project_id = $this->currentProject();
404             $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
405             $d->whereAdd('completed != 1');
406             $d->whereAdd('deleted != 1');
407             $this->elements['milestone_id'] = new HTML_Template_Flexy_Element('select');
408             $this->elements['milestone_id']->setOptions(array(''=>'--select--') + $d->fetchAll('id', 'name'));
409             //$ar = $this->issue->milestone();
410             //$this->elements['milestone_id']->setValue(array_keys($ar));
411         }
412         
413         // FIX ME - need to determine who the owner is..
414         // for a new issue it's the person who created it.
415         // later on it's an assignement???
416         
417        
418         
419         if ($this->id) {
420             $this->elements = HTML_Template_Flexy_Factory::fromArray($this->issue->toArray(), $this->elements);
421         }
422
423         
424         // keywords -- in toArray...
425         // milestone 
426           
427           
428         
429
430           
431     }
432    
433      
434     function eq($a,$b) {
435         return $a == $b;
436     }
437     
438 }