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