Bug 16530: Add a new method to the Branches TT Plugin to avoid c/p
[koha_ffzg] / Koha / Template / Plugin / Branches.pm
index 0b95e42..edbad11 100644 (file)
@@ -25,6 +25,7 @@ use base qw( Template::Plugin );
 
 use C4::Koha;
 use C4::Context;
+use Koha::Libraries;
 
 sub GetName {
     my ( $self, $branchcode ) = @_;
@@ -54,37 +55,29 @@ sub GetURL {
     return $b->{branchurl};
 }
 
-sub OnlyMine {
-    return C4::Branch::onlymine;
-}
-
 sub all {
     my ( $self, $params ) = @_;
     my $selected = $params->{selected};
-    my $dbh = C4::Context->dbh;
-    my @params;
-    my $query = q|
-        SELECT branchcode, branchname
-        FROM branches
-    |;
-    if (    C4::Branch::onlymine
-        and C4::Context->userenv
-        and C4::Context->userenv->{branch} )
-    {
-        $query .= q| WHERE branchcode = ? |;
-        push @params, C4::Context->userenv->{branch};
-    }
-    $query .= q| ORDER BY branchname|;
-    my $branches = $dbh->selectall_arrayref( $query, { Slice => {} }, @params );
-
-    if ( $selected ) {
-        for my $branch ( @$branches ) {
-            if ( $branch->{branchcode} eq $selected ) {
-                $branch->{selected} = 1;
-            }
+    my $unfiltered = $params->{unfiltered} || 0;
+
+    my $libraries = $unfiltered
+      ? Koha::Libraries->search( {}, { order_by => ['branchname'] } )->unblessed
+      : Koha::Libraries->search_filtered( {}, { order_by => ['branchname'] } )->unblessed;
+
+    for my $l ( @$libraries ) {
+        if (       defined $selected and $l->{branchcode} eq $selected
+            or not defined $selected and C4::Context->userenv and $l->{branchcode} eq C4::Context->userenv->{branch}
+        ) {
+            $l->{selected} = 1;
         }
     }
-    return $branches;
+
+    return $libraries;
+}
+
+sub InIndependentBranchesMode {
+    my ( $self ) = @_;
+    return ( not C4::Context->preference("IndependentBranches") or C4::Context::IsSuperLibrarian );
 }
 
 1;