Report/SendEventErrors.php
[Pman.Admin] / Report / SendEventErrors.php
1 <?php
2
3 require_once 'Pman/Roo.php';
4
5 class Pman_Admin_Report_SendEventErrors extends Pman_Roo
6 {
7     static $cli_desc = "Send event errors occured in the last 24 hours";
8     
9     static $cli_opts = array(
10         'group' => array(
11             'desc' => 'group to send to',
12             'short' => 't',
13             'default' => '',
14             'min' => 1,
15             'max' => 1,
16         ),
17         'exclude' => array(
18             'desc' => 'list of actions to exclude from report',
19             'short' => 'e',
20             'default' => '',
21             'min' => 1,
22             'max' => 1,
23         ),
24     );
25     
26     function getAuth()
27     {
28         $ff = HTML_FlexyFramework::get();
29         
30         if (!$ff->cli) {
31             die("cli only");
32         }
33         
34         return true;
35     }
36     
37     function get($args, $opts)
38     {
39         $this->opts = $opts;
40         
41         $this->transObj = DB_DataObject::Factory('core_enum');
42         
43         $this->transObj->query('BEGIN');
44         
45         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
46         
47         if(empty($this->opts['group'])){
48             $this->jerr('Missing group - try add [-t {group name}]');
49         }
50         
51         $rcpts = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'email');
52         
53         if(empty($rcpts)){
54             $this->jerr("{$this->opts['group']} does not has any memeber");
55         }
56         
57         $events = DB_DataObject::factory('Events');
58         $events->selectAdd();
59         $events->selectAdd("
60             DISTINCT(Events.action) AS action,
61             COUNT(Events.id) AS total
62         ");
63         
64         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
65         
66         if(!empty($this->opts['exclude'])){
67             $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
68             
69             if(!empty($exclude)){
70                 $events->whereAddIn('!Events.action', $exclude, 'string');
71             }
72         }
73         
74         $events->groupBy('Events.action');
75         $events->orderBy('Events.action ASC');
76         
77         $totals = $events->fetchAll('action', 'total');
78         
79         if(empty($totals)){
80             $this->jerr('Nothing to be sent');
81         }
82         
83         $subject = array();
84         
85         if(!empty($this->opts['subject'])){
86             $subject[] = $this->opts['subject'];
87         }
88         
89         foreach ($totals as $k => $v){
90             
91         }
92         
93         $this->jok("Done");
94         
95     }
96     
97     
98 }