You are here

function _heartbeat_messages_rebuild in Heartbeat 6.4

Rebuild the messages and check their status

1 call to _heartbeat_messages_rebuild()
heartbeat_messages_rebuild in ./heartbeat.module
Function that gathers all messages from all modules New ones and existing ones

File

./heartbeat.module, line 1243

Code

function _heartbeat_messages_rebuild($defaults, $stored) {
  $operations = array(
    'inserted' => 0,
    'deleted' => 0,
    'diffs' => array(),
  );

  // Build hashtables for default and stored messages
  $defaults_lookup = $stored_lookup = array();
  foreach ($defaults as $default) {
    $defaults_lookup[$default['message_id']] = $default['custom'];
  }

  // First loop through the stored messages
  foreach ($stored as $key => $cached) {

    // uninstall messages that are
    // 1) not there anymore and
    // 2) are not defined as custom
    if (!isset($defaults_lookup[$cached['message_id']]) && !($cached['custom'] & HEARTBEAT_MESSAGE_CUSTOM)) {
      heartbeat_messages_uninstall($cached['message_id'], 'message');
      unset($stored[$key]);
      $operations['deleted']++;
    }
    else {
      $stored_lookup[$cached['message_id']] = $cached;
    }
  }
  foreach ($defaults as $key => $default) {

    // Find new messages (if in defaults and not in stored)
    if (!isset($stored_lookup[$default['message_id']])) {
      heartbeat_message_insert($default);
      $stored[] = $default;
      $operations['inserted']++;
    }
    else {

      // Hacky code to avoid detecting revertable changes. The merge_separator_t was a dirty fix
      // resulting in more hacky stuff.
      // TODO Review the concept of translatable messages and their merging separators.
      $stored_concat_args = heartbeat_decode_message_variables($stored_lookup[$default['message_id']]['concat_args']);
      if (isset($stored_concat_args['merge_separator_t'])) {
        unset($stored_concat_args['merge_separator_t']);
      }
      if (isset($stored_concat_args['merge_end_separator_t'])) {
        unset($stored_concat_args['merge_end_separator_t']);
      }
      $stored_lookup[$default['message_id']]['concat_args'] = heartbeat_encode_message_variables($stored_concat_args);

      //$stored_lookup[$default['message_id']]['variables'] = heartbeat_decode_message_variables($stored_lookup[$default['message_id']]['variables']);

      //$stored_lookup[$default['message_id']]['concat_args'] = heartbeat_decode_message_variables($stored_lookup[$default['message_id']]['concat_args']);
      $default['variables'] = heartbeat_encode_message_variables($default['variables']);
      $default['concat_args'] = heartbeat_encode_message_variables($default['concat_args']);
      if (isset($default['attachments'])) {
        $default['attachments'] = serialize($default['attachments']);
      }
      $diffs = array_diff_assoc($default, $stored_lookup[$default['message_id']]);

      // TODO Check how to solve these diffs once and for all.
      // dsm($default['variables']); and dsm($stored_lookup[$default['message_id']]['variables']);
      // are never the same. Also lookat concat_args and custom.
      if (!empty($diffs)) {
        $operations['diffs'][$default['message_id']] = $diffs;
      }
    }
  }
  return $operations;
}