Bug 15006: [QA Follow-up] Satisfy qa tools with one tab less
[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                 &GetBranchName
31                 &GetBranch
32                 &GetBranches
33                 &GetBranchesLoop
34                 &mybranch
35         );
36     @EXPORT_OK = qw( &onlymine &mybranch );
37 }
38
39 =head1 NAME
40
41 C4::Branch - Koha branch module
42
43 =head1 SYNOPSIS
44
45 use C4::Branch;
46
47 =head1 DESCRIPTION
48
49 The functions in this module deal with branches.
50
51 =head1 FUNCTIONS
52
53 =head2 GetBranches
54
55   $branches = &GetBranches();
56
57 Returns informations about ALL branches, IndependentBranches Insensitive.
58
59 Create a branch selector with the following code.
60
61 =head3 in PERL SCRIPT
62
63     my $branches = GetBranches;
64     my @branchloop;
65     foreach my $thisbranch (sort keys %$branches) {
66         my $selected = 1 if $thisbranch eq $branch;
67         my %row =(value => $thisbranch,
68                     selected => $selected,
69                     branchname => $branches->{$thisbranch}->{branchname},
70                 );
71         push @branchloop, \%row;
72     }
73
74 =head3 in TEMPLATE
75
76     <select name="branch" id="branch">
77         <option value=""></option>
78             [% FOREACH branchloo IN branchloop %]
79                 [% IF ( branchloo.selected ) %]
80                     <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
81                 [% ELSE %]
82                     <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
83                 [% END %]
84             [% END %]
85     </select>
86
87 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
88
89 =cut
90
91 sub GetBranches {
92     my ($onlymine) = @_;
93
94     # returns a reference to a hash of references to ALL branches...
95     my %branches;
96     my $dbh = C4::Context->dbh;
97     my $sth;
98     my $query = "SELECT * FROM branches";
99     my @bind_parameters;
100     if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
101         $query .= ' WHERE branchcode = ? ';
102         push @bind_parameters, C4::Context->userenv->{branch};
103     }
104     $query .= " ORDER BY branchname";
105     $sth = $dbh->prepare($query);
106     $sth->execute(@bind_parameters);
107
108     my $relations_sth =
109       $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
110     $relations_sth->execute();
111     my %relations;
112     while ( my $rel = $relations_sth->fetchrow_hashref ) {
113         push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
114     }
115
116     while ( my $branch = $sth->fetchrow_hashref ) {
117         foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
118             $branch->{category}{$cat} = 1;
119         }
120         $branches{ $branch->{'branchcode'} } = $branch;
121     }
122     return ( \%branches );
123 }
124
125 sub onlymine {
126     return
127          C4::Context->preference('IndependentBranches')
128       && C4::Context->userenv
129       && !C4::Context->IsSuperLibrarian()
130       && C4::Context->userenv->{branch};
131 }
132
133 # always returns a string for OK comparison via "eq" or "ne"
134 sub mybranch {
135     C4::Context->userenv           or return '';
136     return C4::Context->userenv->{branch} || '';
137 }
138
139 sub GetBranchesLoop {  # since this is what most pages want anyway
140     my $branch   = @_ ? shift : mybranch();     # optional first argument is branchcode of "my branch", if preselection is wanted.
141     my $onlymine = @_ ? shift : onlymine();
142     my $branches = GetBranches($onlymine);
143     my @loop;
144     foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
145         push @loop, {
146             value      => $branchcode,
147             branchcode => $branchcode,
148             selected   => ($branchcode eq $branch) ? 1 : 0,
149             branchname => $branches->{$branchcode}->{branchname},
150         };
151     }
152     return \@loop;
153 }
154
155 =head2 GetBranchName
156
157 =cut
158
159 sub GetBranchName {
160     my ($branchcode) = @_;
161     my $dbh = C4::Context->dbh;
162     my $sth;
163     $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
164     $sth->execute($branchcode);
165     my $branchname = $sth->fetchrow_array;
166     return ($branchname);
167 }
168
169 =head2 GetBranch
170
171 $branch = GetBranch( $query, $branches );
172
173 =cut
174
175 sub GetBranch {
176     my ( $query, $branches ) = @_;    # get branch for this query from branches
177     my $branch = $query->param('branch');
178     my %cookie = $query->cookie('userenv');
179     ($branch)                || ($branch = $cookie{'branchname'});
180     ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
181     return $branch;
182 }
183
184 1;
185 __END__
186
187 =head1 AUTHOR
188
189 Koha Development Team <http://koha-community.org/>
190
191 =cut