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         'only' => array(
25             'desc' => 'list of actions only to report',
26             'short' => 'O',
27             'default' => '',
28             'min' => 1,
29             'max' => 1,
30         ),
31         'uid' => array(
32             'desc' => 'Unique identifier - eg. FAILREPORT',
33             'short' => 'U',
34             'default' => 'STD',
35             'min' => 1,
36             'max' => 1,
37         ),
38         'subject' => array(
39             'desc' => 'email subject',
40             'short' => 's',
41             'default' => '',
42             'min' => 1,
43             'max' => 1,
44         ),
45         'helo' => array(
46             'desc' => 'mail helo to use',
47             'short' => 'l',
48             'default' => '',
49             'min' => 1,
50             'max' => 1,
51         ),
52         'host' => array(
53             'desc' => 'mail host to use',
54             'short' => 'o',
55             'default' => '',
56             'min' => 1,
57             'max' => 1,
58         ),
59      
60         'list' => array(
61             'desc' => 'list the current actions in the database',
62             'short' => 'L',
63             'default' => '',
64             'min' => 1,
65             'max' => 1,
66         ),
67         'debug' => array(
68             'desc' => 'Turn on database debugging',
69             'short' => 'd',
70             'default' => '',
71             'min' => 1,
72             'max' => 1,
73         ), 
74     );
75     
76     function getAuth()
77     {
78         $ff = HTML_FlexyFramework::get();
79         
80         if (!$ff->cli) {
81             die("cli only");
82         }
83         
84         return true;
85     }
86     
87     function get($args, $opts)
88     {
89         $this->opts = $opts;
90         
91         if (!empty($this->opts['debug'])) {
92             DB_DataObject::debugLevel(1);
93           
94         }
95         if(!empty($this->opts['list'])){
96             $this->listTypes();
97         }
98         
99         if(empty($this->opts['group'])){
100             $this->jerr('Missing group - try add [-t {group name}]');
101         }
102         
103         $rcpts = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'email');
104         
105         
106         if(empty($rcpts)){
107             $this->jerr("{$this->opts['group']} does not has any memeber");
108         }
109         // see the last date of notification to these users...
110         $rcpt_ids = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'id');
111         
112         $min = 0;
113          
114         if ($this->opts['uid'] != 'STD') {
115             $events = DB_DataObject::factory('Events');
116             $events->action = 'ERROR-REPORT-' . $this->opts['uid'];
117             $events->orderBy('id DESC');
118             $events->limit(1);        
119             if ($events->find(true)) {
120                 $min = $events->id;
121             }
122         }
123         
124         
125         $events = DB_DataObject::factory('Events');
126         $events->selectAdd();
127         $events->selectAdd("
128             DISTINCT(Events.action) AS action,
129             COUNT(Events.id) AS total
130         ");
131         $events->whereAdd('Eventsid > '. $min);
132         
133         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
134         
135         if(!empty($this->opts['exclude'])){
136             $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
137             
138             if(!empty($exclude)){
139                 $events->whereAddIn('!Events.action', $exclude, 'string');
140             }
141         }
142         if(!empty($this->opts['only'])){
143             $only= array_unique(array_filter(array_map('trim', explode(',', $this->opts['only']))));
144             
145             if(!empty($only)){
146                 $events->whereAddIn('Events.action', $only, 'string');
147             }
148         }
149         
150         
151         $events->groupBy('Events.action');
152         $events->orderBy('Events.action ASC');
153         
154         $summary = $events->fetchAll('action', 'total');
155         
156         if(empty($summary)){
157             $this->jerror('ERROR-REPORT-' . $this->opts['uid'], 'Nothing to be sent');
158         }
159         
160         $subject = array();
161         
162         foreach ($summary as $k => $v){
163             $subject[] = "{$v} {$k}";
164         }
165         
166         $subject = implode(', ', $subject);
167         
168         if(!empty($this->opts['subject'])){
169             $subject = "{$this->opts['subject']} $subject";
170         }
171         
172         
173         $events = DB_DataObject::factory('Events');
174         $events->autoJoin();
175         
176         $events->selectAdd();
177         $events->selectAdd("
178             Events.id AS id,
179             Events.event_when AS event_when,
180             Events.action AS action,
181             Events.remarks AS remarks
182             
183         ");
184         $events->selectAddPersonEmail();
185         $events->whereAdd('Events.id > '. $min);
186         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
187         
188         $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
189         
190         if(!empty($exclude)){
191             $events->whereAddIn('!Events.action', $exclude, 'string');
192         }
193         if(!empty($only)){
194             $events->whereAddIn('Events.action', $only, 'string');
195         }
196         
197         if(!$events->count()){  // this is the second count we are doing...
198             $this->jerr('Nothing to be sent');
199         }
200         
201         
202         $this->addEvent('ERROR-REPORT-' . $this->opts['uid'], false, $subject);
203
204         
205         $errors = $events->fetchAll();
206         
207         if(!empty($this->opts['host'])){
208             // reset the mail settings..
209             HTML_FlexyFramework::get()->Mail = array(
210                                         'host' => $this->opts['host']
211             );
212         }
213         
214         if(!empty($this->opts['helo'])){
215             HTML_FlexyFramework::get()->Mail['helo'] = $this->opts['helo'];
216         }
217         
218         
219         
220         $content = array(
221             'template'      => 'EVENT_ERRORS_REPORT',
222             'rcpts'         => $rcpts,
223             'errors'        => $errors,
224             'subject'       => $subject
225         );
226
227         $sent = DB_DataObject::factory('core_email')->send($content);
228         
229         if(!is_object($sent)){
230             $this->jok("Done");
231         }
232         
233         $this->jerr($sent);
234         
235     }
236     
237     function listTypes()
238     {
239          $events = DB_DataObject::factory('Events');
240         $events->selectAdd();
241         $events->selectAdd("
242             DISTINCT(Events.action) AS action
243         ");
244         $events->whereAdd("action != ''");
245         $ar = $events->fetchAll('action');
246         echo implode(",", $ar);
247         echo "\n";
248         exit;
249         
250     }
251     
252     
253 }