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