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