sync
[web.mtrack] / MTrackWeb / Cron / Notify.php
index 714f847..313352c 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 require_once 'MTrackWeb.php';
 
+// is this used currently - it's supposed to be for sending out notificatios on changes?
+
 class MTrackWeb_Cron_Notify extends MTrackWeb
 {
     
@@ -17,7 +19,7 @@ class MTrackWeb_Cron_Notify extends MTrackWeb
     
     function get()    
     {
-        DB_DataObject::debugLevel(1);
+        //DB_DataObject::debugLevel(1);
         date_default_timezone_set('UTC');
         
         // what's the baserul..
@@ -31,42 +33,53 @@ class MTrackWeb_Cron_Notify extends MTrackWeb
         $e->action = 'MTRACK.NOTIFY';
         $e->selectAdd();
         $e->selectAdd('MAX(event_when) as event_when');
-        
-        if (!$e->find(true)) {
-            $e->event_when = date('Y-m-d H:i:s', 0); // should be a long time ago..
+        $e->find(true);
+        if (empty($e->event_when)) {
+            $e->event_when = date('Y-m-d H:i:s', strtotime("NOW - 2 days")); // should be a long time ago..
         }
          
         
         
-        stream_filter_register("mtrackcanonical", 'CanonicalLineEndingFilter');
-        stream_filter_register("mtrackunix", 'UnixLineEndingFilter');
+        //stream_filter_register("mtrackcanonical", 'CanonicalLineEndingFilter');
+        //stream_filter_register("mtrackunix", 'UnixLineEndingFilter');
         
         $w = DB_DataObject::factory('core_watch');
+        //echo "gather watched";
+       // DB_DataObject::debugLevel(1);
         // returns a list of user with the objects they are watching..
         $watches = $w->watched('email');
-        
+        //echo "\nDONE WATCHED\n";
         $CS = DB_DataObject::factory('mtrack_change');
         
         $messages = array();
         $cache = array();
         foreach($watches as $uid => $ar) {
+            //echo "gather changes";
+            //print_r($uid); print_r($ar);
             // we can now query mtrack_change for anything that occured in that list..
             $messages = $CS->gatherChanges($e->event_when, $uid, $ar);
-            print_R($messages);exit;
+            //print_R($messages);exit;
             // we should cache the emails. = as multiple people might be getting the same email.
             
             // should be like a list of tickets
             foreach ($messages as $m=>$ar) {
+                
                 $lr = explode(':', $m);
                 $o = DB_DataObject::Factory($lr[0]);
+                $o->autoJoin();
                 $o->get($lr[1]);
                 if (!isset($cache[implode(',', $ar)])) {
                     $cs = clone($CS);
-                    $cs->whereAddIn('id', $ar, 'int');
+                    $cs->whereAddIn('mtrack_change.id', $ar, 'int');
                     $cs->autoJoin();
                     $cache[implode(',', $ar)] = $cs->fetchAll();
                 }
-                
+                $u  = DB_DataObject::factory('core_person');
+                $u->get($uid);
+                $method =  'notify_'. $lr[0] ;
+                if (method_exists($this,$method)) {
+                    $this->$method($o,  $cache[implode(',', $ar)], $u);
+                }
             }
             
             
@@ -74,8 +87,9 @@ class MTrackWeb_Cron_Notify extends MTrackWeb
             
             
             
+            
         }
-        print_R($messages);
+       // print_R($messages);
         die("done");
         
         
@@ -101,9 +115,42 @@ class MTrackWeb_Cron_Notify extends MTrackWeb
      *   - to user...
      *   
      */
-    function calcAddress($items, $user) {
+    function calcAddress($ticket, $changes, $user)
+    {
+        $ff = HTML_FlexyFramework::get();
+        
+        
+        $changers = array();
+        foreach($changes as $c) {
+            if ($change->person_id == $user->id) {
+                continue;
+            }
+            if (in_array($change->person_id_name, $changers)) {
+                continue;
+            }
+            $changers[] = $change->person_id_name;
+            
+        }
+        $fromname = '"'. addslashes($project->toEventString()). '"';
+        if (count($changers) == 1) {
+            $fromname = '"'. addslashes($changers[0]).  '"';
+        }
+        
+        $p = $ticket->project();
+        $headers['To'] = '"'. addslashes($user->name). '" <' . $user->email .'>';
+        $headers['From'] = '"'. addslashes($project->toEventString()). '" '. 
+                $ff->MTrackWeb['email_address']; // could be the user who made the change...
         
+        //if (count($from) > 1) {
+        //    $rep = array();
+        //    array_shift($from);
+         //       foreach ($from as $email) {
+         // $rep[] = make_email($email[0], $email[1]);
+        // }
         
+        $headers['Reply-To'] =  $ff->MTrackWeb['email_address'];
+        //}
+        return $headers;
     }
     
     /**
@@ -118,10 +165,16 @@ class MTrackWeb_Cron_Notify extends MTrackWeb
     function notify_mtrack_ticket($ticket, $changes, $user)
     {
         //global $MAX_DIFF;
-      
+        $ff = HTML_FlexyFramework::get();
+     
+        
+        
+        
+     
+     
         // from is always the system.. ??
         // unless all the changers are the same..
-        $headers = $this->calcAddress($item, $user);
+        $headers = $this->calcAddress($ticket, $changes, $user);
         
           
         //$headers['To'] = '"'. addslashes($user->name). '" <' . $user->email .'>';
@@ -169,14 +222,15 @@ class MTrackWeb_Cron_Notify extends MTrackWeb
         // }
     
         $headers['Subject'] = sprintf("%s#%s %s (%s %s)",
-          $subj, $ticket->id, $ticket->summary, $ticket->status_name, $ticket->classification_id_name);
+          $subj, $ticket->id, $ticket->summary,
+          $ticket->status_name, $ticket->classification_id_name);
     
-        $owner = strlen($ticket->owner) ? $ticket->owner_id_name : 'nobody';
+        $owner =  $ticket->owner_id ? $ticket->owner_id_name : 'nobody';
     
         $body = 
-            sprintf("%s%s/Ticket.php/%s\n\n", $this->HTTP_HOST, $this->baseURL, $ticket->ident) .
+            sprintf("%s/Ticket.php/%s\n\n", $ff->MTrackWeb['url'], $ticket->id) .
             sprintf("#%s: %s (%s %s)\n", $ticket->id, $ticket->summary, $ticket->status_name, $ticket->classification_id_name) .
-            sprintf("Responsible: %s (%s / %s)\n", $owner, $ticket->priority, $ticket->severity) .
+            sprintf("Responsible: %s (%s / %s)\n", $owner, $ticket->priority_id_name, $ticket->severity_id_name) .
     
             //sprintf("Milestone: %s\n", join(', ', $T->getMilestones()));
             //sprintf("Component: %s\n", join(', ', $T->getComponents()));
@@ -238,28 +292,33 @@ class MTrackWeb_Cron_Notify extends MTrackWeb
                     //case 'commit?'  
                       
                     default:
-                        if (!strlen($audit->oldvalue)) {
-                            $body .= "Set {$audit->field()} to: {$audit->value}\n";
+                        $oldvalue = $audit->oldvalue($this);
+                        $value = $audit->oldvalue($this);
+                        if (!strlen($oldvalue)) {
+                            $body .= "Set {$audit->field()} to: {$value}\n";
                             continue;
                         }
-                        if (!strlen($audit->value)) {
-                            $body .= "Removed {$audit->field()} - was: {$audit->oldvalue}\n";
+                        
+                        if (!strlen($value)) {
+                            $body .= "Removed {$audit->field()} - was: {$oldvalue}\n";
                             continue;
                         }
-                        $body .= "Changed {$audit->field()} from :{$audit->oldvalue} -> {$audit->value}\n";
+                        $body .= "Changed {$audit->field()} from :{$oldvalue} -> {$value}\n";
                         continue;  
                 }
             }
         }
-        $body .= sprintf("%s%s/Ticket.php/%s\n\n", $this->HTTP_HOST, $this->baseURL, $ticket->ident);
-           
+        
+        $body .= sprintf("%s/Ticket.php/%s\n\n", $ff->MTrackWeb['url'], $ticket->id);
+        
+        echo $body;   
     
         //  send_mail($udata['email'], $plain);
     }
     
 }
 
-
+/*
 class CanonicalLineEndingFilter extends php_user_filter {
     function filter($in, $out, &$consumed, $closing)
     {
@@ -281,4 +340,6 @@ class CanonicalLineEndingFilter extends php_user_filter {
       }
       return PSFS_PASS_ON;
     }
-    }
\ No newline at end of file
+}
+*/
+    
\ No newline at end of file