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         $this->get($_REQUEST['id']);
107          
108         /// $this->preview = isset($_POST['preview']) ? true : false;
109
110         // hopefull get has sorted out permissions..
111          
112         //    $CS = MTrackChangeset::begin("ticket:X", $comment);
113
114         $old = clone($this->issue);
115         
116         if (!$this->issue->id) {
117             // set some defaults.
118             $en = DB_DataObject:;factory('core_enum');
119             $en->setFrom(array('etype' => 'ticketstate', 'name' => 'new'));
120             $en->find(true);
121             $this->issue->status = $en->id;
122         }
123         $this->issue->setFrom($_POST); 
124         
125         /*
126         $kw = $this->issue->getKeywords();
127           $kill = array_values($kw);
128           foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
129             if (!strlen($w)) {
130               continue;
131             }
132             $x = array_search($w, $kw);
133             if ($x === false) {
134               $k = MTrackKeyword::loadByWord($w);
135               if ($k === null) {
136                 $k = new MTrackKeyword;
137                 $k->keyword = $w;
138                 $k->save($CS);
139               }
140               $this->issue->assocKeyword($k);
141             } else {
142               $w = array_search($w, $kill);
143               if ($w !== false) {
144                 unset($kill[$w]);
145               }
146             }
147           }
148           foreach ($kill as $w) {
149             $this->issue->dissocKeyword($w);
150           }
151      
152         
153           $ms = $this->issue->getMilestones();
154           $kill = $ms;
155           if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
156             foreach ($_POST['milestone'] as $mid) {
157               $this->issue->assocMilestone($mid);
158               unset($kill[$mid]);
159             }
160           }
161           foreach ($kill as $mid) {
162             $this->issue->dissocMilestone($mid);
163           }
164         
165           $ms = $this->issue->getComponents();
166           $kill = $ms;
167           if (isset($_POST['component']) && is_array($_POST['component'])) {
168             foreach ($_POST['component'] as $mid) {
169               $this->issue->assocComponent($mid);
170               unset($kill[$mid]);
171             }
172           }
173           foreach ($kill as $mid) {
174             $this->issue->dissocComponent($mid);
175           }
176           
177             if (!empty($_POST['comment'])) {
178                $this->issue->addComment($_POST['comment']);
179             }
180           
181           $this->issue->addEffort(
182             empty($_POST['spent']) ? 0 : $_POST['spent'], 
183             empty($_POST['estimate']) ? 0 : $_POST['estimate']
184         );
185         */
186         if ($this->issue->id) {
187             $this->issue->update($old);
188         } else {
189             $this->issue->insert();
190         }
191         // issue ticket... - add watches etc... 
192         
193         /*
194         //$this->issue->save($CS);
195         
196           if (!count($this->error)) {
197             try {
198               $this->issue->save($CS);
199               
200               // make sure everyone is watching it!!!!
201                 if($this->issue->owner && $this->issue->tid) {
202                   // make sure owner is tracking it...
203                     MTrackWatch::watch_object('ticket', $this->issue->tid,  $this->issue->owner);
204                 }
205                 
206                 if ($this->id == 'new') {
207                     MTrackWatch::watch_object('ticket', $this->issue->tid,  MTrackAuth::whoami());
208                 }
209               
210               
211               $CS->setObject("ticket:" . $this->issue->tid);
212             } catch (Exception $e) {
213               $this->error[] = $e->getMessage();
214             }
215         }
216         */
217         /*
218         if (!count($this->error)) {
219             if (!empty($_FILES['attachments'])) {
220                 require_once 'MTrack/Attachment.php';
221                 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
222                       
223                     MTrackAttachment::add("ticket:{$this->issue->tid}",
224                         $_FILES['attachments']['tmp_name'][$fileid],
225                         $_FILES['attachments']['name'][$fileid],
226                         $CS
227                     );
228                 }
229             }
230         }
231         if (!count($this->error) && $this->id != 'new') {
232             require_once 'MTrack/Attachment.php';
233             MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
234         }
235
236         if (isset($_POST['apply']) && !count($this->error)) {
237           $CS->commit();
238           header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
239           exit;
240         }
241         */
242         $this->jok($this->issue->id);
243     }
244       
245          
246     function initEditForm($params = array())
247     {
248         require_once 'HTML/Template/Flexy/Element.php';
249         require_once 'HTML/Template/Flexy/Factory.php';
250         $this->elements = array();
251         
252         
253         
254         foreach(array( 'classification', 'priority', 'severity', 'resolution' ) as $c)  {
255             $d = DB_DataObject::factory('core_enum');
256             $d->etype = $c;
257             $d->orderBy('seqid ASC, name ASC');
258             if (!$d->count()) {
259                 $d->createBaseEntries();
260                 
261             }
262             $ar = $d->fetchAll('id','name');
263             if (!$this->id) {
264                 $ar = array_merge( array(0 => '-- Select --'),$ar);
265             }
266             $this->elements[$c] = new HTML_Template_Flexy_Element('select');
267             
268             
269             
270             $this->elements[$c]->setOptions($ar);
271             
272         }
273         
274         if (false && $this->currentProject()) {
275             $d = DB_DataObject::factory('mtrack_project_component');
276             $d->project_id = $this->currentProject();
277             $d->orderBy('name');
278             $d->whereAdd('deleted != 1');
279             $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
280             $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
281             $ar = $this->issue->components();
282             $this->elements['component[]']->setValue(array_keys($ar));
283         
284         
285             $d = DB_DataObject::factory('mtrack_milestone');
286             $d->project_id = $this->currentProject();
287             $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
288             $d->whereAdd('completed != 1');
289             $d->whereAdd('deleted != 1');
290             $this->elements['milestone'] = new HTML_Template_Flexy_Element('select');
291             $this->elements['milestone']->setOptions($d->fetchAll('id', 'name'));
292             $ar = $this->issue->milestone();
293             $this->elements['milestone']->setValue(array_keys($ar));
294         }
295         
296         // FIX ME - need to determine who the owner is..
297         // for a new issue it's the person who created it.
298         // later on it's an assignement???
299         
300         $users = array();
301          
302         $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
303         $this->elements['owner']->setOptions($users);
304         
305         
306         
307         // keywords -- in toArray...
308         // milestone 
309           
310           
311         
312
313           
314     }
315    
316      
317     function eq($a,$b) {
318         return $a == $b;
319     }
320     
321 }