MTrackWeb/Ticket.php
[web.mtrack] / MTrackWeb / Ticket.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_Ticket 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         $this->masterTemplate = 'ticket.html';
35         $this->id = $pi ?  $pi: (isset($_GET['id']) ? $_GET['id'] : 0);
36         $this->id  = (int) $this->id;
37         $this->loadIssue();
38         $this->initEditForm();
39
40     }
41     
42    function loadIssue( )
43    {
44      
45         // -- load issue..
46
47         $this->issue = DB_DataObject::factory('mtrack_ticket');
48         $this->issue->autoJoin();
49         if (!$this->id ||  !$this->issue->get($this->id)) {
50             $this->jerr("no such ticket");
51         }
52         
53         if ($this->issue->project_id != $this->currentProject()) {
54             $this->jerr("not in current project");
55         }
56         
57         
58         
59         
60         
61         //$this->issue->augmentFormFields($this->fieldset());
62
63
64         $this->preview = false;
65         $this->error = array();
66        
67        
68         if (!$this->id && !$this->hasPerm('MTrack.Issue','A')) {
69             return HTML_FlexyFramework::run('Noperm');
70         }
71         
72         if ($this->id &&  (  !$this->hasPerm('MTrack.Issue','S')    )) {
73             $this->jerr('no permission');
74         }
75         
76         $this->editable = !$this->hasPerm('MTrack.Issue','E') ;
77          
78         //$this->issue->milestoneURL = $this->baseURL.'/Milestone'; // fix me later..
79     
80         $this->showEditBar = false;
81          
82         if ($this->editable && $this->id   && !$this->preview) {
83             $this->showEditBar = true;
84         }
85          
86  
87         
88       
89     }
90    
91     function post() // handle the post...
92     {
93         
94         $this->id = empty($_POST['id']) ? 0 : $_POST['id'];
95         $this->loadIssue();
96        
97          
98         //  $this->preview = isset($_POST['preview']) ? true : false;
99         if (!$this->hasPerm('MTrack.Issue','E') ) {
100             $this->jerr("permission denied");
101         }
102           
103            
104         $comment = empty($_POST['comment']) ? '' : $_POST['comment'];
105          
106         
107         //    $CS = MTrackChangeset::begin("ticket:X", $comment);
108            
109
110         $act= explode('_', $_POST['status'] , 2);
111             //var_dump($act);exit;
112             // input type='radio' id='none' name='status' value='{issue.status}' checked='checked'> 
113             //   <label for='accept'>Assign this ticket to <b style="color:red">ME</b></label><br>
114             // <input type='radio' id='ticketstate_{cid}' name='status' value='change_{cid}' flexy:ignore="yes"> 
115             //           <input type='radio' id='resolution_{cid}' name='status' value='resolution_{cid}'  flexy:ignore="yes"> 
116                
117         switch ($act[0]) {
118           
119                 
120               
121             case 'accept':
122                 // will be applied to the issue further down
123                 $this->issue->owner = $this->authUser->id;
124                 if ($this->issue->status_name == 'new') {
125                     $d = DB_DataObject::factory('core_enum');
126                     $d->etype = 'status';
127                     $d->name = 'open'; // assigned?
128                     $d->find(true); 
129                     $this->issue->status = $d->id;
130                 }
131                 break;
132                 
133                 
134               case 'resolution':
135                 //$this->issue->resolution = $_POST['resolution'];
136                 $this->issue->resolution_id = $act[1];
137                 $d = DB_DataObject::factory('core_enum');
138                 $d->etype = 'status';
139                 $d->name = 'closed'; // assigned?
140                 $d->find(true);
141                 $this_>issue->status = $d->id;
142                 //$this->issue->close();
143                 //$_POST['estimated'] = $this->issue->estimated;
144                 break;  
145                 
146             case 'change':
147                 $this->issue->status = $act[1];
148                 break;
149             }
150           }
151
152           $fields = array(
153             'summary',
154             'description',
155             'classification',
156             'priority',
157             'severity',
158             'changelog',
159             'owner',
160             'cc',
161           );
162
163           $this->issue->applyPOSTData($_POST);
164
165          
166           
167           foreach ($fields as $fieldname) {
168             if (isset($_POST[$fieldname]) && strlen($_POST[$fieldname])) {
169               $this->issue->$fieldname = $_POST[$fieldname];
170             } else {
171               $this->issue->$fieldname = null;
172             }
173           }
174
175           $kw = $this->issue->getKeywords();
176           $kill = array_values($kw);
177           foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
178             if (!strlen($w)) {
179               continue;
180             }
181             $x = array_search($w, $kw);
182             if ($x === false) {
183               $k = MTrackKeyword::loadByWord($w);
184               if ($k === null) {
185                 $k = new MTrackKeyword;
186                 $k->keyword = $w;
187                 $k->save($CS);
188               }
189               $this->issue->assocKeyword($k);
190             } else {
191               $w = array_search($w, $kill);
192               if ($w !== false) {
193                 unset($kill[$w]);
194               }
195             }
196           }
197           foreach ($kill as $w) {
198             $this->issue->dissocKeyword($w);
199           }
200
201           $ms = $this->issue->getMilestones();
202           $kill = $ms;
203           if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
204             foreach ($_POST['milestone'] as $mid) {
205               $this->issue->assocMilestone($mid);
206               unset($kill[$mid]);
207             }
208           }
209           foreach ($kill as $mid) {
210             $this->issue->dissocMilestone($mid);
211           }
212
213           $ms = $this->issue->getComponents();
214           $kill = $ms;
215           if (isset($_POST['component']) && is_array($_POST['component'])) {
216             foreach ($_POST['component'] as $mid) {
217               $this->issue->assocComponent($mid);
218               unset($kill[$mid]);
219             }
220           }
221           foreach ($kill as $mid) {
222             $this->issue->dissocComponent($mid);
223           }
224           
225             if (!empty($_POST['comment'])) {
226                $this->issue->addComment($_POST['comment']);
227             }
228           
229           $this->issue->addEffort(
230             empty($_POST['spent']) ? 0 : $_POST['spent'], 
231             empty($_POST['estimate']) ? 0 : $_POST['estimate']
232         );
233
234           if (!count($this->error)) {
235             try {
236               $this->issue->save($CS);
237               
238               // make sure everyone is watching it!!!!
239                 if($this->issue->owner && $this->issue->tid) {
240                   // make sure owner is tracking it...
241                     MTrackWatch::watch_object('ticket', $this->issue->tid,  $this->issue->owner);
242                 }
243                 
244                 if ($this->id == 'new') {
245                     MTrackWatch::watch_object('ticket', $this->issue->tid,  MTrackAuth::whoami());
246                 }
247               
248               
249               $CS->setObject("ticket:" . $this->issue->tid);
250             } catch (Exception $e) {
251               $this->error[] = $e->getMessage();
252             }
253         }
254
255         if (!count($this->error)) {
256             if (!empty($_FILES['attachments'])) {
257                 require_once 'MTrack/Attachment.php';
258                 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
259                       
260                     MTrackAttachment::add("ticket:{$this->issue->tid}",
261                         $_FILES['attachments']['tmp_name'][$fileid],
262                         $_FILES['attachments']['name'][$fileid],
263                         $CS
264                     );
265                 }
266             }
267         }
268         if (!count($this->error) && $this->id != 'new') {
269             require_once 'MTrack/Attachment.php';
270             MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
271         }
272
273         if (isset($_POST['apply']) && !count($this->error)) {
274           $CS->commit();
275           header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
276           exit;
277         }
278     }
279       
280          
281     function initEditForm($params = array())
282     {
283         require_once 'HTML/Template/Flexy/Element.php';
284         require_once 'HTML/Template/Flexy/Factory.php';
285         $this->elements = array();
286         
287         
288          
289         // FIX ME - need to determine who the owner is..
290         // for a new issue it's the person who created it.
291         // later on it's an assignement???
292         
293         $users = array();
294          
295         $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
296         $this->elements['owner']->setOptions($users);
297         
298         
299         
300         // keywords -- in toArray...
301         // milestone 
302           
303     
304     
305         foreach(array(  'resolution', 'ticketstate' ) as $c)  {
306             $d = DB_DataObject::factory('core_enum');
307             $d->etype = $c;
308             $d->orderBy('seqid ASC, name ASC');
309             if (!$d->count()) {
310                 $d->createBaseEntries();
311                 
312             }
313             $this->{$c} =  $d->fetchAll('id','name');
314             
315         }
316         
317     
318            
319         //    unset($ST['closed']);
320         //    unset($ST[$this->issue->status]);
321            
322         //    $this->change_status = empty($ST) ? array() : array_keys($ST);
323         
324         // admin can only change to 'fixed'?
325              
326         $this->elements = HTML_Template_Flexy_Factory::fromArray($this->issue->toArray(), $this->elements);
327  
328         
329
330           
331     }
332    
333      
334     function eq($a,$b) {
335         return $a == $b;
336     }
337     
338 }