You are here

function _views_bulk_operations_execute in Views Bulk Operations (VBO) 6

Same name and namespace in other branches
  1. 6.3 views_bulk_operations.module \_views_bulk_operations_execute()

Helper function to execute the chosen action upon selected objects.

2 calls to _views_bulk_operations_execute()
views_bulk_operations_execute in ./views_bulk_operations.module
API function to programmatically invoke a VBO.
views_bulk_operations_form_submit in ./views_bulk_operations.module
Form submit function for views_bulk_operations_form().

File

./views_bulk_operations.module, line 1448
Allows operations to be performed on items selected in a view.

Code

function _views_bulk_operations_execute($view, $objects, $operation, $operation_arguments, $options) {
  global $user;

  // Get the object info we're dealing with.
  $object_info = _views_bulk_operations_object_info_for_view($view);
  if (!$object_info) {
    return;
  }

  // Add action arguments.
  $params = array();
  if ($operation['configurable'] && is_array($operation_arguments)) {
    $params += $operation_arguments;
  }

  // Add static callback arguments. Note that in the case of actions, static arguments
  // are picked up from the database in actions_do().
  if (isset($operation['callback arguments'])) {
    $params += $operation['callback arguments'];
  }

  // Add this view as parameter.
  $params['view'] = array(
    'vid' => !empty($view->vid) ? $view->vid : $view->name,
    'exposed_input' => $view
      ->get_exposed_input(),
    'arguments' => $view->args,
  );

  // Add static settings to the params.
  if (!empty($options['settings'])) {
    $params['settings'] = $options['settings'];
  }

  // Add object info to the params.
  $params['object_info'] = $object_info;
  if ($operation['aggregate'] != VBO_AGGREGATE_FORCED && $options['execution_type'] == VBO_EXECUTION_BATCH) {

    // Save the options in the session because Batch API doesn't give a way to
    // send a parameter to the finished callback.
    $_SESSION['vbo_options']['display_result'] = $options['display_result'];
    $_SESSION['vbo_options']['operation'] = $operation;
    $_SESSION['vbo_options']['params'] = $params;
    $_SESSION['vbo_options']['object_info'] = $object_info;
    $batch = array(
      'title' => t('Performing %operation on selected items...', array(
        '%operation' => $operation['label'],
      )),
      'finished' => '_views_bulk_operations_execute_finished',
    );

    // If they have max performance checked, use the high performant batch process.
    if ($options['max_performance']) {
      $batch += array(
        'operations' => array(
          array(
            '_views_bulk_operations_execute_multiple',
            array(
              $view->base_field,
              $operation,
              $objects,
              $params,
              $object_info,
              TRUE,
            ),
          ),
        ),
      );
    }
    else {
      $operations = array();
      foreach ($objects as $num => $row) {
        $oid = $row->{$view->base_field};
        $operations[] = array(
          '_views_bulk_operations_execute_single',
          array(
            $oid,
            $row,
          ),
        );
      }
      $batch += array(
        'operations' => $operations,
      );
    }
    batch_set($batch);
  }
  else {
    if ($operation['aggregate'] != VBO_AGGREGATE_FORCED && module_exists('drupal_queue') && $options['execution_type'] == VBO_EXECUTION_QUEUE) {
      drupal_queue_include();
      foreach ($objects as $row) {
        $oid = $row->{$view->base_field};
        $job = array(
          'description' => t('Perform %operation on @type %oid.', array(
            '%operation' => $operation['label'],
            '@type' => t($object_info['type']),
            '%oid' => $oid,
          )),
          'arguments' => array(
            $oid,
            $row,
            $operation,
            $params,
            $user->uid,
            $options['display_result'],
            $object_info,
          ),
        );
        $queue = DrupalQueue::get('views_bulk_operations');
        $queue
          ->createItem($job);
        $oids[] = $oid;
      }
      if ($options['display_result']) {
        drupal_set_message(t('Enqueued %operation on @types %oid.', array(
          '%operation' => $operation['label'],
          '@types' => format_plural(count($objects), $object_info['type'], $object_info['type'] . 's'),
          '%oid' => implode(', ', $oids),
        )));
      }
    }
    else {

      /*if ($options['execution_type'] == VBO_EXECUTION_DIRECT)*/
      @set_time_limit(0);
      $context['results']['rows'] = 0;
      $context['results']['time'] = microtime(TRUE);
      _views_bulk_operations_execute_multiple($view->base_field, $operation, $objects, $params, $object_info, FALSE, $context);
      _views_bulk_operations_execute_finished(TRUE, $context['results'], array(), $options + array(
        'operation' => $operation,
        'params' => $params,
      ));
    }
  }
}