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