You are here

public function HeartbeatActivity::create_grouped_message in Heartbeat 6.4

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

File

includes/heartbeatactivity.inc, line 189
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);
  }

  // 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 ($this->template->concat_args['show_remaining_items']) {
      $hidden_remains = theme('heartbeat_activity_remaining', $beat, $target, $remains);
    }

    // TODO Make a decision on how to handle the target on merged groups
    // This code in comment is/was a attempt to have more grouping words
    // The difficult and almost impossible part is the fact that you always
    // have to target something to group on (for the same node or same user)

    //foreach ($matches[1] as $target) {
    $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) {
          if (isset($this->template->concat_args['merge_separator_t']) && strlen(strip_tags($this->template->concat_args['merge_separator_t'])) > 0) {
            $merged_string .= ' ' . t($this->template->concat_args['merge_separator']);
          }
          else {
            $merged_string .= ' ' . $this->template->concat_args['merge_separator'];
          }
          $merged_string .= ' ' . $stored_variables["@" . $target];
        }
        elseif ($i == $count || $count == 2) {
          if (isset($this->template->concat_args['merge_end_separator_t']) && strlen(strip_tags($this->template->concat_args['merge_end_separator_t'])) > 0) {
            $merged_string .= ' ' . t($this->template->concat_args['merge_end_separator']);
          }
          else {
            $merged_string .= ' ' . $this->template->concat_args['merge_end_separator'];
          }
          if (count($remains) >= 1 && $hidden_remains != '') {

            // Add the last item.
            $merged_string .= ' ' . $stored_variables["@" . $target];
            if ($this->template->concat_args['show_remaining_items']) {

              // Add the remains.
              $message_extension .= $hidden_remains;

              // Add the link to the remaining items.
              $merged_string .= theme('hearbeat_activity_remain_link', $beat, count($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;
}