You are here

function views_bulk_operations_direct_process in Views Bulk Operations (VBO) 7.3

Processes the passed rows directly (without batching and queueing).

1 call to views_bulk_operations_direct_process()
views_bulk_operations_execute in ./views_bulk_operations.module
Entry point for executing the chosen operation upon selected rows.

File

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

Code

function views_bulk_operations_direct_process($operation, $rows, $options) {
  global $user;
  drupal_set_time_limit(0);

  // Prepare an array of status information. Imitates the Batch API naming
  // for consistency. Passed to views_bulk_operations_execute_finished().
  $context = array();
  $context['results']['progress'] = 0;
  $context['results']['log'] = array();
  if ($operation
    ->aggregate()) {

    // Load all entities.
    $entity_type = $operation->entityType;
    $entity_ids = array();
    foreach ($rows as $row_index => $row) {
      $entity_ids[] = $row['entity_id'];
    }
    $entities = _views_bulk_operations_entity_load($entity_type, $entity_ids, $options['revision']);
    $skip_permission_check = $operation
      ->getAdminOption('skip_permission_check');

    // Filter out entities that can't be accessed.
    foreach ($entities as $id => $entity) {
      if (!$skip_permission_check && !_views_bulk_operations_entity_access($operation, $entity_type, $entity, $user)) {
        $context['results']['log'][] = t('Skipped %operation on @type %title due to insufficient permissions.', array(
          '%operation' => $operation
            ->label(),
          '@type' => $entity_type,
          '%title' => entity_label($entity_type, $entity),
        ));
        unset($entities[$id]);
      }
    }

    // If there are any entities left, execute the operation on them.
    if ($entities) {
      $operation_context = array(
        'view_info' => $options['view_info'],
      );

      // Pass the selected rows to the operation if needed.
      if ($operation
        ->needsRows()) {
        $operation_context['rows'] = array();
        foreach ($rows as $row_index => $row) {
          $operation_context['rows'][$row_index] = $row['views_row'];
        }
      }
      $operation
        ->execute($entities, $operation_context);
    }
  }
  else {

    // Imitate a queue and process the entities one by one.
    $queue_item_data = array(
      'uid' => $user->uid,
      'arguments' => array(
        $rows,
        $operation,
        $options,
      ),
    );
    views_bulk_operations_queue_item_process($queue_item_data, $context['results']['log']);
  }
  $context['results']['progress'] += count($rows);
  $context['results']['log'][] = t('Performed %operation on @items.', array(
    '%operation' => $operation
      ->label(),
    '@items' => format_plural(count($rows), '1 item', '@count items'),
  ));
  views_bulk_operations_execute_finished(TRUE, $context['results'], array());
}