Permalink
Browse files

Adds export functionality to tables.

To enable exporting of a table, pass "show_export"=>true to $opts.  The table will be generated with "export" set to true in $opts, in case the export structure should be different from the html structure.
  • Loading branch information...
1 parent 1d320ed commit 8e7abd72fe2f2fe742291f6645c6f944094cf005 @elplatt committed Aug 16, 2012
Showing with 137 additions and 7 deletions.
  1. +2 −0 .gitignore
  2. +25 −0 crm/export-csv.php
  3. +69 −1 crm/include/core/theme.inc.php
  4. +3 −2 crm/include/member/page.inc.php
  5. +26 −4 crm/include/member/table.inc.php
  6. +12 −0 crm/style.css
View
@@ -1,2 +1,4 @@
error_log
config.inc.php
+php.ini
+
View
@@ -0,0 +1,25 @@
+<?
+/*
+ Copyright 2009-2012 Edward L. Platt <elplatt@alum.mit.edu>
+
+ This file is part of the Seltzer CRM Project
+ export-csv.php - Exports a table to csv format.
+
+ Seltzer is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ any later version.
+
+ Seltzer is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Seltzer. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+require_once('include/crm.inc.php');
+header('Content-type: text/csv');
+header('Content-Disposition: attachment; filename="export.csv"');
+print theme('table_csv', $_GET['name'], json_decode($_GET['opts'], true));
@@ -425,6 +425,15 @@ function theme_table ($table_name, $opts = NULL) {
return '';
}
+ // Count rows
+ $column_count = sizeof($table['columns']);
+ $row_count = sizeof($table['rows']);
+
+ // Generate url for export
+ $new_opts = $opts;
+ $new_opts['export'] = true;
+ $export = 'export-csv.php?name=' . $table_name . '&opts=' . urlencode(json_encode($new_opts));
+
// Open table
$output = "<table";
if (!empty($table['id'])) {
@@ -455,7 +464,13 @@ function theme_table ($table_name, $opts = NULL) {
$output .= $col['title'];
$output .= '</th>';
}
- $output .= "</tr></thead>";
+ $output .= "</tr>";
+ if ($opts['show_export']) {
+ $output .= '<tr class="subhead"><td colspan="' . $column_count . '">';
+ $output .= $row_count . ' results, export: <a href="' . $export . '">csv</a>';
+ $output .= "</td></tr>";
+ }
+ $output .= "</thead>";
// Output table body
$output .= "<tbody>";
@@ -491,13 +506,66 @@ function theme_table ($table_name, $opts = NULL) {
$output .= '</tr>';
}
+ if ($opts['show_export']) {
+ $output .= '<tr class="subhead"><td colspan="' . $column_count . '">';
+ $output .= $row_count . ' results, export: <a href="' . $export . '">csv</a>';
+ $output .= "</td></tr>";
+ }
+
$output .= "</tbody>";
$output .= "</table>";
return $output;
}
/**
+ * Themes tabular data as a CSV.
+ *
+ * @param $table_name The name of the table or the table data.
+ * @param $opts Options to pass to the data function.
+ * @return The CSV for a table.
+*/
+function theme_table_csv ($table_name, $opts = NULL) {
+
+ // Check if $table_name is a string
+ if (is_string($table_name)) {
+ // Construct the name of the function to generate a table
+ $generator = $table_name . '_table';
+ if (function_exists($generator)) {
+ $table = call_user_func($generator, $opts);
+ } else {
+ return '';
+ }
+ } else {
+ // Support old style of passing the data directly
+ $table = $table_name;
+ }
+
+ // Check if table is empty
+ if (empty($table['rows'])) {
+ return '';
+ }
+
+ // Loop through headers
+ $cells = array();
+ foreach ($table['columns'] as $col) {
+ $cells[] = str_replace('"', '\"', $col['title']);
+ }
+ $output .= join(',', $cells) . "\n";
+
+ // Loop through rows
+ foreach ($table['rows'] as $row) {
+ $cells = array();
+ foreach ($row as $i => $cell) {
+ $cells[] = str_replace('"', '\"', $cell);
+ }
+ $output .= join(',', $cells) . "\n";
+ }
+
+ return $output;
+}
+
+/**
* Themes a table with headers in the left column instead of the top row.
*
* @param $table_name The name of the table or the table data.
@@ -38,8 +38,9 @@ function member_page (&$page_data, $page_name, $options) {
// Add view tab
if (user_access('member_view')) {
- page_add_content_top($page_data, 'View', theme('table', 'member', array('filter'=>$_SESSION['member_filter'])));
- page_add_content_top($page_data, 'View', theme('member_filter_form'));
+ $view .= theme('member_filter_form');
+ $view .= theme('table', 'member', array('filter'=>$_SESSION['member_filter'], 'show_export'=>true));
+ page_add_content_top($page_data, 'View', $view);
}
// Add add tab
@@ -33,6 +33,16 @@ function member_table ($opts = NULL) {
return NULL;
}
+ // Determine settings
+ $export = false;
+ foreach ($opts as $option => $value) {
+ switch ($option) {
+ case 'export':
+ $export = $value;
+ break;
+ }
+ }
+
// Get member data
$members = member_data($opts);
@@ -47,15 +57,21 @@ function member_table ($opts = NULL) {
$table['columns'] = array();
if (user_access('member_view')) {
- $table['columns'][] = array('title'=>'Name','class'=>'');
+ if ($export) {
+ $table['columns'][] = array('title'=>'Last','class'=>'');
+ $table['columns'][] = array('title'=>'First','class'=>'');
+ $table['columns'][] = array('title'=>'Middle','class'=>'');
+ } else {
+ $table['columns'][] = array('title'=>'Name','class'=>'');
+ }
$table['columns'][] = array('title'=>'Membership','class'=>'');
$table['columns'][] = array('title'=>'E-Mail','class'=>'');
$table['columns'][] = array('title'=>'Phone','class'=>'');
$table['columns'][] = array('title'=>'Emergency Contact','class'=>'');
$table['columns'][] = array('title'=>'Emergency Phone','class'=>'');
}
// Add ops column
- if (user_access('member_edit') || user_access('member_delete')) {
+ if (!$export && (user_access('member_edit') || user_access('member_delete'))) {
$table['columns'][] = array('title'=>'Ops','class'=>'');
}
@@ -81,7 +97,13 @@ function member_table ($opts = NULL) {
}
// Add cells
- $row[] = $name_link;
+ if ($export) {
+ $row[] = $member['contact']['lastName'];
+ $row[] = $member['contact']['firstName'];
+ $row[] = $member['contact']['middleName'];
+ } else {
+ $row[] = $name_link;
+ }
$row[] = $plan;
$row[] = $member['contact']['email'];
$row[] = $member['contact']['phone'];
@@ -103,7 +125,7 @@ function member_table ($opts = NULL) {
}
// Add ops row
- if (user_access('member_edit') || user_access('member_delete')) {
+ if (!$export && (user_access('member_edit') || user_access('member_delete'))) {
$row[] = join(' ', $ops);
}
View
@@ -88,6 +88,18 @@ table.seltzer-table td, table.seltzer-table th {
padding: 0.2em 0.5em;
text-align: left;
}
+table.seltzer-table tr.subhead td {
+ background: #99dce6;
+ background: #7bcfeb;
+ border-bottom: none;
+ color: white;
+ font-weight: normal;
+ border-top: solid 1px #0099cb;
+}
+table.seltzer-table tr.subhead td a {
+ color: white;
+ font-weight: normal;
+}
table.seltzer-table tr.even td, table.seltzer-table tr.even th {
background-color: #eeeeff;
}

0 comments on commit 8e7abd7

Please sign in to comment.