'views_bonus_export', 'callback' => 'views_bonus_export', 'type' => MENU_CALLBACK, 'access' => (user_access('export views as CSV') || user_access('export views as DOC')), ); } return $items; } /** * Implementation of hook_views_style_plugins */ function views_bonus_export_views_style_plugins() { return array( 'views_csv' => array( 'name' => t('Views Bonus: CSV file'), 'theme' => 'views_bonus_export_csv', 'needs_table_header' => true, 'needs_fields' => true, 'even_empty' => true, ), 'views_doc' => array( 'name' => t('Views Export: Doc file'), 'theme' => 'views_bonus_export_doc', 'needs_table_header' => true, 'needs_fields' => true, 'even_empty' => true, ), ); } /** * Menu callback to make the CSV/DOC */ function views_bonus_export($type, $vid) { if (!is_numeric($vid)) { drupal_not_found(); return; } $view = views_load_view($vid); $result = views_build_view('items', $view); switch ($type) { case 'csv': if(user_access('export views as CSV')) { theme('views_bonus_export_csv', $view, $result['items']); break; } else { drupal_access_denied(); } case 'doc': if(user_access('export views as DOC')) { theme('views_bonus_export_doc', $view, $result['items']); break; } else { drupal_access_denied(); } } } /** * Main Function to export a view as CSV */ function theme_views_bonus_export_csv($view, $nodes) { if (!user_access('export views as CSV')) { return; } $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, $view); $value = preg_replace('/<.*?>/', '', $value); // strip html tags $value = str_replace(array("\r", "\n", ','), ' ', $value); // strip line breaks and commas $value = str_replace('"', '""', $value); // escape " characters $value = decode_entities($value); $values[] = '"' . $value . '"'; } } $output .= implode(',', $values) . "\r\n"; } drupal_set_header('Content-Type: text/x-comma-separated-values'); drupal_set_header('Content-Disposition: attachment; filename="view-'. $view->name .'.csv"'); print $output; module_invoke_all('exit'); exit; } /** * Main Function to export a view as DOC */ function theme_views_bonus_export_doc($view, $nodes) { if (!user_access('export views as DOC')) { return; } drupal_set_header('Content-Type: application/msword'); drupal_set_header('Content-Disposition: attachment; filename="view-'. $view->name .'.doc"'); $table = theme('views_view_table', $view, $nodes, null); $table = preg_replace('/<\/?(a|span) ?.*?>/', '', $table); // strip 'a' and 'span' tags print $table; module_invoke_all('exit'); exit; } /** * Implementation of hook_views_pre_view */ function views_bonus_export_views_pre_view($view, $items) { if (!user_access('export views as CSV') || !user_access('export views as CSV') || $view->page_type == 'teaser' || $view->block == true || !$view->vid) { return; } $url_filter = NULL; $url_filters = array(); if(!($view->used_filters == NULL)) { $active_filters = $view->used_filters; foreach($active_filters as $key => $value) { if (is_array($value)) { foreach ($value as $multi) { $url_filters[] = $key .'[]='. $multi; } } else { $url_filters[] = $key .'='. $value; } } } if (!empty($url_filters)) { $url_filter = implode('&', $url_filters); } $links = array(); if (($image = theme('image', drupal_get_path('module', 'views_bonus_export').'/csv.png', t('CSV export'), t('Export this table to an Spreadsheet-readable CSV file'))) && (user_access('export views as CSV'))) { $links[] = l($image, 'views_bonus_export/csv/'. $view->vid, array('class' => 'xml-icon'), $url_filter, NULL, FALSE, TRUE); } if (($image = theme('image', drupal_get_path('module', 'views_bonus_export').'/doc.png', t('DOC export'), t('Export this table to an Wordprocessor-readable DOC file'))) && (user_access('export views as DOC'))) { $links[] = l($image, 'views_bonus_export/doc/'. $view->vid, array('class' => 'xml-icon'), $url_filter, NULL, FALSE, TRUE); } $output = implode('  ', $links); return $output; }