/[drupal]/contributions/modules/subscription/subscription.module
ViewVC logotype

Contents of /contributions/modules/subscription/subscription.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.15 - (show annotations) (download) (as text)
Thu Apr 6 14:51:18 2006 UTC (3 years, 7 months ago) by deekayen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.14: +32 -33 lines
File MIME type: text/x-php
issue #38980
1 <?php
2 /* $Id: subscription.module,v 1.14 2006/04/04 19:29:32 ee Exp $ */
3
4 function subscription_help($section) {
5 switch ($section) {
6 case 'admin/modules#description':
7 $output = t("Users may be notified of new content according to preference.");
8 break;
9 }
10 return $output;
11 }
12 /**
13 * Implementation of hook_menu().
14 */
15 function subscription_menu($may_cache) {
16 global $user;
17 $items = array();
18 if (!$may_cache && $user->uid) {
19 $items[] = array('path' => "user/$user->uid/subscriptions",
20 'title' => t('my subscriptions'),
21 'access' => user_access('manage subscriptions'),
22 'callback' => 'subscription_list',
23 'type' => MENU_LOCAL_TASK);
24 }
25 elseif ($may_cache) {
26 $items[] = array('path' => 'subscription/add',
27 'title' => t('Subscribe to content'),
28 'access' => user_access('manage subscriptions'),
29 'callback' => 'subscription_subscribe',
30 'type' => MENU_CALLBACK);
31 $items[] = array('path' => 'subscription/del',
32 'title' => t('Unsubscribe from content'),
33 'access' => user_access('manage subscriptions'),
34 'callback' => 'subscription_unsubscribe',
35 'type' => MENU_CALLBACK);
36 $items[] = array('path' => 'admin/subscription',
37 'title' => t('subscription'),
38 'access' => user_access('manage global channels'),
39 'callback' => 'subscription_admin_channels');
40 $items[] = array('path' => 'subscription/admin/del/channel',
41 'title' => t('delete channel'),
42 'access' => user_access('manage global channels'),
43 'callback' => 'subscription_admin_delete_channel',
44 'type' => MENU_CALLBACK);
45 }
46 return $items;
47 }
48 /**
49 * Implementation of hook_perm().
50 * TODO: rename manage subscriptions to something less admin sounding, like create subscriptions
51 */
52 function subscription_perm() {
53 return array('manage subscriptions', 'manage global channels');
54 }
55 /**
56 * Implementation of hook_subscription()
57 *
58 */
59 function subscription_subscription($op, $arg1 = null, $arg2 = null) {
60 switch ($op) {
61 case 'channel':
62 $channels = array();
63 $channels[] = array('name' => t('Debugger'), 'id' => 'debug', 'handler' => 'subscription_debuggerchannel');
64 $channels[] = array('name' => t('Simple mailer'), 'id' => 'simple_mail', 'handler' => 'subscription_simplemailchannel');
65 return $channels;
66 case 'formatter':
67 $formatters = array();
68 return $formatters;
69 case 'cron':
70 if (!variable_get('subscription_instant_mode', 0)) {
71 return array();
72 }
73 $new_objects = array();
74 $result = db_query(db_rewrite_sql("SELECT n.*, u.uid, u.name, u.picture, u.data FROM {node} n INNER JOIN {users} u ON u.uid = n.uid WHERE n.created>'%d' AND n.created<='%d'", $arg1, $arg2));
75 $nodes = array();
76 while ($node = db_fetch_object($result)) {
77 $node = drupal_unpack($node);
78 $node->date = format_date($node->created, 'medium');
79 $nodes[] = $node;
80 }
81 $new_objects['node'] = $nodes;
82
83 $result = db_query(db_rewrite_sql("SELECT c.*, u.uid, u.name, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.timestamp>'%d' AND c.timestamp<='%d'", $arg1, $arg2));
84 $comments = array();
85 while ($comment = db_fetch_object($result)) {
86 $comment = drupal_unpack($comment);
87 $comment->date = format_date($comment->timestamp, 'medium');
88 $comments[] = $comment;
89 }
90 $new_objects['comment'] = $comments;
91 return array($new_objects);
92 case 'special':
93 return l(t("Subscribe to all comments"), 'subscription/add/comment');
94 }
95 }
96 /**
97 * Find the most suitable formatter
98 *
99 * @param $object
100 * Object type eg. node
101 * @param $channel
102 * Name of active channels
103 */
104 function subscription_findformatter($object, $channel) {
105 $current = array('name' => t('Default formatter'), 'object' => '*', 'channel' => '*', 'weight' => -1, 'handler' => 'subscription_simpleformatter');
106 $formatters = subscription_get_available_formatters();
107 if (($formatter['object'] == '*' || $formatter['object'] == $object) &&
108 ($formatter['channel'] == '*' || $formatter['channel'] == $channel) &&
109 ($current['object'] == '*' || $formatter['object'] == $current['object']) &&
110 ($current['channel'] == '*' || $formatter['channel'] == $current['channel']) &&
111 ($current['weight'] <= $formatter['weight'])) {
112 $current = $formatter;
113 }
114 return $current;
115
116 }
117 /**
118 * Main admin page
119 */
120 function subscription_admin_channels() {
121 $avail_channels = subscription_get_available_channels();
122 if (!empty($_POST)) {
123 $edit = $_POST['edit'];
124 $cid = db_next_id("subscription_channels", "cid");
125 db_query("INSERT INTO subscription_channels (cid,name,id,daily) VALUES (%d,'%s','%s',%d)", $cid, $edit['name'], $edit['id'], $edit['daily']);
126 drupal_set_message(t("Channel created succesfully"));
127 }
128 $channels = subscription_getchannels();
129 $table = array();
130 foreach ($channels as $channel) {
131 $table[] = array('name' => $channel['name'],
132 'handler' => $channel['id'],
133 'daily' => ($channel['daily'] ? t('daily') : t('instant')),
134 // l(t("configure"), 'subscription/admin/configure/channel/'.$channel['cid']),
135 l(t("delete"), 'subscription/admin/del/channel/'.$channel['cid']));
136 }
137 $table = theme('table', array(t("Name"), t("Type"), t("Frequency"), ""), $table);
138 $page = "<h2>". t("Channel list") ."</h2><p>". $table .'</p>';
139
140 $channel_select = array();
141 foreach ($avail_channels as $channel) {
142 $channel_select[$channel['id']] = $channel['name'];
143 }
144 $form['name'] = array(
145 '#type' => 'textfield',
146 '#title' => t('name'),
147 '#default_value' => '',
148 '#size' => 30,
149 '#maxlength' => 30,
150 );
151 $form['id'] = array(
152 '#type' => 'select',
153 '#title' => t('channel'),
154 '#default_value' => 0,
155 '#options' => $channel_select,
156 );
157 $form['daily'] = array(
158 '#type' => 'select',
159 '#title' => t('daily'),
160 '#default_value' => 0,
161 '#options' => array(t('instant'), t('daily')),
162 );
163 $form["save"] = array(
164 '#type' => 'submit',
165 '#value' => t("Save"),
166 );
167 $page .= "<h2>". t("Add channel") .'</h2><p>'. drupal_get_form('subscription_admin_channels', $form) .'</p>';
168 return $page;
169 }
170 /**
171 * Form validation for the "Add channel" field above.
172 */
173 function subscription_admin_channels_validate($form_id, &$form) {
174 global $form_values;
175 if (!$form['name']) {
176 form_set_error('name', t("The name is empty"));
177 }
178 }
179 /**
180 * remove a channel
181 * TODO: add confirmation page. Do this when porting to 4.7
182 */
183 function subscription_admin_delete_channel() {
184 db_query("DELETE FROM {subscription_channels} WHERE cid=%d", arg(4));
185 db_query("DELETE FROM {subscription_subscriptions} WHERE cid=%d", arg(4));
186 /* TODO
187 * delete all subscriptions from channel
188 */
189 drupal_goto('admin/subscription');
190 }
191 /**
192 * Implementation of hook_link().
193 */
194 function subscription_link($type, $node = 0, $main) {
195 global $user;
196 $links = array();
197 if (user_access('manage subscriptions') && $type == 'node' && $node->comment == 2) {
198 $name = node_invoke($node, 'node_name');
199 if ($main) {
200 if (arg(0) == 'taxonomy' && arg(1) == 'term') {
201 $links[] = l(t('subscribe to this category'), 'subscription/add/taxonomy/'. arg(2), array('title' => t('Receive e-mail whenever new content is posted to "%n".', array('%n' => $name))));
202 }
203 else {
204 //TODO: use proper node->type description and theme('placeholder')
205 $links[] = l(t('subscribe to '.$node->type.'s'), 'subscription/add/node/'. $node->type, array('title' => t('Receive e-mail whenever a new "%n" is posted.', array('%n' => $name))));
206 }
207 }
208 else {
209 // Attention, we use $node, because $node is about some format as $comment
210 // ex. exists an 'uid' key
211 if (subscription_subscribed('comment', $node)) {
212 $links[] = l(t('unsubscribe'), 'user/'.$user->uid.'/subscriptions', array('title' => t('Stop receiving e-mail whenever a new comment is posted to "%title".', array('title' => $node->title))));
213 }
214 else {
215 $links[] = l(t('subscribe to comments'), 'subscription/add/comment/'. $node->nid, array('title' => t('Receive e-mail whenever a comment is posted to "%title."', array('%title' => $node->title))));
216 }
217 }
218 }
219 return $links ? $links : array();
220 }
221 /**
222 * Implementation of hook_nodeapi().
223 */
224 function subscription_nodeapi(&$node, $op, $arg = 0) {
225 if (variable_get('subscription_instant_mode', 0)) {
226 return;
227 }
228 switch ($op) {
229 case 'insert':
230 subscription_trigger(array('node' => array($node)));
231 break;
232 }
233 }
234 /**
235 * Implement hook_comment()
236 */
237 function subscription_comment($op, $comment) {
238 if (variable_get('subscription_instant_mode', 0)) {
239 return;
240 }
241 global $user;
242 if ($op == 'insert') {
243 if ($user->subscription_auto && !subscription_is_subscribed('comment', array('nid' => $comment['nid']))) {
244 subscription_store('comment', array('nid' => $comment['nid']));
245 }
246 $parent_node = node_load($comment['nid']);
247 $new_object = (array_merge($comment, array('parent_uid' => $parent_node->uid)));
248 $newobject = array('comment' => array($comment));
249 subscription_trigger($newobject);
250 }
251 }
252 /**
253 * Invoke hook_subscription and prepare the result
254 */
255 function _subscription_invoke_cron($last_time, $now) {
256 $new = module_invoke_all('subscription', 'cron', $last_time, $now);
257 $summa = array();
258 foreach ($new as $group) {
259 foreach ($group as $type => $objects) {
260 if (array_key_exists($type, $summa)) {
261 $summa[$type] = array_merge($summa[$type], $objects);
262 }
263 else {
264 $summa[$type] = $objects;
265 }
266 }
267 }
268 return $summa;
269 }
270 /**
271 * Implement hook_cron()
272 */
273 function subscription_cron() {
274 $now = time();
275 /* semi-instant mode */
276 $last_time = variable_get('subscription_last_cron', 0);
277 subscription_trigger(_subscription_invoke_cron($last_time, $now));
278 variable_set('subscription_last_cron', $now);
279 /* daily digest */
280 $last_daily = floor(variable_get('subscription_last_daily', 0) / (60 * 60 * 24));
281 if (floor($now / (60 * 60 * 24)) > $last_daily) {
282 subscription_trigger(_subscription_invoke_cron($last_daily, $now), true);
283 }
284 variable_set('subscription_last_daily', $now);
285 }
286
287 /**
288 *
289 */
290 function subscription_get_all($digest = false) {
291 $rules = array();
292 $digest = ($digest ? 1 : 0);
293 $result = db_query("SELECT s.*,c.name,c.id FROM {subscription_subscriptions} s LEFT JOIN {subscription_channels} c ON s.cid=c.cid WHERE c.daily=%d ORDER BY uid, cid, object, `condition`", $digest);
294 while ($rule = db_fetch_array($result)) {
295 $rules[] = $rule;
296 }
297 return $rules;
298 }
299 /**
300 * get available channels by hook_subscription
301 *
302 * @return array of channels
303 */
304 function subscription_get_available_channels() {
305 return module_invoke_all('subscription', 'channel');
306 }
307 /**
308 * get available formatters by hook_subscription
309 *
310 * @return array of formatters
311 */
312 function subscription_get_available_formatters() {
313 static $formatters;
314 if (!isset($formatters)) {
315 $formatters = module_invoke_all('subscription', 'formatter');
316 }
317 return $formatters;
318 }
319 /**
320 * get the php function from the channel id
321 *
322 * @param a channel id from hook_subscription() eg. simple_mail
323 * @return an array of channel or null
324 */
325 function subscription_get_channelhandler_from_id($id) {
326 $available_channels = subscription_get_available_channels();
327 foreach ($available_channels as $channel) {
328 if ($channel['id'] == $id) {
329 return $channel['handler'];
330 }
331 }
332 }
333 /**
334 * Get the current user subscriptions
335 */
336 function subscription_get() {
337 global $user;
338 $rules = array();
339 $result = db_query("SELECT s.*,c.name,c.id FROM {subscription_subscriptions} s LEFT JOIN {subscription_channels} c ON s.cid=c.cid WHERE s.uid = %d ORDER BY uid, cid, object, `condition`", $user->uid);
340 while ($rule = db_fetch_array($result)) {
341 $rules[] = $rule;
342 }
343 return $rules;
344 }
345 function subscription_getchannels() {
346 global $user;
347 $channels = array();
348 $result = db_query("SELECT * FROM {subscription_channels}");
349 while ($channel = db_fetch_array($result)) {
350 $channels[$channel['cid']] = $channel;
351 }
352 return $channels;
353 }
354 function subscription_subscribed($object_type, $object) {
355 $rules = subscription_get();
356 foreach ($rules as $rule) {
357 if (subscription_match($object_type, $object, $rule)) {
358 return true;
359 }
360 }
361 return false;
362 }
363 function subscription_match($object_type, $object, $rule) {
364 if (($object_type == $rule['object']) || ($rule['object'] == '*')) {
365 $condition = unserialize($rule['condition']);
366 if (is_object($object)) {
367 $object = get_object_vars($object);
368 }
369 $common_keys = array_intersect(array_keys($object), array_keys($condition));
370 foreach ($common_keys as $key) {
371 if (is_array($object[$key])) {
372 if (array_diff($object[$key], $condition[$key]) != array()) {
373 return false;
374 }
375 }
376 else if ($object[$key] != $condition[$key]) {
377 return false;
378 }
379 }
380 return true;
381 };
382 return false;
383 }
384 function subscription_notify($channel, $objects) {
385 $function = subscription_get_channelhandler_from_id($channel);
386 call_user_func($function, "notify", $objects);
387 }
388 /**
389 * Notify the users that new object created
390 */
391 function subscription_trigger($object, $digest = false) {
392 $active = array();
393 $rules = subscription_get_all($digest);
394 foreach ($rules as $rule) {
395 foreach ($object as $object_type => $group) {
396 foreach ($group as $okey => $content) {
397 if (subscription_match($object_type, $content, $rule)) {
398 $active[$rule['id']][$rule['uid']][$object_type][] = $content;
399 }
400 }
401 }
402 }
403 if ($active != array()) {
404 foreach ($active as $channel => $objects) {
405 subscription_notify($channel, $objects);
406 }
407 }
408 /** send message **/
409 }
410 /**
411 * Insert a condition into subscription
412 */
413 function subscription_store($type, $condition) {
414 global $user;
415 $channel = subscription_get_default_channel();
416 if ($channel == null) {
417 return null;
418 }
419 $condition = serialize($condition);
420 $sid = db_next_id('subscribe_subscription', 'sid');
421 db_query("INSERT INTO {subscription_subscriptions} (sid,uid,object,`condition`,cid) VALUES (%d,%d,'%s','%s',%d)", $sid, $user->uid, $type, $condition, $channel);
422 return $sid;
423 }
424 /**
425 * Return if exist current user already has a given subscription
426 */
427 function subscription_is_subscribed($type, $condition) {
428 global $user;
429 $condition = serialize($condition);
430 $sid = db_next_id('subscribe_subscription', 'sid');
431 $result = db_query("SELECT * FROM {subscription_subscriptions} WHERE uid=%d AND object='%s' AND `condition`='%s' AND cid=%d", $user->uid, $type, $condition, subscription_get_default_channel());
432 $role = db_fetch_array($result);
433 return $role;
434 }
435 /**
436 * Subscribe to some core object (node,comment...)
437 * TODO: use drupal_get_destination() on the subscribe links so we return user to current page and not my subscriptions page
438 */
439 function subscription_subscribe() {
440 global $user;
441 if (arg(2) == 'comment') {
442 if (arg(3) == null) {
443 $condition = array();
444 }
445 else {
446 $condition = array('nid' => arg(3));
447 }
448 $type = arg(2);
449 }
450 else if (arg(2) == 'user') {
451 if (arg(3) == 'all') {
452 $condition = array('uid' => arg(4));
453 $type = '*';
454 }
455 else {
456 $condition = array('parent_uid' => arg(4));
457 $type = 'comment';
458 }
459 }
460 else if (arg(2) == 'node') {
461 $condition = array('type' => arg(3));
462 $type = 'node';
463 }
464 else if (arg(2) == 'taxonomy') {
465 if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', arg(3))) {
466 $tids = preg_split('/[+ ]/', arg(3));
467 $type = "node";
468 foreach ($tids as $tid) {
469 $condition = array('taxonomy' => array($tid));
470 subscription_store($type, $condition);
471 }
472 drupal_goto('user/'.$user->uid.'/subscriptions');
473 }
474 else if (preg_match('/^([0-9]+,)*[0-9]+$/', arg(3))) {
475 $type = "node";
476 $tids = explode(',', arg(3));
477 $condition = array('taxonomy' => $tids);
478 }
479 }
480 else {
481 /* TODO */
482 die("else");
483 }
484 subscription_store($type, $condition);
485 drupal_goto('user/'.$user->uid.'/subscriptions');
486 }
487 /**
488 * Return the current user's default channel cid
489 *
490 * It is useful when previous current channel was deleted by admin
491 */
492 function subscription_get_default_channel() {
493 global $user;
494 $channels = subscription_getchannels();
495 if (array_key_exists($user->subscription_channel, $channels)) {
496 return ($user->subscription_channel);
497 }
498 elseif (sizeof($channels) > 0) {
499 $channel = reset($channels);
500 return $channel['cid'];
501 }
502 else {
503 return null;
504 }
505 }
506
507 /**
508 * Implementation of hook_user().
509 */
510 function subscription_user($type, &$edit, &$account, $category = NULL) {
511 global $user;
512
513 switch ($type) {
514 case 'form':
515 if ($category == 'account') {
516 $channels = subscription_getchannels();
517 $channel_select = array();
518 foreach ($channels as $channel) {
519 $channel_select[$channel['cid']] = $channel['name'];
520 }
521 $form['subscription'] = array(
522 '#type' => 'fieldset',
523 '#title' => t('Subscription settings'),
524 '#weight' => 2,
525 );
526 $form['subscription']['subscription_channel'] = array(
527 '#type' => 'select',
528 '#title' => t('Default channel'),
529 '#default_value' => subscription_get_default_channel(),
530 '#options' => $channel_select,
531 '#description' => t('Select the destination for new subscriptions.'),
532 );
533 $form['subscription']['subscription_auto'] = array(
534 '#type' => 'checkbox',
535 '#title' => t('Automatically subscribe to those posts in which you have participated.'),
536 '#return_value' => 1,
537 '#default_value' => isset($edit->subscription_auto) ? $edit->subscription_auto : $account->subscription_auto,
538 );
539 return $form;
540 }
541 case 'view':
542 if (user_access('manage subscriptions') && $user->uid != $account->uid) {
543 $links[] = l(t("Subscribe to content created by this user"), 'subscription/add/user/all/' . $account->uid);
544 $links[] = l(t("Subscribe to comments to this user's content"), 'subscription/add/user/comment/' . $account->uid);
545 return array(t('Subscriptions') => array(array('title'=>t("Special subscriptions"),'value'=>theme('item_list', $links))));
546 }
547 }
548 }
549
550 /**
551 * Unsubscribe the current user from a given subscription
552 * TODO: add security, confirmation
553 */
554 function subscription_unsubscribe() {
555 global $user;
556 db_query("DELETE FROM {subscription_subscriptions} WHERE uid=%d AND sid = %d", $user->uid, arg(2));
557 drupal_goto('user/'.$user->uid.'/subscriptions');
558 }
559 /**
560 *
561 */
562 function subscription_list() {
563 global $user;
564 $table = array();
565 if (!empty($_POST)) {
566
567 $edit = $_POST['edit'];
568 $merge_ids = array();
569 foreach ($edit as $key => $value) {
570 if ($value == 1) {
571 $merge_ids[] = str_replace('merge', '', $key);
572 }
573 }
574
575 $main_type = '*';
576 $condition = array();
577 $channel = '';
578 foreach ($merge_ids as $role_id) {
579 $result = db_query("SELECT * FROM {subscription_subscriptions} WHERE sid=%s", $role_id);
580 $role = db_fetch_array($result);
581 $condition = array_merge($condition, unserialize($role['condition']));
582 if ($channel == '') {
583 $channel = $role['cid'];
584 }
585 else {
586 if ($channel != $role['cid']) {
587 form_set_error("merge22", t("The channels are different."));
588 break;
589 }
590 }
591 if ($role['object'] != '*') {
592 if (($main_type != '*') && ($main_type != $role['object'])) {
593 form_set_error("merge22", t("The object type is different."));
594 break;
595 }
596 $main_type = $role['object'];
597 }
598 }
599 if (!form_get_errors()) {
600 $sid = db_next_id('subscribe_subscription', 'sid');
601 db_query("INSERT INTO {subscription_subscriptions} (sid,uid,object,`condition`,cid) VALUES (%d,%d,'%s','%s',%d)", $sid, $user->uid, $main_type, serialize($condition), $channel);
602 foreach ($merge_ids as $role_id) {
603 db_query("DELETE FROM {subscription_subscriptions} WHERE sid=%d", $role_id);
604 }
605 }
606 }
607 $roles = subscription_get();
608 $page = "<h1>Current subscriptions</h1>";
609 if ($roles != array()) {
610 foreach ($roles as $role) {
611 $table[] = array($role['object'], _subscription_condition_readable($role['condition']), $role['name'], l('delete', 'subscription/del/'.$role['sid']),
612 '<input type="checkbox" name="edit[merge'.$role['sid'].']" value="1">'
613 );
614 }
615 // new form api sucks
616 $form = "<form method=\"post\" >";
617 $form .= theme('table', array(t('Object'), t('Condition'), t('Channel'), t('Function'), t('Merge')), $table);
618 $form .= '<input type="submit" value="Merge">';
619 $form .= "</form>";
620 $page .= $form;
621 }
622 else {
623 $page .= t("Currently there are no subscriptions.");
624 }
625 $page .= '<p>'.t("There is an OR relationship between the lines. If you would like to use" .
626 " AND relationships, use the 'Merge' button.")."</p> ";
627 $page .= '<p>'.t("Depending on the channel used, you may receive one message per channel with all events.")."</p>";
628 $page .= '<p>'.t("You can change the subscription target channel from your ").l(t("subscription management page"),'user/'.$user->uid.'/subscriptions')."</p>";
629 $page .= '<h1>'. t("Available special subscriptions") ."</h1>";
630 $page .= theme('item_list', module_invoke_all('subscription', 'special'));
631
632 return $page;
633 }
634 /**
635 *
636 */
637 function _subscription_condition_readable($condition) {
638 $condition = unserialize($condition);
639 $text = array();
640 foreach ($condition as $key => $item) {
641 if (is_array($item)) {
642 $item = implode(',', $item);
643 }
644 $text[] = "$key = $item";
645 }
646 return implode(" AND ", $text);
647 }
648
649 /**
650 * Settings menu to the administration section
651 */
652 function subscription_settings() {
653 $form['subscription_instant_mode'] = array(
654 '#type' => 'select',
655 '#title' => t('Default mode for non-digest events'),
656 '#default_value' => variable_get('subscription_instant_mode', 0),
657 '#options' => array('0' => 'instant (small sites)', '1' => 'cronned (big sites)'),
658 '#description' => t('When to notify on new content: immediately after content is created (suggested for small sites), or after next cron run (suggested for big sites) '),
659 );
660 return $form;
661 }
662 /* DEFAULT CHANNELS */
663 function subscription_simpleformatter($object, $type) {
664 if (is_object($object)) {
665 $object = get_object_vars($object);
666 }
667 $dump = '';
668 if ($type == 'node') {
669 $dump .= '== '. $object['title'] ." ==\n\n";
670 $dump .= wordwrap($object['body'], 72) ."\n\n";
671 $dump .= '('. $object['name'] .' -- '. $object['date'] .")\n";
672 $dump .= 'url: '. url("node/". $object['nid'], NULL, NULL, true) ."\n";
673 return $dump;
674 }
675 else if ($type == 'comment') {
676 $dump .= '== '. $object['subject'] ." ==\n\n";
677 $dump .= wordwrap($object['comment'], 72) ."\n\n";
678 $dump .= '('. $object['name'] ." -- ". $object['date'] .")\n";
679 $dump .= 'url: '. url("node/". $object['nid'], NULL, NULL, true) ."\n";
680 return $dump;
681 }
682 else {
683 return var_export($object, true) ."\n";
684 }
685 }
686 function subscription_format($object, $type, $channel) {
687 $formatter = subscription_findformatter($type, $channel);
688 return call_user_func($formatter['handler'], $object, $type);
689
690 }
691 /*
692 * Object formatter
693 */
694 function subscription_simpleformat($objects, $channel) {
695 $global_header =
696 t('There is new content:') ."\n\n";
697 $group_header =
698 "========================================================================\n".
699 " ". t("New") ." %type%\n".
700 "========================================================================\n";
701 $dump = $global_header;
702 foreach ($objects as $type => $group) {
703 $dump .= preg_replace("/%type%/", $type, $group_header);
704 foreach ($group as $content) {
705 $dump .= subscription_format($content, $type, $channel);
706 $dump .= "------------------------------------------------------------------------\n";
707 }
708 }
709 return $dump;
710 }
711
712 /*
713 * A simple debugger channel
714 *
715 * Make sure that debug folder exists
716 */
717 function subscription_debuggerchannel($op, $objects) {
718 if ($op == 'notify') {
719 foreach ($objects as $uid => $group) {
720 $f = fopen("debug/debug_$uid.txt", "w");
721 fwrite($f, subscription_simpleformat($group, 'debug'));
722 fclose($f);
723 }
724 }
725 }
726 /*
727 * A simple mail channel
728 */
729 function subscription_simplemailchannel($op, $objects) {
730 if ($op == 'notify') {
731 foreach ($objects as $uid => $group) {
732 $body = subscription_simpleformat($group, 'simple_mail');
733 // TODO: vary subject by the contents of the mailing
734 $subject = '['.variable_get('site_name', 'drupal').']'.t('Notification from subscriptions');
735 $from = variable_get("site_mail", ini_get('sendmail_from'));
736 $user = user_load(array('uid' => $uid));
737 if (empty($from) || empty($user->mail) || !user_mail($user->mail, $subject, wordwrap($body, 72), "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: <$from>\nErrors-to: $from\n")) {
738 watchdog('subscription', "Invalid site or user mail address.");
739 }
740 }
741 }
742 }
743 ?>

  ViewVC Help
Powered by ViewVC 1.1.2