sync
[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 = Array())
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         $min = 0;
104          
105         if ($this->opts['uid'] != 'STD') {
106             $events = DB_DataObject::factory('Events');
107             $events->action = 'ERROR-REPORT-' . $this->opts['uid'];
108             $events->orderBy('id DESC');
109             $events->limit(1);        
110             if ($events->find(true)) {
111                 $min = $events->id;
112             }
113         }
114         
115         
116         $events = DB_DataObject::factory('Events');
117         $events->selectAdd();
118         $events->selectAdd("
119             DISTINCT(Events.action) AS action,
120             COUNT(Events.id) AS total
121         ");
122         $events->whereAdd('Events.id > '. $min);
123         $events->whereAdd("Events.action NOT LIKE 'ERROR-REPORT-%'");
124         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
125         
126         if(!empty($this->opts['exclude'])){
127             $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
128             
129             if(!empty($exclude)){
130                 $events->whereAddIn('!Events.action', $exclude, 'string');
131             }
132         }
133         if(!empty($this->opts['only'])){
134             $only= array_unique(array_filter(array_map('trim', explode(',', $this->opts['only']))));
135             
136             if(!empty($only)){
137                 $events->whereAddIn('Events.action', $only, 'string');
138             }
139         }
140         
141         
142         $events->groupBy('Events.action');
143         $events->orderBy('Events.action ASC');
144         
145         $summary = $events->fetchAll('action', 'total');
146         
147         if(empty($summary)){
148             $this->jerror('ERROR-REPORT-' . $this->opts['uid'], 'Nothing to be sent');
149         }
150         
151         $subject = array();
152         
153         foreach ($summary as $k => $v){
154             $subject[] = "{$v} {$k}";
155         }
156         
157         $subject = implode(', ', $subject);
158         
159         if(!empty($this->opts['subject'])){
160             $subject = "{$this->opts['subject']} $subject";
161         }
162         
163         
164         $events = DB_DataObject::factory('Events');
165         $events->autoJoin();
166         
167         $events->selectAdd();
168         $events->selectAdd("
169             Events.id AS id,
170             Events.event_when AS event_when,
171             Events.action AS action,
172             Events.remarks AS remarks
173             
174         ");
175         $events->selectAddPersonEmail();
176         $events->whereAdd("Events.action NOT LIKE 'ERROR-REPORT-%'");
177         $events->whereAdd('Events.id > '. $min);
178         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
179         
180         $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
181         
182         if(!empty($exclude)){
183             $events->whereAddIn('!Events.action', $exclude, 'string');
184         }
185         if(!empty($only)){
186             $events->whereAddIn('Events.action', $only, 'string');
187         }
188         
189         if(!$events->count()){  // this is the second count we are doing...
190             $this->jerr('Nothing to be sent');
191         }
192         
193         
194         $this->addEvent('ERROR-REPORT-' . $this->opts['uid'], false, $subject);
195
196         
197         $errors = $events->fetchAll();
198         
199         if(!empty($this->opts['host'])){
200             // reset the mail settings..
201             HTML_FlexyFramework::get()->Mail = array(
202                                         'host' => $this->opts['host']
203             );
204         }
205         
206         if(!empty($this->opts['helo'])){
207             HTML_FlexyFramework::get()->Mail['helo'] = $this->opts['helo'];
208         }
209         
210         
211         
212         $content = array(
213             'template'      => 'FILE_REPORTING_EMAIL',
214             'rcpts_group'   => $this->opts['group'],
215             'errors'        => $errors,
216             'subject'       => $subject
217         );
218
219         $sent = DB_DataObject::factory('core_email')->send($content);
220         
221         if(!is_object($sent)){
222             $this->jok("Done");
223         }
224         
225         $this->jerr($sent);
226         
227     }
228     
229     function listTypes()
230     {
231         $events = DB_DataObject::factory('Events');
232         $events->selectAdd();
233         $events->selectAdd("
234             DISTINCT(Events.action) AS action
235         ");
236         $events->whereAdd("action != ''");
237         $ar = $events->fetchAll('action');
238         echo implode(",", $ar);
239         echo "\n";
240         exit;
241         
242     }
243     
244     static function test_EVENT_ERRORS_REPORT($pg, $to)
245     {
246         $this->jerr("Not implemented");
247     }
248     
249     
250 }