You are here

function views_bulk_operations_action_form in Views Bulk Operations (VBO) 6

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

Form function for views_bulk_operations_action action.

1 call to views_bulk_operations_action_form()
views_bulk_operations_rules_action_form in ./views_bulk_operations.rules.inc

File

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

Code

function views_bulk_operations_action_form($context) {

  // Some views choke on being rebuilt at this moment because of validation errors in the action form.
  // So we save the error state, reset it, build the views, then reinstate the errors.
  // Also unset the error messages because they'll be displayed again after the loop.
  $errors = form_get_errors();
  if (!empty($errors)) {
    foreach ($errors as $message) {
      unset($_SESSION['messages']['error'][array_search($message, $_SESSION['messages']['error'])]);
    }
  }
  form_set_error(NULL, '', TRUE);

  // Look for all views with VBO styles, and for each find the operations they use.
  // Distinguish between overridden and default views to simplify export.
  $views[0] = t('- Choose a view -');
  $operations[0] = t('- Choose an operation -');
  foreach (views_get_all_views() as $name => $view) {
    foreach (array_keys($view->display) as $display) {
      $display_options =& $view->display[$display]->display_options;
      if (isset($display_options['style_plugin']) && $display_options['style_plugin'] == 'bulk') {
        $vid = $view->name;
        $views[$vid] = $view->name . (!empty($view->description) ? ': ' . $view->description : '');
        $view_clone = clone $view;
        $style_plugin = views_get_plugin('style', $display_options['style_plugin']);
        $style_plugin
          ->init($view_clone, $view_clone->display[$display], $display_options['style_options']);
        if (isset($context['view_vid']) && $vid == $context['view_vid']) {
          $form['#plugin'] = $style_plugin;
        }
        unset($view_clone);
        if (!empty($display_options['style_options']['operations'])) {
          foreach ($display_options['style_options']['operations'] as $key => $options) {
            if (empty($options['selected'])) {
              continue;
            }
            $operations[$key] = $views_operations[$vid][$key] = $style_plugin->all_operations[$key]['label'];
            if (isset($context['operation_key']) && isset($context['view_vid']) && $key == $context['operation_key'] && $vid == $context['view_vid']) {
              $form['#operation'] = $style_plugin
                ->get_operation_info($key);
            }
          }
        }
      }
    }
  }
  if (!empty($errors)) {
    foreach ($errors as $name => $message) {
      form_set_error($name, $message);
    }
  }
  drupal_add_js(array(
    'vbo' => array(
      'action' => array(
        'views_operations' => $views_operations,
      ),
    ),
  ), 'setting');
  drupal_add_js(drupal_get_path('module', 'views_bulk_operations') . '/js/views_bulk_operations.action.js');
  $form['view_vid'] = array(
    '#type' => 'select',
    '#title' => t('View'),
    '#description' => t('Select the VBO to be executed.'),
    '#options' => $views,
    '#default_value' => @$context['view_vid'],
    '#attributes' => array(
      'onchange' => 'Drupal.vbo.action.updateOperations(this.options[this.selectedIndex].value, true);',
    ),
  );
  $form['operation_key'] = array(
    '#type' => 'select',
    '#title' => t('Operation'),
    '#description' => t('Select the operation to be executed.'),
    '#options' => $operations,
    '#default_value' => @$context['operation_key'],
    '#ahah' => array(
      'path' => 'views-bulk-operations/js/action',
      'wrapper' => 'operation-wrapper',
      'method' => 'replace',
    ),
  );
  $form['operation_arguments'] = array(
    '#type' => 'fieldset',
    '#title' => t('Operation arguments'),
    '#description' => t('If the selected action is configurable, this section will show the action\'s arguments form,
                         followed by a text field where a PHP script can be entered to programmatically assemble the arguments.
                        '),
  );
  $form['operation_arguments']['wrapper'] = array(
    '#type' => 'markup',
    '#value' => '',
    '#prefix' => '<div id="operation-wrapper">',
    '#suffix' => '</div>',
  );
  if (isset($form['#operation']) && $form['#operation']['configurable'] && isset($form['#plugin'])) {
    $form['operation_arguments']['wrapper']['operation_form'] = _views_bulk_operations_action_form($form['#operation'], $form['#plugin']->view, NULL, $form['#operation']['options']['settings'], $context);
    if (!empty($form['#operation']['form properties'])) {
      foreach ($form['#operation']['form properties'] as $property) {
        if (isset($form['operation_arguments']['wrapper']['operation_form'][$property])) {
          $form[$property] = $form['operation_arguments']['wrapper']['operation_form'][$property];
        }
      }
    }
    $form['operation_arguments']['wrapper']['operation_arguments'] = array(
      '#type' => 'textarea',
      '#title' => t('Operation arguments'),
      '#description' => t('Enter PHP script that will assemble the operation arguments (and will override the arguments above).
                           These arguments should be of the form: <code>return array(\'argument1\' => \'value1\', ...);</code>
                           and they should correspond to the values returned by the action\'s form submit function.
                           The variables <code>&$object</code> and <code>$context</code> are available to this script.
                          '),
      '#default_value' => @$context['operation_arguments'],
    );
  }
  else {
    $form['operation_arguments']['wrapper']['operation_form'] = array(
      '#type' => 'markup',
      '#value' => t('This operation is not configurable.'),
    );
    $form['operation_arguments']['wrapper']['operation_arguments'] = array(
      '#type' => 'value',
      '#value' => '',
    );
  }
  $form['view_exposed_input'] = array(
    '#type' => 'textarea',
    '#title' => t('View exposed input'),
    '#description' => t('Enter PHP script that will assemble the view exposed input (if the view accepts exposed input).
                         These inputs should be of the form: <code>return array(\'input1\' => \'value1\', ...);</code>
                         and they should correspond to the query values used on the view URL when exposed filters are applied.
                         The variables <code>&$object</code> and <code>$context</code> are available to this script.
                        '),
    '#default_value' => @$context['view_exposed_input'],
  );
  $form['view_arguments'] = array(
    '#type' => 'textarea',
    '#title' => t('View arguments'),
    '#description' => t('Enter PHP script that will assemble the view arguments (if the view accepts arguments).
                         These arguments should be of the form: <code>return array(\'value1\', ...);</code>
                         and they should correspond to the arguments defined in the view.
                         The variables <code>&$object</code> and <code>$context</code> are available to this script.
                        '),
    '#default_value' => @$context['view_arguments'],
  );
  $form['respect_limit'] = array(
    '#type' => 'checkbox',
    '#title' => t('Respect the view\'s item limit'),
    '#default_value' => @$context['respect_limit'],
  );
  return $form;
}