You are here

public function HeartbeatActivity::create_grouped_message in Heartbeat 7

Same name and namespace in other branches
  1. 6.4 includes/heartbeatactivity.inc \HeartbeatActivity::create_grouped_message()

File

includes/heartbeatactivity.inc, line 290
HeartbeatActivity object Defines one heartbeat activity object.

Class

HeartbeatActivity
Class defines an activity message object

Code

public function create_grouped_message($candidates, $max_items_to_group = 5) {
  global $base_url;
  $message_template = $this
    ->rebuild_message(TRUE);
  $message_extension = '';
  $message_template = str_replace("%times%", $this->target_count, $message_template);
  $message_template = str_replace("%count%", $this->target_count, $message_template);

  // Prepare the merged factors from the stored messages
  $merged_string = '';
  $remains = array();
  $target = $this->template->concat_args['group_target'];
  $beat = 'beat-item-' . $this->uaid;
  $unique = $candidates['variables'];
  $count = $candidates['count'];

  //count($candidates['variables']);

  // Limit the number of displayed (grouped) messages by message
  // or left as default global configuration
  if (!empty($this->template->concat_args['group_num_max'])) {
    $max_items_to_group = $this->template->concat_args['group_num_max'];
  }

  // Reduce the variables for substitution to the maximum
  // Add the remains by keeping the others plus the last one,
  // in case the total exceeds the max group setting.
  if ($count > $max_items_to_group) {
    $count = $max_items_to_group;
    $unique = array_slice($unique, 0, $count);
    $remains = array_slice($candidates['variables'], $count - 1);
  }

  // Replacement of placeholder with group targets
  // If there is a match for %variable%
  // Try to replace the variable with the group_target variables
  if (preg_match_all("|\\%(.*)\\%(.*)|U", $message_template, $matches)) {
    if (count($remains) > 0) {
      $hidden_remains = '<ul id="' . $beat . '_remaining_wrapper" style="display: none;">';
      foreach ($remains as $remain) {
        $key = isset($remain["@" . $target]) ? $remain["@" . $target] : (isset($remain["!" . $target]) ? $remain["!" . $target] : $remain["%" . $target]);
        $hidden_remains .= '<li>' . $key . '</li>';
      }
      $hidden_remains .= '</ul>';
    }
    $placeholder = $matches[1][0];
    $i = 1;
    foreach ($unique as $stored_variables) {

      // limit them to the value given by the group variable setting
      if (isset($stored_variables["!" . $target])) {
        if ($i == 1) {
          $merged_string .= $stored_variables["!" . $target];
        }
        elseif ($i < $count && $count > 2) {
          $merged_string .= $this->template->concat_args['merge_separator'];
          $merged_string .= $stored_variables["!" . $target];
        }
        elseif ($i == $count || $count == 2) {
          $merged_string .= ' ' . $this->template->concat_args['merge_end_separator'];
          if (count($remains) >= 1 && $hidden_remains != '') {
            $attributes = array(
              'attributes' => array(
                'id' => $beat . '_remaining',
                'class' => array(
                  'beat-remaining',
                ),
              ),
              'absolute' => TRUE,
              'fragment' => $beat,
            );
            $merged_string .= l(t('@num more', array(
              '@num' => count($remains),
            )), $base_url . request_uri(), $attributes);
            $message_extension .= $hidden_remains;
          }
          else {
            $merged_string .= $stored_variables["!" . $target];
          }
        }
      }
      $i++;
    }
    $message_template = str_replace("%" . $placeholder . "%", $merged_string, $message_template);
    if (isset($message_extension)) {
      $message_template .= $message_extension;
    }
  }
  $this->message = $message_template;
}