You are here

function heartbeat_messages_edit in Heartbeat 6.3

Same name and namespace in other branches
  1. 6.4 heartbeat.admin.inc \heartbeat_messages_edit()
  2. 6.2 heartbeat.admin.inc \heartbeat_messages_edit()

Function to maintain and administer heartbeat messages

Return value

settingsform

1 call to heartbeat_messages_edit()
heartbeat_messages_add in ./heartbeat.admin.inc
Function to maintain and administer heartbeat messages
2 string references to 'heartbeat_messages_edit'
flag_heartbeat_form_alter in modules/flag_heartbeat/flag_heartbeat.module
Implementation of hook_form_alter().
heartbeat_menu in ./heartbeat.module
Implementation of hook_menu().

File

./heartbeat.admin.inc, line 152

Code

function heartbeat_messages_edit(&$form_state, $hid = 0, $edit = array()) {
  drupal_add_js(drupal_get_path('module', 'heartbeat') . '/heartbeat.js');
  $form = array();
  if ($hid) {

    // Get messages
    $result = db_query("SELECT * FROM {heartbeat_messages} WHERE hid = %d LIMIT 1", $hid);
    $message = db_fetch_object($result);

    // concat args , if merging is set
    $concat_args = heartbeat_decode_message_variables($message->concat_args);
    if (!empty($concat_args)) {
      $message->concat_args = array();
      foreach ($concat_args as $arg => $value) {
        $message->concat_args[$arg] = $value;
      }
    }
  }
  else {
    $message = new StdClass();
  }
  $form['message_id'] = array(
    '#type' => 'textfield',
    '#title' => t('Unique but descriptive message id'),
    '#description' => t('Example "heartbeat_add_content" in the format heartbeat_do_something.'),
    '#default_value' => empty($edit['message_id']) ? $message->message_id : $edit['message_id'],
    '#disabled' => $hid ? TRUE : FALSE,
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description of the message'),
    '#description' => t('(most of the time you already have an event in mind)'),
    '#cols' => 60,
    '#rows' => 1,
    '#default_value' => empty($edit['description']) ? $message->description : $edit['description'],
  );
  $form['perms'] = array(
    '#type' => 'select',
    '#title' => t('Message display access'),
    '#description' => t('Defines to whom the message is ment for and who is entitled to see the message.'),
    '#options' => array(
      0 => t('Only me'),
      1 => t('Everyone can see this message'),
      2 => t('People I am connected to'),
    ),
    '#default_value' => empty($edit['perms']) ? $message->perms : $edit['perms'],
  );
  $module_list = module_list();
  asort($module_list);
  $form['module'] = array(
    '#type' => 'select',
    '#title' => t('Module to filter in views'),
    '#options' => $module_list,
    '#default_value' => empty($edit['module']) ? $message->module : $edit['module'],
  );
  $form['message_type'] = array(
    '#type' => 'select',
    '#title' => t('Type of the message and can be used as a filter in views'),
    '#options' => heartbeat_get_message_types(),
    '#default_value' => empty($edit['message_type']) ? $message->message_type : $edit['message_type'],
    '#ahah' => array(
      'path' => 'heartbeat/message_types/js',
      'wrapper' => 'heartbeat-type-messages',
      'event' => 'change',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );

  // Examples with variables
  $form['examples'] = array(
    '#type' => 'fieldset',
    '#title' => t('Examples of message variables'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  // variables
  $form['examples']['tokens']['#type'] = 'markup';
  $form['examples']['tokens']['#value'] = '<p>' . t('Here are a few examples of usage of variables in heartbeat messages:') . '</p><div>';
  $form['examples']['tokens']['#value'] .= '<small>' . t('!username has updated !node_title') . ' (for a single message)</small><br />';
  $form['examples']['tokens']['#value'] .= '<small>' . t('!username has added %node_title%') . ' (for grouped messages with variable summary)</small><br />';

  // Extended example, specific to friendlist
  if (module_exists('friendlist_api')) {
    $form['examples']['tokens']['#value'] .= '<small>' . t('!user1 is now !relation_type with !user2') . ' (use %user2% if user1 becomes friends with lots of users in last timespan)</small><br />';
  }
  $form['examples']['tokens']['#value'] .= '</div><p>' . t('Always append your variables with ! or embed the word in %\'s to group several instances of one part of a message.') . '</p>';

  // TEST

  //$tokens = token_get_values('global'); dsm($tokens);

  // the replacement of @ from # is only needed to view them like that.
  // The actual implementation needs the # for partial message translations
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Single message'),
    '#cols' => 60,
    '#rows' => 1,
    '#required' => TRUE,
    '#default_value' => empty($edit['message']) ? $message->message : $edit['message'],
    '#description' => t('"!" is available to interpret a words as variables.'),
  );
  $desc = t('Type of message when it comes to grouping messages together.<br />
  <strong>Single</strong> is when you want to repeat messages without merging them together. These messages
  are standalone and they dont take notice on previous and upcoming messages.<br />
  <strong>Count</strong> means you want to merge the messages together so you know the occurrency.
  Only one message in its single format will be displayed.<br />
  A <strong>summary</strong> is when you want to group the same instance of several messages together.
  For this you will summarize a part of the message and use it as substitional variables (with separators) to
  form the merged messages. The occurrency of the message instance is also known as the count.<br />');
  $form['type'] = array(
    '#id' => 'heartbeat_message_type',
    '#type' => 'select',
    '#title' => t('Type of message'),
    '#description' => $desc,
    '#options' => drupal_map_assoc(variable_get('heartbeat_types', array(
      'single',
      'summary',
      'count',
    ))),
    '#required' => TRUE,
    '#default_value' => empty($edit['type']) ? $message->concat_args['type'] : $edit['type'],
    '#attributes' => array(
      'onchange' => 'javascript:heartbeat_message_type_onchange(this); return false;',
    ),
  );
  $form['type_summary'] = array(
    '#type' => 'markup',
    '#prefix' => '<div id="type-summary-wrapper">',
    '#suffix' => '</div>',
  );
  $form['type_summary']['message_concat'] = array(
    '#type' => 'textarea',
    '#title' => t('Message to group instances'),
    '#description' => t('You can use "%" to indicate that a variable word needs to be replaced with multiple instances of another variable (target variable). This is used when messages are merged together.<br />! is still available'),
    '#cols' => 60,
    '#rows' => 2,
    '#default_value' => empty($edit['message_concat']) ? $message->message_concat : $edit['message_concat'],
  );

  // These are fields that end up as concatenation arguments (concat_args)
  $form['type_summary']['group_by'] = array(
    '#type' => 'select',
    '#options' => array(
      'none' => t('No grouping'),
      'user' => 'user',
      'node' => 'node',
      'user-user' => 'user-user',
    ),
    '#title' => t('Group by'),
    '#description' => t('<strong>Required for types summary. </strong>Messages with parts that merge together are grouped by user or node.
    E.g. Group by node if you want to summarize users and vice versa.<br />In some cases where the activity uses a relation
    between two users, then set the group by to "user-user". A good example is a friend-relation.'),
    '#required' => FALSE,
    '#default_value' => empty($edit['group_by']) ? $message->concat_args['group_by'] : $edit['group_by'],
  );
  $desc = t('<blockquote>
  Grouped message: !username added %images%.
  Single message: !username added an !image and a nice one.
  Then you will group by user and build a summary of images. The grouping variable here is "image".
  </blockquote>');
  $form['type_summary']['group_target'] = array(
    '#type' => 'textfield',
    '#title' => t('Group or merge variable'),
    '#description' => t('If you used a word between %-signs, you have to fill in the message variable you used that needs to be replaced with a summary.') . '<br /> e.g.:' . $desc,
    '#required' => FALSE,
    '#default_value' => empty($edit['group_target']) ? $message->concat_args['group_target'] : $edit['group_target'],
  );
  $form['type_summary']['merge_separator'] = array(
    '#type' => 'textfield',
    '#title' => t('Fill in the target separator'),
    '#description' => t('Separators between the targets, like a colon. E.g. "title1<strong>,</strong> title2 and title3"'),
    '#required' => FALSE,
    '#default_value' => empty($edit['merge_separator']) ? $message->concat_args['merge_separator'] : $edit['merge_separator'],
  );
  $form['type_summary']['merge_end_separator'] = array(
    '#type' => 'textfield',
    '#title' => t('Fill in the target end separator.'),
    '#description' => t('Separators finishing listed targets. E.g. "title1, title2 <strong>and</strong> title3"'),
    '#required' => FALSE,
    '#default_value' => empty($edit['merge_end_separator']) ? $message->concat_args['merge_end_separator'] : $edit['merge_end_separator'],
  );

  // Hidden elements
  $form['hid'] = array(
    '#type' => 'hidden',
    '#default_value' => empty($edit['hid']) ? $message->hid : $edit['hid'],
  );

  // Buttons
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => 50,
  );
  if (isset($edit['hid'])) {
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );
  }
  $form['data'] = array(
    '#tree' => TRUE,
  );
  return $form;
}