Bug 15758: Koha::Libraries - Remove GetBranchesLoop
[koha.git] / C4 / Branch.pm
1 package C4::Branch;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
19 use strict;
20 #use warnings; FIXME - Bug 2505
21 require Exporter;
22 use C4::Context;
23 use Koha::LibraryCategories;
24
25 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26
27 BEGIN {
28         @ISA    = qw(Exporter);
29         @EXPORT = qw(
30                 &GetBranch
31                 &GetBranches
32                 &mybranch
33         );
34     @EXPORT_OK = qw( &onlymine &mybranch );
35 }
36
37 =head1 NAME
38
39 C4::Branch - Koha branch module
40
41 =head1 SYNOPSIS
42
43 use C4::Branch;
44
45 =head1 DESCRIPTION
46
47 The functions in this module deal with branches.
48
49 =head1 FUNCTIONS
50
51 =head2 GetBranches
52
53   $branches = &GetBranches();
54
55 Returns informations about ALL branches, IndependentBranches Insensitive.
56
57 Create a branch selector with the following code.
58
59 =head3 in PERL SCRIPT
60
61     my $branches = GetBranches;
62     my @branchloop;
63     foreach my $thisbranch (sort keys %$branches) {
64         my $selected = 1 if $thisbranch eq $branch;
65         my %row =(value => $thisbranch,
66                     selected => $selected,
67                     branchname => $branches->{$thisbranch}->{branchname},
68                 );
69         push @branchloop, \%row;
70     }
71
72 =head3 in TEMPLATE
73
74     <select name="branch" id="branch">
75         <option value=""></option>
76             [% FOREACH branchloo IN branchloop %]
77                 [% IF ( branchloo.selected ) %]
78                     <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
79                 [% ELSE %]
80                     <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
81                 [% END %]
82             [% END %]
83     </select>
84
85 =cut
86
87 sub GetBranches {
88     my ($onlymine) = @_;
89
90     # returns a reference to a hash of references to ALL branches...
91     my %branches;
92     my $dbh = C4::Context->dbh;
93     my $sth;
94     my $query = "SELECT * FROM branches";
95     my @bind_parameters;
96     if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
97         $query .= ' WHERE branchcode = ? ';
98         push @bind_parameters, C4::Context->userenv->{branch};
99     }
100     $query .= " ORDER BY branchname";
101     $sth = $dbh->prepare($query);
102     $sth->execute(@bind_parameters);
103
104     my $relations_sth =
105       $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
106     $relations_sth->execute();
107     my %relations;
108     while ( my $rel = $relations_sth->fetchrow_hashref ) {
109         push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
110     }
111
112     while ( my $branch = $sth->fetchrow_hashref ) {
113         foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
114             $branch->{category}{$cat} = 1;
115         }
116         $branches{ $branch->{'branchcode'} } = $branch;
117     }
118     return ( \%branches );
119 }
120
121 sub onlymine {
122     return
123          C4::Context->preference('IndependentBranches')
124       && C4::Context->userenv
125       && !C4::Context->IsSuperLibrarian()
126       && C4::Context->userenv->{branch};
127 }
128
129 # always returns a string for OK comparison via "eq" or "ne"
130 sub mybranch {
131     C4::Context->userenv           or return '';
132     return C4::Context->userenv->{branch} || '';
133 }
134
135 =head2 GetBranch
136
137 $branch = GetBranch( $query, $branches );
138
139 =cut
140
141 sub GetBranch {
142     my ( $query, $branches ) = @_;    # get branch for this query from branches
143     my $branch = $query->param('branch');
144     my %cookie = $query->cookie('userenv');
145     ($branch)                || ($branch = $cookie{'branchname'});
146     ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
147     return $branch;
148 }
149
150 1;
151 __END__
152
153 =head1 AUTHOR
154
155 Koha Development Team <http://koha-community.org/>
156
157 =cut