use Modern::Perl;
use C4::Context;
-use C4::Utils::DataTables;
-use Koha::DateUtils;
-use C4::Members::Attributes qw(SearchIdMatchingAttribute );
+use C4::Utils::DataTables qw( dt_build_orderby );
+use Koha::DateUtils qw( dt_from_string output_pref );
sub search {
my ( $params ) = @_;
my $branchcode = $params->{branchcode};
my $searchtype = $params->{searchtype} || 'contain';
my $searchfieldstype = $params->{searchfieldstype} || 'standard';
+ my $has_permission = $params->{has_permission};
my $dt_params = $params->{dt_params};
unless ( $searchmember ) {
# If branches are independent and user is not superlibrarian
# The search has to be only on the user branch
my $userenv = C4::Context->userenv;
- my @restricted_branchcodes;
- if (C4::Context::only_my_library) {
- push @restricted_branchcodes, $userenv->{branch};
- }
- else {
- my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
- unless (
- $logged_in_user->can(
- { borrowers => 'view_borrower_infos_from_any_libraries' }
- )
- )
- {
- if ( my $library_groups = $logged_in_user->library->library_groups )
- {
- while ( my $library_group = $library_groups->next ) {
- push @restricted_branchcodes,
- $library_group->parent->children->get_column('branchcode');
- }
- }
- else {
- push @restricted_branchcodes, $userenv->{branch};
- }
- }
- }
+ my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
+ my @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
my ($sth, $query, $iTotalQuery, $iTotalRecords, $iTotalDisplayRecords);
my $dbh = C4::Context->dbh;
+
+ # Get the module_bit from a given permission code
+ if ( $has_permission ) {
+ ($has_permission->{module_bit}) = $dbh->selectrow_array(q|
+ SELECT bit FROM userflags WHERE flag=?
+ |, undef, $has_permission->{permission});
+ }
+
+ my (@where, @conditions);
# Get the iTotalRecords DataTable variable
- $query = $iTotalQuery = "SELECT COUNT(borrowers.borrowernumber) FROM borrowers";
+ $iTotalQuery = "SELECT COUNT(borrowers.borrowernumber) FROM borrowers";
+ if ( $has_permission ) {
+ $iTotalQuery .= ' LEFT JOIN user_permissions ON borrowers.borrowernumber=user_permissions.borrowernumber';
+ $iTotalQuery .= ' AND module_bit=? AND code=?';
+ push @conditions, $has_permission->{module_bit}, $has_permission->{subpermission};
+ }
+
if ( @restricted_branchcodes ) {
- $iTotalQuery .= " WHERE borrowers.branchcode IN (" . join( ',', ('?') x @restricted_branchcodes ) . ")";
+ push @where, "borrowers.branchcode IN (" . join( ',', ('?') x @restricted_branchcodes ) . ")";
+ push @conditions, @restricted_branchcodes;
+ }
+ if ( $has_permission ) {
+ push @where, '( borrowers.flags = 1 OR borrowers.flags & (1 << ?) OR module_bit=? AND code=? )';
+ push @conditions, ($has_permission->{module_bit}) x 2, $has_permission->{subpermission};
}
- ($iTotalRecords) = $dbh->selectrow_array( $iTotalQuery, undef, @restricted_branchcodes );
+ $iTotalQuery .= ' WHERE ' . join ' AND ', @where if @where;
+ ($iTotalRecords) = $dbh->selectrow_array( $iTotalQuery, undef, @conditions );
# Do that after iTotalQuery!
if ( defined $branchcode and $branchcode ) {
@restricted_branchcodes = @restricted_branchcodes
- ? grep { /^$branchcode$/ } @restricted_branchcodes
+ ? grep ({ $_ eq $branchcode } @restricted_branchcodes)
? ($branchcode)
: (undef) # Do not return any results
: ($branchcode);
my $select = "SELECT
borrowers.borrowernumber, borrowers.surname, borrowers.firstname,
+ borrowers.othernames,
+ borrowers.flags,
borrowers.streetnumber, borrowers.streettype, borrowers.address,
borrowers.address2, borrowers.city, borrowers.state, borrowers.zipcode,
borrowers.country, cardnumber, borrowers.dateexpiry,
borrowers.borrowernotes, borrowers.branchcode, borrowers.email,
borrowers.userid, borrowers.dateofbirth, borrowers.categorycode,
categories.description AS category_description, categories.category_type,
- branches.branchname";
+ branches.branchname, borrowers.phone";
my $from = "FROM borrowers
- LEFT JOIN branches ON borrowers.branchcode = branches.branchcode
- LEFT JOIN categories ON borrowers.categorycode = categories.categorycode";
+ LEFT JOIN branches ON borrowers.branchcode = branches.branchcode
+ LEFT JOIN categories ON borrowers.categorycode = categories.categorycode";
my @where_args;
+ if ( $has_permission ) {
+ $from .= '
+ LEFT JOIN user_permissions ON borrowers.borrowernumber=user_permissions.borrowernumber
+ AND module_bit=? AND code=?';
+ push @where_args, $has_permission->{module_bit}, $has_permission->{subpermission};
+ }
my @where_strs;
if(defined $firstletter and $firstletter ne '') {
push @where_strs, "borrowers.surname LIKE ?";
my $searchfields = {
standard => C4::Context->preference('DefaultPatronSearchFields') || 'surname,firstname,othernames,cardnumber,userid',
- surname => 'surname',
email => 'email,emailpro,B_email',
borrowernumber => 'borrowernumber',
- userid => 'userid',
phone => 'phone,phonepro,B_phone,altcontactphone,mobile',
- address => 'streettype,address,address2,city,state,zipcode,country',
- dateofbirth => 'dateofbirth',
- sort1 => 'sort1',
- sort2 => 'sort2',
+ address => 'streetnumber,streettype,address,address2,city,state,zipcode,country',
};
# * is replaced with % for sql
}
my @where_strs_or;
- for my $searchfield ( split /,/, $searchfields->{$searchfieldstype} ) {
- push @where_strs_or, "borrowers." . $dbh->quote_identifier($searchfield) . " LIKE ?";
+ if ( defined $searchfields->{$searchfieldstype} ) {
+ for my $searchfield ( split /,/, $searchfields->{$searchfieldstype} ) {
+ push @where_strs_or, "borrowers." . $dbh->quote_identifier($searchfield) . " LIKE ?";
+ push @where_args, $term;
+ }
+ } else {
+ push @where_strs_or, "borrowers." . $dbh->quote_identifier($searchfieldstype) . " LIKE ?";
push @where_args, $term;
}
+
if ( $searchfieldstype eq 'standard' and C4::Context->preference('ExtendedPatronAttributes') and $searchmember ) {
- my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($searchmember);
+ my @matching_borrowernumbers = Koha::Patrons->filter_by_attribute_value($searchmember)->get_column('borrowernumber');
- for my $borrowernumber ( @$matching_borrowernumbers ) {
+ for my $borrowernumber ( @matching_borrowernumbers ) {
push @where_strs_or, "borrowers.borrowernumber = ?";
push @where_args, $borrowernumber;
}
if @where_strs_or;
}
- my $where;
- $where = " WHERE " . join (" AND ", @where_strs) if @where_strs;
+ if ( $has_permission ) {
+ push @where_strs, '( borrowers.flags = 1 OR borrowers.flags & (1 << ?) OR module_bit=? AND code=? )';
+ push @where_args, ($has_permission->{module_bit}) x 2, $has_permission->{subpermission};
+ }
+
+ my $where = @where_strs ? " WHERE " . join (" AND ", @where_strs) : undef;
my $orderby = dt_build_orderby($dt_params);
my $limit;
my $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
$patron->{overdues} = $patron_object->get_overdues->count;
$patron->{issues} = $patron_object->checkouts->count;
+ $patron->{age} = $patron_object->get_age;
my $balance = $patron_object->account->balance;
# FIXME Should be formatted from the template
$patron->{fines} = sprintf("%.2f", $balance);
- if($patron->{dateexpiry} and $patron->{dateexpiry} ne '0000-00-00') {
- $patron->{dateexpiry} = output_pref( { dt => dt_from_string( $patron->{dateexpiry}, 'iso'), dateonly => 1} );
+ if( $patron->{dateexpiry} ) {
+ # FIXME We should not format the date here, do it in template-side instead
+ $patron->{dateexpiry} = output_pref( { dt => scalar dt_from_string( $patron->{dateexpiry}, 'iso'), dateonly => 1} );
} else {
$patron->{dateexpiry} = '';
}