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');
51 foreach($watches as $uid => $ar) {
52 // we can now query mtrack_change for anything that occured in that list..
53 $messages = $CS->gatherChanges($e->event_when, $uid, $ar);
54 print_R($messages);exit;
55 // we should cache the emails. = as multiple people might be getting the same email.
57 // should be like a list of tickets
58 foreach ($messages as $m=>$ar) {
76 /* For each watcher, compute the changes.
77 * Group changes by ticket, sending one email per ticket.
78 * Group tickets into batch updates if the only fields that changed are
79 * bulk update style (milestone, assignment etc.)
81 * For the wiki repo, group by file so that serial edits within the batch
82 * period show up as a single email.
90 * - from (system or a specific user.)
95 function calcAddress($items, $user) {
101 * convert notification into an email..
103 * @param object $ticket the DataObject
104 * @param array $items the ids of the mtrack_change's
105 * @parem object $user DataObject of the user..
109 function notify_mtrack_ticket($ticket, $items, $user)
113 // from is always the system.. ??
114 // unless all the changers are the same..
115 $headers = $this->calcAddress($item, $user);
118 $audit = get_change_audit($items);
119 //$headers['To'] = '"'. addslashes($user->name). '" <' . $user->email .'>';
120 //$headers['From'] = make_email($from[0][0], $from[0][1]);
121 //if (count($from) > 1) {
123 // array_shift($from);
124 // foreach ($from as $email) {
125 // $rep[] = make_email($email[0], $email[1]);
127 //$headers['Reply-To'] = join(', ', $rep);
131 'MIME-Version' => '1.0',
132 'Content-Type' => 'text/plain; charset="UTF-8"',
133 'Content-Transfer-Encoding' => 'quoted-printable',
137 $mid = $ticket->id . '@' . php_uname('n');
143 // $headers['Message-ID'] = "<$mid>";
145 //$headers['Message-ID'] = "<$T->updated.$mid>";
146 //$headers['In-Reply-To'] = "<$mid>";
147 //$headers['References'] = "<$mid>";
149 /* find related project(s) */
150 $p = $ticket->project();
152 $subj = "[" . $p->code . ' - ' . $p->name . "] ";
154 $headers['X-mtrack-project-list'] = $p->code;
155 //foreach ($projects as $pname) {
156 // $headers["X-mtrack-project-$pname"] = $pname;
157 // $headers['X-mtrack-project'][] = $pname;
163 $headers['Subject'] = sprintf("%s#%s %s (%s %s)",
164 $subj, $ticket->id, $ticket->summary, $ticket->status_name, $ticket->classification_id_name);
166 $owner = strlen($ticket->owner) ? $ticket->owner_id_name : 'nobody';
169 sprintf("%s%s/Ticket.php/%s\n\n", $this->HTTP_HOST, $this->baseURL, $ticket->ident) .
170 sprintf("#%s: %s (%s %s)\n", $ticket->id, $ticket->summary, $ticket->status_name, $ticket->classification_id_name) .
171 sprintf("Responsible: %s (%s / %s)\n", $owner, $ticket->priority, $ticket->severity) .
173 //sprintf("Milestone: %s\n", join(', ', $T->getMilestones()));
174 //sprintf("Component: %s\n", join(', ', $T->getComponents()));
177 // Display changed fields grouped by the person that last changed them
178 //$who_changed = array();
179 //foreach ($field_changers as $field => $who) {
180 // $who_changed[$who][] = $field;
183 foreach ($changes as $change) {
185 $body .= "{$change->changedate}: Change by {$change->person_id_name}\n";
186 $body .= str_repeat('-', 80) . "\n";
187 if (!empty($change->reason)) {
188 $body.= $change->reason;
190 $ar = $change->audit();
191 foreach($ar as $audit) {
193 switch($audit->field()) {
195 continue; //??? ignore?
199 // foreach (preg_split("/\s*,\s*/", $old_values[$field]) as $id) {
200 /* if (!strlen($id)) continue;
201 $c = get_component($id);
202 $old[$id] = $c->name;
204 $value = $T->getComponents();
205 $field = 'Component';
211 // foreach (preg_split("/\s*,\s*/", $old_values[$field]) as $id) {
212 /* if (!strlen($id)) continue;
213 $m = get_milestone($id);
214 $old[$id] = $m->name;
217 $value = $T->getMilestones();
218 $field = 'Milestone';
226 $value = $T->getKeywords();
233 if (!strlen($audit->oldvalue)) {
234 $body .= "Set {$audit->field()} to: {$audit->value}\n";
237 if (!strlen($audit->value)) {
238 $body .= "Removed {$audit->field()} - was: {$audit->oldvalue}\n";
241 $body .= "Changed {$audit->field()} from :{$audit->oldvalue} -> {$audit->value}\n";
246 $body .= sprintf("%s%s/Ticket.php/%s\n\n", $this->HTTP_HOST, $this->baseURL, $ticket->ident);
249 // send_mail($udata['email'], $plain);
255 class CanonicalLineEndingFilter extends php_user_filter {
256 function filter($in, $out, &$consumed, $closing)
258 while ($bucket = stream_bucket_make_writeable($in)) {
259 $bucket->data = preg_replace("/\r?\n/", "\r\n", $bucket->data);
260 $consumed += $bucket->datalen;
261 stream_bucket_append($out, $bucket);
266 class UnixLineEndingFilter extends php_user_filter {
267 function filter($in, $out, &$consumed, $closing)
269 while ($bucket = stream_bucket_make_writeable($in)) {
270 $bucket->data = preg_replace("/\r?\n/", "\n", $bucket->data);
271 $consumed += $bucket->datalen;
272 stream_bucket_append($out, $bucket);