--- views.module 2006-07-03 23:06:21.000000000 +1000 +++ views.module.new 2006-07-03 23:13:37.000000000 +1000 @@ -351,6 +351,14 @@ function views_menu($may_cache) { _views_create_menu_item($items, $view, $path, MENU_CALLBACK, $view_args); } } + if (arg(0) == 'view_csv' && arg(1)) { + $view->name = str_replace('.csv', '', arg(1)); + _views_create_menu_item($items, $view, 'view_csv/' . arg(1), MENU_CALLBACK, array(), '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), MENU_CALLBACK, array(), '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, $local_task_type = MENU_NORMAL_ITEM, $args = array()) { +function _views_create_menu_item(&$items, $view, $path, $local_task_type = MENU_NORMAL_ITEM, $args = array(), $format = null) { static $roles = NULL; if ($roles == NULL) { global $user; @@ -370,18 +378,18 @@ 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) { - array_unshift($args, $view->name); +function _views_menu_item($path, $title, $view, $args, $access, $type, $weight = NULL, $format = null) { + array_unshift($args, $view->name, $format); $retval = array('path' => $path, 'title' => $title, 'callback' => 'views_view_page', @@ -488,6 +496,8 @@ function views_get_view($view_name) { * * @param $view_name * The name or id of the view to load + * @param $format + * If set to either 'csv' or 'doc' then output that format. * @param $args * The arguments from the end of the url. Usually passed from the menu system. * @@ -497,13 +507,16 @@ function views_get_view($view_name) { function views_view_page() { $args = func_get_args(); $view_name = array_shift($args); + $format = array_shift($args); $view = views_get_view($view_name); if (!$view) { drupal_not_found(); exit; } - + 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(); @@ -1445,9 +1458,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. */ @@ -1777,6 +1863,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',