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;
40 // var_dump($this->id);
41 $ret = $this->loadIssue();
42 $this->initEditForm();
48 function loadIssue() {
51 $this->issue = DB_DataObject::factory('mtrack_ticket');
54 if (!$this->issue->get($this->id)) {
55 throw new Exception("Invalid ticket $this->id");
57 if ($this->id && $this->currentProject() != $this->issue->project_id) {
58 throw new Exception("Project and ticket do not match..");
65 //$this->issue->augmentFormFields($this->fieldset());
68 $this->preview = false;
69 $this->error = array();
72 if (!$this->id && !$this->hasPerm('MTrack.Issue','A')) {
73 return HTML_FlexyFramework::run('Noperm');
78 !$this->hasPerm('MTrack.Issue','E')
80 return HTML_FlexyFramework::run('Noperm');
89 // new is always editable..????
90 $this->editable = true; //$this->id ?
91 //$this->ticket->hasPerm($this->authUser,'E') : true;
93 $this->issue->milestoneURL = $this->baseURL.'/Milestone'; // fix me later..
95 $this->showEditBar = false;
97 if ($this->editable && $this->id && !$this->preview) {
98 $this->showEditBar = true;
108 function post() // handle the post...
111 $this->id = $_REQUEST['id'];
112 $ret = $this->loadIssue();
114 $this->jerr("load issue failed");
117 //echo '<PRE>';print_R($this);exit;
118 /// $this->preview = isset($_POST['preview']) ? true : false;
120 // hopefull get has sorted out permissions..
122 // $CS = MTrackChangeset::begin("ticket:X", $comment);
124 $old = clone($this->issue);
126 if (!$this->issue->id) {
127 // set some defaults.
128 $en = DB_DataObject::factory('core_enum');
129 $en->setFrom(array('etype' => 'ticketstate', 'name' => 'new'));
131 $this->issue->status = $en->id;
133 $this->issue->setFrom($_POST);
136 $kw = $this->issue->getKeywords();
137 $kill = array_values($kw);
138 foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
142 $x = array_search($w, $kw);
144 $k = MTrackKeyword::loadByWord($w);
146 $k = new MTrackKeyword;
150 $this->issue->assocKeyword($k);
152 $w = array_search($w, $kill);
158 foreach ($kill as $w) {
159 $this->issue->dissocKeyword($w);
163 $ms = $this->issue->getMilestones();
165 if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
166 foreach ($_POST['milestone'] as $mid) {
167 $this->issue->assocMilestone($mid);
171 foreach ($kill as $mid) {
172 $this->issue->dissocMilestone($mid);
175 $ms = $this->issue->getComponents();
177 if (isset($_POST['component']) && is_array($_POST['component'])) {
178 foreach ($_POST['component'] as $mid) {
179 $this->issue->assocComponent($mid);
183 foreach ($kill as $mid) {
184 $this->issue->dissocComponent($mid);
187 if (!empty($_POST['comment'])) {
188 $this->issue->addComment($_POST['comment']);
191 $this->issue->addEffort(
192 empty($_POST['spent']) ? 0 : $_POST['spent'],
193 empty($_POST['estimate']) ? 0 : $_POST['estimate']
196 $this->issue->project_id = $this->currentProject();
199 $CS = DB_DataObject::factory('mtrack_change');
203 if ($this->issue->id) {
204 $this->issue->update($old);
206 $this->issue->insert();
210 $CS->begin($this->issue, $old ? 'Changed' : 'Created');
212 $this->issue->created = $CS->id;
213 $this->issue->update();
215 $CS->add($this->issue, $old);
217 // issue ticket... - add watches etc...
220 //$this->issue->save($CS);
222 if (!count($this->error)) {
224 $this->issue->save($CS);
226 // make sure everyone is watching it!!!!
227 if($this->issue->owner && $this->issue->tid) {
228 // make sure owner is tracking it...
229 MTrackWatch::watch_object('ticket', $this->issue->tid, $this->issue->owner);
232 if ($this->id == 'new') {
233 MTrackWatch::watch_object('ticket', $this->issue->tid, MTrackAuth::whoami());
237 $CS->setObject("ticket:" . $this->issue->tid);
238 } catch (Exception $e) {
239 $this->error[] = $e->getMessage();
244 if (!count($this->error)) {
245 if (!empty($_FILES['attachments'])) {
246 require_once 'MTrack/Attachment.php';
247 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
249 MTrackAttachment::add("ticket:{$this->issue->tid}",
250 $_FILES['attachments']['tmp_name'][$fileid],
251 $_FILES['attachments']['name'][$fileid],
257 if (!count($this->error) && $this->id != 'new') {
258 require_once 'MTrack/Attachment.php';
259 MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
262 if (isset($_POST['apply']) && !count($this->error)) {
264 header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
268 $this->jok($this->issue->id);
272 function initEditForm($params = array())
274 require_once 'HTML/Template/Flexy/Element.php';
275 require_once 'HTML/Template/Flexy/Factory.php';
279 foreach(array( 'classification', 'priority', 'severity', 'resolution' ) as $c) {
280 $d = DB_DataObject::factory('core_enum');
282 $d->orderBy('seqid ASC, name ASC');
284 $d->createBaseEntries();
287 $ar = $d->fetchAll('id','name');
289 $ar = array_merge( array(0 => '-- Select --'),$ar);
291 $this->elements[$c.'_id'] = new HTML_Template_Flexy_Element('select');
292 $this->elements[$c.'_id']->setOptions($ar);
296 if (false && $this->currentProject()) {
297 $d = DB_DataObject::factory('mtrack_project_component');
298 $d->project_id = $this->currentProject();
300 $d->whereAdd('deleted != 1');
301 $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
302 $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
303 $ar = $this->issue->components();
304 $this->elements['component[]']->setValue(array_keys($ar));
307 $d = DB_DataObject::factory('mtrack_milestone');
308 $d->project_id = $this->currentProject();
309 $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
310 $d->whereAdd('completed != 1');
311 $d->whereAdd('deleted != 1');
312 $this->elements['milestone'] = new HTML_Template_Flexy_Element('select');
313 $this->elements['milestone']->setOptions($d->fetchAll('id', 'name'));
314 $ar = $this->issue->milestone();
315 $this->elements['milestone']->setValue(array_keys($ar));
318 // FIX ME - need to determine who the owner is..
319 // for a new issue it's the person who created it.
320 // later on it's an assignement???
324 $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
325 $this->elements['owner']->setOptions($users);
329 $this->elements = HTML_Template_Flexy_Factory::fromArray($this->issue->toArray(), $this->elements);
332 // keywords -- in toArray...