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