--- /home/achapman/drupal/modules/views/views.module 2006-07-03 15:19:34.000000000 +1000 +++ views.module 2006-07-03 15:21:49.000000000 +1000 @@ -351,6 +351,14 @@ function views_menu($may_cache) { _views_create_menu_item($items, $view, $path, $view_args, MENU_CALLBACK); } } + if (arg(0) == 'view_csv' && arg(1)) { + $view->name = str_replace('.csv', '', arg(1)); + _views_create_menu_item($items, $view, 'view_csv/' . arg(1), null, MENU_CALLBACK, 'csv'); + } + elseif (arg(0) == 'view_doc' && arg(1)) { + $view->name = str_replace('.doc', '', arg(1)); + _views_create_menu_item($items, $view, 'view_doc/' . arg(1), null, MENU_CALLBACK, 'doc'); + } } return $items; } @@ -358,7 +366,7 @@ function views_menu($may_cache) { /** * Helper function to add a menu item for a view. */ -function _views_create_menu_item(&$items, $view, $path, $args = array(), $local_task_type = MENU_NORMAL_ITEM) { +function _views_create_menu_item(&$items, $view, $path, $args = array(), $local_task_type = MENU_NORMAL_ITEM, $format = null) { static $roles = NULL; if ($roles == NULL) { global $user; @@ -370,21 +378,21 @@ function _views_create_menu_item(&$items $weight = $view->menu_tab_weight; } $access = !$view->access || array_intersect($view->access, $roles); - $items[] = _views_menu_item($path, $title, $view, $args, $access, $type, $weight); + $items[] = _views_menu_item($path, $title, $view, $args, $access, $type, $weight, $format); if ($type == MENU_DEFAULT_LOCAL_TASK) { - $items[] = _views_menu_item(dirname($path), $title, $view, $args, $access, $local_task_type, $weight); + $items[] = _views_menu_item(dirname($path), $title, $view, $args, $access, $local_task_type, $weight, $format); } } /** * Helper function to create a menu item for a view. */ -function _views_menu_item($path, $title, $view, $args, $access, $type, $weight = NULL) { +function _views_menu_item($path, $title, $view, $args, $access, $type, $weight = NULL, $format = null) { $retval = array('path' => $path, 'title' => $title, 'callback' => 'views_view_page', - 'callback arguments' => array($view, $args), + 'callback arguments' => array($view, $args, $format), 'access' => $access, 'type' => $type, ); @@ -485,8 +493,11 @@ function views_get_view($view_name) { /** * This views a view by page. */ -function views_view_page($view, $args) { +function views_view_page($view, $args, $format = null) { $view = views_get_view($view->name); + if (isset($format)) { + $view->page_type = $format; + } $output = views_build_view('page', $view, $args, $view->use_pager, $view->nodes_per_page); if ($output === FALSE) { drupal_not_found(); @@ -1428,9 +1439,82 @@ function theme_views_view_table($view, $ } $rows[] = $row; } + + $query = array(); + foreach ($_GET as $key => $values) { + foreach ($values as $value) { + $query[] = "{$key}" . urlencode('[]') . "=$value"; + } + } + if (!empty($query)) { + $query = implode('&', $query); + } + else $query = null; + + // TODO: make whether to display export links a setting, and get rid of this. + if (!isset($view->show_export_csv)) $view->show_export_csv = true; + if (!isset($view->show_export_doc)) $view->show_export_doc = true; + + $links = array(); + if ($view->show_export_csv) { + $links[] = l('Export as CSV', "view_csv/{$view->name}.csv", '', $query); + } + if ($view->show_export_doc) { + $links[] = l('Export as DOC', "view_doc/{$view->name}.doc", '', $query); + } + + if (!empty($links)) { + $links_row = array(array('data' => implode(' | ', $links), 'colspan' => count($fields))); + // put export links at the start and end of the table. + $rows[] = $links_row; + array_unshift($rows, $links_row); + } + return theme('table', $view->table_header, $rows); } +function theme_views_view_csv($view, $nodes) { + $fields = _views_get_fields(); + + // headings row + $headings = array(); + foreach ($view->field as $field) { + if ($fields[$field['id']]['visible'] !== false) { + $headings[] = $field['label'] ? $field['label'] : $fields[$field['fullname']]['name']; + } + } + $output .= implode(',', $headings) . "\r\n"; + + // one row for each node + foreach ($nodes as $node) { + $values = array(); + foreach ($view->field as $field) { + if ($fields[$field['id']]['visible'] !== false) { + $value = $field; + $value = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node); + $value = preg_replace('/<.*?>/', '', $value); // strip html tags + $value = str_replace('"', '""', $value); // escape " characters + $value = decode_entities($value); + $values[] = '"' . $value . '"'; + } + } + $output .= implode(',', $values) . "\r\n"; + } + header('Content-Type: text/x-comma-separated-values'); + print $output; + exit; // don't allow full page to render +} + +function theme_views_view_doc($view, $nodes) { + $view->show_export_csv = false; + $view->show_export_doc = false; + header('Content-Type: application/msword'); + $table = theme('views_view_table', $view, $nodes, null); + $table = preg_replace('/<\/?(a|span) ?.*?>/', '', $table); // strip 'a' and 'span' tags + print $table; + exit; // don't allow full page to render +} + /** * Display the nodes of a view as teasers. */ @@ -1760,6 +1844,18 @@ function views_views_style_plugins() { 'needs_fields' => true, 'needs_table_header' => true, ), + 'csv' => array( + 'name' => t('CSV View'), + 'theme' => 'views_view_csv', + 'needs_fields' => true, + 'needs_table_header' => true, + ), + 'doc' => array( + 'name' => t('DOC View'), + 'theme' => 'views_view_doc', + 'needs_fields' => true, + 'needs_table_header' => true, + ), 'teaser' => array( 'name' => t('Teaser List'), 'theme' => 'views_view_teasers',