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         
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         
149         $subject = array();
150         
151         foreach ($summary as $k => $v){
152             $subject[] = "{$v} {$k}";
153         }
154         
155         $subject = implode(', ', $subject);
156         
157         if(!empty($this->opts['subject'])){
158             $subject = "{$this->opts['subject']} $subject";
159         }
160         
161         $this->addEvent('ERROR-REPORT-' . $this->opts['uid'], false, $subject);
162         
163         $events = DB_DataObject::factory('Events');
164         $events->autoJoin();
165         
166         $events->selectAdd();
167         $events->selectAdd("
168             Events.id AS id,
169             Events.event_when AS event_when,
170             Events.action AS action,
171             Events.remarks AS remarks
172             
173         ");
174         $events->selectAddPersonEmail();
175         
176         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
177         
178         $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
179         
180         if(!empty($exclude)){
181             $events->whereAddIn('!Events.action', $exclude, 'string');
182         }
183         
184         if(!$events->count()){
185             $this->jerr('Nothing to be sent');
186         }
187         
188         $errors = $events->fetchAll();
189         
190         if(!empty($this->opts['host'])){
191             // reset the mail settings..
192             HTML_FlexyFramework::get()->Mail = array(
193                                         'host' => $this->opts['host']
194             );
195         }
196         
197         if(!empty($this->opts['helo'])){
198             HTML_FlexyFramework::get()->Mail['helo'] = $this->opts['helo'];
199         }
200         
201         
202         
203         $content = array(
204             'template'      => 'EVENT_ERRORS_REPORT',
205             'rcpts'         => $rcpts,
206             'errors'        => $errors,
207             'subject'       => $subject
208         );
209
210         $sent = DB_DataObject::factory('core_email')->send($content);
211         
212         if(!is_object($sent)){
213             $this->jok("Done");
214         }
215         
216         $this->jerr($sent);
217         
218     }
219     
220     function listTypes()
221     {
222          $events = DB_DataObject::factory('Events');
223         $events->selectAdd();
224         $events->selectAdd("
225             DISTINCT(Events.action) AS action
226         ");
227         $events->whereAdd("action != ''");
228         $ar = $events->fetchAll('action');
229         echo implode(",", $ar);
230         echo "\n";
231         exit;
232         
233     }
234     
235     
236 }