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";
162 $events = DB_DataObject::factory('Events');
165 $events->selectAdd();
168 Events.event_when AS event_when,
169 Events.action AS action,
170 Events.remarks AS remarks
173 $events->selectAddPersonEmail();
174 $events->whereAdd('id > '. $min);
175 $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
177 $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
179 if(!empty($exclude)){
180 $events->whereAddIn('!Events.action', $exclude, 'string');
183 $events->whereAddIn('Events.action', $only, 'string');
186 if(!$events->count()){ // this is the second count we are doing...
187 $this->jerr('Nothing to be sent');
191 $this->addEvent('ERROR-REPORT-' . $this->opts['uid'], false, $subject);
194 $errors = $events->fetchAll();
196 if(!empty($this->opts['host'])){
197 // reset the mail settings..
198 HTML_FlexyFramework::get()->Mail = array(
199 'host' => $this->opts['host']
203 if(!empty($this->opts['helo'])){
204 HTML_FlexyFramework::get()->Mail['helo'] = $this->opts['helo'];
210 'template' => 'EVENT_ERRORS_REPORT',
213 'subject' => $subject
216 $sent = DB_DataObject::factory('core_email')->send($content);
218 if(!is_object($sent)){
228 $events = DB_DataObject::factory('Events');
229 $events->selectAdd();
231 DISTINCT(Events.action) AS action
233 $events->whereAdd("action != ''");
234 $ar = $events->fetchAll('action');
235 echo implode(",", $ar);