Function GetRecentShelves modified so it can tell "koha" user and no user apart
[koha_fer] / C4 / Members.pm
index 3dcd46a..4b525f0 100644 (file)
@@ -28,7 +28,8 @@ use C4::Overdues;
 use C4::Reserves;
 use C4::Accounts;
 use C4::Biblio;
-use C4::SQLHelper qw(InsertInTable UpdateInTable);
+use C4::SQLHelper qw(InsertInTable UpdateInTable SearchInTable);
+use C4::Members::Attributes qw(SearchIdMatchingAttribute);
 
 our ($VERSION,@ISA,@EXPORT,@EXPORT_OK,$debug);
 
@@ -39,6 +40,7 @@ BEGIN {
        @ISA = qw(Exporter);
        #Get data
        push @EXPORT, qw(
+               &Search
                &SearchMember 
                &GetMemberDetails
                &GetMember
@@ -164,7 +166,6 @@ C<$count> is the number of elements in C<$borrowers>.
 
 #'
 #used by member enquiries from the intranet
-#called by member.pl and circ/circulation.pl
 sub SearchMember {
     my ($searchstring, $orderby, $type,$category_type,$filter,$showallbranches ) = @_;
     my $dbh   = C4::Context->dbh;
@@ -184,7 +185,6 @@ sub SearchMember {
     if (@$data){
         return ( scalar(@$data), $data );
     }
-    $sth->finish;
 
     if ( $type eq "simple" )    # simple search for one letter only
     {
@@ -229,16 +229,6 @@ sub SearchMember {
         }
         $query = $query . ") OR cardnumber LIKE ? ";
         push( @bind, $searchstring );
-        if (C4::Context->preference('ExtendedPatronAttributes')) {
-            $query .= "OR borrowernumber IN (
-SELECT borrowernumber
-FROM borrower_attributes
-JOIN borrower_attribute_types USING (code)
-WHERE staff_searchable = 1
-AND attribute like ?
-)";
-            push (@bind, $searchstring);
-        }
         $query .= "order by $orderby";
 
         # FIXME - .= <<EOT;
@@ -251,10 +241,28 @@ AND attribute like ?
     my @results;
     $data = $sth->fetchall_arrayref({});
 
-    $sth->finish;
     return ( scalar(@$data), $data );
 }
 
+sub Search {
+    my ($filter,$orderby, $limit, $columns_out, $search_on_fields,$searchtype) = @_;
+       my @filters;
+       if (ref($filter) eq "ARRAY"){
+               push @filters,@$filter;
+       }
+       else {
+               push @filters,$filter;
+       }
+    if (C4::Context->preference('ExtendedPatronAttributes')) {
+               my $matching_records = C4::Members::Attributes::SearchIdMatchingAttribute($filter);
+               push @filters,@$matching_records;
+    }
+       $searchtype||="start_with";
+       my $data=SearchInTable("borrowers",\@filters,$orderby,$limit,$columns_out,$search_on_fields,$searchtype);
+
+    return ( $data );
+}
+
 =head2 GetMemberDetails
 
 ($borrower) = &GetMemberDetails($borrowernumber, $cardnumber);
@@ -317,7 +325,6 @@ sub GetMemberDetails {
             $accessflagshash->{$flag} = 1;
         }
     }
-    $sth->finish;
     $borrower->{'flags'}     = $flags;
     $borrower->{'authflags'} = $accessflagshash;
 
@@ -596,7 +603,6 @@ sub GetMemberIssuesAndFines {
     my $sth = $dbh->prepare($query);
     $sth->execute($borrowernumber);
     my $issue_count = $sth->fetchrow_arrayref->[0];
-    $sth->finish;
 
     $sth = $dbh->prepare(
         "SELECT COUNT(*) FROM issues 
@@ -605,12 +611,10 @@ sub GetMemberIssuesAndFines {
     );
     $sth->execute($borrowernumber);
     my $overdue_count = $sth->fetchrow_arrayref->[0];
-    $sth->finish;
 
     $sth = $dbh->prepare("SELECT SUM(amountoutstanding) FROM accountlines WHERE borrowernumber = ?");
     $sth->execute($borrowernumber);
     my $total_fines = $sth->fetchrow_arrayref->[0];
-    $sth->finish;
 
     return ($overdue_count, $issue_count, $total_fines);
 }
@@ -857,7 +861,6 @@ sub GetGuarantees {
 
     my @dat;
     my $data = $sth->fetchall_arrayref({}); 
-    $sth->finish;
     return ( scalar(@$data), $data );
 }
 
@@ -889,7 +892,6 @@ sub UpdateGuarantees {
         |;
         my $sth3 = $dbh->prepare($guaquery);
         $sth3->execute;
-        $sth3->finish;
     }
 }
 =head2 GetPendingIssues
@@ -1034,9 +1036,7 @@ sub GetAllIssues {
             $result[$i] = $data2;
             $i++;
         }
-        $sth2->finish;
     }
-    $sth->finish;
 
     return ( $i, \@result );
 }
