--- biblio.module-orig 2007-12-13 23:07:46.000000000 +0100 +++ biblio.module 2008-01-11 22:07:02.000000000 +0100 @@ -448,10 +448,43 @@ function biblio_settings() { '#weight' => 15, ); } + $form['csv'] = array( + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + '#title' => t('CSV (Comma Separated Values)'), + '#description' => t('Simple file format for exporting / importing tables'), + ); + $form['csv']['field_sep'] = array( + '#type' => 'radios', + '#title' => t('Character to sepatate field in a record'), + '#default_value' => variable_get('biblio_csv_field_sep', 'tab'), + '#options' => array('tab' => t('Tabulator'), 'comma' => t('Comma')), + ); + $form['csv']['text_sep'] = array( + '#type' => 'radios', + '#title' => t('Character to enclose fields in a record'), + '#default_value' => variable_get('biblio_csv_text_sep', 'dquote'), + '#options' => array('dquote' => t('Double quotes'), 'quote' => t('Single quotes')), + ); + $form['csv']['col_head'] = array( + '#type' => 'radios', + '#title' => t('Column names in export'), + '#default_value' => variable_get('biblio_csv_col_head', 'label'), + '#options' => array('label' => t('Use field label'), 'col_name' => t('Use DB field name')), + ); + $form['csv']['linebreak_exp'] = array( + '#type' => 'checkbox', + '#title' => t('Break input field into multiple lines'), + '#return_value' => 1, + '#default_value' => variable_get('biblio_linebreak_exp', 1), + '#description' => t('Convert semicolon in input field to linebreak in export'), + ); + return system_settings_form($form); } /* This function parses the module directory for 'style' files, loads them and * calls the info fuction to get some basic information like the short and long @@ -774,11 +807,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 +1703,11 @@ function theme_biblio_export_links($nid) $base = variable_get('biblio_base', 'biblio'); $links .= '

'; + $links .= '
  • '.l(t("csv"),"$base/export/csv/$nid").'
  • '; return $links; } function theme_biblio_short($node, $base='biblio', $style_name='classic', $inline=false) { @@ -1706,11 +1741,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 +1769,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 +1882,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 +2060,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 +2072,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 +2108,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 +3326,84 @@ 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; + } + + switch ( variable_get('biblio_csv_field_sep', 'tab') ) { + case 'tab': $filedsep = "\t"; break; + case 'comma': $filedsep = ','; break; + } + switch ( variable_get('biblio_csv_text_sep', 'dquote') ) { + case 'dquote': $textsep = '"'; break; + case 'quote': $textsep = '\''; break; + } + $label = ( variable_get('biblio_csv_col_head', 'label') == 'label' ? 1 : 0 ); // or 'col_name' + $linebreak = variable_get('biblio_linebreak_exp',1); + + 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'] = 'Type'; + + foreach( array_keys($bfields) as $fieldname ) { + if (!empty($rec->$fieldname) && !in_array($fieldname,array('biblio_citekey','biblio_coins'))) { + $col_array[$fieldname] = $bfields[$fieldname]; // mark field as in use + $text = strtr($rec->$fieldname,$textsep, "$textsep$textsep"); + if ($linebreak) { + $text = strtr($text,';',"\n"); + } + $node_array[$node_id][$fieldname] = trim($text); + } + } + } //end while + + if ($label) { // head line containing column names + $csv = $textsep . join("$textsep$filedsep$textsep", array_values($col_array)) . "$textsep\n"; + } else { // original DB field names + $csv = $textsep . join("$textsep$filedsep$textsep", array_keys($col_array)) . "$textsep\n"; + } + + foreach($node_array as $line_array) { + $csv_line = ''; + foreach(array_keys($col_array) as $col) { + $csv_line .= "$filedsep$textsep" . $line_array[$col] . $textsep; + } + $csv .= substr($csv_line,1) . "\n"; // cut off leading fieldsep and append EOL + } + + drupal_set_header('Content-Type: text/plain; charset=utf-8'); + drupal_set_header('Content-Disposition: attachment; filename=biblio_export.csv'); + + return $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; +} +