You are here

function csm_message_alter in Custom Submit Messages 7

Same name and namespace in other branches
  1. 6 csm.module \csm_message_alter()

Implements hook_message_alter().

File

./csm.module, line 462
The main module file for Custom Submit Messages.

Code

function csm_message_alter(&$messages) {

  // If $messages->messages['status'] doesn't exist then there are no status
  // messages and there's nothing to do
  if (!isset($messages->messages['status'])) {
    if (isset($_SESSION['csm'])) {
      unset($_SESSION['csm']);
    }
    return;
  }

  // Search for submit messages and change any that are found
  // First create arrays containing messages that may need to be changed.
  // Because the messages might not be in English, we need to load the node
  // first. We can then build its create, update, and delete messages in t()
  // and check the output against the message we are looking to change.
  if (!isset($_SESSION['csm'])) {
    return;
  }
  foreach ($_SESSION['csm'] as $key => $nid) {
    $node = variable_get('csm_node_temp_' . $nid, NULL);
    $args = array(
      '@type' => node_type_get_name($node),
      '%title' => $node->title,
    );
    $created = $messages
      ->contains(t('@type %title has been created.', $args), 'status');
    $updated = $messages
      ->contains(t('@type %title has been updated.', $args), 'status');
    $deleted = $messages
      ->contains(t('@type %title has been deleted.', $args), 'status');

    // Then parse through each array, double-checking for messages that need to be
    // changed and then changing them.
    // This code is no longer necessary because the matching technique (above) is
    // tighter, but it is retained so that $relevant_messages is created and can
    // be parsed later on. This could be rewritten so that $created, $updated, and
    // $deleted are parsed directly.
    $message_types_to_check = array(
      'created',
      'updated',
      'deleted',
    );
    foreach ($message_types_to_check as $delta => $type) {
      if (${$type} == FALSE) {
        continue;

        // There are no messages of type $$type so nothing to check
      }
      else {

        // Double check the messages
        foreach (${$type}['status'] as $delta_2 => $message_array) {
          $relevant_messages[] = $message_array['index'];
        }
      }
    }
    if (!isset($relevant_messages)) {

      // No relevant messages so nothing to change. There might be some stray data
      // left if the variable table, though, and also some non-visible messages,
      // so let's delete that.
      if (array_key_exists('csm', $_SESSION)) {
        unset($_SESSION['csm']);
      }
      return;
    }
  }

  // Change the messages:
  foreach ($relevant_messages as $delta => $index) {
    variable_del('csm_node_temp_' . $nid);

    // Check the active language and user then load the msg based on that.
    global $language;
    global $user;
    $relevant_roles = _csm_relevant_role_array($user);
    foreach ($relevant_roles as $key => $role) {
      $message = variable_get('csm_' . $node->op . '_msg_' . $language->language . '_' . $role . '_' . $node->type, '');
      if ($message == "") {

        // message hasn't been set for this rid, so check the next rid
        continue;
      }
      else {

        // message has been set for this rid, so we can stop searching for a
        // message
        break;
      }
    }
    if ($message) {
      if ($message == '<none>') {

        // kill the message
        unset($messages->messages['status'][$delta]);
        $messages->remove_used = TRUE;
        $messages
          ->clean();
      }
      else {
        $message = $message == '<none>' ? '' : token_replace($message, array(
          'node' => $node,
        ));
        $messages->messages['status'][$delta] = t($message);
      }
    }
    unset($message);

    // Log a system message.
    watchdog('csm', '@type: node @msg_type message changed using Custom Submit Messages.', array(
      '@type' => $node->type,
      '@msg_type' => $node->op,
    ), WATCHDOG_NOTICE);
    return;
  }
}