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