Index: biblio.pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/biblio/biblio.pages.inc,v
retrieving revision 1.16
diff -r1.16 biblio.pages.inc
100a101,102
>   // POSIX regular expression matching, case insensitive
>   $match_op = ($db_type == 'pgsql') ? '~*' : 'RLIKE';
105c107,116
<   $pgsql_additions ='';
---
>   // The following is to be used to compare titles
>   // LOWER() is required for case insensitive sorting (at least, in PostgreSQL)
>   $sort_title =" CASE
>                    WHEN SUBSTR(n.title,1,1)='\"'   THEN LOWER(SUBSTR(n.title,2))
>                    WHEN SUBSTR(n.title,1,1)='\''   THEN LOWER(SUBSTR(n.title,2))
>                    WHEN SUBSTR(n.title,1,2)='A '   THEN LOWER(SUBSTR(n.title,3))
>                    WHEN SUBSTR(n.title,1,3)='An '  THEN LOWER(SUBSTR(n.title,4))
>                    WHEN SUBSTR(n.title,1,4)='The ' THEN LOWER(SUBSTR(n.title,5))
>                    ELSE LOWER(n.title)
>                 END ";
122,124c133,134
<     case 'type':
<       if ($db_type == 'pgsql') $pgsql_additions = ', SUBSTR(n.title,1,3) ';
<       $sortby = "ORDER BY b.biblio_type %s, SUBSTR(n.title,1,3) ASC ";
---
>     case 'type': // Sort by bibliography type *name*
>       $sortby = "ORDER BY bt.name %s, $sort_title ASC ";
128,146c138
<       if ($db_type == 'pgsql') {
<         $pgsql_additions = ", CASE
<               WHEN SUBSTR(n.title,1,1)='\"'   THEN SUBSTR(n.title,2)
<               WHEN SUBSTR(n.title,1,1)='\''   THEN SUBSTR(n.title,2)
<               WHEN SUBSTR(n.title,1,2)='A '   THEN SUBSTR(n.title,3)
<               WHEN SUBSTR(n.title,1,3)='An '  THEN SUBSTR(n.title,4)
<               WHEN SUBSTR(n.title,1,4)='The ' THEN SUBSTR(n.title,5)
<               ELSE title
<             END ";
<       }
<       $sortby = "ORDER BY
<   					CASE
<     					WHEN SUBSTR(n.title,1,1)='\"'   THEN SUBSTR(n.title,2)
<     					WHEN SUBSTR(n.title,1,1)='\''   THEN SUBSTR(n.title,2)
<     					WHEN SUBSTR(n.title,1,2)='A '   THEN SUBSTR(n.title,3)
<     					WHEN SUBSTR(n.title,1,3)='An '  THEN SUBSTR(n.title,4)
<     					WHEN SUBSTR(n.title,1,4)='The ' THEN SUBSTR(n.title,5)
<     					ELSE title
<   					END  %s";
---
>       $sortby = "ORDER BY $sort_title %s";
149,152c141,149
<       if ($db_type == 'pgsql') $pgsql_additions = ', bcd.lastname ';
<       $sortby = "ORDER BY bcd.lastname %s ";
<       //$where['bc-rank'] = "bc.rank=0";
<       //$where[] = "bc.ctid=1";
---
>       // Sorting by author is problematic with the current implementation.
>       // In principle, the module should sort according to the last name
>       // of the first author occurring in the citation.
>       // The following implementation assumes that the authors occur in
>       // alphabetical order within each citation, so the first one is the
>       // minimum in lexicographical order.
>       // This assumption is too strong in general, however. But,
>       // how can you determine the right order of the authors within a citation?
>       // (Is the rank field supposed to manage that?)
154a152,160
>       //$where['bc-rank'] = "bc.rank=0";
>       //$where[] = "bc.ctid=1";
>       
>       // Join with the "minimum" author of the given vid, that is,
>       // the author that has the minimum auth_type and is the first in
>       // lexicographical order among the authors of vid with such auth_type.
>       $where[] = " NOT EXISTS (SELECT * FROM biblio_contributor bcx JOIN biblio_contributor_data bcdx ON bcx.cid = bcdx.cid
>                     WHERE bcx.vid = bc.vid and (bcx.auth_type < bc.auth_type or (bcx.auth_type = bc.auth_type and bcdx.lastname < bcd.lastname))) ";
>       $sortby = " ORDER BY bcd.lastname %s ";
157,158c163
<       if ($db_type == 'pgsql') $pgsql_additions = ', SUBSTR(LTRIM(b.biblio_keywords),1,4)';
<       $sortby = "ORDER BY SUBSTR(LTRIM(b.biblio_keywords),1,4) %s ";
---
>       $sortby = "ORDER BY LOWER(SUBSTR(LTRIM(b.biblio_keywords),1,4)) %s ";
162,163c167
<       if ($db_type == 'pgsql') $pgsql_additions = ', SUBSTR(n.title,1,3) ';
<       $sortby = "ORDER BY b.biblio_year %s, b.biblio_date %s, SUBSTR(n.title,1,3) ASC ";
---
>       $sortby = "ORDER BY b.biblio_year %s, b.biblio_date %s, $sort_title ASC ";
225,233c229
<           $where[] = ' CASE
<     					WHEN LEFT(n.title,1)="\""   THEN SUBSTRING(n.title,2,1)
<     					WHEN LEFT(n.title,1)="\'"   THEN SUBSTRING(n.title,2,1)
<     					WHEN LEFT(n.title,2)="A "   THEN SUBSTRING(n.title,3,1)
<     					WHEN LEFT(n.title,3)="An "  THEN SUBSTRING(n.title,4,1)
<     					WHEN LEFT(n.title,4)="The " THEN SUBSTRING(n.title,5,1)
<     					ELSE SUBSTRING(n.title,1,1)
<   					  END
<   					  RLIKE "%s" ';
---
>           $where[] = $sort_title . $match_op . " '%s'";
240c236
<           $where[] = ' UCASE(substring(bcd.lastname,1,1)) = "%s" ';
---
>           $where[] = " UPPER(substring(bcd.lastname,1,1)) = '%s' ";
255c251
<             $where[] = " bcd". $bcc .'.name RLIKE "[[:<:]]%s[[:>:]]" ';
---
>             $where[] = " bcd". $bcc .'.name ' . $match_op . ' "[[:<:]]%s[[:>:]]" ';
268c264
<           $where[] = "b.biblio_publisher RLIKE '%s' ";
---
>           $where[] = "b.biblio_publisher " . $match_op . " '%s' ";
291c287
<      //     $where[] = "b.biblio_keywords RLIKE '%s' ";
---
>      //     $where[] = "b.biblio_keywords " . $match_op . " '%s' ";
317,318c313
<               if ($db_type == 'pgsql') $pgsql_additions = ', SUBSTR(n.title,1,3) ';
<               $sortby = "ORDER BY b.biblio_type %s, b.biblio_year DESC, SUBSTR(n.title,1,3) ASC ";
---
>               $sortby = "ORDER BY bt.name %s, b.biblio_year DESC, $sort_title ASC ";
321,322c316
<               if ($db_type == 'pgsql') $pgsql_additions = ', SUBSTR(n.title,1,3) ';
<               $sortby = "ORDER BY SUBSTRING(n.title,1,3) %s, b.biblio_year DESC ";
---
>               $sortby = "ORDER BY $sort_title %s, b.biblio_year DESC ";
326,327c320
<               if ($db_type == 'pgsql') $pgsql_additions = ', SUBSTR(n.title,1,3) ';
<               $sortby = "ORDER BY b.biblio_year %s, b.biblio_date %s,SUBSTR(n.title,1,3) ASC, b.biblio_type ASC ";
---
>               $sortby = "ORDER BY b.biblio_year %s, b.biblio_date %s, $sort_title ASC, bt.name ASC ";
348c341
<                 $where[] = "b.biblio_$type RLIKE '%s' ";
---
>                 $where[] = "b.biblio_$type " . $match_op . " '%s' ";
366c359
<   $query = db_rewrite_sql("SELECT DISTINCT n.*, b.*, bt.name as biblio_type_name $pgsql_additions FROM {node} n $joins  WHERE $where_clause $limit $sortby ");
---
>   $query = db_rewrite_sql("SELECT n.*, b.*, bt.name as biblio_type_name $pgsql_additions FROM {node} n $joins  WHERE $where_clause $limit $sortby ");
971c964
<       $query_ext =  ' AND UPPER(SUBSTRING(lastname,1,1)) = "%s" ';
---
>       $query_ext =  " AND UPPER(SUBSTRING(lastname,1,1)) = '%s' ";
979c972,983
<     $db_result = db_query('SELECT bd.*, COUNT(*) AS cnt FROM {biblio_contributor} b, {biblio_contributor_data} bd WHERE bd.cid=b.cid '. $query_ext.' GROUP BY b.cid HAVING cnt > 0 ORDER BY  lastname ASC, SUBSTRING(firstname,1,1) ASC, initials ASC',$filter);
---
>     $db_result = db_query('SELECT bd.cid, bd.drupal_uid, bd.name, bd.lastname,
>                              bd.firstname, bd.prefix, bd.suffix, bd.initials,
>                              bd.affiliation, bd.md5, COUNT(*) AS cnt
>                            FROM {biblio_contributor} b,
>                                 {biblio_contributor_data} bd
>                            WHERE bd.cid=b.cid '. $query_ext.'
>                            GROUP BY bd.cid, bd.drupal_uid, bd.name, bd.lastname,
>                                     bd.firstname, bd.prefix, bd.suffix,
>                                     bd.initials, bd.affiliation, bd.md5
>                            HAVING COUNT(*) > 0
>                            ORDER BY  lastname ASC, SUBSTRING(firstname,1,1) ASC,
>                            initials ASC', $filter);
1012c1016
<       $query_ext =  ' AND UPPER(SUBSTRING(word,1,1)) = "%s" ';
---
>       $query_ext =  " AND UPPER(SUBSTRING(word,1,1)) = '%s' ";
1020c1024
<     $db_result = db_query('SELECT bkd.*, COUNT(*) AS cnt FROM {biblio_keyword} bk, {biblio_keyword_data} bkd WHERE bkd.kid=bk.kid '. $query_ext.' GROUP BY bk.kid HAVING cnt > 0 ORDER BY  word ASC', $filter);
---
>     $db_result = db_query('SELECT bkd.kid, bkd.word, COUNT(*) AS cnt FROM {biblio_keyword} bk, {biblio_keyword_data} bkd WHERE bkd.kid=bk.kid '. $query_ext.' GROUP BY bkd.kid, bkd.word HAVING COUNT(*) > 0 ORDER BY  word ASC', $filter);
1042c1046
< }
\ No newline at end of file
---
> }
