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         if(empty($this->opts['group'])){
71             $this->jerr('Missing group - try add [-t {group name}]');
72         }
73         
74         $rcpts = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'email');
75         
76         if(empty($rcpts)){
77             $this->jerr("{$this->opts['group']} does not has any memeber");
78         }
79         
80         $events = DB_DataObject::factory('Events');
81         $events->selectAdd();
82         $events->selectAdd("
83             DISTINCT(Events.action) AS action,
84             COUNT(Events.id) AS total
85         ");
86         
87         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
88         
89         if(!empty($this->opts['exclude'])){
90             $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
91             
92             if(!empty($exclude)){
93                 $events->whereAddIn('!Events.action', $exclude, 'string');
94             }
95         }
96         
97         $events->groupBy('Events.action');
98         $events->orderBy('Events.action ASC');
99         
100         $summary = $events->fetchAll('action', 'total');
101         
102         if(empty($summary)){
103             $this->jerr('Nothing to be sent');
104         }
105         
106         $subject = array();
107         
108         foreach ($summary as $k => $v){
109             $subject[] = "{$v} {$k}";
110         }
111         
112         $subject = implode(', ', $subject);
113         
114         if(!empty($this->opts['subject'])){
115             $subject = "{$this->opts['subject']} $subject";
116         }
117         
118         $events = DB_DataObject::factory('Events');
119         $events->autoJoin();
120         
121         $events->selectAdd();
122         $events->selectAdd("
123             Events.id AS id,
124             Events.event_when AS event_when,
125             Events.action AS action,
126             Events.remarks AS remarks,
127             CASE WHEN Events.person_id != 0 THEN
128                 join_person_id_id.email
129             WHEN Events.hydra_person_id != 0 THEN
130                 join_hydra_person_id_id.email
131             ELSE
132                 ''
133             END AS email
134         ");
135         
136         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
137         
138         $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
139         
140         if(!empty($exclude)){
141             $events->whereAddIn('!Events.action', $exclude, 'string');
142         }
143         
144         if(!$events->count()){
145             $this->jerr('Nothing to be sent');
146         }
147         
148         $errors = $events->fetchAll();
149         
150         if(!empty($this->opts['host'])){
151             // reset the mail settings..
152             HTML_FlexyFramework::get()->Mail = array(
153                                         'host' => $this->opts['host']
154             );
155         }
156         
157         if(!empty($this->opts['helo'])){
158             HTML_FlexyFramework::get()->Mail['helo'] = $this->opts['helo'];
159         }
160         
161         
162         
163         $content = array(
164             'template'      => 'EVENT_ERRORS_REPORT',
165             'rcpts'         => $rcpts,
166             'errors'        => $errors,
167             'subject'       => $subject
168         );
169
170         $sent = DB_DataObject::factory('core_email')->send($content);
171         
172         if(!is_object($sent)){
173             $this->jok("Done");
174         }
175         
176         $this->jerr($sent);
177         
178     }
179     
180     
181 }