Bug 14299: Today's checkouts not always sorting correctly
[koha.git] / svc / members / search
index 0c67a19..6343e2e 100755 (executable)
@@ -4,23 +4,23 @@
 #
 # This file is part of Koha
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 use CGI;
 
-use C4::Auth qw( get_template_and_user );
+use C4::Auth qw( get_template_and_user haspermission get_user_subpermissions );
 use C4::Output qw( output_with_http_headers );
 use C4::Utils::DataTables qw( dt_get_params );
 use C4::Utils::DataTables::Members qw( search );
@@ -44,6 +44,8 @@ my $categorycode = $input->param('categorycode');
 my $branchcode = $input->param('branchcode');
 my $searchtype = $input->param('searchtype');
 my $searchfieldstype = $input->param('searchfieldstype') || 'standard';
+my $has_permission = $input->param('has_permission');
+my $selection_type = $input->param('selection_type');
 
 if ( $searchfieldstype eq "dateofbirth" ) {
     $searchmember = output_pref({dt => dt_from_string($searchmember), dateformat => 'iso', dateonly => 1});
@@ -60,7 +62,8 @@ foreach (grep {$_ =~ /^mDataProp/} keys %dt_params) {
 my $results;
 # If the user filled a term, maybe it's a cardnumber.
 # This cannot be the case if a first letter is given.
-if (    not $firstletter
+if ( $searchmember
+    and not $firstletter
     and $searchfieldstype
     and $searchfieldstype eq 'standard' )
 {
@@ -82,15 +85,39 @@ $results = C4::Utils::DataTables::Members::search(
         searchtype => $searchtype,
         searchfieldstype => $searchfieldstype,
         dt_params => \%dt_params,
-
     }
 ) unless $results;
 
+# It is not recommanded to use the has_permission param if you use the pagination
+# The filter is done AFTER requested the data
+if ($has_permission) {
+    my ( $permission, $subpermission ) = split /\./, $has_permission;
+    my @patrons_with_permission;
+    for my $patron ( @{ $results->{patrons} } ) {
+        my $perms = haspermission( $patron->{userid} );
+        if (   $perms->{superlibrarian} == 1
+            or $perms->{$permission} == 1 )
+        {
+            push @patrons_with_permission, $patron;
+            next;
+        }
+
+        if ($subpermission) {
+            my $subperms = get_user_subpermissions( $patron->{userid} );
+            push @patrons_with_permission, $patron
+              if $subperms->{$permission}->{$subpermission};
+        }
+    }
+    $results->{patrons} = \@patrons_with_permission;
+    $results->{iTotalDisplayRecords} = scalar( @patrons_with_permission );
+}
+
 $template->param(
     sEcho => $sEcho,
     iTotalRecords => $results->{iTotalRecords},
     iTotalDisplayRecords => $results->{iTotalDisplayRecords},
-    aaData => $results->{patrons}
+    aaData => $results->{patrons},
+    selection_type => $selection_type,
 );
 
 output_with_http_headers $input, $cookie, $template->output, 'json';