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