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