View | Details | Raw Unified | Return to bug 18403
Collapse All | Expand All

(-)a/C4/Utils/DataTables/Members.pm (-18 / +47 lines)
Lines 19-52 sub search { Link Here
19
        $searchmember = $dt_params->{sSearch} // '';
19
        $searchmember = $dt_params->{sSearch} // '';
20
    }
20
    }
21
21
22
    my ($sth, $query, $iTotalRecords, $iTotalDisplayRecords);
22
    # If branches are independent and user is not superlibrarian
23
    # The search has to be only on the user branch
24
    my $userenv = C4::Context->userenv;
25
    my @restricted_branchcodes;
26
    if (C4::Context::only_my_library) {
27
        push @restricted_branchcodes, $userenv->{branch};
28
    }
29
    else {
30
        my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
31
        unless (
32
            $logged_in_user->can(
33
                { borrowers => 'view_borrower_infos_from_any_libraries' }
34
            )
35
          )
36
        {
37
            if ( my $library_groups = $logged_in_user->library->library_groups )
38
            {
39
                while ( my $library_group = $library_groups->next ) {
40
                    push @restricted_branchcodes,
41
                      $library_group->parent->children->get_column('branchcode');
42
                }
43
            }
44
            else {
45
                push @restricted_branchcodes, $userenv->{branch};
46
            }
47
        }
48
    }
49
50
    my ($sth, $query, $iTotalQuery, $iTotalRecords, $iTotalDisplayRecords);
23
    my $dbh = C4::Context->dbh;
51
    my $dbh = C4::Context->dbh;
24
    # Get the iTotalRecords DataTable variable
52
    # Get the iTotalRecords DataTable variable
25
    $query = "SELECT COUNT(borrowers.borrowernumber) FROM borrowers";
53
    $query = $iTotalQuery = "SELECT COUNT(borrowers.borrowernumber) FROM borrowers";
26
    $sth = $dbh->prepare($query);
54
    if ( @restricted_branchcodes ) {
27
    $sth->execute;
55
        $iTotalQuery .= " WHERE borrowers.branchcode IN (" . join( ',', ('?') x @restricted_branchcodes ) . ")";
28
    ($iTotalRecords) = $sth->fetchrow_array;
56
    }
57
    ($iTotalRecords) = $dbh->selectrow_array( $iTotalQuery, undef, @restricted_branchcodes );
58
59
    # Do that after iTotalQuery!
60
    if ( defined $branchcode and $branchcode ) {
61
        @restricted_branchcodes = @restricted_branchcodes
62
            ? grep { /^$branchcode$/ } @restricted_branchcodes
63
                ? ($branchcode)
64
                : (undef) # Do not return any results
65
            : ($branchcode);
66
    }
29
67
30
    if ( $searchfieldstype eq 'dateofbirth' ) {
68
    if ( $searchfieldstype eq 'dateofbirth' ) {
31
        # Return an empty list if the date of birth is not correctly formatted
69
        # Return an empty list if the date of birth is not correctly formatted
32
        $searchmember = eval { output_pref( { str => $searchmember, dateformat => 'iso', dateonly => 1 } ); };
70
        $searchmember = eval { output_pref( { str => $searchmember, dateformat => 'iso', dateonly => 1 } ); };
33
        if ( $@ or not $searchmember ) {
71
        if ( $@ or not $searchmember ) {
34
            return {
72
            return {
35
                iTotalRecords        => 0,
73
                iTotalRecords        => $iTotalRecords,
36
                iTotalDisplayRecords => 0,
74
                iTotalDisplayRecords => 0,
37
                patrons              => [],
75
                patrons              => [],
38
            };
76
            };
39
        }
77
        }
40
    }
78
    }
41
79
42
    # If branches are independent and user is not superlibrarian
43
    # The search has to be only on the user branch
44
    if ( C4::Context::only_my_library ) {
45
        my $userenv = C4::Context->userenv;
46
        $branchcode = $userenv->{'branch'};
47
48
    }
49
50
    my $select = "SELECT
80
    my $select = "SELECT
51
        borrowers.borrowernumber, borrowers.surname, borrowers.firstname,
81
        borrowers.borrowernumber, borrowers.surname, borrowers.firstname,
52
        borrowers.streetnumber, borrowers.streettype, borrowers.address,
82
        borrowers.streetnumber, borrowers.streettype, borrowers.address,
Lines 69-77 sub search { Link Here
69
        push @where_strs, "borrowers.categorycode = ?";
99
        push @where_strs, "borrowers.categorycode = ?";
70
        push @where_args, $categorycode;
100
        push @where_args, $categorycode;
71
    }
101
    }
72
    if(defined $branchcode and $branchcode ne '') {
102
    if(@restricted_branchcodes ) {
73
        push @where_strs, "borrowers.branchcode = ?";
103
        push @where_strs, "borrowers.branchcode IN (" . join( ',', ('?') x @restricted_branchcodes ) . ")";
74
        push @where_args, $branchcode;
104
        push @where_args, @restricted_branchcodes;
75
    }
105
    }
76
106
77
    my $searchfields = {
107
    my $searchfields = {
78
- 

Return to bug 18403