Skip to content
views_bulk_operations.drush.inc 4.52 KiB
Newer Older
Karim Ratib's avatar
Karim Ratib committed
<?php

/**
 * Implementation of hook_drush_help().
 */
function views_bulk_operations_drush_help($section) {
  switch ($section) {
Karim Ratib's avatar
Karim Ratib committed
      return dt('List all Views Bulk Operations (VBO) views, along with the operations associated with each.');
Karim Ratib's avatar
Karim Ratib committed
      return dt('Execute a bulk operation based on a Views Bulk Operations (VBO) view.');
  }
}

/**
 * Implementation of hook_drush_command().
 */
function views_bulk_operations_drush_command() {
  $items['vbo-list'] = array(
Karim Ratib's avatar
Karim Ratib committed
    'callback' => 'views_bulk_operations_drush_list',
    'description' => 'List all Views Bulk Operations (VBO) views, along with the operations associated with each.',
  );
  $items['vbo-execute'] = array(
Karim Ratib's avatar
Karim Ratib committed
    'callback' => 'views_bulk_operations_drush_execute',
    'description' => 'Execute a bulk operation based on a Views Bulk Operations (VBO) view.',
    'arguments' => array(
      'vid' => 'ID or name of the view to be executed.',
      'operation' => 'Callback name of the operation to be applied on the view results.',
      'type:[name=]value ...' => 'Parameters to be passed as view input filters, view arguments or operation arguments, where type is respectively {input, argument, operation}.',
Karim Ratib's avatar
Karim Ratib committed
    ),
    'examples' => array(
      '$ drush vbo-execute admin_content node_publish_action' =>
        'Publish nodes returned by view admin_content.',
      '$ drush vbo-execute 44 node_assign_owner_action operation:owner_uid=3' =>
        'Change node ownership on nodes returned by view #44, passing argument owner_uid=3 to the action.',
      '$ drush vbo-execute admin_content node_unpublish_action input:type=expense argument:3' =>
        'Unpublish nodes returned by view admin_content, filtering results of type expense and passing value 3 as first view argument.',
Karim Ratib's avatar
Karim Ratib committed
    ),
  );
  return $items;
}

/**
 * Implementation of 'vbo list' command.
 */
function views_bulk_operations_drush_list() {
  // Impersonate admin.
  global $user;
  $user = user_load(1);
  drupal_save_session(FALSE);

  // Find all VBO views and their associated operations.
  $rows = array(array(sprintf('%5s', dt('View ID')), dt('Name'), dt('Description'), dt('Operations')));
Karim Ratib's avatar
Karim Ratib committed
  foreach (views_get_all_views() as $name => $view) {
    $view->build();
    $vbo = _views_bulk_operations_get_field($view);
    if ($vbo) {
      $operations = array();
      foreach (array_filter($vbo->options['vbo']['selected_operations']) as $operation) {
        if (isset($vbo->all_operations[$operation])) {
          $operations[] = $vbo->all_operations[$operation]['label'] .' ('. $operation .')';
      $operations[] = "---------------";
      $rows[] = array(
        sprintf('%5d', $view->vid),
        $view->name,
        $view->description,
        implode("\n", $operations),
      );
Karim Ratib's avatar
Karim Ratib committed
    }
  }
  drush_print_table($rows, TRUE);
}

/**
 * Implementation of 'vbo execute' command.
Karim Ratib's avatar
Karim Ratib committed
function views_bulk_operations_drush_execute($vid = NULL, $operation = NULL) {
  // Parse arguments.
  if (is_null($vid)) {
    drush_set_error('VIEWS_BULK_OPERATIONS_MISSING_VID', dt('Please specify a view ID to execute.'));
    return;
  }
  if (is_null($operation)) {
    drush_set_error('VIEWS_BULK_OPERATIONS_MISSING_OPERATION', dt('Please specify an operation to execute.'));
    return;
  }
  $args = func_get_args();
  $view_exposed_input = array();
  $operation_arguments = array();
  $view_arguments = array();
Karim Ratib's avatar
Karim Ratib committed
  if (count($args) > 2) for ($i=2; $i<count($args); $i++) {
    $parts = array();
    if (FALSE === preg_match('/^(?<type>\w+):(?:(?<name>\w+)=)?(?<value>(.*?))$/', $args[$i], $parts)) {
      drush_set_error('VIEWS_BULK_OPERATIONS_INVALID_PARAMETER', dt('The parameter %arg should be of the form type:[name=]value where type in {input, argument, operation}.', array('%arg' => $args[$i])));
Karim Ratib's avatar
Karim Ratib committed
      return;
    }
    switch ($parts['type']) {
      case 'input':
        $view_exposed_input[$parts['name']] = $parts['value'];
        break;
      case 'operation':
        $operation_arguments[$parts['name']] = $parts['value'];
        break;
      case 'argument':
        $view_arguments[] = $parts['value'];
        break;
        drush_set_error('VIEWS_BULK_OPERATIONS_UNKNOWN_PARAMETER', dt('The parameter type %type is unknown. Please specify either input, argument or operation.', array('%type' => $parts['type'])));
Karim Ratib's avatar
Karim Ratib committed
  // Impersonate admin.
  global $user;
Karim Ratib's avatar
Karim Ratib committed

  // Execute the VBO.
  views_bulk_operations_execute($vid, $operation, $operation_arguments, $view_exposed_input, $view_arguments);