TRUE, '#view' => NULL, '#process' => array('views_node_selector_process'), ); return $type; } function views_bulk_operations_theme() { return array( 'views_node_selector' => array( 'arguments' => array('element' => NULL), ), 'views_bulk_operations_confirmation' => array( 'arguments' => array('nodes' => NULL), ), ); } function theme_views_node_selector($element) { $view = $element['#view']; // Construct header. $header = array(); $header[] = theme('table_select_header_cell'); foreach ($view->field as $id => $field) { $header[] = $field->label(); } // Construct rows. $rows = array(); foreach ($view->result as $node) { $row = array(); $row[] = array('data' => theme('checkbox', $element[$node->nid])); foreach ($view->field as $id => $field) { $row[] = array('data' => $field->theme($node)); } $rows[] = $row; } $output = theme('table', $header, $rows); return theme('form_element', $element, $output); } function views_node_selector_process($element, $edit) { $view = $element['#view']; $options = array(); foreach ($view->result as $node) { $options[$node->nid] = ''; } $element['#options'] = $options; $element = expand_checkboxes($element); return $element; } function views_bulk_operations_form($form_state, $plugin) { drupal_add_css(drupal_get_path('module', 'views_bulk_operations') . '/views_bulk_operations.css', 'module'); if (!isset($form_state['storage']['step'])) { $step = VIEWS_BULK_OPS_STEP_VIEW; } else switch ($form_state['storage']['step']) { case VIEWS_BULK_OPS_STEP_VIEW: $operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['action']); if ($operation['configurable']) { $step = VIEWS_BULK_OPS_STEP_CONFIG; } else { $step = VIEWS_BULK_OPS_STEP_CONFIRM; } break; case VIEWS_BULK_OPS_STEP_CONFIG: $step = VIEWS_BULK_OPS_STEP_CONFIRM; } $form['step'] = array('#type' => 'value', '#value' => $step); $form['plugin'] = array('#type' => 'value', '#value' => $plugin); switch ($step) { case VIEWS_BULK_OPS_STEP_VIEW: $form['select'] = array( '#type' => 'fieldset', '#title' => t('Bulk operations'), '#prefix' => '
', '#suffix' => '
', ); $form['select']['action'] = array( '#type' => 'select', '#options' => array(0 => t('- Choose an operation -')) + $plugin->get_selected_operations(), '#prefix' => '
', '#suffix' => '
', ); $form['nodes'] = array( '#type' => 'views_node_selector', '#view' => $plugin->view, '#prefix' => '
', '#suffix' => '
', ); $form['select']['submit'] = array( '#type' => 'submit', '#value' => t('Execute'), '#prefix' => '
', '#suffix' => '
', ); break; case VIEWS_BULK_OPS_STEP_CONFIG: $operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['action']); $form += _views_bulk_operations_action_form($operation); $form['execute'] = array( '#type' => 'submit', '#value' => t('Next'), ); 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]['action']); $query = drupal_query_string_encode($_GET, array('q')); $form = confirm_form($form, t('Are you sure you want to perform \'%action\' on selected nodes?', array('%action' => $operation['label'])), $query ? array('path' => $_GET['q'], 'query' => $query) : array('path' => $_GET['q']), theme('views_bulk_operations_confirmation', $form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['nodes'])); } return $form; } function views_bulk_operations_form_validate($form, &$form_state) { switch ($form_state['values']['step']) { case VIEWS_BULK_OPS_STEP_VIEW: if (!array_sum($form_state['values']['nodes'])) { // If all 0, no node selected form_set_error('nodes', t('No nodes selected. Please select one or more nodes.')); } if (!$form_state['values']['action']) { // No action selected form_set_error('action', t('No operation selected. Please select an operation to perform.')); } break; case VIEWS_BULK_OPS_STEP_CONFIG: $plugin = $form_state['values']['plugin']; $operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['action']); _views_bulk_operations_action_validate($operation, $form, $form_state); break; } } function views_bulk_operations_form_submit($form, &$form_state) { 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']; 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']; return; case VIEWS_BULK_OPS_STEP_CONFIRM: break; } $plugin = $form_state['values']['plugin']; $operation = $plugin->get_operation_info($form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['action']); $nodes = $form_state['storage'][VIEWS_BULK_OPS_STEP_VIEW]['nodes']; if ($operation['type'] == 'action') { foreach ($nodes as $nid) { if (is_numeric($nid) && $nid > 0) { $node = node_load($nid); $params = array(); if ($operation['configurable']) { $form_state['values'] += $form_state['storage'][VIEWS_BULK_OPS_STEP_CONFIG]; $params = _views_bulk_operations_action_submit($operation, $form, $form_state); } _views_bulk_operations_action_do($operation, $node, $params); drupal_set_message(t('Performed %action on node %title.', array('%action' => $operation['label'], '%title' => $node->title))); } } } else { // type == 'node' $nodes = array_filter($nodes); if (isset($operation['callback arguments'])) { $args = array_merge(array($nodes), $operation['callback arguments']); } else { $args = array($nodes); } call_user_func_array($operation['callback'], $args); drupal_set_message(t('Performed %action on nodes %nid.', array('%action' => $operation['label'], '%nid' => implode(',', $nodes)))); } unset($form_state['storage']); $form_state['redirect'] = $_GET['q']; } function _views_bulk_operations_action_form($action) { $action_form = $action['callback'].'_form'; return call_user_func($action_form, array()); } function _views_bulk_operations_action_validate($action, $form, $form_values) { $action_validate = $action['callback'].'_validate'; call_user_func($action_validate, $form, $form_values); } function _views_bulk_operations_action_submit($action, $form, $form_values) { $action_submit = $action['callback'].'_submit'; return call_user_func($action_submit, $form, $form_values); } function _views_bulk_operations_action_do($action, $node, $context) { $action_do = $action['callback']; actions_do($action['callback'], $node, $context); node_save($node); } function theme_views_bulk_operations_confirmation($nodes) { $count = 0; $output = t('You selected the following nodes:').'
'; return $output; } function views_bulk_operations_node_operations() { $operations = array( 'bulk_delete' => array( 'label' => t('Delete'), 'callback' => 'views_bulk_operations_delete_nodes', ), ); return $operations; } function views_bulk_operations_delete_nodes($nodes) { foreach ($nodes as $nid) { node_delete($nid); } } function views_bulk_operations_views_default_views() { $view = new view; $view->name = 'admin_content'; $view->description = 'View, edit and delete your site\'s content.'; $view->tag = ''; $view->view_php = ''; $view->base_table = 'node'; $view->is_cacheable = FALSE; $view->api_version = 2; $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ $handler = $view->new_display('default', 'Defaults', 'default'); $handler->override_option('fields', array( 'title' => array( 'label' => 'Title', 'link_to_node' => 1, 'exclude' => 0, 'id' => 'title', 'table' => 'node', 'field' => 'title', 'relationship' => 'none', 'override' => array( 'button' => 'Override', ), ), 'type' => array( 'label' => 'Type', 'link_to_node' => 0, 'exclude' => 0, 'id' => 'type', 'table' => 'node', 'field' => 'type', 'override' => array( 'button' => 'Override', ), 'relationship' => 'none', ), 'name' => array( 'label' => 'Author', 'link_to_user' => 1, 'exclude' => 0, 'id' => 'name', 'table' => 'users', 'field' => 'name', 'override' => array( 'button' => 'Override', ), 'relationship' => 'none', ), 'status' => array( 'label' => 'Published', 'type' => 'yes-no', 'not' => 0, 'exclude' => 0, 'id' => 'status', 'table' => 'node', 'field' => 'status', 'override' => array( 'button' => 'Override', ), 'relationship' => 'none', ), 'edit_node' => array( 'id' => 'edit_node', 'table' => 'node', 'field' => 'edit_node', ), )); $handler->override_option('access', array( 'type' => 'none', 'role' => array(), 'perm' => '', )); $handler->override_option('title', 'Content'); $handler->override_option('items_per_page', 0); $handler->override_option('style_plugin', 'bulk'); $handler->override_option('style_options', array( 'selected_operations' => array( '867e6bb83ef13a39c914e114fab4d6f2' => '867e6bb83ef13a39c914e114fab4d6f2', 'c6d3850086dc3c1bca4bf0625ce78c56' => 'c6d3850086dc3c1bca4bf0625ce78c56', '548749a939aa6334157edff3d8f69351' => '548749a939aa6334157edff3d8f69351', '6a99179e9c90e9edfb34d075fa234d78' => '6a99179e9c90e9edfb34d075fa234d78', 'c4ca4238a0b923820dcc509a6f75849b' => 'c4ca4238a0b923820dcc509a6f75849b', '799e8527bbd3d99e4c8c25a8a674d264' => 0, 'fce859fcce0e81943c3e31c20ccd68ff' => 0, '0341fcc505281031474b68c72e03a997' => 0, '94a098afb1689f950d72ed4c44f8655d' => 0, '15faf15d7f591f8ef8e80238dd7c8b8c' => 0, 'c6db6d4ee28924020b504a5feb2bff9f' => 0, ), )); $handler = $view->new_display('page', 'Page', 'page'); $handler->override_option('path', 'admin/content/node2'); $handler->override_option('menu', array( 'type' => 'none', 'title' => '', 'weight' => 0, )); $handler->override_option('tab_options', array( 'type' => 'none', 'title' => '', 'weight' => 0, )); $views[$view->name] = $view; return $views; }