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.. we should share this code with ticketedit really..
46         //DB_DataObject::debugLevel(1);
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         $this->issue->images = $this->issue->images();
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         $old = clone($this->issue);
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_id = $this->authUser->id;
124                 if ($this->issue->status_name == 'new') {
125                     $d = DB_DataObject::factory('core_enum');
126                     $d->etype = 'ticketstate';
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 = 'ticketstate';
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             // default leave - stays tehe same..
151         }
152         
153         // things that we should not allow..
154         foreach(array(
155             'estimated', 'effort', 'owner_id',
156         ) as $k) {
157             if (isset($_POST[$k])) {
158                 $this->issue->setFrom(array($k => $_POST[$k]));
159             }
160         }
161          
162         //  $this->issue->addEffort(
163         //    empty($_POST['spent']) ? 0 : $_POST['spent'], 
164         //    empty($_POST['estimate']) ? 0 : $_POST['estimate']
165         //);
166         
167         // we should really use begin/commit to determin if something has changed..
168         // and produce an error..
169         
170         
171         $CS = DB_DataObject::factory('mtrack_change');
172         $CS->begin($this->issue, empty($_POST['comment']) ? '' : $_POST['comment']);
173             
174         
175         //if (!empty($_POST['comment'])) {
176             // create a comment CS..
177             
178         //}
179         $CS->add($this->issue, $old);
180         $this->issue->updated = $CS->id;
181         $this->issue->update($old);
182         
183         
184         
185         
186         if (!empty($_FILES['attachments'])) {
187             foreach ($_FILES['attachments']['name'] as $fileid => $name) {
188                 $i = DB_DataObject::factory('Images');      
189                     MTrackAttachment::add("ticket:{$this->issue->tid}",
190                         $_FILES['attachments']['tmp_name'][$fileid],
191                         $_FILES['attachments']['name'][$fileid],
192                         $CS
193                     );
194                 }
195             }
196         }
197         /*
198         if (!count($this->error) && $this->id != 'new') {
199             require_once 'MTrack/Attachment.php';
200             MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
201         } 
202         */
203         
204         
205         
206         
207         
208         $notify_query = "
209                 (ontable='mtrack_ticket' and onid = {$this->issue->id})
210                  OR
211                  (ontable='Projects' and onid = {$this->issue->project_id})
212                  ";
213          $w = DB_DataObject::factory('core_watch');
214         $w->ensureNotify(  'mtrack_ticket' ,
215                             $this->issue->id,
216                             $this->authUser->id,
217                         $notify_query
218             );
219         
220         $w->ensureNotify(  'mtrack_ticket' ,
221                             $this->issue->id,
222                             $this->issue->owner_id,
223                         $notify_query
224             );
225         
226          $w->ensureNotify(  'mtrack_ticket' ,
227                             $this->issue->id,
228                             $this->issue->developer_id,
229                         $notify_query
230             );
231         //DB_DataObject::debugLevel(1);
232         // who to notify..
233        
234         $w->notify( 'mtrack_ticket' , $this->issue->id,
235                  " ( $notify_query )
236                     AND
237                     (person_id != {$this->authUser->id})   "  
238         );
239         
240         
241         
242         // if the owner has changed.. then we should add them to the watch list...
243  
244         // MTrackWatch::watch_object('ticket', $this->issue->tid,  $this->issue->owner);
245         
246         $this->jok("OK");
247         
248     }
249       
250          
251     function initEditForm($params = array())
252     {
253         require_once 'HTML/Template/Flexy/Element.php';
254         require_once 'HTML/Template/Flexy/Factory.php';
255         $this->elements = array();
256         
257         
258          
259         // FIX ME - need to determine who the owner is..
260         // for a new issue it's the person who created it.
261         // later on it's an assignement???
262       //  DB_DataObject::debugLevel(1);
263         $pd = DB_DataObject::factory('ProjectDirectory');
264         $pd->project_id = $this->currentProject();
265         $pd->whereAdd("ProjectDirectory.role != ''");
266         $pd->joinAdd(DB_DataObject::factory('Person'), 'LEFT');
267         $pd->selectAdd();
268         $pd->selectAdd("distinct(Person.id) as id ,  CONCAT(Person.name , '<', Person.email , '>') as name");
269         $pd->groupBy('Person.id, Person.name');
270         $pd->orderBy('Person.name');
271         $users = $pd->fetchAll('id', 'name');
272         
273         
274         //$users = array();
275          
276         $this->elements['owner_id'] = new HTML_Template_Flexy_Element('select');
277         $this->elements['owner_id']->setOptions($users);
278         
279         
280         
281         // keywords -- in toArray...
282         // milestone 
283           
284     
285     
286         foreach(array(  'resolution', 'ticketstate' ) as $c)  {
287             $d = DB_DataObject::factory('core_enum');
288             $d->etype = $c;
289             $d->orderBy('seqid ASC, name ASC');
290             if (!$d->count()) {
291                 $d->createBaseEntries();
292                 
293             }
294             $this->{$c} =  $d->fetchAll('id','name');
295             
296         }
297         
298     
299            
300         //    unset($ST['closed']);
301         //    unset($ST[$this->issue->status]);
302            
303         //    $this->change_status = empty($ST) ? array() : array_keys($ST);
304         
305         // admin can only change to 'fixed'?
306              
307         $this->elements = HTML_Template_Flexy_Factory::fromArray($this->issue->toArray(), $this->elements);
308  
309         
310
311           
312     }
313    
314      
315     function eq($a,$b) {
316         return $a == $b;
317     }
318     
319 }