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',
71 $ff = HTML_FlexyFramework::get();
80 function get($args, $opts)
85 if(!empty($this->opts['list'])){
89 if(empty($this->opts['group'])){
90 $this->jerr('Missing group - try add [-t {group name}]');
93 $rcpts = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'email');
97 $this->jerr("{$this->opts['group']} does not has any memeber");
99 // see the last date of notification to these users...
100 $rcpt_ids = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'id');
104 $events = DB_DataObject::factory('Events');
105 $events->action = 'ERROR-REPORT-' . $this->opts['uid'];
106 $events->orderBy('id DESC');
108 if ($events->find(true)) {
114 $events = DB_DataObject::factory('Events');
115 $events->selectAdd();
117 DISTINCT(Events.action) AS action,
118 COUNT(Events.id) AS total
120 $events->whereAdd('id > '. $min);
122 $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
124 if(!empty($this->opts['exclude'])){
125 $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
127 if(!empty($exclude)){
128 $events->whereAddIn('!Events.action', $exclude, 'string');
131 if(!empty($this->opts['only'])){
132 $only= array_unique(array_filter(array_map('trim', explode(',', $this->opts['only']))));
135 $events->whereAddIn('Events.action', $only, 'string');
140 $events->groupBy('Events.action');
141 $events->orderBy('Events.action ASC');
143 $summary = $events->fetchAll('action', 'total');
146 $this->jerror('ERROR-REPORT-' . $this->opts['uid'], 'Nothing to be sent');
151 foreach ($summary as $k => $v){
152 $subject[] = "{$v} {$k}";
155 $subject = implode(', ', $subject);
157 if(!empty($this->opts['subject'])){
158 $subject = "{$this->opts['subject']} $subject";
161 $this->addEvent('ERROR-REPORT-' . $this->opts['uid'], false, $subject);
163 $events = DB_DataObject::factory('Events');
166 $events->selectAdd();
169 Events.event_when AS event_when,
170 Events.action AS action,
171 Events.remarks AS remarks
174 $events->selectAddPersonEmail();
176 $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
178 $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
180 if(!empty($exclude)){
181 $events->whereAddIn('!Events.action', $exclude, 'string');
184 $events->whereAddIn('Events.action', $only, 'string');
187 if(!$events->count()){
188 $this->jerr('Nothing to be sent');
191 $errors = $events->fetchAll();
193 if(!empty($this->opts['host'])){
194 // reset the mail settings..
195 HTML_FlexyFramework::get()->Mail = array(
196 'host' => $this->opts['host']
200 if(!empty($this->opts['helo'])){
201 HTML_FlexyFramework::get()->Mail['helo'] = $this->opts['helo'];
207 'template' => 'EVENT_ERRORS_REPORT',
210 'subject' => $subject
213 $sent = DB_DataObject::factory('core_email')->send($content);
215 if(!is_object($sent)){
225 $events = DB_DataObject::factory('Events');
226 $events->selectAdd();
228 DISTINCT(Events.action) AS action
230 $events->whereAdd("action != ''");
231 $ar = $events->fetchAll('action');
232 echo implode(",", $ar);