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