You are here

private function heartbeatParser::rebuild_in_groups in Heartbeat 6.3

Same name and namespace in other branches
  1. 6.4 includes/heartbeatparser.inc \heartbeatParser::rebuild_in_groups()
  2. 7 includes/heartbeatparser.inc \heartbeatParser::rebuild_in_groups()

* Function to rebuild the messages in groups * all sets are handled already

1 call to heartbeatParser::rebuild_in_groups()
heartbeatParser::merge_sets in includes/heartbeatparser.inc
Merges sets of messages to fully formatted messages regenerated at runtime to group settings

File

includes/heartbeatparser.inc, line 282

Class

heartbeatParser

Code

private function rebuild_in_groups() {

  // Check the candidates and make the rebuild array
  foreach ($this->_candidates as $single_id => $info) {
    $this->_messages[$single_id]->message = filter_xss($this->_messages[$single_id]->message, $this->_allowed_tags);
    $this->_messages[$single_id]->message_concat = filter_xss($this->_messages[$single_id]->message_concat, $this->_allowed_tags);
    $message = $this->_messages[$single_id];

    // if a candidate exists for this message and
    // it has more than one count
    // Take care!! not message->count because this could be set with the sql as well
    // The purpose would be to fill %times% or its alias %count%
    if ($this->_candidates[$single_id]['count'] > 1) {
      $message_template = $this->_messages[$single_id]->message_concat;
      $message_template = str_replace("%times%", $message->target_count, $message_template);
      $message_template = str_replace("%count%", $message->target_count, $message_template);

      // Prepare the merged factors from the stored messages
      $merged_string = '';
      $unique = $info['variables'];
      $count = $info['count'];

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

      /**
       * TODO Make a message property to handle max_messages_count
       **/
      if ($count > variable_get('heartbeat_activity_grouping_how_many', 6)) {
        $count = variable_get('heartbeat_activity_grouping_how_many', 6);
        $unique = array_slice($unique, 0, $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)) {

        /**
         * TODO Make a decision on how to handle the target on merge 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];
        $target = $message->concat_args['group_target'];

        //dsm($target.$placeholder);
        $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];
            }
            else {
              if ($i < $count && $count > 2) {
                $merged_string .= ' ' . $message->concat_args['merge_separator'];
                $merged_string .= ' ' . $stored_variables["@" . $target];
              }
              else {
                if ($i == $count || $count == 2) {
                  $merged_string .= ' ' . $message->concat_args['merge_end_separator'];
                  $merged_string .= ' ' . $stored_variables["@" . $target];
                }
              }
            }
          }
          $i++;
        }
        $message_template = str_replace("%" . $placeholder . "%", $merged_string, $message_template);

        //}
      }
      $this->_messages[$single_id]->message = $message_template;
    }
  }
}