sync
[web.mtrack] / MTrackWeb / TicketEdit.php
index b639eb4..41e5176 100644 (file)
@@ -31,12 +31,21 @@ class MTrackWeb_TicketEdit extends MTrackWeb
         if (!isset($_REQUEST['ajax_body'])) {
             return;
         }
+        
+        
         $this->masterTemplate = 'ticket_edit.html';
-        $this->id = $pi ?  $pi: (isset($_GET['id']) ? $_GET['id'] : 0);
+        $this->id = $pi ?  $pi : (isset($_GET['id']) ? $_GET['id'] : 0);
         $this->id  = $this->id  == 'new' ? 0 : $this->id;
         $this->id  = (int) $this->id;
+       // var_dump($this->id);
+        $ret = $this->loadIssue();
+        $this->initEditForm();
+        
         
+        return $ret;
+    }
     
+    function loadIssue() { 
         // -- load issue..
 
         $this->issue = DB_DataObject::factory('mtrack_ticket');
@@ -61,15 +70,15 @@ class MTrackWeb_TicketEdit extends MTrackWeb
        
        
         if (!$this->id && !$this->hasPerm('MTrack.Issue','A')) {
-            return HTML_FlexyFramework::run('Noperm');
+            $this->jerr('Permission denied = no rights to create tickets', array('noperm' => true));
+            //return HTML_FlexyFramework::run('Noperm');
         }
         
         
         if ($this->id &&  (
-                !$this->hasPerm('MTrack.Issue','S') || // general permission to view
-                !$this->ticket->hasPerm($this->authUser,'S') // specific permission on this bug.
-                
+                !$this->hasPerm('MTrack.Issue','E')
             )) {
+            $this->jerr('Permission denied = no rights to edit tickets', array('noperm' => true));
             return HTML_FlexyFramework::run('Noperm');
         }
         
@@ -78,9 +87,10 @@ class MTrackWeb_TicketEdit extends MTrackWeb
         
         
         
+        
         // new is always editable..????
-        $this->editable = $this->id ?
-            $this->ticket->hasPerm($this->authUser,'E')  : true;
+        $this->editable = true; //$this->id ?
+            //$this->ticket->hasPerm($this->authUser,'E')  : true;
          
         $this->issue->milestoneURL = $this->baseURL.'/Milestone'; // fix me later..
     
@@ -90,11 +100,8 @@ class MTrackWeb_TicketEdit extends MTrackWeb
             $this->showEditBar = true;
         }
         
-        if (empty($_POST)) {
-            return;
-        }
+         
         
-        $this->initEditForm();
  
         
       
