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 $this->jerr('Permission denied = no rights to create tickets', array('noperm' => true));
74 //return HTML_FlexyFramework::run('Noperm');
79 !$this->hasPerm('MTrack.Issue','E')
81 $this->jerr('Permission denied = no rights to edit tickets', array('noperm' => true));
82 return HTML_FlexyFramework::run('Noperm');
91 // new is always editable..????
92 $this->editable = true; //$this->id ?
93 //$this->ticket->hasPerm($this->authUser,'E') : true;
95 $this->issue->milestoneURL = $this->baseURL.'/Milestone'; // fix me later..
97 $this->showEditBar = false;
99 if ($this->editable && $this->id && !$this->preview) {
100 $this->showEditBar = true;
110 function post() // handle the post...
113 $this->id = $_REQUEST['id'];
114 $ret = $this->loadIssue();
116 $this->jerr("load issue failed");
119 //echo '<PRE>';print_R($this);exit;
120 /// $this->preview = isset($_POST['preview']) ? true : false;
122 // hopefull get has sorted out permissions..
124 // $CS = MTrackChangeset::begin("ticket:X", $comment);
126 $old = clone($this->issue);
128 if (!$this->issue->id) {
129 // set some defaults.
130 $en = DB_DataObject::factory('core_enum');
131 $en->setFrom(array('etype' => 'ticketstate', 'name' => 'new'));
133 $this->issue->status = $en->id;
135 $this->issue->setFrom($_POST);
138 $kw = $this->issue->getKeywords();
139 $kill = array_values($kw);
140 foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
144 $x = array_search($w, $kw);
146 $k = MTrackKeyword::loadByWord($w);
148 $k = new MTrackKeyword;
152 $this->issue->assocKeyword($k);
154 $w = array_search($w, $kill);
160 foreach ($kill as $w) {
161 $this->issue->dissocKeyword($w);
165 $ms = $this->issue->getMilestones();
167 if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
168 foreach ($_POST['milestone'] as $mid) {
169 $this->issue->assocMilestone($mid);
173 foreach ($kill as $mid) {
174 $this->issue->dissocMilestone($mid);
177 $ms = $this->issue->getComponents();
179 if (isset($_POST['component']) && is_array($_POST['component'])) {
180 foreach ($_POST['component'] as $mid) {
181 $this->issue->assocComponent($mid);
185 foreach ($kill as $mid) {
186 $this->issue->dissocComponent($mid);
189 if (!empty($_POST['comment'])) {
190 $this->issue->addComment($_POST['comment']);
193 $this->issue->addEffort(
194 empty($_POST['spent']) ? 0 : $_POST['spent'],
195 empty($_POST['estimate']) ? 0 : $_POST['estimate']
198 $this->issue->project_id = $this->currentProject();
201 $CS = DB_DataObject::factory('mtrack_change');
205 if ($this->issue->id) {
206 $this->issue->update($old);
208 $this->issue->insert();
212 $CS->begin($this->issue, $old ? 'Changed' : 'Created');
214 $this->issue->created = $CS->id;
215 $this->issue->update();
217 $CS->add($this->issue, $old);
219 // issue ticket... - add watches etc...
222 //$this->issue->save($CS);
224 if (!count($this->error)) {
226 $this->issue->save($CS);
228 // make sure everyone is watching it!!!!
229 if($this->issue->owner && $this->issue->tid) {
230 // make sure owner is tracking it...
231 MTrackWatch::watch_object('ticket', $this->issue->tid, $this->issue->owner);
234 if ($this->id == 'new') {
235 MTrackWatch::watch_object('ticket', $this->issue->tid, MTrackAuth::whoami());
239 $CS->setObject("ticket:" . $this->issue->tid);
240 } catch (Exception $e) {
241 $this->error[] = $e->getMessage();
246 if (!count($this->error)) {
247 if (!empty($_FILES['attachments'])) {
248 require_once 'MTrack/Attachment.php';
249 foreach ($_FILES['attachments']['name'] as $fileid => $name) {
251 MTrackAttachment::add("ticket:{$this->issue->tid}",
252 $_FILES['attachments']['tmp_name'][$fileid],
253 $_FILES['attachments']['name'][$fileid],
259 if (!count($this->error) && $this->id != 'new') {
260 require_once 'MTrack/Attachment.php';
261 MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
264 if (isset($_POST['apply']) && !count($this->error)) {
266 header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
270 $this->jok($this->issue->id);
274 function initEditForm($params = array())
276 require_once 'HTML/Template/Flexy/Element.php';
277 require_once 'HTML/Template/Flexy/Factory.php';
281 foreach(array( 'classification', 'priority', 'severity' ) as $c) {
282 $d = DB_DataObject::factory('core_enum');
284 $d->orderBy('seqid ASC, name ASC');
286 $d->createBaseEntries();
289 $ar = $d->fetchAll('id','name');
292 $ak = array_keys($ar);
293 $mid = floor(count($ak) / 2 );
296 //var_dump($ar[$def]);
298 $this->elements[$c.'_id'] = new HTML_Template_Flexy_Element('select');
299 $this->elements[$c.'_id']->setOptions($ar);
301 $this->elements[$c.'_id']->setValue($def);
306 // DB_DAtaObject::DebugLevel(1);
308 $pd = DB_DataObject::factory('ProjectDirectory');
309 $pd->project_id = $this->currentProject();
310 $pd->whereAdd("ProjectDirectory.role != ''");
311 $pd->joinAdd(DB_DataObject::factory('Person'), 'LEFT');
313 $pd->selectAdd("distinct(Person.id) as id , CONCAT(Person.name , '<', Person.email , '>') as name");
314 $pd->groupBy('Person.id, Person.name');
315 $pd->orderBy('Person.name');
316 $users = $pd->fetchAll('id', 'name');
317 $users = array('' => '--select--') + $users;
321 $this->elements['owner_id'] = new HTML_Template_Flexy_Element('select');
322 $this->elements['owner_id']->setOptions($users);
324 $this->elements['developer_id'] = new HTML_Template_Flexy_Element('select');
325 $this->elements['developer_id']->setOptions($users);
328 if (false && $this->currentProject()) {
329 $d = DB_DataObject::factory('mtrack_project_component');
330 $d->project_id = $this->currentProject();
332 $d->whereAdd('deleted != 1');
333 $this->elements['component[]'] = new HTML_Template_Flexy_Element('select');
334 $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
335 $ar = $this->issue->components();
336 $this->elements['component[]']->setValue(array_keys($ar));
339 $d = DB_DataObject::factory('mtrack_milestone');
340 $d->project_id = $this->currentProject();
341 $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
342 $d->whereAdd('completed != 1');
343 $d->whereAdd('deleted != 1');
344 $this->elements['milestone'] = new HTML_Template_Flexy_Element('select');
345 $this->elements['milestone']->setOptions($d->fetchAll('id', 'name'));
346 $ar = $this->issue->milestone();
347 $this->elements['milestone']->setValue(array_keys($ar));
350 // FIX ME - need to determine who the owner is..
351 // for a new issue it's the person who created it.
352 // later on it's an assignement???
357 $this->elements = HTML_Template_Flexy_Factory::fromArray($this->issue->toArray(), $this->elements);
361 // keywords -- in toArray...