remove use of 2xx$3 in Building hierarchy
[koha.git] / C4 / AuthoritiesMarc.pm
index 21c98b2..aef8b6c 100644 (file)
@@ -25,6 +25,7 @@ use C4::Search;
 use C4::AuthoritiesMarc::MARC21;
 use C4::AuthoritiesMarc::UNIMARC;
 use C4::Charset;
+use C4::Debug;
 
 use vars qw($VERSION @ISA @EXPORT);
 
@@ -101,7 +102,7 @@ returns ref to array result and count of results returned
 
 sub SearchAuthorities {
     my ($tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby) = @_;
-    #use Data::Dumper; map {warn "CALL : ".Data::Dumper::Dumper($_);} @_;
+#     warn "CALL : $tags, $and_or, $excluding, $operator, $value, $offset,$length,$authtypecode,$sortby";
     my $dbh=C4::Context->dbh;
     if (C4::Context->preference('NoZebra')) {
     
@@ -204,8 +205,9 @@ sub SearchAuthorities {
         my $n=0;
         my @authtypecode;
         my @auths=split / /,$authtypecode ;
+               my @queries;
         foreach my  $auth (@auths){
-            $query .=" \@attr 1=authtype \@attr 5=100 ".$auth; ##No truncation on authtype
+            push @queries, " \@attr 1=authtype \@attr 5=100 ".$auth; ##No truncation on authtype
             push @authtypecode ,$auth;
             $n++;
         }
@@ -213,14 +215,17 @@ sub SearchAuthorities {
             while ($n>1){$query= "\@or ".$query;$n--;}
         }
         
+        my $dosearch;
         my $and=" \@and " ;
-        my $q2="";
         for(my $i = 0 ; $i <= $#{$value} ; $i++)
         {
             if (@$value[$i]){
             ##If mainentry search $a tag
                 if (@$tags[$i] eq "mainmainentry") {
-                $attr =" \@attr 1=Heading-Main ";
+
+                $attr =" \@attr 1=Heading-Main "; 
+#                $attr =" \@attr 1=Heading ";
+
                 }elsif (@$tags[$i] eq "mainentry") {
                 $attr =" \@attr 1=Heading ";
                 }else{
@@ -236,16 +241,15 @@ sub SearchAuthorities {
                     $attr .=" \@attr 5=1 \@attr 4=6 ";## Word list, right truncated, anywhere
                 }
                 $attr =$attr."\"".@$value[$i]."\"";
-                $q2 =($q2 ne "" ?$and.$q2.$attr:$attr);
+                push @queries, "$attr";
+            $dosearch=1;
             }#if value
         }
         ##Add how many queries generated
-        if ($query=~/\S+/ && $q2 ne ""){    
-          $query= $and.$query.$q2;
-        } 
-        elsif ($q2 ne "") {
-          $query=$q2;
-        }
+               my $query;
+               foreach my $query_part (@queries){
+                       $query=($query?$and.$query_part.$query:$query_part);
+               }
         ## Adding order
         #$query=' @or  @attr 7=2 @attr 1=Heading 0 @or  @attr 7=1 @attr 1=Heading 1'.$query if ($sortby eq "HeadingDsc");
         my $orderstring= ($sortby eq "HeadingAsc"?
@@ -255,8 +259,8 @@ sub SearchAuthorities {
                             '@attr 7=2 @attr 1=Heading 0'
                            :''
                         );            
-        my  $allrecords=" \@attr 1=_ALLRECORDS \@attr 2=103 '' ";
-        $query=($q2?"\@or $orderstring $query":"\@or $orderstring ".($query?"\@and $allrecords $query":$allrecords) );
+        $query=($dosearch?"\@or $orderstring $query":"\@or ".($query?"$and $query":"")." \@attr 1=_ALLRECORDS \@attr 2=103 '' $orderstring ");
+        $debug && warn $query;
         
         $offset=0 unless $offset;
         my $counter = $offset;
@@ -1104,7 +1108,7 @@ sub BuildUnimarcHierarchy{
   my $record = shift @_;
   my $class = shift @_;
   my $authid_constructed = shift @_;
-  my $authid=$record->subfield('250','3');
+  my $authid=$record->field('001')->data();
   my %cell;
   my $parents=""; my $children="";
   my (@loopparents,@loopchildren);
@@ -1125,7 +1129,7 @@ sub BuildUnimarcHierarchy{
   $cell{"class"}=$class;
   $cell{"loopauthid"}=$authid;
   $cell{"current_value"} =1 if $authid eq $authid_constructed;
-  $cell{"value"}=$record->subfield('250',"a");
+  $cell{"value"}=$record->subfield('2..',"a");
   return \%cell;
 }