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