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