You are here

function message_action_create_message_instance in Message 6

Action: Create a message instance.

File

includes/message.rules.inc, line 144
Rules integration for the Message module.

Code

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,
  );
}