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'])) {
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;
42 $this->issue = DB_DataObject::factory('mtrack_ticket');
45 if (!$this->issue->get($this->id)) {
46 throw new Exception("Invalid ticket $this->id");
48 if ($this->id && $this->currentProject() != $this->issue->project_id) {
49 throw new Exception("Project and ticket do not match..");
56 //$this->issue->augmentFormFields($this->fieldset());
59 $this->preview = false;
60 $this->error = array();
63 if (!$this->id && !$this->hasPerm('MTrack.Issue','A')) {
64 return HTML_FlexyFramework::run('Noperm');
69 !$this->hasPerm('MTrack.Issue','S') || // general permission to view
70 !$this->ticket->hasPerm($this->authUser,'S') // specific permission on this bug.
73 return HTML_FlexyFramework::run('Noperm');
81 // new is always editable..????
82 $this->editable = $this->id ?
83 $this->ticket->hasPerm($this->authUser,'E') : true;
85 $this->issue->milestoneURL = $this->baseURL.'/Milestone'; // fix me later..
87 $this->showEditBar = false;
89 if ($this->editable && $this->id && !$this->preview) {
90 $this->showEditBar = true;
93 if (empty($_POST)) { // post is ajax only..
97 $this->initEditForm();
103 function post() // handle the post...
106 $this->get($_REQUEST['id']);
108 /// $this->preview = isset($_POST['preview']) ? true : false;
110 // hopefull get has sorted out permissions..
112 // $CS = MTrackChangeset::begin("ticket:X", $comment);
114 $old = clone($this->issue);
116 if (!$this->issue->id) {
117 // set some defaults.
118 $en = DB_DataObject:;factory('core_enum');
119 $en->setFrom(array('etype' => 'ticketstate', 'name' => 'new'));
121 $this->issue->status = $en->id;
123 $this->issue->setFrom($_POST);
126 $kw = $this->issue->getKeywords();
127 $kill = array_values($kw);
128 foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
132 $x = array_search($w, $kw);
134 $k = MTrackKeyword::loadByWord($w);
136 $k = new MTrackKeyword;
140 $this->issue->assocKeyword($k);
142 $w = array_search($w, $kill);
148 foreach ($kill as $w) {
149 $this->issue->dissocKeyword($w);
153 $ms = $this->issue->getMilestones();
155 if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
156 foreach ($_POST['milestone'] as $mid) {
157 $this->issue->assocMilestone($mid);
161 foreach ($kill as $mid) {
162 $this->issue->dissocMilestone($mid);
165 $ms = $this->issue->getComponents();
167 if (isset($_POST['component']) && is_array($_POST['component'])) {
168 foreach ($_POST['component'] as $mid) {
169 $this->issue->assocComponent($mid);
173 foreach ($kill as $mid) {
174 $this->issue->dissocComponent($mid);
177 if (!empty($_POST['comment'])) {
178 $this->issue->addComment($_POST['comment']);
181 $this->issue->addEffort(
182 empty($_POST['spent']) ? 0 : $_POST['spent'],
183 empty($_POST['estimate']) ? 0 : $_POST['estimate']
186 if ($this->issue->id) {
187 $this->issue->update($old);
189 $this->issue->insert();
191 // issue ticket... - add watches etc...
194 //$this->issue->save($CS);
196 if (!count($this->error)) {
198 $this->issue->save($CS);
200 // make sure everyone is watching it!!!!
201 if($this->issue->owner && $this->issue->tid) {
202 // make sure owner is tracking it...
203 MTrackWatch::watch_object('ticket', $this->issue->tid, $this->issue->owner);
206 if ($this->id == 'new') {
207 MTrackWatch::watch_object('ticket', $this->issue->tid, MTrackAuth::whoami());
211 $CS->setObject("ticket:" . $this->issue->tid);
212 } catch (Exception $e) {
213 $this->error[] = $e->getMessage();
218 if (!count($this->error)) {
219 if (!empty($_FILES['attachments'])) {
220 require_once 'MTrack/Attachment.php';
221 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
223 MTrackAttachment::add("ticket:{$this->issue->tid}",
224 $_FILES['attachments']['tmp_name'][$fileid],
225 $_FILES['attachments']['name'][$fileid],
231 if (!count($this->error) && $this->id != 'new') {
232 require_once 'MTrack/Attachment.php';
233 MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
236 if (isset($_POST['apply']) && !count($this->error)) {
238 header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
242 $this->jok($this->issue->id);
246 function initEditForm($params = array())
248 require_once 'HTML/Template/Flexy/Element.php';
249 require_once 'HTML/Template/Flexy/Factory.php';
250 $this->elements = array();
254 foreach(array( 'classification', 'priority', 'severity', 'resolution' ) as $c) {
255 $d = DB_DataObject::factory('core_enum');
257 $d->orderBy('seqid ASC, name ASC');
259 $d->createBaseEntries();
262 $ar = $d->fetchAll('id','name');
264 $ar = array_merge( array(0 => '-- Select --'),$ar);
266 $this->elements[$c] = new HTML_Template_Flexy_Element('select');
270 $this->elements[$c]->setOptions($ar);
274 if (false && $this->currentProject()) {
275 $d = DB_DataObject::factory('mtrack_project_component');
276 $d->project_id = $this->currentProject();
278 $d->whereAdd('deleted != 1');
279 $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
280 $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
281 $ar = $this->issue->components();
282 $this->elements['component[]']->setValue(array_keys($ar));
285 $d = DB_DataObject::factory('mtrack_milestone');
286 $d->project_id = $this->currentProject();
287 $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
288 $d->whereAdd('completed != 1');
289 $d->whereAdd('deleted != 1');
290 $this->elements['milestone'] = new HTML_Template_Flexy_Element('select');
291 $this->elements['milestone']->setOptions($d->fetchAll('id', 'name'));
292 $ar = $this->issue->milestone();
293 $this->elements['milestone']->setValue(array_keys($ar));
296 // FIX ME - need to determine who the owner is..
297 // for a new issue it's the person who created it.
298 // later on it's an assignement???
302 $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
303 $this->elements['owner']->setOptions($users);
307 // keywords -- in toArray...