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         
32         'subject' => array(
33             'desc' => 'email subject',
34             'short' => 's',
35             'default' => '',
36             'min' => 1,
37             'max' => 1,
38         ),
39         'helo' => array(
40             'desc' => 'mail helo to use',
41             'short' => 'l',
42             'default' => '',
43             'min' => 1,
44             'max' => 1,
45         ),
46         'host' => array(
47             'desc' => 'mail host to use',
48             'short' => 'o',
49             'default' => '',
50             'min' => 1,
51             'max' => 1,
52         ),
53      
54         'list' => array(
55             'desc' => 'list the current actions in the database',
56             'short' => 'L',
57             'default' => '',
58             'min' => 1,
59             'max' => 1,
60         )
61     );
62     
63     function getAuth()
64     {
65         $ff = HTML_FlexyFramework::get();
66         
67         if (!$ff->cli) {
68             die("cli only");
69         }
70         
71         return true;
72     }
73     
74     function get($args, $opts)
75     {
76         $this->opts = $opts;
77         
78           
79         if(!empty($this->opts['list'])){
80             $this->listTypes();
81         }
82         
83         if(empty($this->opts['group'])){
84             $this->jerr('Missing group - try add [-t {group name}]');
85         }
86         
87         $rcpts = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'email');
88         
89         
90         if(empty($rcpts)){
91             $this->jerr("{$this->opts['group']} does not has any memeber");
92         }
93         // see the last date of notification to these users...
94         $rcpt_ids = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'id');
95         
96         $events = DB_DataObject::factory('Events');
97         $events->action = 'ERROR-REPORT';
98         $events->whereAddIn('person_id', $rcpt_ids, 'int');
99         $events->orderBy('id DESC');
100         $events->limit(1);
101         $min = 0;
102         if ($events->find(true)) {
103             $min = $events->id;
104         }
105         
106         
107         
108         $events = DB_DataObject::factory('Events');
109         $events->selectAdd();
110         $events->selectAdd("
111             DISTINCT(Events.action) AS action,
112             COUNT(Events.id) AS total
113         ");
114         $events->whereAdd('id > '. $min);
115         
116         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
117         
118         if(!empty($this->opts['exclude'])){
119             $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
120             
121             if(!empty($exclude)){
122                 $events->whereAddIn('!Events.action', $exclude, 'string');
123             }
124         }
125         
126         $events->groupBy('Events.action');
127         $events->orderBy('Events.action ASC');
128         
129         $summary = $events->fetchAll('action', 'total');
130         
131         if(empty($summary)){
132             $this->jerr('Nothing to be sent');
133         }
134         
135         $subject = array();
136         
137         foreach ($summary as $k => $v){
138             $subject[] = "{$v} {$k}";
139         }
140         
141         $subject = implode(', ', $subject);
142         
143         if(!empty($this->opts['subject'])){
144             $subject = "{$this->opts['subject']} $subject";
145         }
146         
147         $events = DB_DataObject::factory('Events');
148         $events->autoJoin();
149         
150         $events->selectAdd();
151         $events->selectAdd("
152             Events.id AS id,
153             Events.event_when AS event_when,
154             Events.action AS action,
155             Events.remarks AS remarks
156             
157         ");
158         $events->selectAddPersonEmail();
159         
160         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
161         
162         $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
163         
164         if(!empty($exclude)){
165             $events->whereAddIn('!Events.action', $exclude, 'string');
166         }
167         
168         if(!$events->count()){
169             $this->jerr('Nothing to be sent');
170         }
171         
172         $errors = $events->fetchAll();
173         
174         if(!empty($this->opts['host'])){
175             // reset the mail settings..
176             HTML_FlexyFramework::get()->Mail = array(
177                                         'host' => $this->opts['host']
178             );
179         }
180         
181         if(!empty($this->opts['helo'])){
182             HTML_FlexyFramework::get()->Mail['helo'] = $this->opts['helo'];
183         }
184         
185         
186         
187         $content = array(
188             'template'      => 'EVENT_ERRORS_REPORT',
189             'rcpts'         => $rcpts,
190             'errors'        => $errors,
191             'subject'       => $subject
192         );
193
194         $sent = DB_DataObject::factory('core_email')->send($content);
195         
196         if(!is_object($sent)){
197             $this->jok("Done");
198         }
199         
200         $this->jerr($sent);
201         
202     }
203     
204     function listTypes()
205     {
206          $events = DB_DataObject::factory('Events');
207         $events->selectAdd();
208         $events->selectAdd("
209             DISTINCT(Events.action) AS action
210         ");
211         $events->whereAdd("action != ''");
212         $ar = $events->fetchAll('action');
213         echo implode(",", $ar);
214         echo "\n";
215         exit;
216         
217     }
218     
219     
220 }