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' => 'h',
41             'default' => '',
42             'min' => 1,
43             'max' => 1,
44         )
45     );
46     
47     function getAuth()
48     {
49         $ff = HTML_FlexyFramework::get();
50         
51         if (!$ff->cli) {
52             die("cli only");
53         }
54         
55         return true;
56     }
57     
58     function get($args, $opts)
59     {
60         $this->opts = $opts;
61         
62         if(empty($this->opts['group'])){
63             $this->jerr('Missing group - try add [-t {group name}]');
64         }
65         
66         $rcpts = DB_DataObject::factory('groups')->lookupMembers("{$this->opts['group']}",'email');
67         
68         if(empty($rcpts)){
69             $this->jerr("{$this->opts['group']} does not has any memeber");
70         }
71         
72         $events = DB_DataObject::factory('Events');
73         $events->selectAdd();
74         $events->selectAdd("
75             DISTINCT(Events.action) AS action,
76             COUNT(Events.id) AS total
77         ");
78         
79         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
80         
81         if(!empty($this->opts['exclude'])){
82             $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
83             
84             if(!empty($exclude)){
85                 $events->whereAddIn('!Events.action', $exclude, 'string');
86             }
87         }
88         
89         $events->groupBy('Events.action');
90         $events->orderBy('Events.action ASC');
91         
92         $summary = $events->fetchAll('action', 'total');
93         
94         if(empty($summary)){
95             $this->jerr('Nothing to be sent');
96         }
97         
98         $subject = array();
99         
100         foreach ($summary as $k => $v){
101             $subject[] = "{$v} {$k}";
102         }
103         
104         $subject = implode(', ', $subject);
105         
106         if(!empty($this->opts['subject'])){
107             $subject = "{$this->opts['subject']} $subject";
108         }
109         
110         $events = DB_DataObject::factory('Events');
111         $events->autoJoin();
112         
113         $events->selectAdd();
114         $events->selectAdd("
115             Events.id AS id,
116             Events.event_when AS event_when,
117             Events.action AS action,
118             Events.remarks AS remarks,
119             CASE WHEN Events.person_id != 0 THEN
120                 join_person_id_id.email
121             WHEN Events.hydra_person_id != 0 THEN
122                 join_hydra_person_id_id.email
123             ELSE
124                 ''
125             END AS email
126         ");
127         
128         $events->whereAdd("Events.event_when > NOW() - INTERVAL 1 DAY");
129         
130         $exclude = array_unique(array_filter(array_map('trim', explode(',', $this->opts['exclude']))));
131         
132         if(!empty($exclude)){
133             $events->whereAddIn('!Events.action', $exclude, 'string');
134         }
135         
136         if(!$events->count()){
137             $this->jerr('Nothing to be sent');
138         }
139         
140         $errors = $events->fetchAll();
141         
142         if(!empty($this->opts['helo'])){
143             HTML_FlexyFramework::get()->Mail['helo'] = $this->opts['helo'];
144         }
145         
146         if(!empty($this->opts['host'])){
147             HTML_FlexyFramework::get()->Mail['host'] = $this->opts['host'];
148         }
149         
150         $content = array(
151             'template'      => 'EVENT_ERRORS_REPORT',
152             'rcpts'         => $rcpts,
153             'errors'        => $errors,
154             'subject'       => $subject
155         );
156
157         $sent = DB_DataObject::factory('core_email')->send($content);
158         
159         if(!is_object($sent)){
160             $this->jok("Done");
161         }
162         
163         $this->jerr($sent);
164         
165     }
166     
167     
168 }