php8
[web.mtrack] / MTrackWeb / Ticket.php
index d67c71b..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;
@@ -133,143 +133,127 @@ class MTrackWeb_Ticket extends MTrackWeb
                 
               case 'resolution':
                 //$this->issue->resolution = $_POST['resolution'];
-                $this->issue->resolution = $act[1];
-                $this->issue->close();
-                $_POST['estimated'] = $this->issue->estimated;
+                $this->issue->resolution_id = $act[1];
+                $d = DB_DataObject::factory('core_enum');
+                $d->etype = 'ticketstate';
+                $d->name = 'closed'; // assigned?
+                $d->find(true);
+                $this->issue->status = $d->id;
+                //$this->issue->close();
+                //$_POST['estimated'] = $this->issue->estimated;
                 break;  
                 
             case 'change':
                 $this->issue->status = $act[1];
                 break;
+            
+            // default leave - stays tehe same..
+        }
+        
+        // things that we should not allow..
+        foreach(array(
+            'estimated', 'effort', 'owner_id',
+        ) as $k) {
+            if (isset($_POST[$k])) {
+                $this->issue->setFrom(array($k => $_POST[$k]));
             }
-          }
-
-          $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();
-          $kill = array_values($kw);
-          foreach (preg_split('/[ \t,]+/', $_POST['keywords']) as $w) {
-            if (!strlen($w)) {
-              continue;
-            }
-            $x = array_search($w, $kw);
-            if ($x === false) {
-              $k = MTrackKeyword::loadByWord($w);
-              if ($k === null) {
-                $k = new MTrackKeyword;
-                $k->keyword = $w;
-                $k->save($CS);
-              }
-              $this->issue->assocKeyword($k);
-            } else {
-              $w = array_search($w, $kill);
-              if ($w !== false) {
-                unset($kill[$w]);
-              }
-            }
-          }
-          foreach ($kill as $w) {
-            $this->issue->dissocKeyword($w);
-          }
-
-          $ms = $this->issue->getMilestones();
-          $kill = $ms;
-          if (isset($_POST['milestone']) && is_array($_POST['milestone'])) {
-            foreach ($_POST['milestone'] as $mid) {
-              $this->issue->assocMilestone($mid);
-              unset($kill[$mid]);
-            }
-          }
-          foreach ($kill as $mid) {
-            $this->issue->dissocMilestone($mid);
-          }
-
-          $ms = $this->issue->getComponents();
-          $kill = $ms;
-          if (isset($_POST['component']) && is_array($_POST['component'])) {
-            foreach ($_POST['component'] as $mid) {
-              $this->issue->assocComponent($mid);
-              unset($kill[$mid]);
-            }
-          }
-          foreach ($kill as $mid) {
-            $this->issue->dissocComponent($mid);
-          }
-          
-            if (!empty($_POST['comment'])) {
-               $this->issue->addComment($_POST['comment']);
-            }
-          
-          $this->issue->addEffort(
-            empty($_POST['spent']) ? 0 : $_POST['spent'], 
-            empty($_POST['estimate']) ? 0 : $_POST['estimate']
-        );
-
-          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);
-                }
+        //  $this->issue->addEffort(
+        //    empty($_POST['spent']) ? 0 : $_POST['spent'], 
+        //    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']);
+            
+        
+        //if (!empty($_POST['comment'])) {
+            // create a comment CS..
+            
+        //}
+        $CS->add($this->issue, $old);
+        $this->issue->updated = $CS->id;
+        $this->issue->update($old);
+        
+        
+        
+        
+        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");
+        
     }
       
          
@@ -284,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);