3 # This file is part of Koha.
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.
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.
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>.
20 #use warnings; FIXME - Bug 2505
23 use Koha::LibraryCategories;
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28 # set the version for version checking
29 $VERSION = 3.07.00.049;
39 @EXPORT_OK = qw( &onlymine &mybranch );
44 C4::Branch - Koha branch module
52 The functions in this module deal with branches.
58 $branches = &GetBranches();
60 Returns informations about ALL branches, IndependentBranches Insensitive.
61 GetBranchInfo() returns the same information.
63 Create a branch selector with the following code.
67 my $branches = GetBranches;
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},
75 push @branchloop, \%row;
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>
86 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
91 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
98 # returns a reference to a hash of references to ALL branches...
100 my $dbh = C4::Context->dbh;
102 my $query = "SELECT * FROM branches";
104 if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
105 $query .= ' WHERE branchcode = ? ';
106 push @bind_parameters, C4::Context->userenv->{branch};
108 $query .= " ORDER BY branchname";
109 $sth = $dbh->prepare($query);
110 $sth->execute(@bind_parameters);
113 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
114 $relations_sth->execute();
116 while ( my $rel = $relations_sth->fetchrow_hashref ) {
117 push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
120 while ( my $branch = $sth->fetchrow_hashref ) {
121 foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
122 $branch->{category}{$cat} = 1;
124 $branches{ $branch->{'branchcode'} } = $branch;
126 return ( \%branches );
131 C4::Context->preference('IndependentBranches')
132 && C4::Context->userenv
133 && !C4::Context->IsSuperLibrarian()
134 && C4::Context->userenv->{branch};
137 # always returns a string for OK comparison via "eq" or "ne"
139 C4::Context->userenv or return '';
140 return C4::Context->userenv->{branch} || '';
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);
148 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
150 value => $branchcode,
151 branchcode => $branchcode,
152 selected => ($branchcode eq $branch) ? 1 : 0,
153 branchname => $branches->{$branchcode}->{branchname},
164 my ($branchcode) = @_;
165 my $dbh = C4::Context->dbh;
167 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
168 $sth->execute($branchcode);
169 my $branchname = $sth->fetchrow_array;
170 return ($branchname);
175 $branch = GetBranch( $query, $branches );
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] );
190 $results = GetBranchInfo($branchcode);
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.
199 my ($branchcode,$categorytype) = @_;
200 my $dbh = C4::Context->dbh;
207 "Select * from branches where branchcode = ? order by branchcode");
208 $sth->execute($branchcode);
211 $sth = $dbh->prepare("Select * from branches order by branchcode");
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=? ";
221 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
222 push @bind, $categorytype;
224 my $nsth=$dbh->prepare($query);
225 $nsth->execute( @bind );
227 while ( my ($cat) = $nsth->fetchrow_array ) {
230 $data->{'categories'} = \@cats;
231 push( @results, $data );
241 Koha Development Team <http://koha-community.org/>