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     );
68     
69     function getAuth()
70     {
71         $ff = HTML_FlexyFramework::get();
72         
73         if (!$ff->cli) {
74             die("cli only");
75         }
76         
77         return true;
78     }
79     
80     function get($args, $opts)
81     {
82         $this->opts = $opts;
83         
84           
85         if(!empty($this->opts['list'])){
86             $this->listTypes();
87         }
88         
89         if(empty($this->opts['group'])){
90             $this->jerr('Missing group - try add [-t {group name}]');
91         }
92         
93         $rcpts = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'email');
94         
95         
96         if(empty($rcpts)){
97             $this->jerr("{$this->opts['group']} does not has any memeber");
98         }
99         // see the last date of notification to these users...
100         $rcpt_ids = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'id');
101         
102         $min = 0;
103         if (!empty($this->opts['uid'])) {
104             $events = DB_DataObject::factory('Events');
105             $events->action = 'ERROR-REPORT-' . $this->opts['uid'];
106             $events->orderBy('id DESC');
107             $events->limit(1);        
108             if ($events->find(true)) {
109                 $min = $events->id;
110             }
111         }
112         
113         
114         $events = DB_DataObject::factory('Events');
115         $events->selectAdd();
116         $events->selectAdd("
117             DISTINCT(Events.action) AS action,
118             COUNT(Events.id) AS total
119         ");
120         $events->whereAdd('id > '. $min);
121         
122         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
123         
124         if(!empty($this->opts['exclude'])){
125             $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
126             
127             if(!empty($exclude)){
128                 $events->whereAddIn('!Events.action', $exclude, 'string');
129             }
130         }
131         if(!empty($this->opts['only'])){
132             $only= array_unique(array_filter(array_map('trim', explode(',', $this->opts['only']))));
133             
134             if(!empty($only)){
135                 $events->whereAddIn('Events.action', $only, 'string');
136             }
137         }
138         
139         
140         $events->groupBy('Events.action');
141         $events->orderBy('Events.action ASC');
142         
143         $summary = $events->fetchAll('action', 'total');
144         
145         if(empty($summary)){
146             $this->jerror('ERROR-REPORT-' . $this->opts['uid'], 'Nothing to be sent');
147         }
148         $this->addEvent('ERROR-REPORT-' . $this->opts['uid'], 'false', ;
149         $e = DB_DataObject::factory('Events');
150         $e->init('ERROR-REPORT',false,"Sending");
151         $e->event_when = date('Y-m-d H:i:s');
152         $eid = $e->insert();
153         
154         $subject = array();
155         
156         foreach ($summary as $k => $v){
157             $subject[] = "{$v} {$k}";
158         }
159         
160         $subject = implode(', ', $subject);
161         
162         if(!empty($this->opts['subject'])){
163             $subject = "{$this->opts['subject']} $subject";
164         }
165         
166         $events = DB_DataObject::factory('Events');
167         $events->autoJoin();
168         
169         $events->selectAdd();
170         $events->selectAdd("
171             Events.id AS id,
172             Events.event_when AS event_when,
173             Events.action AS action,
174             Events.remarks AS remarks
175             
176         ");
177         $events->selectAddPersonEmail();
178         
179         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
180         
181         $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
182         
183         if(!empty($exclude)){
184             $events->whereAddIn('!Events.action', $exclude, 'string');
185         }
186         
187         if(!$events->count()){
188             $this->jerr('Nothing to be sent');
189         }
190         
191         $errors = $events->fetchAll();
192         
193         if(!empty($this->opts['host'])){
194             // reset the mail settings..
195             HTML_FlexyFramework::get()->Mail = array(
196                                         'host' => $this->opts['host']
197             );
198         }
199         
200         if(!empty($this->opts['helo'])){
201             HTML_FlexyFramework::get()->Mail['helo'] = $this->opts['helo'];
202         }
203         
204         
205         
206         $content = array(
207             'template'      => 'EVENT_ERRORS_REPORT',
208             'rcpts'         => $rcpts,
209             'errors'        => $errors,
210             'subject'       => $subject
211         );
212
213         $sent = DB_DataObject::factory('core_email')->send($content);
214         
215         if(!is_object($sent)){
216             $this->jok("Done");
217         }
218         
219         $this->jerr($sent);
220         
221     }
222     
223     function listTypes()
224     {
225          $events = DB_DataObject::factory('Events');
226         $events->selectAdd();
227         $events->selectAdd("
228             DISTINCT(Events.action) AS action
229         ");
230         $events->whereAdd("action != ''");
231         $ar = $events->fetchAll('action');
232         echo implode(",", $ar);
233         echo "\n";
234         exit;
235         
236     }
237     
238     
239 }