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