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