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         $this->masterTemplate = 'ticket_edit.html';
35         $this->id = $pi ?  $pi: (isset($_GET['id']) ? $_GET['id'] : 0);
36         $this->id  = $this->id  == 'new' ? 0 : $this->id;
37         $this->id  = (int) $this->id;
38         
39     
40         // -- load issue..
41
42         $this->issue = DB_DataObject::factory('mtrack_ticket');
43
44         if ($this->id) {
45             if (!$this->issue->get($this->id)) {
46                 throw new Exception("Invalid ticket $this->id");
47             }
48             if ($this->id && $this->currentProject() != $this->issue->project_id) {
49                 throw new Exception("Project and ticket do not match..");
50             }
51             
52         } 
53         
54         
55         
56         //$this->issue->augmentFormFields($this->fieldset());
57
58
59         $this->preview = false;
60         $this->error = array();
61        
62        
63         if (!$this->id && !$this->hasPerm('MTrack.Issue','A')) {
64             return HTML_FlexyFramework::run('Noperm');
65         }
66         
67         
68         if ($this->id &&  (
69                 !$this->hasPerm('MTrack.Issue','S') || // general permission to view
70                 !$this->ticket->hasPerm($this->authUser,'S') // specific permission on this bug.
71                 
72             )) {
73             return HTML_FlexyFramework::run('Noperm');
74         }
75         
76         
77         
78         
79         
80         
81         // new is always editable..????
82         $this->editable = $this->id ?
83             $this->ticket->hasPerm($this->authUser,'E')  : true;
84          
85         $this->issue->milestoneURL = $this->baseURL.'/Milestone'; // fix me later..
86     
87         $this->showEditBar = false;
88          
89         if ($this->editable && $this->id   && !$this->preview) {
90             $this->showEditBar = true;
91         }
92         
93         if (empty($_POST)) { // post is ajax only..
94             return;
95         }
96         
97         $this->initEditForm();
98  
99         
100       
101     }
102    
103     function post() // handle the post...
104     {
105        
106         $ret = $this->get($_REQUEST['id']);
107         if (!empty($ret)) {
108             print_r($ret);
109             exit;
110          
111         }
112         /// $this->preview = isset($_POST['preview']) ? true : false;
113
114         // hopefull get has sorted out permissions..
115          
116         //    $CS = MTrackChangeset::begin("ticket:X", $comment);
117         
118         $old = clone($this->issue);
119         
120         if (!$this->issue->id) {
121             // set some defaults.
122             $en = DB_DataObject::factory('core_enum');
123             $en->setFrom(array('etype' => 'ticketstate', 'name' => 'new'));
124             $en->find(true);
125             $this->issue->status = $en->id;
126         }
127         $this->issue->setFrom($_POST); 
128         
129         /*
130         $kw = $this->issue->getKeywords();
131           $kill = array_values($kw);
132           foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
133             if (!strlen($w)) {
134               continue;
135             }
136             $x = array_search($w, $kw);
137             if ($x === false) {
138               $k = MTrackKeyword::loadByWord($w);
139               if ($k === null) {
140                 $k = new MTrackKeyword;
141                 $k->keyword = $w;
142                 $k->save($CS);
143               }
144               $this->issue->assocKeyword($k);
145             } else {
146               $w = array_search($w, $kill);
147               if ($w !== false) {
148                 unset($kill[$w]);
149               }
150             }
151           }
152           foreach ($kill as $w) {
153             $this->issue->dissocKeyword($w);
154           }
155      
156         
157           $ms = $this->issue->getMilestones();
158           $kill = $ms;
159           if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
160             foreach ($_POST['milestone'] as $mid) {
161               $this->issue->assocMilestone($mid);
162               unset($kill[$mid]);
163             }
164           }
165           foreach ($kill as $mid) {
166             $this->issue->dissocMilestone($mid);
167           }
168         
169           $ms = $this->issue->getComponents();
170           $kill = $ms;
171           if (isset($_POST['component']) && is_array($_POST['component'])) {
172             foreach ($_POST['component'] as $mid) {
173               $this->issue->assocComponent($mid);
174               unset($kill[$mid]);
175             }
176           }
177           foreach ($kill as $mid) {
178             $this->issue->dissocComponent($mid);
179           }
180           
181             if (!empty($_POST['comment'])) {
182                $this->issue->addComment($_POST['comment']);
183             }
184           
185           $this->issue->addEffort(
186             empty($_POST['spent']) ? 0 : $_POST['spent'], 
187             empty($_POST['estimate']) ? 0 : $_POST['estimate']
188         );
189         */
190         if ($this->issue->id) {
191             $this->issue->update($old);
192         } else {
193             $this->issue->insert();
194         }
195         // issue ticket... - add watches etc... 
196         
197         /*
198         //$this->issue->save($CS);
199         
200           if (!count($this->error)) {
201             try {
202               $this->issue->save($CS);
203               
204               // make sure everyone is watching it!!!!
205                 if($this->issue->owner && $this->issue->tid) {
206                   // make sure owner is tracking it...
207                     MTrackWatch::watch_object('ticket', $this->issue->tid,  $this->issue->owner);
208                 }
209                 
210                 if ($this->id == 'new') {
211                     MTrackWatch::watch_object('ticket', $this->issue->tid,  MTrackAuth::whoami());
212                 }
213               
214               
215               $CS->setObject("ticket:" . $this->issue->tid);
216             } catch (Exception $e) {
217               $this->error[] = $e->getMessage();
218             }
219         }
220         */
221         /*
222         if (!count($this->error)) {
223             if (!empty($_FILES['attachments'])) {
224                 require_once 'MTrack/Attachment.php';
225                 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
226                       
227                     MTrackAttachment::add("ticket:{$this->issue->tid}",
228                         $_FILES['attachments']['tmp_name'][$fileid],
229                         $_FILES['attachments']['name'][$fileid],
230                         $CS
231                     );
232                 }
233             }
234         }
235         if (!count($this->error) && $this->id != 'new') {
236             require_once 'MTrack/Attachment.php';
237             MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
238         }
239
240         if (isset($_POST['apply']) && !count($this->error)) {
241           $CS->commit();
242           header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
243           exit;
244         }
245         */
246         $this->jok($this->issue->id);
247     }
248       
249          
250     function initEditForm($params = array())
251     {
252         require_once 'HTML/Template/Flexy/Element.php';
253         require_once 'HTML/Template/Flexy/Factory.php';
254         $this->elements = array();
255         
256         
257         
258         foreach(array( 'classification', 'priority', 'severity', 'resolution' ) as $c)  {
259             $d = DB_DataObject::factory('core_enum');
260             $d->etype = $c;
261             $d->orderBy('seqid ASC, name ASC');
262             if (!$d->count()) {
263                 $d->createBaseEntries();
264                 
265             }
266             $ar = $d->fetchAll('id','name');
267             if (!$this->id) {
268                 $ar = array_merge( array(0 => '-- Select --'),$ar);
269             }
270             $this->elements[$c] = new HTML_Template_Flexy_Element('select');
271             
272             
273             
274             $this->elements[$c]->setOptions($ar);
275             
276         }
277         
278         if (false && $this->currentProject()) {
279             $d = DB_DataObject::factory('mtrack_project_component');
280             $d->project_id = $this->currentProject();
281             $d->orderBy('name');
282             $d->whereAdd('deleted != 1');
283             $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
284             $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
285             $ar = $this->issue->components();
286             $this->elements['component[]']->setValue(array_keys($ar));
287         
288         
289             $d = DB_DataObject::factory('mtrack_milestone');
290             $d->project_id = $this->currentProject();
291             $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
292             $d->whereAdd('completed != 1');
293             $d->whereAdd('deleted != 1');
294             $this->elements['milestone'] = new HTML_Template_Flexy_Element('select');
295             $this->elements['milestone']->setOptions($d->fetchAll('id', 'name'));
296             $ar = $this->issue->milestone();
297             $this->elements['milestone']->setValue(array_keys($ar));
298         }
299         
300         // FIX ME - need to determine who the owner is..
301         // for a new issue it's the person who created it.
302         // later on it's an assignement???
303         
304         $users = array();
305          
306         $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
307         $this->elements['owner']->setOptions($users);
308         
309         
310         
311         // keywords -- in toArray...
312         // milestone 
313           
314           
315         
316
317           
318     }
319    
320      
321     function eq($a,$b) {
322         return $a == $b;
323     }
324     
325 }