You are here

function privatemsg_operation_execute in Privatemsg 6

Same name and namespace in other branches
  1. 6.2 privatemsg.module \privatemsg_operation_execute()
  2. 7.2 privatemsg.module \privatemsg_operation_execute()
  3. 7 privatemsg.module \privatemsg_operation_execute()

Execute an operation on a number of threads.

Parameters

$operation: The operation that should be executed. @see hook_privatemsg_thread_operations()

$threads: An array of thread ids. The array is filtered before used, a checkboxes array can be directly passed to it.

3 calls to privatemsg_operation_execute()
privatemsg_filter_add_tag_submit in privatemsg_filter/privatemsg_filter.module
Form callback for adding a tag to threads.
privatemsg_filter_remove_tag_submit in privatemsg_filter/privatemsg_filter.module
Form callback for removing a tag to threads.
privatemsg_list_submit in ./privatemsg.module
Process privatemsg_list form submissions.

File

./privatemsg.module, line 2408
Allows users to send private messages to other users.

Code

function privatemsg_operation_execute($operation, $threads, $account = null) {

  // Filter out unchecked threads, this gives us an array of "checked" threads.
  $threads = array_filter($threads);
  if (empty($threads)) {

    // Do not execute anything if there are no checked threads.
    return;
  }

  // Add in callback arguments if present.
  if (isset($operation['callback arguments'])) {
    $args = array_merge(array(
      $threads,
    ), $operation['callback arguments']);
  }
  else {
    $args = array(
      $threads,
    );
  }

  // Add the user object to the arguments.
  if ($account) {
    $args[] = $account;
  }

  // Execute the chosen action and pass the defined arguments.
  call_user_func_array($operation['callback'], $args);

  // Check if that operation has defined an undo callback.
  if (isset($operation['undo callback']) && ($undo_function = $operation['undo callback'])) {

    // Add in callback arguments if present.
    if (isset($operation['undo callback arguments'])) {
      $undo_args = array_merge(array(
        $threads,
      ), $operation['undo callback arguments']);
    }
    else {
      $undo_args = array(
        $threads,
      );
    }

    // Avoid saving the complete user object in the session.
    if ($account) {
      $undo_args['account'] = $account->uid;
    }

    // Store the undo callback in the session and display a "Undo" link.
    // @todo: Provide a more flexible solution for such an undo action, operation defined string for example.
    $_SESSION['privatemsg']['undo callback'] = array(
      'function' => $undo_function,
      'args' => $undo_args,
    );
    $undo = url('messages/undo/action', array(
      'query' => drupal_get_destination(),
    ));
    drupal_set_message(t('The previous action can be <a href="!undo">undone</a>.', array(
      '!undo' => $undo,
    )));
  }
}