@@ -1084,7 +1084,6 @@ sub GetMemberAccountRecords {
         $total += int(1000 * $data->{'amountoutstanding'}); # convert float to integer to avoid round-off errors
     }
     $total /= 1000;
-    $sth->finish;
     return ( $total, \@acctlines,$numlines);
 }
 
@@ -1127,7 +1126,6 @@ sub GetBorNotifyAcctRecord {
         $total += int(100 * $data->{'amountoutstanding'});
     }
     $total /= 100;
-    $sth->finish;
     return ( $total, \@acctlines, $numlines );
 }
 
@@ -1166,7 +1164,6 @@ sub checkuniquemember {
         $sth->execute( uc($surname), ucfirst($firstname));
     }
     my @data = $sth->fetchrow;
-    $sth->finish;
     ( $data[0] ) and return $data[0], $data[1];
     return 0;
 }
@@ -1188,7 +1185,6 @@ sub checkcardnumber {
     else {
         return 0;
     }
-    $sth->finish();
 }  
 
 
@@ -1307,7 +1303,6 @@ sub GetborCatFromCatType {
         push @codes, $data->{'categorycode'};
         $labels{ $data->{'categorycode'} } = $data->{'description'};
     }
-    $sth->finish;
     return ( \@codes, \%labels );
 }
 
@@ -1336,7 +1331,6 @@ sub GetBorrowercategory {
         $sth->execute($catcode);
         my $data =
         $sth->fetchrow_hashref;
-        $sth->finish();
         return $data;
     } 
     return;  
@@ -1360,7 +1354,6 @@ sub GetBorrowercategoryList {
     $sth->execute;
     my $data =
     $sth->fetchall_arrayref({});
-    $sth->finish();
     return $data;
 }    # sub getborrowercategory
 
@@ -1387,7 +1380,6 @@ sub ethnicitycategories {
         push @codes, $data->{'code'};
         $labels{ $data->{'code'} } = $data->{'name'};
     }
-    $sth->finish;
     return ( \@codes, \%labels );
 }
 
@@ -1410,7 +1402,6 @@ sub fixEthnicity {
     my $sth       = $dbh->prepare("Select name from ethnicity where code = ?");
     $sth->execute($ethnicity);
     my $data = $sth->fetchrow_hashref;
-    $sth->finish;
     return $data->{'name'};
 }    # sub fixEthnicity
 
@@ -1460,7 +1451,6 @@ sub get_institutions {
     while ( my $data = $sth->fetchrow_hashref() ) {
         $orgs{ $data->{'borrowernumber'} } = $data;
     }
-    $sth->finish();
     return ( \%orgs );
 
 }    # sub get_institutions
@@ -1483,7 +1473,6 @@ sub add_member_orgs {
     foreach my $otherborrowernumber (@$otherborrowers) {
         $sth->execute( $borrowernumber, $otherborrowernumber );
     }
-    $sth->finish();
 
 }    # sub add_member_orgs
 
@@ -1521,7 +1510,6 @@ sub GetCities {
 
 #test to know if the table contain some records if no the function return nothing
     my $id = @id;
-    $sth->finish;
     if ( $id == 1 ) {
         # all we have is the one blank row
         return ();
@@ -1605,7 +1593,6 @@ sub DelMember {
           WHERE borrowernumber=?|;
     my $sth = $dbh->prepare($query);
     $sth->execute($borrowernumber);
-    $sth->finish;
     $query = "
        DELETE
        FROM borrowers
@@ -1613,7 +1600,6 @@ sub DelMember {
    ";
     $sth = $dbh->prepare($query);
     $sth->execute($borrowernumber);
-    $sth->finish;
     logaction("MEMBERS", "DELETE", $borrowernumber, "") if C4::Context->preference("BorrowersLog");
     return $sth->rows;
 }
@@ -1683,7 +1669,6 @@ ORDER BY road_type|;
 
 #test to know if the table contain some records if no the function return nothing
     my $id = @id;
-    $sth->finish;
     if ( $id eq 0 ) {
         return ();
     }