2 require_once 'MTrackWeb.php';
4 class MTrackWeb_Cron_Notify extends MTrackWeb
9 $ff = HTML_FlexyFramework::get();
13 HTML_FlexyFramework::ensureSingle(__FILE__, $this);
20 DB_DataObject::debugLevel(1);
21 date_default_timezone_set('UTC');
23 // what's the baserul..
26 $MAX_DIFF = 200 * 1024;
27 $USE_BATCHING = false;
30 $e= DB_DataObject::factory('Events');
31 $e->action = 'MTRACK.NOTIFY';
33 $e->selectAdd('MAX(event_when) as event_when');
35 if (!$e->find(true)) {
36 $e->event_when = date('Y-m-d H:i:s', 0); // should be a long time ago..
41 stream_filter_register("mtrackcanonical", 'CanonicalLineEndingFilter');
42 stream_filter_register("mtrackunix", 'UnixLineEndingFilter');
44 $w = DB_DataObject::factory('core_watch');
45 // returns a list of user with the objects they are watching..
46 $watches = $w->watched('email');
48 $CS = DB_DataObject::factory('mtrack_change');
52 foreach($watches as $uid => $ar) {
53 // we can now query mtrack_change for anything that occured in that list..
54 $messages = $CS->gatherChanges($e->event_when, $uid, $ar);
55 //print_R($messages);exit;
56 // we should cache the emails. = as multiple people might be getting the same email.
58 // should be like a list of tickets
59 foreach ($messages as $m=>$ar) {
60 $lr = explode(':', $m);
61 $o = DB_DataObject::Factory($lr[0]);
63 if (!isset($cache[implode(',', $ar)])) {
65 $cs->whereAddIn('id', $ar, 'int');
67 $cache[implode(',', $ar)] = $cs->fetchAll();
69 $u = DB_DataObject::factory('Person');
71 $method = 'notify_'. $lr[0] ;
72 if (method_exists($this,$method)) {
73 $this->$method($o, $cache[implode(',', $ar)], $u)
84 // print_R($messages);
91 /* For each watcher, compute the changes.
92 * Group changes by ticket, sending one email per ticket.
93 * Group tickets into batch updates if the only fields that changed are
94 * bulk update style (milestone, assignment etc.)
96 * For the wiki repo, group by file so that serial edits within the batch
97 * period show up as a single email.
105 * - from (system or a specific user.)
110 function calcAddress($items, $user) {
116 * convert notification into an email..
118 * @param object $ticket the DataObject
119 * @param array $items the ids of the mtrack_change's
120 * @parem object $user DataObject of the user..
124 function notify_mtrack_ticket($ticket, $changes, $user)
128 // from is always the system.. ??
129 // unless all the changers are the same..
130 $headers = $this->calcAddress($item, $user);
133 //$headers['To'] = '"'. addslashes($user->name). '" <' . $user->email .'>';
134 //$headers['From'] = make_email($from[0][0], $from[0][1]);
135 //if (count($from) > 1) {
137 // array_shift($from);
138 // foreach ($from as $email) {
139 // $rep[] = make_email($email[0], $email[1]);
141 //$headers['Reply-To'] = join(', ', $rep);
145 'MIME-Version' => '1.0',
146 'Content-Type' => 'text/plain; charset="UTF-8"',
147 'Content-Transfer-Encoding' => 'quoted-printable',
151 $mid = $ticket->id . '@' . php_uname('n');
157 // $headers['Message-ID'] = "<$mid>";
159 //$headers['Message-ID'] = "<$T->updated.$mid>";
160 //$headers['In-Reply-To'] = "<$mid>";
161 //$headers['References'] = "<$mid>";
163 /* find related project(s) */
164 $p = $ticket->project();
166 $subj = "[" . $p->code . ' - ' . $p->name . "] ";
168 $headers['X-mtrack-project-list'] = $p->code;
169 //foreach ($projects as $pname) {
170 // $headers["X-mtrack-project-$pname"] = $pname;
171 // $headers['X-mtrack-project'][] = $pname;
177 $headers['Subject'] = sprintf("%s#%s %s (%s %s)",
178 $subj, $ticket->id, $ticket->summary, $ticket->status_name, $ticket->classification_id_name);
180 $owner = strlen($ticket->owner) ? $ticket->owner_id_name : 'nobody';
183 sprintf("%s%s/Ticket.php/%s\n\n", $this->HTTP_HOST, $this->baseURL, $ticket->ident) .
184 sprintf("#%s: %s (%s %s)\n", $ticket->id, $ticket->summary, $ticket->status_name, $ticket->classification_id_name) .
185 sprintf("Responsible: %s (%s / %s)\n", $owner, $ticket->priority, $ticket->severity) .
187 //sprintf("Milestone: %s\n", join(', ', $T->getMilestones()));
188 //sprintf("Component: %s\n", join(', ', $T->getComponents()));
191 // Display changed fields grouped by the person that last changed them
192 //$who_changed = array();
193 //foreach ($field_changers as $field => $who) {
194 // $who_changed[$who][] = $field;
197 foreach ($changes as $change) {
199 $body .= "{$change->changedate}: Change by {$change->person_id_name}\n";
200 $body .= str_repeat('-', 80) . "\n";
201 if (!empty($change->reason)) {
202 $body.= $change->reason;
204 $ar = $change->cachedAudit();
205 foreach($ar as $audit) {
207 switch($audit->field()) {
209 continue; //??? ignore?
213 // foreach (preg_split("/\s*,\s*/", $old_values[$field]) as $id) {
214 /* if (!strlen($id)) continue;
215 $c = get_component($id);
216 $old[$id] = $c->name;
218 $value = $T->getComponents();
219 $field = 'Component';
225 // foreach (preg_split("/\s*,\s*/", $old_values[$field]) as $id) {
226 /* if (!strlen($id)) continue;
227 $m = get_milestone($id);
228 $old[$id] = $m->name;
231 $value = $T->getMilestones();
232 $field = 'Milestone';
240 $value = $T->getKeywords();
247 if (!strlen($audit->oldvalue)) {
248 $body .= "Set {$audit->field()} to: {$audit->value}\n";
251 if (!strlen($audit->value)) {
252 $body .= "Removed {$audit->field()} - was: {$audit->oldvalue}\n";
255 $body .= "Changed {$audit->field()} from :{$audit->oldvalue} -> {$audit->value}\n";
261 $body .= sprintf("%s%s/Ticket.php/%s\n\n", $this->HTTP_HOST, $this->baseURL, $ticket->ident);
265 // send_mail($udata['email'], $plain);
271 class CanonicalLineEndingFilter extends php_user_filter {
272 function filter($in, $out, &$consumed, $closing)
274 while ($bucket = stream_bucket_make_writeable($in)) {
275 $bucket->data = preg_replace("/\r?\n/", "\r\n", $bucket->data);
276 $consumed += $bucket->datalen;
277 stream_bucket_append($out, $bucket);
282 class UnixLineEndingFilter extends php_user_filter {
283 function filter($in, $out, &$consumed, $closing)
285 while ($bucket = stream_bucket_make_writeable($in)) {
286 $bucket->data = preg_replace("/\r?\n/", "\n", $bucket->data);
287 $consumed += $bucket->datalen;
288 stream_bucket_append($out, $bucket);