You are here

function _views_bulk_operations_execute_multiple in Views Bulk Operations (VBO) 6

Helper function for multiple execution operations.

1 call to _views_bulk_operations_execute_multiple()
_views_bulk_operations_execute in ./views_bulk_operations.module
Helper function to execute the chosen action upon selected objects.
1 string reference to '_views_bulk_operations_execute_multiple'
_views_bulk_operations_execute in ./views_bulk_operations.module
Helper function to execute the chosen action upon selected objects.

File

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

Code

function _views_bulk_operations_execute_multiple($base_field, $operation, $objects, $params, $object_info, $batch, &$context) {

  // Setup our batch process.
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($objects);
  }
  if (empty($context['results']['time'])) {
    $context['results']['time'] = microtime(TRUE);
    $context['results']['rows'] = 0;
  }
  if ($operation['aggregate'] != VBO_AGGREGATE_FORBIDDEN) {
    $oids = array();
    while ($row = _views_bulk_operations_execute_next($context['sandbox']['progress'], $objects, $batch)) {
      $context['sandbox']['progress']++;
      $oid = $row->{$base_field};
      if (isset($object_info['access'])) {
        $object = call_user_func($object_info['load'], $oid);
        if (!$object) {
          unset($objects[$num]);
          $context['results']['log'][] = t('Skipped %operation on @type %oid because it was not found.', array(
            '%operation' => $operation['label'],
            '@type' => t($operation['type']),
            '%oid' => $oid,
          ));
          continue;
        }
        if (!_views_bulk_operations_object_permission($operation, $object, $object_info)) {
          unset($objects[$num]);
          $context['results']['log'][] = t('Skipped %operation on @type %title due to insufficient permissions.', array(
            '%operation' => $operation['label'],
            '@type' => t($object_info['type']),
            '%title' => $object->{$object_info['title']},
          ));
          continue;
        }
      }
      $oids[] = $oid;
    }
    if (!empty($objects)) {
      _views_bulk_operations_action_aggregate_do($operation, $oids, $objects, $params, $object_info);
      $context['results']['log'][] = t('Performed aggregate %operation on @types %oids.', array(
        '%operation' => $operation['label'],
        '@types' => format_plural(count($objects), $object_info['type'], $object_info['type'] . 's'),
        '%oids' => implode(',', $oids),
      ));
      $context['message'] = t('Performed aggregate %operation on !count @types.', array(
        '%operation' => $operation['label'],
        '!count' => count($objects),
        '@types' => format_plural(count($objects), $object_info['type'], $object_info['type'] . 's'),
      ));
      $context['results']['rows'] += count($objects);
    }
  }
  else {
    $oids = array();
    while ($row = _views_bulk_operations_execute_next($context['sandbox']['progress'], $objects, $batch)) {
      $context['sandbox']['progress']++;
      $oid = $row->{$base_field};
      $object = call_user_func($object_info['load'], $oid);
      if (!$object) {
        $context['results']['log'][] = t('Skipped %operation on @type id %oid because it was not found.', array(
          '%operation' => $operation['label'],
          '@type' => t($operation['type']),
          '%oid' => $oid,
        ));
        continue;
      }
      if (!_views_bulk_operations_object_permission($operation, $object, $object_info)) {
        $context['results']['log'][] = t('Skipped %operation on @type %title due to insufficient permissions.', array(
          '%operation' => $operation['label'],
          '@type' => t($object_info['type']),
          '%title' => $object->{$object_info['title']},
        ));
        continue;
      }
      _views_bulk_operations_action_do($operation, $oid, $object, $row, $params, $object_info);
      $context['results']['log'][] = t('Performed %operation on @type %title.', array(
        '%operation' => $operation['label'],
        '@type' => t($object_info['type']),
        '%title' => $object->{$object_info['title']},
      ));
      $context['results']['rows'] += 1;
      $oids[] = $oid;
    }
    $context['message'] = t('Performed %operation on !count @types.', array(
      '%operation' => $operation['label'],
      '!count' => count($oids),
      '@types' => format_plural(count($oids), $object_info['type'], $object_info['type'] . 's'),
    ));
  }

  // Update batch progress.
  $context['finished'] = empty($context['sandbox']['max']) ? 1 : $context['sandbox']['progress'] / $context['sandbox']['max'];
}