MTrackWeb/Cron/Notify.php
[web.mtrack] / MTrackWeb / Cron / Notify.php
1 <?php
2 require_once 'MTrackWeb.php';
3
4 class MTrackWeb_Cron_Notify extends MTrackWeb
5 {
6     
7     function getAuth()
8     {
9         $ff = HTML_FlexyFramework::get();
10         if (!$ff->cli) {
11             die("access denied");
12         }
13         HTML_FlexyFramework::ensureSingle(__FILE__, $this);
14         return true;
15         
16     }
17     
18     function get()    
19     {
20         DB_DataObject::debugLevel(1);
21         date_default_timezone_set('UTC');
22         
23         // what's the baserul..
24     
25         
26         $MAX_DIFF = 200 * 1024;
27         $USE_BATCHING = false;
28         
29          
30         $e= DB_DataObject::factory('Events');
31         $e->action = 'MTRACK.NOTIFY';
32         $e->selectAdd();
33         $e->selectAdd('MAX(event_when) as event_when');
34         $e->find(true);
35         if (empty($e->event_when)) {
36             $e->event_when = date('Y-m-d H:i:s', strtotime("NOW - 2 days")); // should be a long time ago..
37         }
38          
39         
40         
41         //stream_filter_register("mtrackcanonical", 'CanonicalLineEndingFilter');
42         //stream_filter_register("mtrackunix", 'UnixLineEndingFilter');
43         
44         $w = DB_DataObject::factory('core_watch');
45         echo "gather watched";
46         DB_DataObject::debugLevel(1);
47         // returns a list of user with the objects they are watching..
48         $watches = $w->watched('email');
49         echo "\nDONE WATCHED\n";
50         $CS = DB_DataObject::factory('mtrack_change');
51         
52         $messages = array();
53         $cache = array();
54         foreach($watches as $uid => $ar) {
55             echo "gather changes";
56             print_r($uid); print_r($ar);
57             // we can now query mtrack_change for anything that occured in that list..
58             $messages = $CS->gatherChanges($e->event_when, $uid, $ar);
59             //print_R($messages);exit;
60             // we should cache the emails. = as multiple people might be getting the same email.
61             
62             // should be like a list of tickets
63             foreach ($messages as $m=>$ar) {
64                 
65                 $lr = explode(':', $m);
66                 $o = DB_DataObject::Factory($lr[0]);
67                 $o->autoJoin();
68                 $o->get($lr[1]);
69                 if (!isset($cache[implode(',', $ar)])) {
70                     $cs = clone($CS);
71                     $cs->whereAddIn('mtrack_change.id', $ar, 'int');
72                     $cs->autoJoin();
73                     $cache[implode(',', $ar)] = $cs->fetchAll();
74                 }
75                 $u  = DB_DataObject::factory('Person');
76                 $u->get($uid);
77                 $method =  'notify_'. $lr[0] ;
78                 if (method_exists($this,$method)) {
79                     $this->$method($o,  $cache[implode(',', $ar)], $u);
80                 }
81             }
82             
83             
84             
85             
86             
87             
88             
89         }
90        // print_R($messages);
91         die("done");
92         
93         
94         
95         
96          
97         /* For each watcher, compute the changes.
98          * Group changes by ticket, sending one email per ticket.
99          * Group tickets into batch updates if the only fields that changed are
100          * bulk update style (milestone, assignment etc.)
101          *
102          * For the wiki repo, group by file so that serial edits within the batch
103          * period show up as a single email.
104          */
105          
106     }
107     
108     
109     /**
110      * sets:
111      *   - from (system or a specific user.)
112      *   - replyto (system)
113      *   - to user...
114      *   
115      */
116     function calcAddress($changes, $user) {
117         $ff = HTML_FlexyFramework::get();
118      
119         
120         $headers['To'] = '"'. addslashes($user->name). '" <' . $user->email .'>';
121         $headers['From'] = $ff->MTrackWeb['email_address']; // could be the user who made the change...
122         
123         //if (count($from) > 1) {
124         //    $rep = array();
125         //    array_shift($from);
126          //       foreach ($from as $email) {
127          // $rep[] = make_email($email[0], $email[1]);
128         // }
129         
130         $headers['Reply-To'] =  $ff->MTrackWeb['email_address'];
131         //}
132         return $headers;
133     }
134     
135     /**
136      * convert notification into an email..
137      *
138      * @param object $ticket the DataObject
139      * @param array $items the ids of the mtrack_change's
140      * @parem object $user DataObject of the user..
141      * 
142      */
143     
144     function notify_mtrack_ticket($ticket, $changes, $user)
145     {
146         //global $MAX_DIFF;
147         $ff = HTML_FlexyFramework::get();
148      
149         // from is always the system.. ??
150         // unless all the changers are the same..
151         $headers = $this->calcAddress($changes, $user);
152         
153           
154         //$headers['To'] = '"'. addslashes($user->name). '" <' . $user->email .'>';
155         //$headers['From'] = make_email($from[0][0], $from[0][1]);
156         //if (count($from) > 1) {
157         //    $rep = array();
158         //    array_shift($from);
159          //       foreach ($from as $email) {
160          // $rep[] = make_email($email[0], $email[1]);
161         // }
162         //$headers['Reply-To'] = join(', ', $rep);
163         //}
164      
165         $headers += array(
166             'MIME-Version' => '1.0',
167             'Content-Type' => 'text/plain; charset="UTF-8"',
168             'Content-Transfer-Encoding' => 'quoted-printable',
169         );
170     
171         
172         $mid = $ticket->id . '@' . php_uname('n');
173         
174         
175         
176         
177         //if ($is_initial) {
178         //    $headers['Message-ID'] = "<$mid>";
179         //} else {
180         //$headers['Message-ID'] = "<$T->updated.$mid>";
181         //$headers['In-Reply-To'] = "<$mid>";
182         //$headers['References'] = "<$mid>";
183         //}
184       /* find related project(s) */
185         $p = $ticket->project();
186          
187         $subj = "[" . $p->code . ' - ' . $p->name . "] ";
188     
189         $headers['X-mtrack-project-list'] = $p->code;
190         //foreach ($projects as $pname) {
191         //  $headers["X-mtrack-project-$pname"] = $pname;
192          // $headers['X-mtrack-project'][] = $pname;
193         //}
194         //} else {
195         //$subj = '';
196         // }
197     
198         $headers['Subject'] = sprintf("%s#%s %s (%s %s)",
199           $subj, $ticket->id, $ticket->summary, $ticket->status_name, $ticket->classification_id_name);
200     
201         $owner = strlen($ticket->owner_id) ? $ticket->owner_id_name : 'nobody';
202     
203         $body = 
204             sprintf("%s/Ticket.php/%s\n\n", $ff->MTrackWeb['url'], $ticket->id) .
205             sprintf("#%s: %s (%s %s)\n", $ticket->id, $ticket->summary, $ticket->status_name, $ticket->classification_id_name) .
206             sprintf("Responsible: %s (%s / %s)\n", $owner, $ticket->priority_id_name, $ticket->severity_id_name) .
207     
208             //sprintf("Milestone: %s\n", join(', ', $T->getMilestones()));
209             //sprintf("Component: %s\n", join(', ', $T->getComponents()));
210             "\n";
211     
212       // Display changed fields grouped by the person that last changed them
213         //$who_changed = array();
214         //foreach ($field_changers as $field => $who) {
215         //  $who_changed[$who][] = $field;
216         //}
217         $body.="\n\n";
218         foreach ($changes as $change) {
219             
220             $body .= "{$change->changedate}:  Change by {$change->person_id_name}\n";
221             $body .= str_repeat('-', 80) . "\n";
222             if (!empty($change->reason)) {
223                 $body.= $change->reason;
224             }
225             $ar = $change->cachedAudit();
226             foreach($ar as $audit) {
227                 
228                 switch($audit->field()) {
229                     case 'id':
230                         continue; //??? ignore?
231                     case '@components':
232                         continue;
233                     //  $old = array();
234                     //  foreach (preg_split("/\s*,\s*/", $old_values[$field]) as $id) {
235                      /*   if (!strlen($id)) continue;
236                         $c = get_component($id);
237                         $old[$id] = $c->name;
238                       }
239                       $value = $T->getComponents();
240                       $field = 'Component';
241                       break;
242                      */
243                     case '@milestones':
244                         continue;
245                     //  $old = array();
246                     //  foreach (preg_split("/\s*,\s*/", $old_values[$field]) as $id) {
247                       /*  if (!strlen($id)) continue;
248                         $m = get_milestone($id);
249                         $old[$id] = $m->name;
250                       }
251                       $value = array();
252                       $value = $T->getMilestones();
253                       $field = 'Milestone';
254                       break;
255                       */
256                     case '@keywords':
257                         continue;
258                     /*
259                       $old = array();
260                       $field = 'Keywords';
261                       $value = $T->getKeywords();
262                       break;
263                       */
264                       
265                     //case 'commit?'  
266                       
267                     default:
268                         if (!strlen($audit->oldvalue)) {
269                             $body .= "Set {$audit->field()} to: {$audit->value}\n";
270                             continue;
271                         }
272                         if (!strlen($audit->value)) {
273                             $body .= "Removed {$audit->field()} - was: {$audit->oldvalue}\n";
274                             continue;
275                         }
276                         $body .= "Changed {$audit->field()} from :{$audit->oldvalue} -> {$audit->value}\n";
277                         continue;  
278                 }
279             }
280         }
281         
282         $body .= sprintf("%s/Ticket.php/%s\n\n", $ff->MTrackWeb['url'], $ticket->id);
283         
284         echo $body;   
285     
286         //  send_mail($udata['email'], $plain);
287     }
288     
289 }
290
291
292 class CanonicalLineEndingFilter extends php_user_filter {
293     function filter($in, $out, &$consumed, $closing)
294     {
295       while ($bucket = stream_bucket_make_writeable($in)) {
296         $bucket->data = preg_replace("/\r?\n/", "\r\n", $bucket->data);
297         $consumed += $bucket->datalen;
298         stream_bucket_append($out, $bucket);
299       }
300       return PSFS_PASS_ON;
301     }
302     }
303     class UnixLineEndingFilter extends php_user_filter {
304     function filter($in, $out, &$consumed, $closing)
305     {
306       while ($bucket = stream_bucket_make_writeable($in)) {
307         $bucket->data = preg_replace("/\r?\n/", "\n", $bucket->data);
308         $consumed += $bucket->datalen;
309         stream_bucket_append($out, $bucket);
310       }
311       return PSFS_PASS_ON;
312     }
313     }