You are here

function heartbeat_rules_default_action_form in Heartbeat 6.4

Same name and namespace in other branches
  1. 6.3 heartbeat.rules.inc \heartbeat_rules_default_action_form()

Action drupal message configuration form.

1 call to heartbeat_rules_default_action_form()
heartbeat_rules_users_action_form in modules/heartbeat_rules/hrules.rules.inc
User action drupal message configuration form.

File

modules/heartbeat_rules/hrules.rules.inc, line 118
Heartbeat rules definitions.

Code

function heartbeat_rules_default_action_form($settings, &$form, $form_state) {
  drupal_add_js(drupal_get_path('module', 'hrules') . '/hrules.js');

  // Add the default empty values to the settings hash
  $settings += array(
    'uid_param' => '',
    'uid_target_param' => '',
    'nid_param' => '',
    'nid_target_param' => '',
    'message_id_param' => '',
    'variables_param' => '',
  );

  // The parameters needed.
  $form['settings']['uid_param'] = array(
    '#type' => 'textfield',
    '#size' => '15',
    '#maxsize' => '25',
    '#title' => t('User Id'),
    '#default_value' => $settings['uid_param'],
    '#description' => t('The user doing the activity. Think well about who is the acting user for content related events. Is it the currently logged-in user or the author of the content.'),
    '#weight' => -5,
  );
  $form['settings']['uid_target_param'] = array(
    '#type' => 'textfield',
    '#size' => '15',
    '#maxsize' => '25',
    '#title' => t('User target Id'),
    '#default_value' => $settings['uid_target_param'],
    '#description' => t('The user target of the activity'),
    '#weight' => -4,
  );
  $form['settings']['nid_param'] = array(
    '#type' => 'textfield',
    '#size' => '15',
    '#maxsize' => '25',
    '#title' => t('Node context Id'),
    '#default_value' => $settings['nid_param'],
    '#description' => t('The node for activity context'),
    '#weight' => -3,
  );
  $form['settings']['nid_target_param'] = array(
    '#type' => 'textfield',
    '#size' => '15',
    '#maxsize' => '25',
    '#title' => t('Node target Id'),
    '#default_value' => $settings['nid_target_param'],
    '#description' => t('The node target of the activity. Only set one if the node is used! It is only handy to merge nodes within node targets, and thus separating the node target contexts (E.g.: Groups, references, ...'),
    '#weight' => -3,
  );

  // The message that needs to be linked to it
  $messages = heartbeat_messages('all', FALSE, FALSE);
  $options = array(
    0 => t('No message selected'),
  );
  foreach ($messages as $message) {
    $options[$message['message_id']] = !empty($message['description']) ? $message['description'] : str_replace('_', ' ', $message['message_id']);
  }

  // Choose a message and addin ahah behavior to show variables
  $message_id = empty($settings['message_id_param']) ? 0 : $settings['message_id_param'];
  $form['settings']['message_id_param'] = array(
    '#type' => 'select',
    '#title' => t('Choose a message'),
    '#default_value' => $message_id,
    '#options' => $options,
    '#weight' => -2,
    '#description' => t('The message'),
    '#attributes' => array(
      'onchange' => 'javascript:heartbeat_rule_get_variables(this); return false;',
    ),
  );

  // Show variables if message_id_param is set
  $heartbeat_message_id = empty($form_state['values']['settings']['message_id_param']) ? $message_id : $form_state['values']['settings']['message_id_param'];

  // Show default information on how variables will appear
  $form['settings']['variables_info'] = array(
    '#type' => 'markup',
    '#value' => t('Variables in the chosen message will appear here. Assign them to a token,
      available for this type of event. Variables are supported to build heartbeat messages
       at all time from a log.<br />
      The syntax you must use goes like this. You give your variable you want to use,
      prefixed by @. (or # is for variables that need to be pretranslated)
      <br />Assign your variable to a token available from the token replacement lists above.
      <br /><strong>Note</strong> One variable assignment per line and no colons as assignment because
      colons are in the tokens as well.
      <br /><strong>Synthax</strong><pre>
@username=[author:user-name-url]
@node_type=[node:type]
@node_title=[node:title-link]
@types=pages</pre>'),
  );
  $template_message = $template_message_concat = $default_values = '';
  if ($heartbeat_message_id) {
    $message = heartbeat_message_load($heartbeat_message_id, 'message_id');
    if (isset($message)) {
      $default_values = heartbeat_rule_action_get_variables($message, $settings['variables_param']);
      $template_message = $message->message;
      $template_message_concat = $message->message_concat;
    }
    else {
      drupal_set_message(t('The message template "@template" has been removed or the module that had the definition for it has been disabled.', array(
        '@template' => $heartbeat_message_id,
      )));
    }
  }
  $display = empty($settings['variables_param']) ? 'none' : 'block';
  $template = '<blockquote>';
  if (!empty($template_message)) {
    $template .= t('<strong>Message</strong>: <div id="message-message">!message</div>', array(
      '!message' => $template_message,
    ));
  }
  if (!empty($template_message_concat)) {
    $template .= t('<strong>Grouping message</strong>: <div id="message-message">!message</div>', array(
      '!message' => $template_message_concat,
    ));
  }
  $template .= '</blockquote>';
  $form['settings']['variables_param'] = array(
    '#type' => 'textarea',
    '#title' => t('Assign a token to each of these variables used in the selected message'),
    '#prefix' => '<div class="clear-block" id="heartbeat-wrapper">' . $template . '<div id="message-variables" style="display: ' . $display . '">',
    '#suffix' => '</div></div>',
    '#default_value' => $default_values,
    '#cols' => 60,
    '#rows' => 5,
  );
}