sync
[Pman.Admin] / Report / SendEventErrors.php
index ec67e17..d2aa0f8 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-
 require_once 'Pman/Roo.php';
 
 class Pman_Admin_Report_SendEventErrors extends Pman_Roo
@@ -21,6 +21,20 @@ class Pman_Admin_Report_SendEventErrors extends Pman_Roo
             'min' => 1,
             'max' => 1,
         ),
+        'only' => array(
+            'desc' => 'list of actions only to report',
+            'short' => 'O',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+        ),
+        'uid' => array(
+            'desc' => 'Unique identifier - eg. FAILREPORT',
+            'short' => 'U',
+            'default' => 'STD',
+            'min' => 1,
+            'max' => 1,
+        ),
         'subject' => array(
             'desc' => 'email subject',
             'short' => 's',
@@ -37,11 +51,26 @@ class Pman_Admin_Report_SendEventErrors extends Pman_Roo
         ),
         'host' => array(
             'desc' => 'mail host to use',
-            'short' => 'h',
+            'short' => 'o',
             'default' => '',
             'min' => 1,
             'max' => 1,
-        )
+        ),
+     
+        'list' => array(
+            'desc' => 'list the current actions in the database',
+            'short' => 'L',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+        ),
+        'debug' => array(
+            'desc' => 'Turn on database debugging',
+            'short' => 'd',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+        ), 
     );
     
     function getAuth()
@@ -55,27 +84,43 @@ class Pman_Admin_Report_SendEventErrors extends Pman_Roo
         return true;
     }
     
-    function get($args, $opts)
+    function get($args,   $opts = Array())
     {
         $this->opts = $opts;
-        print_R($this->opts);exit;
+        
+        if (!empty($this->opts['debug'])) {
+            DB_DataObject::debugLevel(1);
+          
+        }
+        if(!empty($this->opts['list'])){
+            $this->listTypes();
+        }
+        
         if(empty($this->opts['group'])){
             $this->jerr('Missing group - try add [-t {group name}]');
         }
         
-        $rcpts = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'email');
-        
-        if(empty($rcpts)){
-            $this->jerr("{$this->opts['group']} does not has any memeber");
+        $min = 0;
+         
+        if ($this->opts['uid'] != 'STD') {
+            $events = DB_DataObject::factory('Events');
+            $events->action = 'ERROR-REPORT-' . $this->opts['uid'];
+            $events->orderBy('id DESC');
+            $events->limit(1);        
+            if ($events->find(true)) {
+                $min = $events->id;
+            }
         }
         
+        
         $events = DB_DataObject::factory('Events');
         $events->selectAdd();
         $events->selectAdd("
             DISTINCT(Events.action) AS action,
             COUNT(Events.id) AS total
         ");
-        
+        $events->whereAdd('Events.id > '. $min);
+        $events->whereAdd("Events.action NOT LIKE 'ERROR-REPORT-%'");
         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
         
         if(!empty($this->opts['exclude'])){
@@ -85,6 +130,14 @@ class Pman_Admin_Report_SendEventErrors extends Pman_Roo
                 $events->whereAddIn('!Events.action', $exclude, 'string');
             }
         }
+        if(!empty($this->opts['only'])){
+            $only= array_unique(array_filter(array_map('trim', explode(',', $this->opts['only']))));
+            
+            if(!empty($only)){
+                $events->whereAddIn('Events.action', $only, 'string');
+            }
+        }
+        
         
         $events->groupBy('Events.action');
         $events->orderBy('Events.action ASC');
@@ -92,7 +145,7 @@ class Pman_Admin_Report_SendEventErrors extends Pman_Roo
         $summary = $events->fetchAll('action', 'total');
         
         if(empty($summary)){
-            $this->jerr('Nothing to be sent');
+            $this->jerror('ERROR-REPORT-' . $this->opts['uid'], 'Nothing to be sent');
         }
         
         $subject = array();
@@ -107,6 +160,7 @@ class Pman_Admin_Report_SendEventErrors extends Pman_Roo
             $subject = "{$this->opts['subject']} $subject";
         }
         
+        
         $events = DB_DataObject::factory('Events');
         $events->autoJoin();
         
@@ -115,16 +169,12 @@ class Pman_Admin_Report_SendEventErrors extends Pman_Roo
             Events.id AS id,
             Events.event_when AS event_when,
             Events.action AS action,
-            Events.remarks AS remarks,
-            CASE WHEN Events.person_id != 0 THEN
-                join_person_id_id.email
-            WHEN Events.hydra_person_id != 0 THEN
-                join_hydra_person_id_id.email
-            ELSE
-                ''
-            END AS email
+            Events.remarks AS remarks
+            
         ");
-        
+        $events->selectAddPersonEmail();
+        $events->whereAdd("Events.action NOT LIKE 'ERROR-REPORT-%'");
+        $events->whereAdd('Events.id > '. $min);
         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
         
         $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
@@ -132,24 +182,36 @@ class Pman_Admin_Report_SendEventErrors extends Pman_Roo
         if(!empty($exclude)){
             $events->whereAddIn('!Events.action', $exclude, 'string');
         }
+        if(!empty($only)){
+            $events->whereAddIn('Events.action', $only, 'string');
+        }
         
-        if(!$events->count()){
+        if(!$events->count()){  // this is the second count we are doing...
             $this->jerr('Nothing to be sent');
         }
         
+        
+        $this->addEvent('ERROR-REPORT-' . $this->opts['uid'], false, $subject);
+
+        
         $errors = $events->fetchAll();
         
+        if(!empty($this->opts['host'])){
+            // reset the mail settings..
+            HTML_FlexyFramework::get()->Mail = array(
+                                        'host' => $this->opts['host']
+            );
+        }
+        
         if(!empty($this->opts['helo'])){
             HTML_FlexyFramework::get()->Mail['helo'] = $this->opts['helo'];
         }
         
-        if(!empty($this->opts['host'])){
-            HTML_FlexyFramework::get()->Mail['host'] = $this->opts['host'];
-        }
+        
         
         $content = array(
-            'template'      => 'EVENT_ERRORS_REPORT',
-            'rcpts'         => $rcpts,
+            'template'      => 'FILE_REPORTING_EMAIL',
+            'rcpts_group'   => $this->opts['group'],
             'errors'        => $errors,
             'subject'       => $subject
         );
@@ -164,5 +226,25 @@ class Pman_Admin_Report_SendEventErrors extends Pman_Roo
         
     }
     
+    function listTypes()
+    {
+        $events = DB_DataObject::factory('Events');
+        $events->selectAdd();
+        $events->selectAdd("
+            DISTINCT(Events.action) AS action
+        ");
+        $events->whereAdd("action != ''");
+        $ar = $events->fetchAll('action');
+        echo implode(",", $ar);
+        echo "\n";
+        exit;
+        
+    }
+    
+    static function test_EVENT_ERRORS_REPORT($pg, $to)
+    {
+        $this->jerr("Not implemented");
+    }
+    
     
 }
\ No newline at end of file