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