',
);
break;
case VIEWS_BULK_OPS_STEP_CONFIG:
$operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['operation']);
$form['operation'] = array('#type' => 'value', '#value' => $operation);
$form += _views_bulk_operations_action_form(
$operation,
$plugin->view,
array_filter($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['objects']['selection']),
$plugin->get_operation_settings($operation)
);
$form['execute'] = array(
'#type' => 'submit',
'#value' => t('Next'),
'#weight' => 98,
);
$query = drupal_query_string_encode($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['exposed_input']);
$form['cancel'] = array(
'#type' => 'markup',
'#value' => t('Cancel', array('@view' => url($_GET['q'], array('query' => $query)))),
'#weight' => 99,
);
drupal_set_title(t('Set parameters for %action', array('%action' => $operation['label'])));
break;
case VIEWS_BULK_OPS_STEP_CONFIRM:
$operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['operation']);
$query = drupal_query_string_encode($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['exposed_input']);
$objects = $form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['objects']['selection'];
$form = confirm_form($form,
t('Are you sure you want to perform %action on selected rows?', array('%action' => $operation['label'])),
array('path' => $_GET['q'], 'query' => $query),
theme('views_bulk_operations_confirmation', $objects, $plugin->view)
);
break;
}
// Use views_bulk_operations_form_submit() for form submit, regardless of form_id.
$form['#submit'][] = 'views_bulk_operations_form_submit';
$form['#validate'][] = 'views_bulk_operations_form_validate';
$form['#cache'] = TRUE;
return $form;
}
/**
* Implementation of hook_form_alter().
*
* Usability improvements to standard "Send e-mail" action.
*/
function views_bulk_operations_form_alter(&$form, $form_state, $form_id) {
if (strpos($form_id, 'views_bulk_operations_form') === 0) {
if (isset($form['operation']) && ($form['operation']['#value']['callback'] == 'system_send_email_action' || $form['operation']['#value'] == 'system_send_email_action')) {
// Hide recipient field, as we have already chosen it from user list.
$form['recipient']['#default_value'] = '%author';
$form['recipient']['#type'] = 'hidden';
}
}
}
/**
* Validate the selected operation.
*
* @see views_bulk_operations_form()
*/
function views_bulk_operations_form_validate($form, &$form_state) {
switch ($form_state['values']['step']) {
case VIEWS_BULK_OPS_STEP_VIEW:
$_SESSION['vbo_values'][$_GET['q']] = $form_state['values'];
if (!array_filter($form_state['values']['objects']['selection'])) { // If all 0, no row selected
form_set_error('objects', t('No row selected. Please select one or more rows.'));
}
if (!empty($form_state['clicked_button']['#hash'])) {
$form_state['values']['operation'] = $form_state['clicked_button']['#hash'];
}
if (!$form_state['values']['operation']) { // No action selected
form_set_error('operation', t('No operation selected. Please select an operation to perform.'));
}
break;
case VIEWS_BULK_OPS_STEP_SINGLE:
$_SESSION['vbo_values'][$_GET['q']] = $form_state['values'];
if (!array_filter($form_state['values']['objects']['selection'])) { // If all 0, no row selected
form_set_error('objects', t('No row selected. Please select one or more rows.'));
}
$plugin = $form['#plugin'];
$operation = $plugin->get_operation_info($form_state['values']['operation']);
if ($operation['configurable']) {
_views_bulk_operations_action_validate($operation, $form, $form_state);
}
break;
case VIEWS_BULK_OPS_STEP_CONFIG:
$plugin = $form['#plugin'];
$operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['operation']);
_views_bulk_operations_action_validate($operation, $form, $form_state);
break;
}
}
/**
* Helper function to adjust the selected set of nodes based on different situations.
*/
function _views_bulk_operations_adjust_selection(&$selection, $select_all, $exposed_input, $arguments, $plugin) {
if ($select_all) {
// Adjust selection to select all nodes across pages.
$view = views_get_view($plugin->view->vid ? $plugin->view->vid : $plugin->view->name);
$view->set_exposed_input($exposed_input);
$view->set_arguments($arguments);
$view->build($plugin->view->current_display);
$view->query->set_limit(NULL); // reset the work done by the pager
$view->query->set_offset(NULL);
// HACK for date_api_filter_handler: set $_GET with the exposed_input.
$_GET += $exposed_input;
$view->execute($plugin->view->current_display);
$selection = array();
foreach ($view->result as $num => $result) {
$selection[$num + 1] = $result;
}
}
else {
// Adjust selection to filter out previous selections.
$results = array();
foreach ($plugin->view->result as $num => $result) {
if ($selection[$num + 1]) {
$results[$num + 1] = $result;
}
}
$selection = $results;
}
// Adjust sticky selection accordingly.
$_SESSION['vbo_values'][$_GET['q']]['objects'] = array('selection' => $selection, 'select_all' => $select_all);
}
/**
* Submit handler for the selected operation.
*
* @see views_bulk_operations_form()
*/
function views_bulk_operations_form_submit($form, &$form_state) {
$plugin = $form['#plugin'];
switch ($form_state['values']['step']) {
case VIEWS_BULK_OPS_STEP_VIEW:
$form_state['storage']['step'] = $form_state['values']['step'];
$form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW] = $form_state['values'];
_views_bulk_operations_adjust_selection(
$form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['objects']['selection'],
$form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['objects']['select_all'],
$form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['exposed_input'],
$form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['arguments'],
$plugin
);
$operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['operation']);
if (!$operation['configurable'] && $plugin->options['skip_confirmation']) {
break; // Go directly to execution
}
return;
case VIEWS_BULK_OPS_STEP_SINGLE:
$form_state['storage']['step'] = $form_state['values']['step'];
$form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW] = $form_state['values'];
$form_state['storage'][VIEWS_BULK_OPS_STEP_CONFIG] = $form_state['values']; // we're not taking any chances
_views_bulk_operations_adjust_selection(
$form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['objects']['selection'],
$form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['objects']['select_all'],
$form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['exposed_input'],
$form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['arguments'],
$plugin
);
$operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['operation']);
if ($operation['configurable']) {
$form_state['storage']['operation_arguments'] = _views_bulk_operations_action_submit($operation, $form, $form_state);
}
if ($plugin->options['skip_confirmation']) {
break; // Go directly to execution
}
return;
case VIEWS_BULK_OPS_STEP_CONFIG:
$form_state['storage']['step'] = $form_state['values']['step'];
$form_state['storage'][VIEWS_BULK_OPS_STEP_CONFIG] = $form_state['values'];
$operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['operation']);
$form_state['storage']['operation_arguments'] = _views_bulk_operations_action_submit($operation, $form, $form_state);
if ($plugin->options['skip_confirmation']) {
break; // Go directly to execution
}
return;
case VIEWS_BULK_OPS_STEP_CONFIRM:
break;
}
// Clean up unneeded SESSION variables.
unset($_SESSION['vbo_values'][$_GET['q']]);
// Execute the VBO.
$operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['operation']);
$operation_arguments = array();
if ($operation['configurable']) {
$form_state['values'] += $form_state['storage'][VIEWS_BULK_OPS_STEP_CONFIG];
$operation_arguments = $form_state['storage']['operation_arguments'];
}
_views_bulk_operations_execute(
$plugin->view,
$form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['objects']['selection'],
$operation,
$operation_arguments,
array('execution_type' => $plugin->options['execution_type'], 'display_result' => $plugin->options['display_result'], 'settings' => $plugin->get_operation_settings($operation))
);
// Clean up the form.
unset($form_state['storage']);
$form_state['redirect'] = isset($form_state['values']['exposed_input']['destination']) ? $form_state['values']['exposed_input']['destination'] : $_GET['q'];
}
/**
* Helper function to execute the chosen action upon selected objects.
*/
function _views_bulk_operations_execute($view, $objects, $operation, $operation_arguments, $options) {
// Get the object info we're dealing with.
$object_info = _views_bulk_operations_object_info_for_view($view);
if (!$object_info) return;
// Add action arguments.
$params = array();
if ($operation['configurable'] && is_array($operation_arguments)) {
$params += $operation_arguments;
}
// Add static callback arguments. Note that in the case of actions, static arguments
// are picked up from the database in actions_do().
if (isset($operation['callback arguments'])) {
$params += $operation['callback arguments'];
}
// Add this view as parameter.
$params['view'] = array(
'vid' => !empty($view->vid) ? $view->vid : $view->name,
'exposed_input' => $view->get_exposed_input(),
'arguments' => $view->args,
);
// Add static settings to the params.
if (!empty($options['settings'])) {
$params['settings'] = $options['settings'];
}
if (version_compare(VERSION, '6.10', '<')) {
// Hack to force actions_do() to process any number of invocations.
// Check http://drupal.org/node/290282 to understand more.
// This was fixed as of D6.10: http://cvs.drupal.org/viewvc.py/drupal/drupal/includes/actions.inc?view=log&pathrev=DRUPAL-6-10
variable_set('actions_max_stack', 10000000);
}
if ($operation['aggregate'] != VBO_AGGREGATE_FORCED && $options['execution_type'] == VBO_EXECUTION_BATCH) {
$operations = array();
foreach ($objects as $num => $row) {
$oid = $row->{$view->base_field};
$operations[] = array('_views_bulk_operations_batch_process', array($oid, $row));
}
// Save the options in the session because Batch API doesn't give a way to
// send a parameter to the finished callback.
$_SESSION['vbo_options']['display_result'] = $options['display_result'];
$_SESSION['vbo_options']['operation'] = $operation;
$_SESSION['vbo_options']['params'] = $params;
$_SESSION['vbo_options']['object_info'] = $object_info;
$batch = array(
'operations' => $operations,
'finished' => '_views_bulk_operations_batch_finished',
'title' => t('Performing %action on selected rows...', array('%action' => $operation['label'])),
);
batch_set($batch);
}
else if ($operation['aggregate'] != VBO_AGGREGATE_FORCED && module_exists('job_queue') && $options['execution_type'] == VBO_EXECUTION_QUEUE) {
global $user;
foreach ($objects as $row) {
$oid = $row->{$view->base_field};
job_queue_add('_views_bulk_operations_queue_process',
t('Perform %action on @type %oid.', array(
'%action' => $operation['label'],
'@type' => t($object_info['type']),
'%oid' => $oid
)),
array($oid, $row, $operation, $params, $user->uid, $options['display_result'], $object_info)
);
$oids[] = $oid;
}
if ($options['display_result']) {
drupal_set_message(t('Enqueued %action on @types %oid. Check the queued jobs page.', array(
'%action' => $operation['label'],
'@types' => format_plural(count($objects), t($object_info['type']), t($object_info['type'] .'s')),
'%oid' => implode(', ', $oids),
'@queue' => url('admin/reports/job_queue')
)));
}
}
else /*if ($options['execution_type'] == VBO_EXECUTION_DIRECT)*/ {
@set_time_limit(0);
$context['results']['rows'] = 0;
$context['results']['time'] = microtime(TRUE);
_views_bulk_operations_direct_process($view, $operation, $objects, $params, $object_info, $context);
_views_bulk_operations_direct_finished(TRUE, $context['results'], array(), $options['display_result']);
}
}
/**
* Helper function to handle Job queue operations.
*/
function _views_bulk_operations_queue_process($oid, $row, $operation, $params, $uid, $display_result, $object_info) {
module_load_include('inc', 'node', 'node.admin');
$object = call_user_func($object_info['load'], $oid);
$account = user_load(array('uid' => $uid));
if (!_views_bulk_operations_object_permission($operation, $object, $object_info, $account)) {
watchdog('views bulk operations', 'Skipped %action on @type %title due to insufficient permissions.', array(
'%action' => $operation['label'],
'@type' => t($object_info['type']),
'%title' => $object->{$object_info['title']},
), WATCHDOG_ALERT);
return;
}
_views_bulk_operations_action_do($operation, $oid, $object, $row, $params, $object_info, $account);
if ($display_result) {
watchdog('views bulk operations', 'Performed %action on @type %title.', array(
'%action' => $operation['label'],
'@type' => t($object_info['type']),
'%title' => $object->{$object_info['title']},
), WATCHDOG_INFO);
}
}
/**
* Helper function to handle Batch API operations.
*/
function _views_bulk_operations_batch_process($oid, $row, &$context) {
module_load_include('inc', 'node', 'node.admin');
$operation = $_SESSION['vbo_options']['operation'];
$params = $_SESSION['vbo_options']['params'];
$object_info = $_SESSION['vbo_options']['object_info'];
if (!isset($context['results']['time'])) {
$context['results']['time'] = microtime(TRUE);
}
$object = call_user_func($object_info['load'], $oid);
if (!_views_bulk_operations_object_permission($operation, $object, $object_info)) {
$context['results']['log'][] = t('Skipped %action on @type %title due to insufficient permissions.', array(
'%action' => $operation['label'],
'@type' => t($object_info['type']),
'%title' => $object->{$object_info['title']},
));
return;
}
_views_bulk_operations_action_do($operation, $oid, $object, $row, $params, $object_info);
$context['results']['log'][] = t('Performed %action on @type %title.', array(
'%action' => $operation['label'],
'@type' => t($object_info['type']),
'%title' => $object->{$object_info['title']},
));
if (isset($context['results']['rows'])) {
$context['results']['rows'] += 1;
}
else {
$context['results']['rows'] = 1;
}
}
/**
* Helper function to cleanup Batch API operations.
*/
function _views_bulk_operations_batch_finished($success, $results, $operations, $display_result = NULL) {
if ($success) {
if ($results['rows'] > 0) {
$message = t('!results rows processed in about !time ms:', array('!results' => $results['rows'], '!time' => round((microtime(TRUE) - $results['time']) * 1000)));
}
else {
$message = t('No rows were processed:');
}
$message .= "\n". theme('item_list', $results['log']);
}
else {
// An error occurred.
// $operations contains the operations that remained unprocessed.
$error_operation = reset($operations);
$message = t('An error occurred while processing @operation with arguments: @arguments',
array('@operation' => $error_operation[0], '@arguments' => print_r($error_operation[0], TRUE)));
}
if (version_compare(VERSION, '6.10', '<')) {
// See http://cvs.drupal.org/viewvc.py/drupal/drupal/includes/actions.inc?view=log&pathrev=DRUPAL-6-10
variable_set('actions_max_stack', 35);
}
if ($display_result || @$_SESSION['vbo_options']['display_result']) {
_views_bulk_operations_log($message);
}
unset($_SESSION['vbo_options']); // unset the options which were used for just one invocation
}
/**
* Helper function for direct execution operations.
*/
function _views_bulk_operations_direct_process($view, $operation, $objects, $params, $object_info, &$context) {
if ($operation['aggregate'] != VBO_AGGREGATE_FORBIDDEN) {
if (isset($object_info['access'])) {
foreach ($objects as $num => $row) {
$oid = $row->{$view->base_field};
$object = call_user_func($object_info['load'], $oid);
if (!_views_bulk_operations_object_permission($operation, $object, $object_info)) {
unset($objects[$num]);
$context['results']['log'][] = t('Skipped %action on @type %title due to insufficient permissions.', array(
'%action' => $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 %action on @types %oids.', array(
'%action' => $operation['label'],
'@types' => format_plural(count($objects), t($object_info['type']), t($object_info['type'] .'s')),
'%oids' => implode(',', $oids),
));
$context['results']['rows'] += count($objects);
}
}
else foreach ($objects as $num => $row) {
$oid = $row->{$view->base_field};
$object = call_user_func($object_info['load'], $oid);
if (!_views_bulk_operations_object_permission($operation, $object, $object_info)) {
$context['results']['log'][] = t('Skipped %action on @type %title due to insufficient permissions.', array(
'%action' => $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 %action on @type %title.', array(
'%action' => $operation['label'],
'@type' => t($object_info['type']),
'%title' => $object->{$object_info['title']},
));
$context['results']['rows'] += 1;
}
}
/**
* Helper function to cleanup direct execution operations.
*/
function _views_bulk_operations_direct_finished($success, $results, $operations, $display_result) {
_views_bulk_operations_batch_finished($success, $results, $operations, $display_result);
}
/**
* Helper function to execute one operation.
*/
function _views_bulk_operations_action_do($operation, $oid, $object, $row, $params, $object_info, $account = NULL) {
_views_bulk_operations_action_permission($operation, $account);
// Add the object to the context.
if (isset($object_info['context'])) {
$params[$object_info['context']] = $object;
}
else {
$params[$object_info['type']] = $object;
}
// If the operation type is different from the view type, normalize the context first.
$actual_object = $object;
if ($object_info['type'] != $operation['type']) {
if (isset($object_info['normalize']) && function_exists($object_info['normalize'])) {
$actual_object = call_user_func($object_info['normalize'], $operation['type'], $object);
}
$params['hook'] = $object_info['hook'];
}
if (is_null($actual_object)) { // Normalize function can return NULL: we don't want that
$actual_object = $object;
}
$params['row'] = $row; // Expose the original view row to the action
if ($operation['source'] == 'action') {
actions_do($operation['callback'], $actual_object, $params);
if ($operation['type'] == 'node' && ($operation['access op'] & VBO_ACCESS_OP_UPDATE)) { // Save nodes explicitly if needed
$node_options = variable_get('node_options_'. $actual_object->type, array('status', 'promote'));
if (in_array('revision', $node_options) && !isset($actual_object->revision)) {
$actual_object->revision = TRUE;
$actual_object->log = '';
}
node_save($actual_object);
}
}
else { // source == 'operation'
$args = array_merge(array(array($oid)), $params);
call_user_func_array($operation['callback'], $args);
}
}
/**
* Helper function to execute an aggregate operation.
*/
function _views_bulk_operations_action_aggregate_do($operation, $oids, $objects, $params, $object_info) {
_views_bulk_operations_action_permission($operation);
$params[$operation['type']] = $objects;
if ($operation['source'] == 'action') {
actions_do($operation['callback'], $oids, $params);
}
else {
$args = array_merge(array($oids), $params);
call_user_func_array($operation['callback'], $args);
}
}
/**
* Helper function to verify access permission to execute operation.
*/
function _views_bulk_operations_action_permission($operation, $account = NULL) {
if (module_exists('actions_permissions')) {
$perm = actions_permissions_get_perm($operation['label'], $operation['callback']);
if (!user_access($perm, $account)) {
global $user;
watchdog('actions permissions', 'An attempt by user %user to !perm was blocked due to insufficient permissions.',
array('!perm' => $perm, '%user' => isset($account) ? $account->name : $user->name), WATCHDOG_ALERT);
drupal_access_denied();
}
}
// Check against additional permissions.
if (!empty($operation['permissions'])) foreach ($operation['permissions'] as $perm) {
if (!user_access($perm, $account)) {
global $user;
watchdog('actions permissions', 'An attempt by user %user to !perm was blocked due to insufficient permissions.',
array('!perm' => $perm, '%user' => isset($account) ? $account->name : $user->name), WATCHDOG_ALERT);
drupal_access_denied();
}
}
}
/**
* Helper function to verify access permission to operate on object.
*/
function _views_bulk_operations_object_permission($operation, $object, $object_info, $account = NULL) {
// Check against object access permissions.
if (!isset($object_info['access'])) return TRUE;
$access_ops = array(
VBO_ACCESS_OP_VIEW => 'view',
VBO_ACCESS_OP_UPDATE => 'update',
VBO_ACCESS_OP_CREATE => 'create',
VBO_ACCESS_OP_DELETE => 'delete',
);
foreach ($access_ops as $bit => $op) {
if ($operation['access op'] & $bit) {
if (!call_user_func($object_info['access'], $op, $object, $account)) {
return FALSE;
}
}
}
return TRUE;
}
/**
* Helper function to let the configurable action provide its configuration form.
*/
function _views_bulk_operations_action_form($action, $view, $selection, $settings) {
$action_form = $action['callback'].'_form';
return call_user_func($action_form, array('view' => $view, 'selection' => $selection, 'settings' => $settings));
}
/**
* Helper function to let the configurable action validate the form if it provides a validator.
*/
function _views_bulk_operations_action_validate($action, $form, $form_values) {
$action_validate = $action['callback'].'_validate';
if (function_exists($action_validate)) {
call_user_func($action_validate, $form, $form_values);
}
}
/**
* Helper function to let the configurable action process the configuration form.
*/
function _views_bulk_operations_action_submit($action, $form, $form_values) {
$action_submit = $action['callback'].'_submit';
return call_user_func($action_submit, $form, $form_values);
}
/**
* Theme function to show the confirmation page before executing the action.
*/
function theme_views_bulk_operations_confirmation($objects, $view) {
$object_info = _views_bulk_operations_object_info_for_view($view);
$items = array();
foreach ($objects as $num => $row) {
$oid = $row->{$view->base_field};
if (isset($view->pager_original) && count($items) >= $view->pager_original->get_items_per_page()) {
$items[] = t('...and !count more.', array('!count' => count($objects) - count($items)));
break;
}
if ($object = call_user_func($object_info['load'], $oid)) {
$items[] = check_plain((string)$object->{$object_info['title']});
}
}
$output = theme('item_list', $items, t('You selected the following !count rows:', array('!count' => count($objects))));
return $output;
}
/**
* Implementation of hook_forms().
*
* Force each instance of function to use the same callback.
*/
function views_bulk_operations_forms() {
// Get the form ID.
$args = func_get_args();
$form_id = $args[0];
// Ensure we map a callback for our form and not something else.
$forms = array();
if (strpos($form_id, 'views_bulk_operations_form') === 0) {
// Let the forms API know where to get the form data corresponding
// to this form id.
$forms[$form_id] = array('callback' => 'views_bulk_operations_form');
}
return $forms;
}
/**
* Implementation of hook_views_bulk_operations_object_info()
*
* Hook used by VBO to be able to handle different objects as does Views 2 and the Drupal core action system.
*
* The array returned for each object type contains:
* 'type' (required) => the object type name, should be the same as 'type' field in hook_action_info().
* 'context' (optional) => the context name that should receive the object, defaults to the value of 'type' above.
* 'base_table' (required) => the Views 2 table name corresponding to that object type, should be the same as the $view->base_table attribute.
* 'oid' (currently unused) => an attribute on the object that returns the unique object identifier (should be the same as $view->base_field).
* 'load' (required) => a function($oid) that returns the corresponding object.
* 'title' (required) => an attribute on the object that returns a human-friendly identifier of the object.
* 'access' (optional) => a function($op, $node, $account = NULL) that behaves like node_access().
*
* The following attributes allow VBO to show actions on view types different than the action's type:
* 'hook' (optional) => the name of the hook supported by this object type, as defined in the 'hooks' attribute of hook_action_info().
* 'normalize' (optional) => a function($type, $object) that takes an object type and the object instance, returning additional context information for cross-type
*
* e.g., an action declaring hooks => array('user') while of type 'system' will be shown on user views, and VBO will call the user's 'normalize' function to
* prepare the action to fit the user context.
*/
function views_bulk_operations_views_bulk_operations_object_info() {
$object_info = array(
'node' => array(
'type' => 'node',
'base_table' => 'node',
'load' => '_views_bulk_operations_node_load',
'oid' => 'nid',
'title' => 'title',
'access' => 'node_access',
'hook' => 'nodeapi',
'normalize' => '_views_bulk_operations_normalize_node_context',
),
'user' => array(
'type' => 'user',
'base_table' => 'users',
'load' => 'user_load',
'oid' => 'uid',
'title' => 'name',
'context' => 'account',
'hook' => 'user',
'normalize' => '_views_bulk_operations_normalize_user_context',
),
'comment' => array(
'type' => 'comment',
'base_table' => 'comments',
'load' => '_comment_load',
'oid' => 'cid',
'title' => 'subject',
'hook' => 'comment',
'normalize' => '_views_bulk_operations_normalize_comment_context',
),
'term' => array(
'type' => 'term',
'base_table' => 'term_data',
'load' => 'taxonomy_get_term',
'oid' => 'tid',
'title' => 'name',
'hook' => 'taxonomy',
),
'file' => array(
'type' => 'file',
'base_table' => 'files',
'load' => '_views_bulk_operations_file_load',
'oid' => 'fid',
'title' => 'filename',
),
);
return $object_info;
}
/**
* Load function for objects of type 'node'.
*/
function _views_bulk_operations_node_load($nid) {
return node_load($nid, NULL, TRUE);
}
/**
* Load function for objects of type 'file'.
*/
function _views_bulk_operations_file_load($fid) {
return db_fetch_object(db_query("SELECT * FROM {files} WHERE fid = %d", $fid));
}
/**
* Normalize function for node context.
*
* @see _trigger_normalize_node_context()
*/
function _views_bulk_operations_normalize_node_context($type, $node) {
switch ($type) {
// If an action that works on comments is being called in a node context,
// the action is expecting a comment object. But we do not know which comment
// to give it. The first? The most recent? All of them? So comment actions
// in a node context are not supported.
// An action that works on users is being called in a node context.
// Load the user object of the node's author.
case 'user':
return user_load(array('uid' => $node->uid));
}
}
/**
* Normalize function for comment context.
*
* @see _trigger_normalize_comment_context()
*/
function _views_bulk_operations_normalize_comment_context($type, $comment) {
switch ($type) {
// An action that works with nodes is being called in a comment context.
case 'node':
return node_load(is_array($comment) ? $comment['nid'] : $comment->nid);
// An action that works on users is being called in a comment context.
case 'user':
return user_load(array('uid' => is_array($comment) ? $comment['uid'] : $comment->uid));
}
}
/**
* Normalize function for user context.
*
* @see _trigger_normalize_user_context()
*/
function _views_bulk_operations_normalize_user_context($type, $account) {
switch ($type) {
// If an action that works on comments is being called in a user context,
// the action is expecting a comment object. But we have no way of
// determining the appropriate comment object to pass. So comment
// actions in a user context are not supported.
// An action that works with nodes is being called in a user context.
// If a single node is being viewed, return the node.
case 'node':
// If we are viewing an individual node, return the node.
if ((arg(0) == 'node') && is_numeric(arg(1)) && (arg(2) == NULL)) {
return node_load(array('nid' => arg(1)));
}
}
}
/**
* Helper function to return all object info.
*/
function _views_bulk_operations_get_object_info($reset = FALSE) {
static $object_info = array();
if ($reset || empty($object_info)) {
$object_info = module_invoke_all('views_bulk_operations_object_info');
}
return $object_info;
}
/**
* Helper function to return object info for a given view.
*/
function _views_bulk_operations_object_info_for_view($view) {
foreach (_views_bulk_operations_get_object_info() as $object_info) {
if ($object_info['base_table'] == $view->base_table) {
return $object_info;
}
}
watchdog('views bulk operations', 'Could not find object info for view table @table.', array('@table' => $view->base_table), WATCHDOG_ERROR);
return NULL;
}
/**
* Implementation of hook_action_info().
*/
function views_bulk_operations_action_info() {
$actions = array();
$files = file_scan_directory(drupal_get_path('module', 'views_bulk_operations'), '(.*).action.inc$');
if ($files) foreach ($files as $file) {
require_once($file->filename);
$action_info_fn = 'views_bulk_operations_'. str_replace('.', '_', basename($file->filename, '.inc')).'_info';
$action_info = call_user_func($action_info_fn);
if (is_array($action_info)) $actions += $action_info;
}
// Add VBO's own programmatic action.
$actions['views_bulk_operations_action'] = array(
'description' => t('Execute a VBO programmatically'),
'type' => 'system',
'configurable' => TRUE,
'rules_ignore' => TRUE,
);
return $actions;
}
/**
* Form function for views_bulk_operations_action action.
*/
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.
$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 = empty($view->vid) ? $view->name : $view->vid;
$views[$vid] = $view->name . (!empty($view->description) ? ': ' . $view->description : '');
$style_plugin = views_get_plugin('style', $display_options['style_plugin']);
$style_plugin->init($view, $view->display[$display], $display_options['style_options']);
foreach (array_filter($display_options['style_options']['selected_operations']) as $operation) {
$views_operations[$vid][$operation] = $style_plugin->all_operations[$operation]['label'];
$operations[$operation] = $style_plugin->all_operations[$operation]['label'];
}
}
}
}
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') .'/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);'),
);
$form['operation_callback'] = array(
'#type' => 'select',
'#title' => t('Operation'),
'#description' => t('Select the operation to be executed.'),
'#options' => $operations,
'#default_value' => @$context['operation_callback'],
);
$form['operation_arguments'] = array(
'#type' => 'textarea',
'#title' => t('Operation arguments'),
'#description' => t('Enter PHP script that will assemble the operation arguments (in the case of configurable actions).
These arguments should be of the form: return array(\'argument1\' => \'value1\', ...);
and they should correspond to the values returned by the action\'s form submit function.
The variables &$object and $context are available to this script.
'),
'#default_value' => @$context['operation_arguments'],
);
$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 input should be of the form: return array(\'input1\' => \'value1\', ...);
and they should correspond to the query values used on the view URL when exposed filters are applied.
The variables &$object and $context 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 input should be of the form: return array(\'value1\', ...);
and they should correspond to the arguments defined in the view.
The variables &$object and $context are available to this script.
'),
'#default_value' => @$context['view_arguments'],
);
return $form;
}
/**
* Form validate function for views_bulk_operations_action action.
*/
function views_bulk_operations_action_validate($form, $form_state) {
if (empty($form_state['values']['view_vid'])) {
form_set_error('view_vid', t('You must choose a view to be executed.'));
}
if (empty($form_state['values']['operation_callback'])) {
form_set_error('operation_callback', t('You must choose an operation to be executed.'));
}
}
/**
* Form submit function for views_bulk_operations_action action.
*/
function views_bulk_operations_action_submit($form, $form_state) {
return array(
'view_vid' => $form_state['values']['view_vid'],
'operation_callback' => $form_state['values']['operation_callback'],
'operation_arguments' => $form_state['values']['operation_arguments'],
'view_exposed_input' => $form_state['values']['view_exposed_input'],
'view_arguments' => $form_state['values']['view_arguments'],
);
}
/**
* Execution function for views_bulk_operations_action action.
*/
function views_bulk_operations_action(&$object, $context) {
$operation_arguments = array();
if (!empty($context['operation_arguments'])) {
$operation_arguments = eval($context['operation_arguments']);
}
$view_exposed_input = array();
if (!empty($context['view_exposed_input'])) {
$view_exposed_input = eval($context['view_exposed_input']);
}
$view_arguments = array();
if (!empty($context['view_arguments'])) {
$view_arguments = eval($context['view_arguments']);
}
views_bulk_operations_execute($context['view_vid'], $context['operation_callback'], $operation_arguments, $view_exposed_input, $view_arguments);
}
/**
* API function to programmatically invoke a VBO.
*/
function views_bulk_operations_execute($vid, $operation_callback, $operation_arguments = array(), $view_exposed_input = array(), $view_arguments = array()) {
$view = views_get_view($vid);
if (!is_object($view)) {
_views_bulk_operations_report_error('Could not find view %vid.', array('%vid' => $vid));
return;
}
// Find the view display that has the VBO style.
$found = FALSE;
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') {
$view->set_display($display);
$found = TRUE;
break;
}
}
if (!$found) {
_views_bulk_operations_report_error('Could not find a VBO display in view %vid.', array('%vid' => $vid));
return;
}
// Execute the view.
$view->set_exposed_input($view_exposed_input);
$view->set_arguments($view_arguments);
$view->build($plugin->view->current_display);
$view->query->set_limit(NULL); // reset the work done by the pager
$view->query->set_offset(NULL);
$view->execute();
// Find the selected operation.
$plugin = $view->style_plugin;
$operations = $plugin->get_selected_operations();
if (!isset($operations[$operation_callback])) {
_views_bulk_operations_report_error('Could not find operation %operation in view %vid.', array('%operation' => $operation_callback, '%vid' => $vid));
return;
}
$operation = $plugin->get_operation_info($operation_callback);
// Execute the operation on the view results.
foreach ($view->result as $num => $result) {
$objects[$num + 1] = $result;
}
$execution_type = $plugin->options['execution_type'];
if ($execution_type == VBO_EXECUTION_BATCH) {
$execution_type = VBO_EXECUTION_DIRECT; // we don't yet support Batch API here
}
_views_bulk_operations_execute(
$view,
$objects,
$operation,
$operation_arguments,
array('execution_type' => $execution_type, 'display_result' => $plugin->options['display_result'], 'settings' => $plugin->get_operation_settings($operation))
);
}
/**
* Helper function to report an error.
*/
function _views_bulk_operations_report_error($msg, $arg) {
watchdog('views bulk operations', $msg, $arg, WATCHDOG_ERROR);
if (function_exists('drush_set_error')) {
drush_set_error('VIEWS_BULK_OPERATIONS_EXECUTION_ERROR', strip_tags(dt($msg, $arg)));
}
}
/**
* Helper function to log an information.
*/
function _views_bulk_operations_log($msg) {
if (function_exists('drush_log')) {
drush_log(strip_tags($msg), 'ok');
}
else {
drupal_set_message($msg);
}
}
/**
* API function to add actions to a VBO.
*/
function views_bulk_operations_add_actions($vid, $actions) {
$view = views_get_view($vid);
if (!is_object($view)) {
_views_bulk_operations_report_error('Could not find view %vid.', array('%vid' => $vid));
return;
}
// Find the view display that has the VBO style.
$found = FALSE;
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') {
$found = TRUE;
break;
}
}
if (!$found) {
_views_bulk_operations_report_error('Could not find a VBO display in view %vid.', array('%vid' => $vid));
return;
}
// Iterate on the desired actions.
$selected_operations = $display_options['style_options']['selected_operations'];
$ignored = array();
if (!empty($actions)) foreach ($actions as $action) {
$modified = FALSE;
if (is_numeric($action)) { // aid
$action_object = db_fetch_object(db_query("SELECT * FROM {actions} WHERE aid = %d", $action));
if (is_object($action_object)) {
$parameters = unserialize($action_object->parameters);
$key = $action_object->callback . (empty($parameters) ? '' : ':'. md5($action_object->parameters));
if (isset($selected_operations[$key])) { // available for this view
$display_options['style_options']['selected_operations'][$key] = $key;
$modified = TRUE;
}
}
}
else { // callback or title
if (isset($selected_operations[$action])) { // callback and available for this view
$display_options['style_options']['selected_operations'][$action] = $action;
$modified = TRUE;
}
else { // try the title
$action_object = db_fetch_object(db_query("SELECT * FROM {actions} WHERE UPPER(description) = '%s'", strtoupper(db_escape_string($action))));
if (is_object($action_object)) {
$parameters = unserialize($action_object->parameters);
$key = $action_object->callback . (empty($parameters) ? '' : ':'. md5($action_object->parameters));
if (isset($selected_operations[$key])) { // available for this view
$display_options['style_options']['selected_operations'][$key] = $key;
$modified = TRUE;
}
}
}
}
if (!$modified) {
$ignored[] = $action;
}
}
// Save the view if anything was changed.
if (count($actions) > count($ignored)) {
$view->save();
views_object_cache_clear('view', $vid);
$msg = t('View %vid was successfully modified.', array('%vid' => $vid));
if (!empty($ignored)) {
$msg .= ' ' . t('The following actions were ignored: %ignored.', array('%ignored' => implode(', ', $ignored)));
}
}
else {
$msg = t('View %vid was not modified, because all actions were ignored.', array('%vid' => $vid));
}
_views_bulk_operations_log($msg);
}
function _views_bulk_operations_get_oid($row, $base_field) {
return $row->$base_field;
}