Hi,

I'm using Notifications http://drupal.org/project/notifications on my site to alert members of Organic Groups when nodes are posted or changed.

This is working fine, but I have some custom content types, for example "milestone" and "todo" which I would like to create my own events for. These content types use a CCK "user reference" field to identify the person responsible for a task, and I'd like to setup an event to notify users when they are assigned.

Having read through http://drupal.org/node/253102 and http://drupal.org/node/293299 several times, I'm still at a loss, and wondered if anyone could help?

As far as I can tell, I just need to implement hook_notifications(), and call notifications_event() at the appropriate time, however I'm getting an error:

warning: array_merge() [function.array-merge]: Argument #1 is not an array in .../sites/all/modules/notifications/notifications.module on line 408.

The code i have is as follows:

function kairn_notifications($op, &$arg0 = NULL, $arg1 = NULL, $arg2 = NULL) {

	if ($op = 'event types') {
		$types[] = array(
			'type' => 'node',
			'action' => 'reassigned',
			'name' => t('[type-name]: [title]'),
			'line' => t('The [type-name] has been reassigned'),
			'digest' => array('node', 'nid'), 
			'description' => t('Node reassigned'),
		);
		return $types;
	}
}

and the following is called from my drupal action (which works fine in itself):

function kairn_mail_action(&$object, $context = array()) {

        $event = array(
          'module' => 'kairn',
          'uid' => $object->uid,
          'oid' => $object->nid,
          'type' => 'assignment',
          'action' => 'reassigned',
          'node' => $object,
          'params' => array('nid' => $object->nid),      
        );

	notifications_event($event);
}

Apologies if I'm missing something completely obvious. I've looked in the notifications_content.module and can't work out what I'm doing wrong.

Thanks in advance,
Pete

Comments

petebarnett’s picture

I've still had no luck with this. Is there anyone familiar with the notifications framework who could help?
'twould be much appreciated :)

Thanks,
Peter

pathfinderelite’s picture

I came across you post (while look for notifications info as well), and noticed a small error in your code...

if ($op = 'event types') {

...I think you meant to use == instead of =. It may be the completely obvious something that your looking for.

jeff h’s picture

Hey Pete,

Did you get this working? If so, any chance you'd be willing to share your code? I for one would be quite interested in some kind of integration between Notifications and CCK User Reference fields :)

Jeff

petebarnett’s picture

Hey pathfinder, that's probably it!

I actually ended up using the Messaging Framework directly to send my messages, because when I thought about it, nobody needed to subscribe to the particular event as such. A message only needed to go to the assignee of a task (basically, "someone has assigned a task to you").

I was a bit confused as to how the Messaging Framework and Notifications actually tied together when I posted this, but the documentation was updated afterwards and made things a lot clearer!

Jeff, as I haven't implemented Notifications as such, I'm not sure if my code does exactly what you're after, but I do act upon changes to a CCK User Reference field and send a message to the user that's selected.
That's was just a case of implementing hook_nodeapi for the "update" operation and checking the node object to see if the field has been changed.

Here are some code snippets:


/**
 *  This is just a helper function called by my hook_nodeapi
 *  It's called when $op is 'update' and $node->type is 'list_task'
 */
function kairn_nodeapi_update_list_task(&$node, $a3, $a4) {
		
	$original = node_load($node->nid);

	// check whether it's been reassigned
	if ($node->assignee != $original->assignee)
	{
		// send notification to user
		_notify_assignment($node, $node->assignee);
	}
}

function _notify_assignment($node, $assignee) {

	if ( function_exists('messaging_message_send_user') ) {
		
		$text = array();
		
		// build the message
		$text['type'] = 'kairn-node-assigned';
		$text['subject'] = messaging_message_part('kairn-node-assigned', 'subject');
		$text['body'] = messaging_message_part('kairn-node-assigned', 'main');
		
		$objects['user'] = user_load($assignee);
		$objects['node'] = $node;
		$objects['global'] = NULL;
						
		// replace all tokens
		$message = token_replace_multiple($text, $objects);
		// send it
		messaging_message_send_user( user_load($assignee), $message);
	}
}

/**
 * This just defines custom message types, templates as per Messaging Framework documentation
 */
function kairn_messaging($op, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) {
  switch ($op) {
    case 'message groups':
      $info['kairn-node-assigned'] = array(
      	'module' => 'kairn',
      	'name' => t('Node assignment'),
      	'description' => t('Notification of item being assigned to a user'),
      );
      return $info;
      
    case 'message keys':      
      $type = $arg1;
      switch ($type) {
        case 'kairn-node-assigned':
          return array(
            'subject' => t('Subject for milestone reminder'),
            'header' => t('Header for milestone reminder'),
            'main' => t('Content for milestone reminder'),
            'footer' => t('Footer for milestone reminder'),
          );
      }
      break;
      
    case 'messages':
      $type = $arg1;
      
      if ($type == 'kairn-node-assigned') {
      	return array(
      		'subject' => t('Assignment notification from [site-name]'),
      		'header' => t("Greetings,"),
      		'main' => array(
      			t('A [type-name] has been assigned to you. '),
      			t('View this [type-name] at [node-url]'),
      		),
      		'footer' => array(
      			t('This is an automatic message from [site-name]'),
      		),
      	);
		}
		
      break;
      
    case 'tokens':
      $type = $arg1;
      $tokens = array();
      // These are the token groups that will be used for this module's messages
      $tokens = array('global','user','node');
      return $tokens;
  }
}

petebarnett’s picture

actually,

just realised, that example doesn't use a CCK field, but that'd just be a case of checking $node->field_<yourfieldname>[0][uid] instead of $node->assignee.

Cheers,
Pete

jeff h’s picture

Wonderful! Thank you very much for sharing :) I will definitely give this a go.

Jeff