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 $ret = $this->get($_REQUEST['id']);
112 echo '<PRE>';print_R($this);exit;
113 /// $this->preview = isset($_POST['preview']) ? true : false;
115 // hopefull get has sorted out permissions..
117 // $CS = MTrackChangeset::begin("ticket:X", $comment);
119 $old = clone($this->issue);
121 if (!$this->issue->id) {
122 // set some defaults.
123 $en = DB_DataObject::factory('core_enum');
124 $en->setFrom(array('etype' => 'ticketstate', 'name' => 'new'));
126 $this->issue->status = $en->id;
128 $this->issue->setFrom($_POST);
131 $kw = $this->issue->getKeywords();
132 $kill = array_values($kw);
133 foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
137 $x = array_search($w, $kw);
139 $k = MTrackKeyword::loadByWord($w);
141 $k = new MTrackKeyword;
145 $this->issue->assocKeyword($k);
147 $w = array_search($w, $kill);
153 foreach ($kill as $w) {
154 $this->issue->dissocKeyword($w);
158 $ms = $this->issue->getMilestones();
160 if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
161 foreach ($_POST['milestone'] as $mid) {
162 $this->issue->assocMilestone($mid);
166 foreach ($kill as $mid) {
167 $this->issue->dissocMilestone($mid);
170 $ms = $this->issue->getComponents();
172 if (isset($_POST['component']) && is_array($_POST['component'])) {
173 foreach ($_POST['component'] as $mid) {
174 $this->issue->assocComponent($mid);
178 foreach ($kill as $mid) {
179 $this->issue->dissocComponent($mid);
182 if (!empty($_POST['comment'])) {
183 $this->issue->addComment($_POST['comment']);
186 $this->issue->addEffort(
187 empty($_POST['spent']) ? 0 : $_POST['spent'],
188 empty($_POST['estimate']) ? 0 : $_POST['estimate']
191 if ($this->issue->id) {
192 $this->issue->update($old);
194 $this->issue->insert();
196 // issue ticket... - add watches etc...
199 //$this->issue->save($CS);
201 if (!count($this->error)) {
203 $this->issue->save($CS);
205 // make sure everyone is watching it!!!!
206 if($this->issue->owner && $this->issue->tid) {
207 // make sure owner is tracking it...
208 MTrackWatch::watch_object('ticket', $this->issue->tid, $this->issue->owner);
211 if ($this->id == 'new') {
212 MTrackWatch::watch_object('ticket', $this->issue->tid, MTrackAuth::whoami());
216 $CS->setObject("ticket:" . $this->issue->tid);
217 } catch (Exception $e) {
218 $this->error[] = $e->getMessage();
223 if (!count($this->error)) {
224 if (!empty($_FILES['attachments'])) {
225 require_once 'MTrack/Attachment.php';
226 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
228 MTrackAttachment::add("ticket:{$this->issue->tid}",
229 $_FILES['attachments']['tmp_name'][$fileid],
230 $_FILES['attachments']['name'][$fileid],
236 if (!count($this->error) && $this->id != 'new') {
237 require_once 'MTrack/Attachment.php';
238 MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
241 if (isset($_POST['apply']) && !count($this->error)) {
243 header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
247 $this->jok($this->issue->id);
251 function initEditForm($params = array())
253 require_once 'HTML/Template/Flexy/Element.php';
254 require_once 'HTML/Template/Flexy/Factory.php';
255 $this->elements = array();
259 foreach(array( 'classification', 'priority', 'severity', 'resolution' ) as $c) {
260 $d = DB_DataObject::factory('core_enum');
262 $d->orderBy('seqid ASC, name ASC');
264 $d->createBaseEntries();
267 $ar = $d->fetchAll('id','name');
269 $ar = array_merge( array(0 => '-- Select --'),$ar);
271 $this->elements[$c] = new HTML_Template_Flexy_Element('select');
275 $this->elements[$c]->setOptions($ar);
279 if (false && $this->currentProject()) {
280 $d = DB_DataObject::factory('mtrack_project_component');
281 $d->project_id = $this->currentProject();
283 $d->whereAdd('deleted != 1');
284 $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
285 $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
286 $ar = $this->issue->components();
287 $this->elements['component[]']->setValue(array_keys($ar));
290 $d = DB_DataObject::factory('mtrack_milestone');
291 $d->project_id = $this->currentProject();
292 $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
293 $d->whereAdd('completed != 1');
294 $d->whereAdd('deleted != 1');
295 $this->elements['milestone'] = new HTML_Template_Flexy_Element('select');
296 $this->elements['milestone']->setOptions($d->fetchAll('id', 'name'));
297 $ar = $this->issue->milestone();
298 $this->elements['milestone']->setValue(array_keys($ar));
301 // FIX ME - need to determine who the owner is..
302 // for a new issue it's the person who created it.
303 // later on it's an assignement???
307 $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
308 $this->elements['owner']->setOptions($users);
312 // keywords -- in toArray...