--- biblio.module-orig 2007-12-13 23:07:46.000000000 +0100
+++ biblio.module 2008-01-11 04:20:33.909399750 +0100
@@ -774,11 +774,12 @@ function biblio_show_node($node, $base,
if ( biblio_access('export',$node) && $teaser == false ) {
$rows[] = array(array('data' => ''.t('Export').'','align'=>'right','valign'=>'center','width'=>'20%'),
array('data' => ' '),
array('data' => l(t("Tagged"),"$base/export/tagged/$node->nid").' '.
l(t("XML"),"$base/export/xml/$node->nid").' '.
- l(t("BibTex"),"$base/export/bib/$node->nid")
+ l(t("BibTex"),"$base/export/bib/$node->nid").' '.
+ l(t("csv"),"$base/export/csv/$node->nid")
));
}
$output = '
';
@@ -1669,10 +1670,11 @@ function theme_biblio_export_links($nid)
$base = variable_get('biblio_base', 'biblio');
$links .= '
';
$links .= '- '.l(t("Tagged"),"$base/export/tagged/$nid").'
';
$links .= '- '.l(t("XML"),"$base/export/xml/$nid").'
';
$links .= '- '.l(t("BibTex"),"$base/export/bib/$nid").'
';
+ $links .= '
'.l(t("csv"),"$base/export/csv/$nid").'';
return $links;
}
function theme_biblio_short($node, $base='biblio', $style_name='classic', $inline=false) {
@@ -1706,11 +1708,12 @@ function theme_biblio_list($node, $base=
if ( biblio_access('export',$node)) {
$base = variable_get('biblio_base', 'biblio');
$output .= '
';
$output .= l("Tagged","$base/export/tagged/$node->nid") . ' ';
$output .= l("XML","$base/export/xml/$node->nid") . ' ';
- $output .= l("BibTex","$base/export/bib/$node->nid") ;
+ $output .= l("BibTex","$base/export/bib/$node->nid") . ' ';
+ $output .= l("csv","$base/export/csv/$node->nid") ;
$output .= '';
}
return $output;
}
@@ -1733,11 +1736,11 @@ function biblio_show_results($result, $a
}
if (!$inline) {
// Add some links to the top of the page to change the sorting/ordering...
$order = ($attrib['order'] == "desc" || $attrib['order'] == "DESC")?"asc":"desc";
if ( biblio_access('export',$node)) {
- $content .= '
'.t('Export @count results', array('@count' => $pager_total_items[0])).':['.l("Tagged","$base/export/tagged"). ']['.l("XML","$base/export/xml"). ']['.l("BibTex","$base/export/bib").']
';
+ $content .= '
'.t('Export @count results', array('@count' => $pager_total_items[0])).':['.l("Tagged","$base/export/tagged"). ']['.l("XML","$base/export/xml"). ']['.l("BibTex","$base/export/bib").']['.l("csv","$base/export/csv"). ']
';
}else{
$content .= '
'.t('Found @count results', array('@count' => $pager_total_items[0])) .'
';
}
if (user_access('show sort links')) {
$sort_attr = array("title"=>t("Click a second time to reverse the sort order"));
@@ -1846,11 +1849,12 @@ function biblio_show_results($result, $a
function biblio_db_search() {
global $user;
$inline = false;
$join = array();
- $join[] = "left join {biblio} b on n.vid=b.vid";
+ $join[] = _biblio_cck_join($dummy) . ' left join {node} n on b.vid=n.vid'; // sequence of joins, cck first!
+
if (variable_get('biblio_view_only_own', 0) ) {
$limit .= " AND n.uid=$user->uid ";
}
$pager_attrib = array("sort" => variable_get('biblio_sort', 'year'), 'order' => variable_get('biblio_order', 'DESC'));
@@ -2023,12 +2027,13 @@ $join = array();
$joins = implode(' ', $join);
$where_clause = count($where) > 1 ? '('. implode(') AND (', $where) .')': $where[0];
- $query = db_rewrite_sql("SELECT DISTINCT n.*, b.* FROM {node} n $joins WHERE $where_clause $published $limit $sortby ");
- $count_query = db_rewrite_sql("SELECT DISTINCT COUNT(*) FROM {node} n $joins WHERE $where_clause $published $limit ");
+ $query = db_rewrite_sql("SELECT DISTINCT * FROM {biblio} b $joins WHERE $where_clause $published $limit $sortby ");
+ $count_query = db_rewrite_sql("SELECT DISTINCT COUNT(*) FROM {biblio} b $joins WHERE $where_clause $published $limit ");
+
//echo $query; die;
$_SESSION['last_biblio_query'] = $query;
$terms[] = $pager_attrib['order'];
$_SESSION['last_biblio_query_terms'] = $terms;
@@ -2034,23 +2039,33 @@ $join = array();
$result = pager_query($query, variable_get('biblio_rowsperpage', 25),0,$count_query,$terms);
biblio_show_results($result, $pager_attrib, $args,$inline);
}
+
+
function _biblio_export($format="tagged", $nid = null, $version = 7) {
$params=array();
- if ($nid === null && isset($_SESSION['last_biblio_query']) && !empty($_SESSION['last_biblio_query']) ) {
+
+ $biblio_fields = array();
+ $cck_join = _biblio_cck_join($biblio_fields);
+ $default_query = "SELECT * FROM {biblio} b" . $cck_join . " left join {node} n on b.vid=n.vid";
+
+ if ($nid === null || empty($nid)) {
+ if (isset($_SESSION['last_biblio_query']) && !empty($_SESSION['last_biblio_query'])) {
$query = $_SESSION['last_biblio_query'];
$params = $_SESSION['last_biblio_query_terms'];
- } elseif (!empty($nid)) {
- $query = db_rewrite_sql("SELECT * FROM {node} n left join {biblio} b on n.vid=b.vid
- WHERE n.nid=%d ");
+ } else {
+ $query = db_rewrite_sql($default_query);
+ }
+ } else {
+ $query = $default_query . " WHERE n.nid=%d ";
+ $query = db_rewrite_sql($query);
$params[] = $nid;
- }else{
- return;
}
+// watchdog('biblio','query-E='.$query, WATCHDOG_NOTICE);
$result = db_query($query,$params);
if (db_num_rows($result)) {
switch($format) {
case "tagged":
@@ -2060,13 +2075,19 @@ $params=array();
_endnote_XML_export($result, $version );
break;
case "bib":
_bibtex_export($result);
break;
+ case "csv":
+ // require_once(drupal_get_path('module', 'biblio') . '/csv_export.inc');
+ print _biblio_csv_export($result,$biblio_fields);
+ break;
}
}
}
+
+
function _bibtex_export($result) {
$bibtex = '';
while ($pub = db_fetch_object($result)) {
$type = "article";
@@ -3272,5 +3293,68 @@ function biblio_contextObject($node) {
$co["rft_id"] = "info:doi/" . $node->biblio_doi ;
elseif (!empty($node->biblio_url ))
$co["rft_id"] = $node->biblio_url ;
return $co;
}
+
+
+function _biblio_csv_export($result,$bfields){
+
+ $query_biblio_fields = 'SELECT name, title FROM {biblio_fields}';
+ $res_biblio_fields = db_query($query_biblio_fields);
+ while ($rec = db_fetch_object($res_biblio_fields)){
+ $bfields[$rec->name] = $rec->title;
+ }
+
+ $query_biblio_types = 'SELECT tid, name FROM {biblio_types}';
+ $res_biblio_types = db_query($query_biblio_types);
+ while ($rec = db_fetch_object($res_biblio_types)){
+ $btypes[$rec->tid] = $rec->name;
+ }
+
+ while ($rec = db_fetch_object($result)){
+ $node_id = $rec->nid;
+ $node_array[$node_id]['Type'] = $btypes[$rec->biblio_type]; // there is no "label" for "type"
+ $col_array['Type'] += 1;
+
+ foreach( array_keys($bfields) as $fieldname ) {
+ if (!empty($rec->$fieldname) && !in_array($fieldname,array('biblio_citekey','biblio_coins'))) {
+ $col_array[$bfields[$fieldname]] +=1; // mark field as in use
+ $node_array[$node_id][$bfields[$fieldname]] = trim(strtr(strtr($rec->$fieldname,'"','""'),';',"\n"));
+ }
+ }
+ } //end while
+
+
+ // Enclosing text in "
" is neccessary to enshure
+ // multi line fields (like author) are handled correctly.
+ // Therefore existing " must be excaped before.
+ $csv = '"' . join("\"\t\"", array_keys($col_array)) . "\"\n";
+ foreach($node_array as $line_array) {
+ $csv_line = '';
+ foreach(array_keys($col_array) as $col) {
+ $csv_line .= "\t\"" . $line_array[$col] . '"';
+ }
+ $csv .= substr($csv_line,1) . "\n";
+ }
+
+ header('Content-type: application/text');
+ header('Content-Disposition: filename="biblio_export.csv"');
+
+ return $csv; // return utf8_decode($csv);
+}
+
+//function _biblio_cck_join($biblio_fields = array()) { // works not with php4
+function _biblio_cck_join(&$biblio_fields) {
+ $cck_join = '';
+ $biblio_fields['nid'] = 'Node-ID'; // identify records for update operations
+ $query_cck_fields = "SELECT field_name, label from {node_field_instance} where type_name='biblio' and not (widget_type='image')";
+ $res_cck_fields = db_query($query_cck_fields);
+ while ($rec = db_fetch_object($res_cck_fields)){
+ $cck_table = 'content_' . $rec->field_name;
+ $cck_field = $rec->field_name . '_value';
+ $biblio_fields[$cck_field] = $rec->label;
+ $cck_join .= ' left join {' . $cck_table . '} on b.vid=' . $cck_table . '.vid';
+ }
+ return $cck_join;
+}
+