function views_bulk_operations_drush_execute in Views Bulk Operations (VBO) 7.3
Same name and namespace in other branches
- 6.3 views_bulk_operations.drush.inc \views_bulk_operations_drush_execute()
Implementation of 'vbo execute' command.
1 string reference to 'views_bulk_operations_drush_execute'
- views_bulk_operations_drush_command in ./
views_bulk_operations.drush.inc - Implementation of hook_drush_command().
File
- ./
views_bulk_operations.drush.inc, line 77
Code
function views_bulk_operations_drush_execute($vid = NULL, $operation_id = NULL) {
$args = func_get_args();
// 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_id)) {
drush_set_error('VIEWS_BULK_OPERATIONS_MISSING_OPERATION', dt('Please specify an operation to execute.'));
return;
}
$view_exposed_input = array();
$operation_arguments = array();
$view_arguments = array();
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],
)));
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;
default:
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'],
)));
return;
}
}
}
// Impersonate admin.
global $user;
$user = user_load(1);
drupal_save_session(FALSE);
// Load the view.
$view = views_get_view($vid);
if (!is_object($view)) {
_views_bulk_operations_report_error('Could not find view %vid.', array(
'%vid' => $vid,
));
return;
}
// Build the view, so that the VBO field can be found.
$view
->set_exposed_input($view_exposed_input);
$view
->set_arguments($view_arguments);
$view
->build();
$view->query
->set_limit(NULL);
// reset the work done by the pager
$view->query
->set_offset(NULL);
// Find the VBO field.
$vbo = _views_bulk_operations_get_field($view);
if (!$vbo) {
_views_bulk_operations_report_error('Could not find a VBO field in view %vid.', array(
'%vid' => $vid,
));
return;
}
$view
->execute();
// Find the selected operation.
$operations = $vbo
->get_selected_operations();
if (!isset($operations[$operation_id])) {
_views_bulk_operations_report_error('Could not find operation %operation_id in view %vid.', array(
'%operation_id' => $operation_id,
'%vid' => $vid,
));
return;
}
$operation = views_bulk_operations_get_operation($operation_id, $vbo
->get_entity_type(), $vbo
->get_operation_options($operation_id));
if ($operation_arguments) {
$operation->formOptions = $operation_arguments;
}
// Select all rows.
$rows = array();
$current = 1;
foreach ($view->result as $row_index => $result) {
$rows[$row_index] = array(
'entity_id' => $result->{$vbo->real_field},
'views_row' => array(),
'position' => array(
'current' => $current++,
'total' => $view->total_rows,
),
);
// Some operations require full selected rows.
if ($operation
->needsRows()) {
$rows[$row_index]['views_row'] = $result;
}
}
// Enqueue the fetched rows.
$queue_name = 'views_bulk_operations_active_queue_' . db_next_id();
$options = array(
'revision' => $vbo->revision,
'entity_load_capacity' => $vbo
->get_vbo_option('entity_load_capacity', 10),
);
views_bulk_operations_enqueue_rows($queue_name, $rows, $operation, $options);
// Process the queue using Batch API.
$batch = array(
'file' => drupal_get_path('module', 'views_bulk_operations') . '/views_bulk_operations.module',
'operations' => array(
array(
'views_bulk_operations_active_queue_process',
array(
$queue_name,
$operation,
$vbo->view->total_rows,
),
),
),
'finished' => '_views_bulk_operations_execute_finished',
'title' => t('Performing %operation on the selected items...', array(
'%operation' => $operation
->label(),
)),
);
batch_set($batch);
drush_backend_batch_process();
// Looks like drush has no way to show messages set in subprocesses,
// so all batch messages get lost. Setting a success message manually here.
drush_log(dt('Performed "!operation" on @items.', array(
'!operation' => $operation
->label(),
'@items' => format_plural($vbo->view->total_rows, '1 item', '@count items'),
)), 'ok');
}