Plugin for field 124e
[koha.git] / C4 / AuthoritiesMarc.pm
index 13324b4..c5a9dba 100644 (file)
@@ -51,6 +51,7 @@ $VERSION = 0.01;
        &AUTHaddword
        &MARCaddword &MARCdelword
        &char_decode
+       &FindDuplicate
  );
 
 sub authoritysearch {
@@ -61,7 +62,27 @@ sub authoritysearch {
        #               where m1.authid=m2.authid and
        #               (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
 
+       # 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") {
+                       @$tags[$i] = $tag_to_report."a";
+               }
+       }
+
        # "Normal" statements
+       # quote marc fields/subfields
+       for (my $i=0;$i<=$#{$tags};$i++) {
+               if (@$tags[$i]) {
+                       @$tags[$i] = $dbh->quote(@$tags[$i]);
+               }
+       }
        my @normal_tags = ();
        my @normal_and_or = ();
        my @normal_operator = ();
@@ -69,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]))
@@ -132,7 +158,7 @@ sub authoritysearch {
                                        my $subfieldcode = $subf[$i][0];
                                        my $subfieldvalue = $subf[$i][1];
                                        my $tagsubf = $tag.$subfieldcode;
-                                       $summary =~ s/\[(.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue\[$1$tagsubf$2]$2$3/g;
+                                       $summary =~ s/\[(.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
                                }
                        }
                }
@@ -156,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;
        }
@@ -178,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,";
@@ -511,7 +540,11 @@ sub AUTHmodauthority {
 # 2nd recreate it
        &AUTHdelauthority($dbh,$authid,1);
        &AUTHaddauthority($dbh,$record,$authid,AUTHfind_authtypecode($dbh,$authid));
-       # FIXME : modify the authority in biblio too.
+       # save the file in localfile/modified_authorities
+       my $filename = C4::Context->config("intranetdir")."/localfile/modified_authorities/$authid.authid";
+       open AUTH, "> $filename";
+       print AUTH $authid;
+       close AUTH;
 }
 
 sub AUTHdelauthority {
@@ -823,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
@@ -837,6 +921,28 @@ Paul POULAIN paul.poulain@free.fr
 
 # $Id$
 # $Log$
+# 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).
+# * adding a select box to requet "contain" or "begin with" search.
+# * fixing some bug in authority search (related to "main entry" search)
+#
+# Revision 1.9.2.1  2005/02/24 13:12:13  tipaul
+# saving authority modif in a text file. This will be used soon with another script (in crontab). The script in crontab will retrieve every authorityid in the directory localfile/authorities and modify every biblio using this authority. Those modifs may be long. So they can't be done through http, because we may encounter a webserver timeout, and kill the process before end of the job.
+# So, it will be done through a cron job.
+# (/me agree we need some doc for command line scripts)
+#
+# Revision 1.9  2004/12/23 09:48:11  tipaul
+# Minor changes in summary "exploding" (the 3 digits AFTER the subfield were not on the right place).
+#
 # Revision 1.8  2004/11/05 10:11:39  tipaul
 # export auth_count_usage (bugfix)
 #