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 &GetBranchesInCategory
42 @EXPORT_OK = qw( &onlymine &mybranch );
47 C4::Branch - Koha branch module
55 The functions in this module deal with branches.
61 $branches = &GetBranches();
63 Returns informations about ALL branches, IndependentBranches Insensitive.
64 GetBranchInfo() returns the same information.
66 Create a branch selector with the following code.
70 my $branches = GetBranches;
72 foreach my $thisbranch (sort keys %$branches) {
73 my $selected = 1 if $thisbranch eq $branch;
74 my %row =(value => $thisbranch,
75 selected => $selected,
76 branchname => $branches->{$thisbranch}->{branchname},
78 push @branchloop, \%row;
83 <select name="branch" id="branch">
84 <option value=""></option>
85 [% FOREACH branchloo IN branchloop %]
86 [% IF ( branchloo.selected ) %]
87 <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
89 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
94 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
101 # returns a reference to a hash of references to ALL branches...
103 my $dbh = C4::Context->dbh;
105 my $query = "SELECT * FROM branches";
107 if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
108 $query .= ' WHERE branchcode = ? ';
109 push @bind_parameters, C4::Context->userenv->{branch};
111 $query .= " ORDER BY branchname";
112 $sth = $dbh->prepare($query);
113 $sth->execute(@bind_parameters);
116 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
117 $relations_sth->execute();
119 while ( my $rel = $relations_sth->fetchrow_hashref ) {
120 push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
123 while ( my $branch = $sth->fetchrow_hashref ) {
124 foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
125 $branch->{category}{$cat} = 1;
127 $branches{ $branch->{'branchcode'} } = $branch;
129 return ( \%branches );
134 C4::Context->preference('IndependentBranches')
135 && C4::Context->userenv
136 && !C4::Context->IsSuperLibrarian()
137 && C4::Context->userenv->{branch};
140 # always returns a string for OK comparison via "eq" or "ne"
142 C4::Context->userenv or return '';
143 return C4::Context->userenv->{branch} || '';
146 sub GetBranchesLoop { # since this is what most pages want anyway
147 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
148 my $onlymine = @_ ? shift : onlymine();
149 my $branches = GetBranches($onlymine);
151 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
153 value => $branchcode,
154 branchcode => $branchcode,
155 selected => ($branchcode eq $branch) ? 1 : 0,
156 branchname => $branches->{$branchcode}->{branchname},
167 my ($branchcode) = @_;
168 my $dbh = C4::Context->dbh;
170 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
171 $sth->execute($branchcode);
172 my $branchname = $sth->fetchrow_array;
173 return ($branchname);
178 $error = &ModBranch($newvalue);
180 This function modifies an existing branch
182 C<$newvalue> is a ref to an array which contains all the columns from branches table.
189 my $dbh = C4::Context->dbh;
193 (branchcode,branchname,branchaddress1,
194 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
195 branchcountry,branchphone,branchfax,branchemail,
196 branchurl,branchip,branchprinter,branchnotes,opac_info,
197 branchreplyto, branchreturnpath)
198 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
200 my $sth = $dbh->prepare($query);
202 $data->{'branchcode'}, $data->{'branchname'},
203 $data->{'branchaddress1'}, $data->{'branchaddress2'},
204 $data->{'branchaddress3'}, $data->{'branchzip'},
205 $data->{'branchcity'}, $data->{'branchstate'},
206 $data->{'branchcountry'},
207 $data->{'branchphone'}, $data->{'branchfax'},
208 $data->{'branchemail'}, $data->{'branchurl'},
209 $data->{'branchip'}, $data->{'branchprinter'},
210 $data->{'branchnotes'}, $data->{opac_info},
211 $data->{'branchreplyto'}, $data->{'branchreturnpath'}
213 return 1 if $dbh->err;
217 SET branchname=?,branchaddress1=?,
218 branchaddress2=?,branchaddress3=?,branchzip=?,
219 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
220 branchfax=?,branchemail=?,branchurl=?,branchip=?,
221 branchprinter=?,branchnotes=?,opac_info=?,
222 branchreplyto=?, branchreturnpath=?
225 my $sth = $dbh->prepare($query);
227 $data->{'branchname'},
228 $data->{'branchaddress1'}, $data->{'branchaddress2'},
229 $data->{'branchaddress3'}, $data->{'branchzip'},
230 $data->{'branchcity'}, $data->{'branchstate'},
231 $data->{'branchcountry'},
232 $data->{'branchphone'}, $data->{'branchfax'},
233 $data->{'branchemail'}, $data->{'branchurl'},
234 $data->{'branchip'}, $data->{'branchprinter'},
235 $data->{'branchnotes'}, $data->{opac_info},
236 $data->{'branchreplyto'}, $data->{'branchreturnpath'},
237 $data->{'branchcode'},
240 # sort out the categories....
242 my @cats = Koha::LibraryCategories->search;
243 foreach my $cat (@cats) {
244 my $code = $cat->categorycode;
245 if ( $data->{$code} ) {
246 push( @checkedcats, $code );
249 my $branchcode = uc( $data->{'branchcode'} );
250 my $branch = GetBranchInfo($branchcode);
251 $branch = $branch->[0];
252 my $branchcats = $branch->{'categories'};
255 foreach my $bcat (@$branchcats) {
257 unless ( grep { /^$bcat$/ } @checkedcats ) {
258 push( @removecats, $bcat );
261 foreach my $ccat (@checkedcats) {
262 unless ( grep { /^$ccat$/ } @$branchcats ) {
263 push( @addcats, $ccat );
266 foreach my $cat (@addcats) {
269 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
271 $sth->execute( $branchcode, $cat );
273 foreach my $cat (@removecats) {
276 "delete from branchrelations where branchcode=? and categorycode=?"
278 $sth->execute( $branchcode, $cat );
284 $branch = GetBranch( $query, $branches );
289 my ( $query, $branches ) = @_; # get branch for this query from branches
290 my $branch = $query->param('branch');
291 my %cookie = $query->cookie('userenv');
292 ($branch) || ($branch = $cookie{'branchname'});
293 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
297 =head2 GetBranchDetail
299 $branch = &GetBranchDetail($branchcode);
301 Given the branch code, the function returns a
302 hashref for the corresponding row in the branches table.
306 sub GetBranchDetail {
307 my ($branchcode) = shift or return;
308 my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
309 $sth->execute($branchcode);
310 return $sth->fetchrow_hashref();
313 =head2 GetBranchesInCategory
315 my $branches = GetBranchesInCategory($categorycode);
317 Returns a href: keys %$branches eq (branchcode,branchname) .
321 sub GetBranchesInCategory {
322 my ($categorycode) = @_;
324 my $dbh = C4::Context->dbh();
325 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
326 where r.branchcode=b.branchcode and r.categorycode=?");
327 $sth->execute($categorycode);
328 while (my $branch = $sth->fetchrow) {
329 push @branches, $branch;
331 return( \@branches );
336 $results = GetBranchInfo($branchcode);
338 returns C<$results>, a reference to an array of hashes containing branches.
339 if $branchcode, just this branch, with associated categories.
340 if ! $branchcode && $categorytype, all branches in the category.
345 my ($branchcode,$categorytype) = @_;
346 my $dbh = C4::Context->dbh;
353 "Select * from branches where branchcode = ? order by branchcode");
354 $sth->execute($branchcode);
357 $sth = $dbh->prepare("Select * from branches order by branchcode");
361 while ( my $data = $sth->fetchrow_hashref ) {
362 my @bind = ($data->{'branchcode'});
363 my $query= "select r.categorycode from branchrelations r";
364 $query .= ", branchcategories c " if($categorytype);
365 $query .= " where branchcode=? ";
367 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
368 push @bind, $categorytype;
370 my $nsth=$dbh->prepare($query);
371 $nsth->execute( @bind );
373 while ( my ($cat) = $nsth->fetchrow_array ) {
376 $data->{'categories'} = \@cats;
377 push( @results, $data );
387 Koha Development Team <http://koha-community.org/>