Merge branch 'bug_9052' into 3.12-master
[koha.git] / members / member.pl
index df688d1..d25dba9 100755 (executable)
@@ -6,6 +6,7 @@
 
 
 # Copyright 2000-2002 Katipo Communications
+# Copyright 2010 BibLibre
 #
 # This file is part of Koha.
 #
@@ -57,7 +58,8 @@ my $branches = GetBranches;
 my @branchloop;
 
 foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
-  my $selected = 1 if $branches->{$_}->{branchcode} eq $$patron{branchcode};
+  my $selected;
+  $selected = 1 if $branches->{$_}->{branchcode} eq $$patron{branchcode};
   my %row = ( value => $_,
         selected => $selected,
         branchname => $branches->{$_}->{branchname},
@@ -74,10 +76,11 @@ foreach my $category (@categories){
                         };
        $categories_dislay{$$category{categorycode}} = $hash;
 }
+my $AddPatronLists = C4::Context->preference("AddPatronLists") || '';
 $template->param( 
-        "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
+        "AddPatronLists_$AddPatronLists" => "1",
             );
-if (C4::Context->preference("AddPatronLists")=~/code/){
+if ($AddPatronLists=~/code/){
     $categories[0]->{'first'}=1;
 }  
 
@@ -92,22 +95,35 @@ else {
        @orderby = ({surname=>0},{firstname=>0});
 }
 
+my $searchfields = $input->param('searchfields');
+my @searchfields = $searchfields ? split( ',', $searchfields ) : ( "firstname", "surname", "othernames", "cardnumber", "userid", "email" );
+
 $member =~ s/,//g;   #remove any commas from search string
 $member =~ s/\*/%/g;
 
+my $from = ( $startfrom - 1 ) * $resultsperpage;
+my $to   = $from + $resultsperpage;
+
 my ($count,$results);
+if ($member || keys %$patron) {
+    #($results)=Search($member || $patron,{surname=>1,firstname=>1},[$from,$to],undef,["firstname","surname","email","othernames"]  );
+    my $search_scope = ( $quicksearch ? "field_start_with" : "start_with" );
+    ($results) = Search( $member || $patron, \@orderby, undef, undef, \@searchfields, $search_scope );
+}
 
-my @searchpatron;
-push @searchpatron, $member if ($member);
-push @searchpatron, $patron if (keys %$patron);
-my $from= ($startfrom-1)*$resultsperpage;
-my $to=$from+$resultsperpage;
- #($results)=Search(\@searchpatron,{surname=>1,firstname=>1},[$from,$to],undef,["firstname","surname","email","othernames"]  ) if (@searchpatron);
- my $search_scope=($quicksearch?"field_start_with":"contain");
- ($results)=Search(\@searchpatron,\@orderby,undef,undef,["firstname","surname","email","othernames","cardnumber","userid"],$search_scope  ) if (@searchpatron);
-if ($results){
-       $count =scalar(@$results);
+if ($results) {
+       for my $field ('categorycode','branchcode'){
+               next unless ($patron->{$field});
+               @$results = grep { $_->{$field} eq $patron->{$field} } @$results; 
+       }
+    $count = scalar(@$results);
 }
+
+if($count == 1){
+    print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=" . @$results[0]->{borrowernumber});
+    exit;
+}
+
 my @resultsdata;
 $to=($count>$to?$to:$count);
 my $index=$from;
@@ -119,12 +135,13 @@ foreach my $borrower(@$results[$from..$to-1]){
 
   my %row = (
     count => $index++,
-       %$borrower,
-       %{$categories_dislay{$$borrower{categorycode}}},
+    %$borrower,
+    (defined $categories_dislay{ $borrower->{categorycode} }?   %{ $categories_dislay{ $borrower->{categorycode} } }:()),
     overdues => $od,
     issues => $issue,
     odissue => "$od/$issue",
     fines =>  sprintf("%.2f",$fines),
+    branchname => $branches->{$borrower->{branchcode}}->{branchname},
     );
   push(@resultsdata, \%row);
 }
@@ -158,7 +175,7 @@ $template->param(
     startfrom => $startfrom,
     from      => ($startfrom-1)*$resultsperpage+1,  
     to        => $to,
-    multipage => ($count != $to+1 || $startfrom!=1),
+    multipage => ($count != $to || $startfrom!=1),
     advsearch => ($$patron{categorycode} || $$patron{branchcode}),
     branchloop=>\@branchloop,
     categories=>\@categories,