View source
<?php
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,
),
);
}
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;
}
}
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;
}
}
function message_rules_action_info() {
$info = array();
$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;
}
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,
);
}
}
function message_action_create_message_instance($message, $account, $settings, $element, &$state) {
$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;
}
}
}
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,
);
}
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')) {
$value = rules_input_evaluator_php_apply($settings['code'], $settings['vars'], $state, FALSE);
}
else {
ob_start();
$value = eval($settings['code']);
ob_end_clean();
}
}
return $value;
}