You are here

function views_bulk_operations_form_submit in Views Bulk Operations (VBO) 5

Same name and namespace in other branches
  1. 6.3 views_bulk_operations.module \views_bulk_operations_form_submit()
  2. 6 views_bulk_operations.module \views_bulk_operations_form_submit()
  3. 7.3 views_bulk_operations.module \views_bulk_operations_form_submit()

File

./views_bulk_operations.module, line 333
Allow bulk node operations directly within views.

Code

function views_bulk_operations_form_submit($form_id, $form_values) {
  switch ($form_values['step']) {
    case VIEWS_BULK_OPS_STEP_CONFIRM:
      $nodes = unserialize($form_values['nodes']);
      $preserved_action_form_elements = unserialize($form_values['preserved_action_form_elements']);
      if (!is_array($nodes)) {
        return;
      }
      break;
    case VIEWS_BULK_OPS_STEP_SINGLE:
      $nodes = $form_values['nodes'];
      $preserved_action_form_elements = $form_values;
      break;
    default:
      return;
  }
  $operations = _views_bulk_operations_get_operations('enabled', $form_values['vid']);
  $operation = $operations[$form_values['action']];

  // Check if the user selected all nodes.
  if ($nodes['select_all']) {
    $view = module_invoke('views', 'get_view', $form_values['vid']);
    $items = views_build_view('items', $view);
    $nodes = array();
    foreach ($items['items'] as $node) {
      $nodes[$node->nid] = $node->nid;
    }
  }
  else {
    unset($nodes['select_all']);
  }
  $nodes = array_filter($nodes);

  // Set parameters.
  $params = array();
  if ($operation['configurable']) {
    $params += _views_bulk_operations_action_submit($operation, $form_id, $preserved_action_form_elements);
  }
  else {
    if (is_array($preserved_action_form_elements)) {
      $params += $preserved_action_form_elements;
    }
  }
  if ($operation['type'] == 'node' && isset($operation['callback arguments'])) {
    $params += $operation['callback arguments'];
  }

  // FIXME Hack to force actions_do() to process any number of invocations.
  // Check http://drupal.org/node/290282 to understand more.
  variable_set('actions_max_stack', 10000000);
  if (module_exists('job_queue') && variable_get('vbo_queue_' . $form_values['vid'], 0)) {

    // Deferred execution
    foreach ($nodes as $nid) {
      $node = node_load($nid);
      if (!$node) {
        continue;
      }
      global $user;
      job_queue_add('_views_bulk_operations_queue_process', t('Perform %action on node %title.', array(
        '%action' => $operation['label'],
        '%title' => $node->title,
      )), array(
        $operation,
        $node,
        $params,
        $user->uid,
      ));
    }
    drupal_set_message(t('Enqueued %action on nodes %nid. Check the <a href="@queue">queued jobs page</a>.', array(
      '%action' => $operation['label'],
      '%nid' => implode(', ', $nodes),
      '@queue' => url('admin/logs/job_queue'),
    )));
  }
  else {

    // Direct execution
    set_time_limit(0);
    if ($operation['type'] == 'node') {

      // Add node_access() check.
      foreach ($nodes as $i => $nid) {
        $node = node_load(array(
          'nid' => $nid,
        ));
        if (!node_access('update', $node)) {
          unset($nodes[$i]);
          drupal_set_message(t('Skipped %action on node %title due to insufficient permissions.', array(
            '%action' => $operation['label'],
            '%title' => $node->title,
          )));
        }
      }
      if (!empty($nodes)) {
        $args = array_merge(array(
          $nodes,
        ), $params);
        call_user_func_array($operation['callback'], $args);
        cache_clear_all();
        drupal_set_message(t('Performed %action on nodes %nid.', array(
          '%action' => $operation['label'],
          '%nid' => implode(', ', $nodes),
        )));
      }
    }
    else {
      if ($operation['type'] == 'action') {
        foreach ($nodes as $nid) {
          $node = node_load(array(
            'nid' => $nid,
          ));
          if (!$node) {
            continue;
          }

          // Add node_access() check.
          if (!node_access('update', $node)) {
            drupal_set_message(t('Skipped %action on node %title due to insufficient permissions.', array(
              '%action' => $operation['label'],
              '%title' => $node->title,
            )));
            continue;
          }
          _views_bulk_operations_action_do($operation, $node, $params);
          drupal_set_message(t('Performed %action on node %title.', array(
            '%action' => $operation['label'],
            '%title' => $node->title,
          )));
        }
      }
    }
  }
  drupal_goto($_GET['q'], drupal_query_string_encode($_GET, array(
    'q',
  )));
}