You are here

function heartbeat_messages_edit in Heartbeat 6.4

Same name and namespace in other branches
  1. 6.2 heartbeat.admin.inc \heartbeat_messages_edit()
  2. 6.3 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
3 string references to 'heartbeat_messages_edit'
hds_form_alter in modules/hds/hds.module
Implementation of hook_form_alter().
heartbeat_menu in ./heartbeat.module
Implementation of hook_menu().
og_activity_form_alter in modules/og_activity/og_activity.module
Implementation of hook_form_alter().

File

./heartbeat.admin.inc, line 931
Admnistration tasks for heartbeat.

Code

function heartbeat_messages_edit(&$form_state, $message) {
  drupal_add_js(drupal_get_path('module', 'heartbeat') . '/heartbeat-admin.js');
  $form = array();
  $form_state['message'] = $message;
  $edit = !empty($message->message_id);
  if ($edit) {
    $form['message_id_display'] = array(
      '#type' => 'textfield',
      '#title' => t('Unique but descriptive message id'),
      '#description' => t('Example "heartbeat_add_content" in the format heartbeat_do_something.'),
      '#default_value' => $edit ? $message->message_id : '',
      '#disabled' => TRUE,
    );
    $form['message_id'] = array(
      '#type' => 'hidden',
      '#default_value' => $message->message_id,
    );
  }
  else {
    $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' => '',
    );
  }
  $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' => $edit ? empty($message->description) ? '' : $message->description : '',
  );
  $form['perms'] = array(
    '#type' => 'select',
    '#title' => t('Message display access'),
    '#description' => t('Defines to whom the message is meant for and who is entitled to see the message.'),
    '#options' => _heartbeat_perms_options(),
    '#default_value' => !isset($message->perms) ? HEARTBEAT_PUBLIC_TO_ALL : $message->perms,
  );
  $form['roles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Limit this message with roles'),
    '#description' => t('Select the roles to filter activity. Leaving empty means the messages will always be shown.'),
    '#options' => user_roles(),
    '#default_value' => empty($message->roles) ? array() : $message->roles,
  );
  $form['custom'] = array(
    '#type' => 'hidden',
    '#default_value' => $edit ? $message->custom : HEARTBEAT_MESSAGE_CUSTOM,
  );

  // Create data to add in custom settings.
  $form['data'] = array(
    '#tree' => TRUE,
    '#weight' => 100,
  );
  $form['data']['heartbeat_show_message_times'] = array(
    '#title' => t('Show the time of action in message displays'),
    '#type' => 'checkbox',
    '#default_value' => $edit && isset($message->variables['heartbeat_show_message_times']) ? $message->variables['heartbeat_show_message_times'] : 1,
    '#description' => t('Disabling the display overrules all other settings.'),
  );
  $tags = (string) (!empty($message->tags) ? implode(",", $message->tags) : '');

  // Autocomplete heartbeat tags
  $form['tags'] = array(
    '#type' => 'textfield',
    '#title' => t('Heartbeat tags'),
    '#description' => t('Enter a comma-separated list of tags for this message. It is used for tagged message displays (also available in heartbeat Views).'),
    '#default_value' => $tags,
    '#autocomplete_path' => 'heartbeat/autocomplete/tag',
  );

  // 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>';

  // 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($message->message) ? '' : $message->message,
    '#description' => t('"!" is available to interpret a words as variables.<br />Note that the actor variable needs to be <strong>@username</strong> to be handled correct.'),
  );
  $desc = t("Type of message when it comes to grouping messages together.<br />\n    <strong>Summary</strong> is when you want to group the same instance of several messages together.\n    For this you will summarize a part of the message and use it as substitional variables (with separators) to\n    form the merged messages. The occurrency of the message instance is also known as the count.<br />\n    <strong>Count</strong> means you want to merge the messages together so you know the occurrency.\n    Only one message in its single format will be displayed.<br />\n    <strong>Single</strong> is when you want to repeat messages without merging them together. These messages\n    are standalone and they don't take notice on previous and upcoming messages.<br />");
  $form['type'] = array(
    '#id' => 'heartbeat_message_type',
    '#type' => 'select',
    '#title' => t('Type of message'),
    '#description' => $desc,
    '#options' => drupal_map_assoc(array(
      'single',
      'summary',
      'count',
    )),
    '#required' => TRUE,
    '#default_value' => empty($message->concat_args['type']) ? '' : $message->concat_args['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']['show_remaining_items'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show display items'),
    '#description' => t('This includes a link that will open up the remaining items sliding down the message body.'),
    '#default_value' => !isset($message->concat_args['show_remaining_items']) ? 1 : $message->concat_args['show_remaining_items'],
  );
  $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($message->message_concat) ? '' : $message->message_concat,
  );

  // These are fields that end up as concatenation arguments (concat_args)
  $group_by = !empty($message->concat_args['group_by']) ? $message->concat_args['group_by'] : 'none';
  $form['type_summary']['group_by'] = array(
    '#type' => 'select',
    '#options' => array(
      'none' => t('No grouping'),
      'user' => t('Group by user to summarize nodes'),
      'node' => t('Group by node to summarize users'),
      'node-target' => t('Group by target object id to summarize nodes'),
      'user-user' => t('Group by user to summarize users'),
    ),
    '#title' => t('Message groups'),
    '#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' => $group_by,
    '#attributes' => array(
      'onchange' => 'javascript: if ($(this).val() == \'user-user\') {$(\'#group-by-target-wrapper\').show();} else {$(\'#group-by-target-wrapper\').hide();} return false;',
    ),
  );
  $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('Variable to summarize'),
    '#description' => t('If you used a word between %-signs, you have to fill in the variable you want to summarize.') . '<br /> e.g.:' . $desc,
    '#required' => FALSE,
    '#default_value' => empty($message->concat_args['group_target']) ? '' : $message->concat_args['group_target'],
  );
  $form['type_summary']['group_by_target'] = array(
    '#type' => 'textfield',
    '#prefix' => '<div id="group-by-target-wrapper" style="display: ' . ($group_by == 'user-user' ? 'block' : 'none') . ';">',
    '#suffix' => '</div>',
    '#title' => t('The group by variable.'),
    '#description' => t('This is the part that you don\'t want to summarize. Group by parameter indicates your intensions. This always stays the same and can never be the same as the group variable.'),
    '#required' => FALSE,
    '#default_value' => empty($message->concat_args['group_by_target']) ? '' : $message->concat_args['group_by_target'],
  );
  $form['type_summary']['group_num_max'] = array(
    '#title' => 'Maximum number of messages to group',
    '#type' => 'textfield',
    '#size' => 5,
    '#default_value' => empty($message->concat_args['group_num_max']) ? '' : $message->concat_args['group_num_max'],
    '#description' => 'Maximum number of items that can be grouped to create one summarized message.',
  );
  $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($message->concat_args['merge_separator']) ? '' : $message->concat_args['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($message->concat_args['merge_end_separator']) ? '' : $message->concat_args['merge_end_separator'],
  );

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

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

  // Set the attachments
  $attachments = module_invoke_all('heartbeat_attachments', $form_state['message']);
  if (!empty($attachments)) {
    $form['attachments'] = array(
      '#tree' => TRUE,
      '#type' => 'fieldset',
      '#title' => t('Attachments'),
    ) + $attachments;
  }
  return $form;
}