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         'subject' => array(
25             'desc' => 'email subject',
26             'short' => 's',
27             'default' => '',
28             'min' => 1,
29             'max' => 1,
30         ),
31     );
32     
33     function getAuth()
34     {
35         $ff = HTML_FlexyFramework::get();
36         
37         if (!$ff->cli) {
38             die("cli only");
39         }
40         
41         return true;
42     }
43     
44     function get($args, $opts)
45     {
46         $this->opts = $opts;
47         
48         $this->transObj = DB_DataObject::Factory('core_enum');
49         
50         $this->transObj->query('BEGIN');
51         
52         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
53         
54         if(empty($this->opts['group'])){
55             $this->jerr('Missing group - try add [-t {group name}]');
56         }
57         
58         $rcpts = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'email');
59         
60         if(empty($rcpts)){
61             $this->jerr("{$this->opts['group']} does not has any memeber");
62         }
63         
64         $events = DB_DataObject::factory('Events');
65         $events->selectAdd();
66         $events->selectAdd("
67             DISTINCT(Events.action) AS action,
68             COUNT(Events.id) AS total
69         ");
70         
71         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
72         
73         if(!empty($this->opts['exclude'])){
74             $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
75             
76             if(!empty($exclude)){
77                 $events->whereAddIn('!Events.action', $exclude, 'string');
78             }
79         }
80         
81         $events->groupBy('Events.action');
82         $events->orderBy('Events.action ASC');
83         
84         $summary = $events->fetchAll('action', 'total');
85         
86         if(empty($summary)){
87             $this->jerr('Nothing to be sent');
88         }
89         
90         $subject = array();
91         
92         foreach ($summary as $k => $v){
93             $subject[] = "{$v} {$k}";
94         }
95         
96         $subject = implode(', ', $subject);
97         
98         if(!empty($this->opts['subject'])){
99             $subject = "{$this->opts['subject']} $subject";
100         }
101         
102         $events = DB_DataObject::factory('Events');
103         $events->autoJoin();
104         
105         $events->selectAdd();
106         $events->selectAdd("
107             Events.id AS id,
108             Events.event_when AS event_when,
109             Events.action AS action,
110             Events.remarks AS remarks,
111             CASE WHEN Events.person_id != 0 THEN
112                 join_person_id_id.email
113             WHEN Events.hydra_person_id != 0 THEN
114                 join_hydra_person_id_id.email
115             ELSE
116                 ''
117             END AS email
118         ");
119         
120         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
121         
122         $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
123         
124         if(!empty($exclude)){
125             $events->whereAddIn('!Events.action', $exclude, 'string');
126         }
127         
128         if(!$events->count()){
129             $this->jerr('Nothing to be sent');
130         }
131         
132         $list = $events->fetchAll();
133         
134         $content = array(
135             'template'      => 'EVENT_ERRORS_REPORT',
136             'rcpts'         => $rcpts,
137             'list'          => $list,
138             'subject'       => $subject
139         );
140
141         $sent = DB_DataObject::factory('core_email')->send($content);
142         
143         if(!is_object($sent)){
144             $this->jok("Done");
145         }
146         
147         $this->jerr($sent);
148         
149     }
150     
151     
152 }