1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
4 //require_once 'MTrack/Captcha.php';
7 require_once 'MTrackWeb.php';
8 class MTrackWeb_TicketEdit extends MTrackWeb
15 var $tid = 0; // or the MD5 rep.
22 //require_once 'MTrack/ACL.php';
23 // MTrackACL::requireAllRights('Browser', 'read');
31 if (!isset($_REQUEST['ajax_body'])) {
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;
41 $ret = $this->loadIssue();
42 $this->initEditForm();
46 function loadIssue() {
49 $this->issue = DB_DataObject::factory('mtrack_ticket');
52 if (!$this->issue->get($this->id)) {
53 throw new Exception("Invalid ticket $this->id");
55 if ($this->id && $this->currentProject() != $this->issue->project_id) {
56 throw new Exception("Project and ticket do not match..");
63 //$this->issue->augmentFormFields($this->fieldset());
66 $this->preview = false;
67 $this->error = array();
70 if (!$this->id && !$this->hasPerm('MTrack.Issue','A')) {
71 return HTML_FlexyFramework::run('Noperm');
76 !$this->hasPerm('MTrack.Issue','S') || // general permission to view
77 !$this->ticket->hasPerm($this->authUser,'S') // specific permission on this bug.
80 return HTML_FlexyFramework::run('Noperm');
88 // new is always editable..????
89 $this->editable = $this->id ?
90 $this->ticket->hasPerm($this->authUser,'E') : true;
92 $this->issue->milestoneURL = $this->baseURL.'/Milestone'; // fix me later..
94 $this->showEditBar = false;
96 if ($this->editable && $this->id && !$this->preview) {
97 $this->showEditBar = true;
107 function post() // handle the post...
110 $this->id = $_REQUEST['id'];
111 $ret = $this->loadIssue();
113 $this->jerr("load issue failed");
116 //echo '<PRE>';print_R($this);exit;
117 /// $this->preview = isset($_POST['preview']) ? true : false;
119 // hopefull get has sorted out permissions..
121 // $CS = MTrackChangeset::begin("ticket:X", $comment);
123 $old = clone($this->issue);
125 if (!$this->issue->id) {
126 // set some defaults.
127 $en = DB_DataObject::factory('core_enum');
128 $en->setFrom(array('etype' => 'ticketstate', 'name' => 'new'));
130 $this->issue->status = $en->id;
132 $this->issue->setFrom($_POST);
135 $kw = $this->issue->getKeywords();
136 $kill = array_values($kw);
137 foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
141 $x = array_search($w, $kw);
143 $k = MTrackKeyword::loadByWord($w);
145 $k = new MTrackKeyword;
149 $this->issue->assocKeyword($k);
151 $w = array_search($w, $kill);
157 foreach ($kill as $w) {
158 $this->issue->dissocKeyword($w);
162 $ms = $this->issue->getMilestones();
164 if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
165 foreach ($_POST['milestone'] as $mid) {
166 $this->issue->assocMilestone($mid);
170 foreach ($kill as $mid) {
171 $this->issue->dissocMilestone($mid);
174 $ms = $this->issue->getComponents();
176 if (isset($_POST['component']) && is_array($_POST['component'])) {
177 foreach ($_POST['component'] as $mid) {
178 $this->issue->assocComponent($mid);
182 foreach ($kill as $mid) {
183 $this->issue->dissocComponent($mid);
186 if (!empty($_POST['comment'])) {
187 $this->issue->addComment($_POST['comment']);
190 $this->issue->addEffort(
191 empty($_POST['spent']) ? 0 : $_POST['spent'],
192 empty($_POST['estimate']) ? 0 : $_POST['estimate']
195 $this->issue = $this->currentProject();
197 if ($this->issue->id) {
198 $this->issue->update($old);
200 $this->issue->insert();
202 // issue ticket... - add watches etc...
205 //$this->issue->save($CS);
207 if (!count($this->error)) {
209 $this->issue->save($CS);
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);
217 if ($this->id == 'new') {
218 MTrackWatch::watch_object('ticket', $this->issue->tid, MTrackAuth::whoami());
222 $CS->setObject("ticket:" . $this->issue->tid);
223 } catch (Exception $e) {
224 $this->error[] = $e->getMessage();
229 if (!count($this->error)) {
230 if (!empty($_FILES['attachments'])) {
231 require_once 'MTrack/Attachment.php';
232 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
234 MTrackAttachment::add("ticket:{$this->issue->tid}",
235 $_FILES['attachments']['tmp_name'][$fileid],
236 $_FILES['attachments']['name'][$fileid],
242 if (!count($this->error) && $this->id != 'new') {
243 require_once 'MTrack/Attachment.php';
244 MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
247 if (isset($_POST['apply']) && !count($this->error)) {
249 header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
253 $this->jok($this->issue->id);
257 function initEditForm($params = array())
259 require_once 'HTML/Template/Flexy/Element.php';
260 require_once 'HTML/Template/Flexy/Factory.php';
261 $this->elements = array();
265 foreach(array( 'classification', 'priority', 'severity', 'resolution' ) as $c) {
266 $d = DB_DataObject::factory('core_enum');
268 $d->orderBy('seqid ASC, name ASC');
270 $d->createBaseEntries();
273 $ar = $d->fetchAll('id','name');
275 $ar = array_merge( array(0 => '-- Select --'),$ar);
277 $this->elements[$c] = new HTML_Template_Flexy_Element('select');
281 $this->elements[$c]->setOptions($ar);
285 if (false && $this->currentProject()) {
286 $d = DB_DataObject::factory('mtrack_project_component');
287 $d->project_id = $this->currentProject();
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));
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));
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???
313 $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
314 $this->elements['owner']->setOptions($users);
318 // keywords -- in toArray...