Bug 19926: Add the Koha::Object->unblessed_all_relateds method
[koha-ffzg.git] / Koha / Libraries.pm
index fefc3ab..38a61a4 100644 (file)
@@ -45,14 +45,21 @@ Koha::Libraries - Koha Library Object set class
 sub search_filtered {
     my ( $self, $params, $attributes ) = @_;
 
-    if (    C4::Context->preference('IndependentBranches')
-        and C4::Context->userenv
-        and not C4::Context->IsSuperLibrarian()
-        and C4::Context->userenv->{branch}
-    ) {
-        $params->{branchcode} = C4::Context->userenv->{branch};
+    my @branchcodes;
+    my $userenv = C4::Context->userenv;
+    if ( $userenv and $userenv->{number} ) {
+        my $only_from_group = $params->{only_from_group};
+        if ( $only_from_group ) {
+            my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
+            my @branchcodes = $logged_in_user->libraries_where_can_see_patrons;
+            $params->{branchcode} = { -in => \@branchcodes } if @branchcodes;
+        } else {
+            if ( C4::Context::only_my_library ) {
+                $params->{branchcode} = C4::Context->userenv->{branch};
+            }
+        }
     }
-
+    delete $params->{only_from_group};
     return $self->SUPER::search( $params, $attributes );
 }