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