@@ -103,138 +110,57 @@ class MTrackWeb_TicketEdit extends MTrackWeb
     function post() // handle the post...
     {
        
-        $this->get($_REQUEST['id']);
-        die("TODO");
-        if (isset($_POST['cancel'])) {
-            header("Location: {$$this->baseURL}/Ticket/$this->issue->nsident");
+        $this->id = $_REQUEST['id'];
+        $ret = $this->loadIssue();
+        if (!empty($ret)) {
+            $this->jerr("load issue failed");
             exit;
-          }
-          
-          if (!MTrack_Captcha::check('ticket')) {
-            $this->error[] = "CAPTCHA failed, please try again";
-          }
-          $this->preview = isset($_POST['preview']) ? true : false;
+        }
+        //echo '<PRE>';print_R($this);exit;
+        /// $this->preview = isset($_POST['preview']) ? true : false;
 
-          $comment = '';
-          try {
-            if (!$this->id) {
-              MTrackACL::requireAllRights("Tickets", 'create');
-            } else {
-              MTrackACL::requireAllRights("ticket:" . $this->issue->tid, 'modify');
+        // hopefull get has sorted out permissions..
+         
+        //    $CS = MTrackChangeset::begin("ticket:X", $comment);
+        
+        $old = clone($this->issue);
+        
+        if (!$this->issue->id) {
+            // set some defaults.
+            $en = DB_DataObject::factory('core_enum');
+            $en->setFrom(array('etype' => 'ticketstate', 'name' => 'new'));
+            $en->find(true);
+            $this->issue->status = $en->id;
+        }
+        
+        
+        //  VALIDATE POST!??!
+        
+        
+        if (!empty($_REQUEST['act_now'])) { //=+ adding act now..
+            // check project / assigned person / !closed for more than 3 'act_now'
+            //DB_DataObject::DebugLevel(1);
+            $xx = DB_DataObject::factory('mtrack_ticket');
+            $xx->owner_id = $this->issue->owner_id;
+            $xx->project_id  = $this->issue->project_id;
+            $xx->act_now = 1;
+            $xx->resolution_id = 0;
+            $xx->whereAdd('id != ' . ((int) $this->issue->id)); // just in case we are double setting...
+            if ($xx->count() > 2) {
+                $this->jerr("To many act-now tasks assigned to that person, wait until the have completed some");
             }
-          } catch (Exception $e) {
-            $this->error[] = $e->getMessage();
-          }
-          
-          if (!$this->id) {
-            $comment = empty($_POST['comment']) ? '' : $_POST['comment'];
-          }
-          
-          if (!strlen($comment)) {
-            $comment = $_POST['summary'];
-          }
-          try {
-            $CS = MTrackChangeset::begin("ticket:X", $comment);
-          } catch (Exception $e) {
-            $this->error[] = $e->getMessage();
-            $CS = null;
-          }
-          if (!$this->id) {
-            // compute next id number.
-            // We don't use auto-number, because we allow for importing multiple
-            // projects with their own ticket sequence.
-            // During "normal" user-driven operation, we do want plain old id numbers
-            // so we compute it here, under a transaction
-            $db = MTrackDB::get();
-            
             
             
-            switch($db->getAttribute(PDO::ATTR_DRIVER_NAME)) {
-                case 'pgsql':
-                    // Some versions of postgres don't like that we have "abc123" for
-                    // identifiers, so match on the bigest number nsident fields only
-                    $max = "select max(cast(nsident as integer)) + 1 from tickets where nsident ~ '^\\\\d+$'";
-                    break;
-                
-                case 'mysql':
-                    $max = "select max(cast(nsident as UNSIGNED)) + 1 from tickets";
-                    break;
-                
-                default:
-                    $max = 'select max(cast(nsident as integer)) + 1 from tickets';   
-                    break;
-            }
-            
             
-             
-            list($this->issue->nsident) = MTrackDB::q($max)->fetchAll(PDO::FETCH_COLUMN, 0);
-            if ($this->issue->nsident === null) {
-              $this->issue->nsident = 1;
-            }
-          }
-
-          if (isset($_POST['action']) && !$this->preview) {
-            $act= explode('_', $_POST['action'] , 2);
-            //var_dump($act);exit;
-            switch ($act[0]) {
-              case 'leave':
-                break;
-              case 'reopen':
-                $this->issue->reOpen();
-                break;
-              case 'fixed':
-                $this->issue->resolution = 'fixed';
-                $this->issue->close();
-                $_POST['estimated'] = $this->issue->estimated;
-                break;
-                
-              
-              case 'accept':
-                // will be applied to the issue further down
-                $_POST['owner'] = MTrackAuth::whoami();
-                if ($this->issue->status == 'new') {
-                  $this->issue->status = 'open';
-                }
-                break;
-                
-                
-              case 'resolve':
-                //$this->issue->resolution = $_POST['resolution'];
-                $this->issue->resolution = $act[1];
-                $this->issue->close();
-                $_POST['estimated'] = $this->issue->estimated;
-                break;  
-                
-              case 'change':
-                $this->issue->status = $act[1];
-                break;
-            }
-          }
-
-          $fields = array(
-            'summary',
-            'description',
-            'classification',
-            'priority',
-            'severity',
-            'changelog',
-            'owner',
-            'cc',
-          );
-
-          $this->issue->applyPOSTData($_POST);
-
-         
-          
-          foreach ($fields as $fieldname) {
-            if (isset($_POST[$fieldname]) && strlen($_POST[$fieldname])) {
-              $this->issue->$fieldname = $_POST[$fieldname];
-            } else {
-              $this->issue->$fieldname = null;
-            }
-          }
-
-          $kw = $this->issue->getKeywords();
+        }
+        
+        
+        
+        
+        $this->issue->setFrom($_POST); 
+        
+        /*
+        $kw = $this->issue->getKeywords();
           $kill = array_values($kw);
           foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
             if (!strlen($w)) {
@@ -259,7 +185,8 @@ class MTrackWeb_TicketEdit extends MTrackWeb
           foreach ($kill as $w) {
             $this->issue->dissocKeyword($w);
           }
-
+     
+        
           $ms = $this->issue->getMilestones();
           $kill = $ms;
           if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
@@ -271,7 +198,7 @@ class MTrackWeb_TicketEdit extends MTrackWeb
           foreach ($kill as $mid) {
             $this->issue->dissocMilestone($mid);
           }
-
+        
           $ms = $this->issue->getComponents();
           $kill = $ms;
           if (isset($_POST['component']) && is_array($_POST['component'])) {
@@ -292,7 +219,80 @@ class MTrackWeb_TicketEdit extends MTrackWeb
             empty($_POST['spent']) ? 0 : $_POST['spent'], 
             empty($_POST['estimate']) ? 0 : $_POST['estimate']
         );
-
+        */
+        
+        
+        
+        
+        $this->issue->project_id = $this->currentProject();
+        
+        
+        $CS = DB_DataObject::factory('mtrack_change');
+        
+       
+        
+        if ($this->issue->id) {
+            if (0 === $this->issue->update($old)) {
+                $this->jerr("nothing changed?");
+            
+            }
+        } else {
+            $this->issue->insert();
+            $old = false;
+        }
+        
+        $CS->begin($this->issue, $old ? 'Changed' : 'Created');
+        if (!$old) {
+            $this->issue->created = $CS->id;
+            $this->issue->update();
+        }
+        $CS->add($this->issue, $old);
+        
+        // issue ticket... - add watches etc... 
+        
+        
+        
+         
+        
+        $notify_query = "
+                (ontable='mtrack_ticket' and onid = {$this->issue->id})
+                 OR
+                 (ontable='Projects' and onid = {$this->issue->project_id})
+                 ";
+         $w = DB_DataObject::factory('core_watch');
+        $w->ensureNotify(  'mtrack_ticket' ,
+                            $this->issue->id,
+                            $this->authUser->id,
+                        $notify_query
+            );
+        
+        $w->ensureNotify(  'mtrack_ticket' ,
+                            $this->issue->id,
+                            $this->issue->owner_id,
+                        $notify_query
+            );
+        
+         $w->ensureNotify(  'mtrack_ticket' ,
+                            $this->issue->id,
+                            $this->issue->developer_id,
+                        $notify_query
+            );
+        //DB_DataObject::debugLevel(1);
+        // who to notify.. -- originall did not send to issuer..
+        // we should probably make this configurable..
+       
+        $w->notify( 'mtrack_ticket' , $this->issue->id,
+                   $notify_query 
+                   /*
+                 " ( $notify_query )
+                    AND
+                    (person_id != {$this->authUser->id})   "  */
+        );
+        
+        
+        /*
+        //$this->issue->save($CS);
+        
           if (!count($this->error)) {
             try {
               $this->issue->save($CS);
@@ -313,7 +313,8 @@ class MTrackWeb_TicketEdit extends MTrackWeb
               $this->error[] = $e->getMessage();
             }
         }
-
+        */
+        /*
         if (!count($this->error)) {
             if (!empty($_FILES['attachments'])) {
                 require_once 'MTrack/Attachment.php';
@@ -337,6 +338,8 @@ class MTrackWeb_TicketEdit extends MTrackWeb
           header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
           exit;
         }
+        */
+        $this->jok($this->issue->id);
     }
       
          
@@ -344,11 +347,10 @@ class MTrackWeb_TicketEdit extends MTrackWeb
     {
         require_once 'HTML/Template/Flexy/Element.php';
         require_once 'HTML/Template/Flexy/Factory.php';
-        $this->elements = array();
         
         
         
-        foreach(array( 'classification', 'priority', 'severity', 'resolution' ) as $c)  {
+        foreach(array( 'classification', 'priority', 'severity' ) as $c)  {
             $d = DB_DataObject::factory('core_enum');
             $d->etype = $c;
             $d->orderBy('seqid ASC, name ASC');
@@ -357,18 +359,46 @@ class MTrackWeb_TicketEdit extends MTrackWeb
                 
             }
             $ar = $d->fetchAll('id','name');
-            if (!$this->id) {
-                $ar = array_merge( array(0 => '-- Select --'),$ar);
-            }
-            $this->elements[$c] = new HTML_Template_Flexy_Element('select');
             
+            // mid point..
+            $ak = array_keys($ar);
+            $mid = floor(count($ak) / 2 );
+            $def = $ak[$mid];
+            
+            //var_dump($ar[$def]);
             
+            $this->elements[$c.'_id'] = new HTML_Template_Flexy_Element('select');
+            $this->elements[$c.'_id']->setOptions($ar);
+            if (!$this->id) {
+                $this->elements[$c.'_id']->setValue($def);
+            }
             
-            $this->elements[$c]->setOptions($ar);
             
         }
+       // DB_DAtaObject::DebugLevel(1);
+        
+        $pd = DB_DataObject::factory('ProjectDirectory');
+        $pd->project_id = $this->currentProject();
+        $pd->whereAdd("ProjectDirectory.role != ''");
+        $pd->joinAdd(DB_DataObject::factory('core_person'), 'LEFT');
+        $pd->selectAdd();
+        $pd->selectAdd("distinct(Person.id) as id ,  CONCAT(Person.name , '<', Person.email , '>') as name");
+        $pd->groupBy('Person.id, Person.name');
+        $pd->orderBy('Person.name');
+        $users = $pd->fetchAll('id', 'name');
+        $users = array('' => '--select--') + $users;
+        
+        //$users = array();
+         
+        $this->elements['owner_id'] = new HTML_Template_Flexy_Element('select');
+        $this->elements['owner_id']->setOptions($users);
         
-        if (false && $this->currentProject()) {
+        $this->elements['developer_id'] = new HTML_Template_Flexy_Element('select');
+        $this->elements['developer_id']->setOptions($users);
+        
+          
+        if ($this->currentProject()) {
+            /*
             $d = DB_DataObject::factory('mtrack_project_component');
             $d->project_id = $this->currentProject();
             $d->orderBy('name');
@@ -377,29 +407,29 @@ class MTrackWeb_TicketEdit extends MTrackWeb
             $this->elements['component[]']->setOptions($d->fetchAll('id', 'name'));
             $ar = $this->issue->components();
             $this->elements['component[]']->setValue(array_keys($ar));
-        
-        
+            */
+            //DB_DataObject::debugLevel(1);
             $d = DB_DataObject::factory('mtrack_milestone');
             $d->project_id = $this->currentProject();
             $d->orderBy('(case when duedate is null then 1 else 0 end), duedate, name');
             $d->whereAdd('completed != 1');
             $d->whereAdd('deleted != 1');
-            $this->elements['milestone'] = new HTML_Template_Flexy_Element('select');
-            $this->elements['milestone']->setOptions($d->fetchAll('id', 'name'));
-            $ar = $this->issue->milestone();
-            $this->elements['milestone']->setValue(array_keys($ar));
+            $this->elements['milestone_id'] = new HTML_Template_Flexy_Element('select');
+            $this->elements['milestone_id']->setOptions(array(''=>'--select--') + $d->fetchAll('id', 'name'));
+            //$ar = $this->issue->milestone();
+            //$this->elements['milestone_id']->setValue(array_keys($ar));
         }
         
         // FIX ME - need to determine who the owner is..
         // for a new issue it's the person who created it.
         // later on it's an assignement???
         
-        $users = array();
-         
-        $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
-        $this->elements['owner']->setOptions($users);
-        
+       
         
+        if ($this->id) {
+            $this->elements = HTML_Template_Flexy_Factory::fromArray($this->issue->toArray(), $this->elements);
+        }
+
         
         // keywords -- in toArray...
         // milestone