sync
[web.mtrack] / MTrackWeb / Ticket.php
index 736ccfe..7170d20 100644 (file)
@@ -42,8 +42,8 @@ class MTrackWeb_Ticket extends MTrackWeb
    function loadIssue( )
    {
      
-        // -- load issue..
-
+        // -- load issue.. we should share this code with ticketedit really..
+        //DB_DataObject::debugLevel(1);
         $this->issue = DB_DataObject::factory('mtrack_ticket');
         $this->issue->autoJoin();
         if (!$this->id ||  !$this->issue->get($this->id)) {
@@ -83,7 +83,7 @@ class MTrackWeb_Ticket extends MTrackWeb
             $this->showEditBar = true;
         }
          
+        $this->issue->images = $this->issue->images();
         
       
     }
@@ -93,7 +93,7 @@ class MTrackWeb_Ticket extends MTrackWeb
         
         $this->id = empty($_POST['id']) ? 0 : $_POST['id'];
         $this->loadIssue();
-       
+        $old = clone($this->issue);
          
         //  $this->preview = isset($_POST['preview']) ? true : false;
         if (!$this->hasPerm('MTrack.Issue','E') ) {
@@ -120,10 +120,10 @@ class MTrackWeb_Ticket extends MTrackWeb
               
             case 'accept':
                 // will be applied to the issue further down
-                $this->issue->owner = $this->authUser->id;
+                $this->issue->owner_id = $this->authUser->id;
                 if ($this->issue->status_name == 'new') {
                     $d = DB_DataObject::factory('core_enum');
-                    $d->etype = 'status';
+                    $d->etype = 'ticketstate';
                     $d->name = 'open'; // assigned?
                     $d->find(true); 
                     $this->issue->status = $d->id;
@@ -135,7 +135,7 @@ class MTrackWeb_Ticket extends MTrackWeb
                 //$this->issue->resolution = $_POST['resolution'];
                 $this->issue->resolution_id = $act[1];
                 $d = DB_DataObject::factory('core_enum');
-                $d->etype = 'status';
+                $d->etype = 'ticketstate';
                 $d->name = 'closed'; // assigned?
                 $d->find(true);
                 $this->issue->status = $d->id;
@@ -152,7 +152,7 @@ class MTrackWeb_Ticket extends MTrackWeb
         
         // things that we should not allow..
         foreach(array(
-            'estimated', 'effort', 'owner',
+            'estimated', 'effort', 'owner_id',
         ) as $k) {
             if (isset($_POST[$k])) {
                 $this->issue->setFrom(array($k => $_POST[$k]));
@@ -164,6 +164,10 @@ class MTrackWeb_Ticket extends MTrackWeb
         //    empty($_POST['estimate']) ? 0 : $_POST['estimate']
         //);
         
+        // we should really use begin/commit to determin if something has changed..
+        // and produce an error..
+        
+        
         $CS = DB_DataObject::factory('mtrack_change');
         $CS->begin($this->issue, empty($_POST['comment']) ? '' : $_POST['comment']);
             
@@ -172,52 +176,84 @@ class MTrackWeb_Ticket extends MTrackWeb
             // create a comment CS..
             
         //}
+        $CS->add($this->issue, $old);
+        $this->issue->updated = $CS->id;
+        $this->issue->update($old);
         
-
-          if (!count($this->error)) {
-            try {
-              $this->issue->save($CS);
-              
-              // make sure everyone is watching it!!!!
-                if($this->issue->owner && $this->issue->tid) {
-                  // make sure owner is tracking it...
-                    MTrackWatch::watch_object('ticket', $this->issue->tid,  $this->issue->owner);
-                }
+        
+        
+        
+        if (!empty($_FILES['attachments'])) {
+            foreach ($_FILES['attachments']['name'] as $fileid => $name) {
+                $i = DB_DataObject::factory('Images');
+                $i->object($this->issue);
+                $i->ontable = $this->issue->tableName();
+                $i->onid = $this->issue->id;
+                $i->created_by = $this->authUser->id;
+                $i->mimetype = $_FILES['attachments']["type"][$fileid];
+                $i->createFrom($_FILES['attachments']['tmp_name'][$fileid],$_FILES['attachments']['name'][$fileid]);
                 
-                if ($this->id == 'new') {
-                    MTrackWatch::watch_object('ticket', $this->issue->tid,  MTrackAuth::whoami());
-                }
-              
-              
-              $CS->setObject("ticket:" . $this->issue->tid);
-            } catch (Exception $e) {
-              $this->error[] = $e->getMessage();
-            }
-        }
-
-        if (!count($this->error)) {
-            if (!empty($_FILES['attachments'])) {
-                require_once 'MTrack/Attachment.php';
-                foreach ($_FILES['attachments']['name'] as $fileid => $name) {
-                      
-                    MTrackAttachment::add("ticket:{$this->issue->tid}",
-                        $_FILES['attachments']['tmp_name'][$fileid],
-                        $_FILES['attachments']['name'][$fileid],
-                        $CS
-                    );
-                }
+                //    MTrackAttachment::add("ticket:{$this->issue->tid}",
+                //        $_FILES['attachments']['tmp_name'][$fileid],
+                //        $_FILES['attachments']['name'][$fileid],
+                //        $CS
+                //    );
+                //}
             }
         }
+        /*
         if (!count($this->error) && $this->id != 'new') {
             require_once 'MTrack/Attachment.php';
             MTrackAttachment::process_delete("ticket:{$this->issue->tid}", $CS);
-        }
-
-        if (isset($_POST['apply']) && !count($this->error)) {
-          $CS->commit();
-          header("Location: {$this->baseURL}/Ticket/{$this->issue->nsident}");
-          exit;
-        }
+        } 
+        */
+        
+        
+        
+        
+        
+        $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..
+        // 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})   "   */
+        );
+        
+        
+        
+        // if the owner has changed.. then we should add them to the watch list...
+        // MTrackWatch::watch_object('ticket', $this->issue->tid,  $this->issue->owner);
+        
+        $this->jok("OK");
+        
     }
       
          
@@ -232,11 +268,22 @@ class MTrackWeb_Ticket extends MTrackWeb
         // 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???
+      //  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();
+        //$users = array();
          
-        $this->elements['owner'] = new HTML_Template_Flexy_Element('select');
-        $this->elements['owner']->setOptions($users);
+        $this->elements['owner_id'] = new HTML_Template_Flexy_Element('select');
+        $this->elements['owner_id']->setOptions($users);