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