diff --git a/views_data_export.module b/views_data_export.module index 158a418..09e3835 100644 --- a/views_data_export.module +++ b/views_data_export.module @@ -237,59 +237,106 @@ function views_data_export_action_info() { 'type' => 'entity', 'label' => t('Export to CSV'), 'configurable' => FALSE, - 'aggregate' => TRUE, 'behavior' => array('views_data_export_export_csv'), ), ); } /** - * CSV export action callback. + * CSV export VBO Configuration form. */ -function views_data_export_action_csv_export($entity, &$context) { +function views_data_export_action_csv_export_views_bulk_operations_form($options) { + // Load available views_data_export displays from current view. + $view = menu_get_object('views_ui_cache', 5); + $display_options = array(); + foreach ($view->display as $id => $display) { + if ($display->display_plugin == 'views_data_export') { + $option_title = t('@display_title (@display_id)', array('@display_title' => $display->display_title, '@display_id' => $display->id)); + $display_options[$display->id] = $option_title; + } + } + ksort($display_options); - if (isset($context['view_info'])) { - $view = views_get_view($context['view_info']['name']); - $display_id = FALSE; + $form['display_id'] = array( + '#title' => t('Display'), + '#type' => 'select', + '#options' => $display_options, + '#default_value' => !empty($options['display']) ? $options['display'] : '', + ); + return $form; +} - foreach ($view->display as $display) { - if ($display->display_plugin == 'views_data_export') { - $display_id = $display->id; - break; +/** + * CSV export action callback. + */ +function views_data_export_action_csv_export($entity, &$context) { + if (isset($context['view_info']) && !empty($context['settings']['display_id'])) { + $export_context = &views_data_export_action_context(); + $entity_ids = &$export_context['entity_ids']; + list($entity_id) = entity_extract_ids($context['entity_type'], $entity); + $entity_ids[] = $entity_id; + + $progress = $context['progress']; + $last_row = $progress['current'] == $progress['total']; + + if ($last_row) { + if ($batch = &batch_get()) { + $export_context['context'] = $context; + $batch['form_state']['redirect'] = FALSE; + $batch['form_state']['rebuild'] = TRUE; + $batch['redirect_callback'] = 'views_data_export_action_csv_export_deliver'; + } + else { + $view = views_get_view($context['view_info']['name']); + $view->execute_display($context['settings']['display_id'], array('views_data_export_entity_ids' => $entity_ids)); + // If the export is configured to run as a batch job, it will handle the + // display of progress & result/download pages, deliver the generated + // file, and never return here. Otherwise, executing the display here + // won't cause the file to be delivered to the browser for download. + // Dying here ensures that it does, but there must be a better way. + drupal_exit(); } } + } +} - if ($display_id) { - $args = array(); - // If a subset of the available entities was selected, provide the list - // of selected IDs to the view as a contrived argument, which we'll - // use later to alter the query. - // Extra code for backward/forward compatibility. - // @see https://www.drupal.org/node/2488146. - if (isset($context['view_info']['total_rows'])) { - $total_rows = $context['view_info']['total_rows']; +/** + * Get list of entity ids processed by views_data_export_action_csv_export. + * + * If the export is batched, the batch form state is used as persistent storage. + */ +function &views_data_export_action_context() { + if ($batch = &batch_get()) { + if (!isset($batch['form_state']['views_data_export_action_context'])) { + $batch['form_state']['views_data_export_action_context'] = array('entity_ids' => array()); + } + $export_context = &$batch['form_state']['views_data_export_action_context']; + } + else { + $export_context = &drupal_static(__FUNCTION__); + if (!isset($export_context)) { + if (isset($_SESSION['batch_form_state']['views_data_export_action_context'])) { + $export_context = $_SESSION['batch_form_state']['views_data_export_action_context']; + unset($_SESSION['batch_form_state']); } else { - $countView = clone $view; - $countView->execute($context['view_info']['display']); - $total_rows = $countView->total_rows; - } - $entity_ids = array_keys($entity); - if (count($entity_ids) < $total_rows) { - $args['entity_ids'] = $entity_ids; + $export_context = array('entity_ids' => array()); } - $view->execute_display($display_id, $args); - // If the export is configured to run as a batch job, it will handle the - // display of progress & result/download pages, deliver the generated - // file, and never return here. Otherwise, executing the display here - // won't cause the file to be delivered to the browser for download. - // Dying here ensures that it does, but there must be a better way. - die(); - } - else { - drupal_set_message(t("Can't export data, because the !view view does not have a !display display.", array('!view' => $view->name, '!display' => 'views_data_export')), 'error'); } } + return $export_context; +} + +/** + * Delivery callback for views_data_export_action_csv_export. + * + * Starts the actual export after preparing filter in views_data_export_action_csv_export. + */ +function views_data_export_action_csv_export_deliver($path, $options) { + $export_context = &views_data_export_action_context(); + $context = $export_context['context']; + $view = views_get_view($context['view_info']['name']); + $view->execute_display($context['settings']['display_id'], array('views_data_export_entity_ids' => $export_context['entity_ids'])); } /** @@ -299,10 +346,10 @@ function views_data_export_views_query_alter(&$view, &$query) { if ($view->display_handler->plugin_name == 'views_data_export') { // If an explicit list of entity IDs was provided, use that to refine the // query. - if (isset($view->args['entity_ids'])) { - $query->add_where(0, $view->base_table . '.' . $view->base_field, $view->args['entity_ids'], 'IN'); + if (isset($view->args['views_data_export_entity_ids'])) { + $query->add_where(0, $view->base_table . '.' . $view->base_field, $view->args['views_data_export_entity_ids'], 'IN'); // Now remove this fake argument to avoid confusion. - unset($view->args['entity_ids']); + unset($view->args['views_data_export_entity_ids']); } } }