Plugin for field 124e
[koha.git] / C4 / AuthoritiesMarc.pm
index fd1a20f..c5a9dba 100644 (file)
@@ -51,6 +51,7 @@ $VERSION = 0.01;
        &AUTHaddword
        &MARCaddword &MARCdelword
        &char_decode
+       &FindDuplicate
  );
 
 sub authoritysearch {
@@ -63,18 +64,21 @@ sub authoritysearch {
 
        # the marclist may contain "mainentry". In this case, search the tag_to_report, that depends on
        # the authtypecode. Then, search on $a of this tag_to_report
+       # also store main entry MARC tag, to extract it at end of search
+       my $mainentrytag;
+       my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
+       $sth->execute($authtypecode);
+       my ($tag_to_report) = $sth->fetchrow;
+       $mainentrytag = $tag_to_report;
        for (my $i=0;$i<$#{$tags};$i++) {
                if (@$tags[$i] eq "mainentry") {
-                       my $sth = $dbh->prepare("select auth_tag_to_report from auth_types where authtypecode=?");
-                       $sth->execute($authtypecode);
-                       my ($tag_to_report) = $sth->fetchrow;
                        @$tags[$i] = $tag_to_report."a";
                }
        }
 
        # "Normal" statements
        # quote marc fields/subfields
-       for (my $i=0;$i<$#{$tags};$i++) {
+       for (my $i=0;$i<=$#{$tags};$i++) {
                if (@$tags[$i]) {
                        @$tags[$i] = $dbh->quote(@$tags[$i]);
                }
@@ -86,6 +90,11 @@ sub authoritysearch {
        # Extracts the NOT statements from the list of statements
        for(my $i = 0 ; $i <= $#{$value} ; $i++)
        {
+               # replace * by %
+               @$value[$i] =~ s/\*/%/g;
+               # remove % at the beginning
+               @$value[$i] =~ s/^%//g;
+           @$value[$i] =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\}|\/)/ /g if @$operator[$i] eq "contains";
                if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
                {
                        foreach my $word (split(/ /, @$value[$i]))
@@ -173,6 +182,8 @@ sub authoritysearch {
                $newline{authid} = $result[$counter];
                $newline{used} = &AUTHcount_usage($result[$counter]);
                $newline{biblio_fields} = $tags_using_authtype;
+               $newline{even} = $counter % 2;
+               $newline{mainentry} = $record->field($mainentrytag)->subfield('a') if $record->field($mainentrytag);
                $counter++;
                push @finalresult, \%newline;
        }
@@ -195,6 +206,7 @@ sub create_request {
        for(my $i=0; $i<=@$value;$i++) {
                if (@$value[$i]) {
                        $nb_active++;
+#                      warn " @$tags[$i]";
                        if ($nb_active==1) {
                                if (@$operator[$i] eq "start") {
                                        $sql_tables .= "auth_subfield_table as m$nb_table,";
@@ -844,6 +856,57 @@ sub nsb_clean {
        return($string) ;
 }
 
+sub FindDuplicate {
+       my ($record,$authtypecode)=@_;
+       warn "IN for ".$record->as_formatted;
+       my $dbh = C4::Context->dbh;
+
+#      warn "".$record->as_formatted;
+       my $sth = $dbh->prepare("select auth_tag_to_report,summary from auth_types where authtypecode=?");
+       $sth->execute($authtypecode);
+       my ($auth_tag_to_report,$taglist) = $sth->fetchrow;
+       $sth->finish;
+       # build a request for authoritysearch
+       my (@tags, @and_or, @excluding, @operator, @value, $offset, $length);
+       # search on biblio.title
+#      warn " tag a reporter : $auth_tag_to_report";
+#      warn "taglist ".$taglist;
+       my @subfield = split /\[/,  $taglist;
+       my $max = @subfield;
+       for (my $i=1; $i<$max;$i++){
+               warn " ".$subfield[$i];
+               $subfield[$i]=substr($subfield[$i],3,1);
+#              warn " ".$subfield[$i];
+       }
+       
+       if ($record->fields($auth_tag_to_report)) {
+               my $sth = $dbh->prepare("select tagfield,tagsubfield from auth_subfield_structure where tagfield=? and authtypecode=? ");
+               $sth->execute($auth_tag_to_report,$authtypecode);
+#              warn " field $auth_tag_to_report exists";
+               while (my ($tag,$subfield) = $sth->fetchrow){
+                       if ($record->field($tag)->subfield($subfield)) {
+                               warn "tag :".$tag." subfield: $subfield value : ".$record->field($tag)->subfield($subfield);
+                               push @tags, $tag.$subfield;
+#                              warn "'".$tag.$subfield."' value :". $record->field($tag)->subfield($subfield);
+                               push @and_or, "and";
+                               push @excluding, "";
+                               push @operator, "=";
+                               push @value, $record->field($tag)->subfield($subfield);
+                       }
+               }
+       }
+       my ($finalresult,$nbresult) = authoritysearch($dbh,\@tags,\@and_or,\@excluding,\@operator,\@value,0,10,$authtypecode);
+       # there is at least 1 result => return the 1st one
+       if ($nbresult) {
+               warn "XXXXX $nbresult => ".@$finalresult[0]->{authid},@$finalresult[0]->{summary};
+               return @$finalresult[0]->{authid},@$finalresult[0]->{summary};
+       }
+       # no result, returns nothing
+       return;
+}
+
+
 END { }       # module clean-up code here (global destructor)
 
 =back
@@ -858,8 +921,14 @@ Paul POULAIN paul.poulain@free.fr
 
 # $Id$
 # $Log$
-# Revision 1.10  2005/03/01 13:40:48  tipaul
-# merging 2.2 branch with head. Sorry for not making it before, many many commits done here
+# Revision 1.17  2005/06/01 12:51:02  tipaul
+# some fixes & improvements for dictionnary search in librarian interface
+#
+# Revision 1.16  2005/05/04 15:43:43  tipaul
+# synch'ing 2.2 and head
+#
+# Revision 1.9.2.3  2005/04/28 08:45:33  tipaul
+# porting FindDuplicate feature for authorities from HEAD to rel_2_2, works correctly now.
 #
 # Revision 1.9.2.2  2005/02/28 14:03:13  tipaul
 # * adding search on "main entry" (ie $a subfield) on a given authority (the "search everywhere" field is still here).