3 require_once 'Pman/Roo.php';
5 class Pman_Admin_Report_SendEventErrors extends Pman_Roo
7 static $cli_desc = "Send event errors occured in the last 24 hours";
9 static $cli_opts = array(
11 'desc' => 'group to send to',
18 'desc' => 'list of actions to exclude from report',
25 'desc' => 'list of actions only to report',
32 'desc' => 'Unique identifier - eg. FAILREPORT',
39 'desc' => 'email subject',
46 'desc' => 'mail helo to use',
53 'desc' => 'mail host to use',
61 'desc' => 'list the current actions in the database',
68 'desc' => 'Turn on database debugging',
78 $ff = HTML_FlexyFramework::get();
87 function get($args, $opts = Array())
91 if (!empty($this->opts['debug'])) {
92 DB_DataObject::debugLevel(1);
95 if(!empty($this->opts['list'])){
99 if(empty($this->opts['group'])){
100 $this->jerr('Missing group - try add [-t {group name}]');
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');
110 if ($events->find(true)) {
116 $events = DB_DataObject::factory('Events');
117 $events->selectAdd();
119 DISTINCT(Events.action) AS action,
120 COUNT(Events.id) AS total
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");
126 if(!empty($this->opts['exclude'])){
127 $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
129 if(!empty($exclude)){
130 $events->whereAddIn('!Events.action', $exclude, 'string');
133 if(!empty($this->opts['only'])){
134 $only= array_unique(array_filter(array_map('trim', explode(',', $this->opts['only']))));
137 $events->whereAddIn('Events.action', $only, 'string');
142 $events->groupBy('Events.action');
143 $events->orderBy('Events.action ASC');
145 $summary = $events->fetchAll('action', 'total');
148 $this->jerror('ERROR-REPORT-' . $this->opts['uid'], 'Nothing to be sent');
153 foreach ($summary as $k => $v){
154 $subject[] = "{$v} {$k}";
157 $subject = implode(', ', $subject);
159 if(!empty($this->opts['subject'])){
160 $subject = "{$this->opts['subject']} $subject";
164 $events = DB_DataObject::factory('Events');
167 $events->selectAdd();
170 Events.event_when AS event_when,
171 Events.action AS action,
172 Events.remarks AS remarks
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");
180 $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
182 if(!empty($exclude)){
183 $events->whereAddIn('!Events.action', $exclude, 'string');
186 $events->whereAddIn('Events.action', $only, 'string');
189 if(!$events->count()){ // this is the second count we are doing...
190 $this->jerr('Nothing to be sent');
194 $this->addEvent('ERROR-REPORT-' . $this->opts['uid'], false, $subject);
197 $errors = $events->fetchAll();
199 if(!empty($this->opts['host'])){
200 // reset the mail settings..
201 HTML_FlexyFramework::get()->Mail = array(
202 'host' => $this->opts['host']
206 if(!empty($this->opts['helo'])){
207 HTML_FlexyFramework::get()->Mail['helo'] = $this->opts['helo'];
213 'template' => 'EVENT_ERRORS_REPORT',
214 'rcpts_group' => $this->opts['group'],
216 'subject' => $subject
219 $sent = DB_DataObject::factory('core_email')->send($content);
221 if(!is_object($sent)){
231 $events = DB_DataObject::factory('Events');
232 $events->selectAdd();
234 DISTINCT(Events.action) AS action
236 $events->whereAdd("action != ''");
237 $ar = $events->fetchAll('action');
238 echo implode(",", $ar);
244 static function test_EVENT_ERRORS_REPORT($pg, $to)
246 $this->jerr("Not implemented");