You are here

message.rules.inc in Message 6

Rules integration for the Message module.

File

includes/message.rules.inc
View source
<?php

// $Id: message.rules.inc,v 1.2 2010/06/07 10:54:11 amitaibu Exp $

/**
 * @file
 * Rules integration for the Message module.
 */

/**
 * Implementation of hook_rules_data_type_info().
 */
function message_rules_data_type_info() {
  return array(
    'message' => array(
      'label' => t('message'),
      'class' => 'message_data_type_message',
      'savable' => TRUE,
      'identifiable' => TRUE,
      'module' => 'Message',
      'token type' => FALSE,
    ),
    'message_instance' => array(
      'label' => t('message instance'),
      'class' => 'message_data_type_message_instance',
      'savable' => TRUE,
      'identifiable' => TRUE,
      'module' => 'Message',
      'token type' => FALSE,
    ),
  );
}

/**
 * Defines the message data type.
 */
class message_data_type_message extends rules_data_type {
  function load($name) {
    return message_load($name);
  }
  function save() {
    $message =& $this
      ->get();
    message_save($message);
    return TRUE;
  }
  function get_identifier() {
    $message =& $this
      ->get();
    return $message->name;
  }

}

/**
 * Defines the message instance data type.
 */
class message_data_type_message_instance extends rules_data_type {
  function load($iid) {
    return message_instance_load($iid);
  }
  function save() {
    $message_instance =& $this
      ->get();
    message_instance_save($message_instance);
    return TRUE;
  }
  function get_identifier() {
    $message_instance =& $this
      ->get();
    return $message_instance->iid;
  }

}

/**
 * Implementation of hook_rules_action_info().
 */
function message_rules_action_info() {
  $info = array();

  // Term actions.
  $info['message_action_load_message'] = array(
    'label' => t('Load a message'),
    'new variables' => array(
      'message' => array(
        'type' => 'message',
        'label' => t('Message'),
      ),
    ),
    'eval input' => array(
      'message|message_text',
    ),
    'help' => t('Loading a message will allow you to act on this term, for example you will be create a new message instance.'),
    'module' => 'Message',
  );
  $info['message_action_create_message_instance'] = array(
    'label' => t('Create a message instance'),
    'arguments' => array(
      'message' => array(
        'type' => 'message',
        'label' => t('Message'),
      ),
      'user' => array(
        'type' => 'user',
        'label' => t('User'),
      ),
    ),
    'new variables' => array(
      'message_instance' => array(
        'type' => 'message_instance',
        'label' => t('Message instance'),
      ),
    ),
    'eval input' => array(
      'arguments',
      'code',
      'entity_type',
      'eid',
      'extra_identifier',
    ),
    'module' => 'Message',
  );
  $info['message_action_assign_message_instance_to_realm'] = array(
    'label' => t('Assign a message instance to realm'),
    'arguments' => array(
      'message_instance' => array(
        'type' => 'message_instance',
        'label' => t('Message instance'),
      ),
    ),
    'eval input' => array(
      'plugin_name',
      'realm_id',
    ),
    'module' => 'Message',
  );
  return $info;
}

/**
 * Action: Load a term.
 */
function message_action_load_message($settings) {
  if ($message = message_load(!empty($settings['message']['message_text']) ? $settings['message']['message_text'] : $settings['message']['message_select'])) {
    return array(
      'message' => $message,
    );
  }
}

/**
 * Action: Create a message instance.
 */
function message_action_create_message_instance($message, $account, $settings, $element, &$state) {

  // Prepare the values to pass to message_instance_save();
  $message_instance = new stdClass();
  $args = array();
  $message_instance->name = $message->name;
  $message_instance->uid = $account->uid;
  if (!empty($settings['code'])) {
    $message_instance->arguments = _message_rules_get_php_value($settings, $state);
  }
  elseif (!empty($settings['arguments'])) {
    $list = explode("\n", $settings['arguments']);
    $list = array_map('trim', $list);
    foreach ($list as $opt) {
      if (strpos($opt, '|') !== FALSE) {
        list($key, $value) = explode('|', $opt);
        $message_instance->arguments[$key] = $value;
      }
    }
  }

  // Populate other variables.
  unset($settings['arguments'], $settings['code']);
  foreach ($settings as $key => $value) {
    if (!empty($value)) {
      $message_instance->{$key} = $value;
    }
  }
  $message_instance = message_instance_save($message_instance);
  return array(
    'message_instance' => $message_instance,
  );
}

/**
 * Action: Assign a message instance to realm.
 */
function message_action_assign_message_instance_to_realm($message_instance, $settings) {
  $plugin = message_get_plugin_message($settings['plugin_name']);
  $realm = new stdClass();
  $realm->iid = $message_instance->iid;
  $realm->realm = $plugin['realm'];
  $realm->realm_id = $settings['realm_id'];
  message_realm_save($realm);
}
function _message_rules_get_php_value($settings, &$state) {
  $value = '';
  if ($settings['code']) {
    if (function_exists('rules_input_evaluator_php_apply')) {

      // Support adding variables to the php code, if php module is present.
      $value = rules_input_evaluator_php_apply($settings['code'], $settings['vars'], $state, FALSE);
    }
    else {
      ob_start();
      $value = eval($settings['code']);
      ob_end_clean();
    }
  }
  return $value;
}

Functions

Namesort descending Description
message_action_assign_message_instance_to_realm Action: Assign a message instance to realm.
message_action_create_message_instance Action: Create a message instance.
message_action_load_message Action: Load a term.
message_rules_action_info Implementation of hook_rules_action_info().
message_rules_data_type_info Implementation of hook_rules_data_type_info().
_message_rules_get_php_value

Classes

Namesort descending Description
message_data_type_message Defines the message data type.
message_data_type_message_instance Defines the message instance data type.