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