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;
40 @EXPORT_OK = qw( &onlymine &mybranch );
45 C4::Branch - Koha branch module
53 The functions in this module deal with branches.
59 $branches = &GetBranches();
61 Returns informations about ALL branches, IndependentBranches Insensitive.
62 GetBranchInfo() returns the same information.
64 Create a branch selector with the following code.
68 my $branches = GetBranches;
70 foreach my $thisbranch (sort keys %$branches) {
71 my $selected = 1 if $thisbranch eq $branch;
72 my %row =(value => $thisbranch,
73 selected => $selected,
74 branchname => $branches->{$thisbranch}->{branchname},
76 push @branchloop, \%row;
81 <select name="branch" id="branch">
82 <option value=""></option>
83 [% FOREACH branchloo IN branchloop %]
84 [% IF ( branchloo.selected ) %]
85 <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
87 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
92 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
99 # returns a reference to a hash of references to ALL branches...
101 my $dbh = C4::Context->dbh;
103 my $query = "SELECT * FROM branches";
105 if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
106 $query .= ' WHERE branchcode = ? ';
107 push @bind_parameters, C4::Context->userenv->{branch};
109 $query .= " ORDER BY branchname";
110 $sth = $dbh->prepare($query);
111 $sth->execute(@bind_parameters);
114 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
115 $relations_sth->execute();
117 while ( my $rel = $relations_sth->fetchrow_hashref ) {
118 push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
121 while ( my $branch = $sth->fetchrow_hashref ) {
122 foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
123 $branch->{category}{$cat} = 1;
125 $branches{ $branch->{'branchcode'} } = $branch;
127 return ( \%branches );
132 C4::Context->preference('IndependentBranches')
133 && C4::Context->userenv
134 && !C4::Context->IsSuperLibrarian()
135 && C4::Context->userenv->{branch};
138 # always returns a string for OK comparison via "eq" or "ne"
140 C4::Context->userenv or return '';
141 return C4::Context->userenv->{branch} || '';
144 sub GetBranchesLoop { # since this is what most pages want anyway
145 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
146 my $onlymine = @_ ? shift : onlymine();
147 my $branches = GetBranches($onlymine);
149 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
151 value => $branchcode,
152 branchcode => $branchcode,
153 selected => ($branchcode eq $branch) ? 1 : 0,
154 branchname => $branches->{$branchcode}->{branchname},
165 my ($branchcode) = @_;
166 my $dbh = C4::Context->dbh;
168 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
169 $sth->execute($branchcode);
170 my $branchname = $sth->fetchrow_array;
171 return ($branchname);
176 $error = &ModBranch($newvalue);
178 This function modifies an existing branch
180 C<$newvalue> is a ref to an array which contains all the columns from branches table.
187 my $dbh = C4::Context->dbh;
191 (branchcode,branchname,branchaddress1,
192 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
193 branchcountry,branchphone,branchfax,branchemail,
194 branchurl,branchip,branchprinter,branchnotes,opac_info,
195 branchreplyto, branchreturnpath)
196 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
198 my $sth = $dbh->prepare($query);
200 $data->{'branchcode'}, $data->{'branchname'},
201 $data->{'branchaddress1'}, $data->{'branchaddress2'},
202 $data->{'branchaddress3'}, $data->{'branchzip'},
203 $data->{'branchcity'}, $data->{'branchstate'},
204 $data->{'branchcountry'},
205 $data->{'branchphone'}, $data->{'branchfax'},
206 $data->{'branchemail'}, $data->{'branchurl'},
207 $data->{'branchip'}, $data->{'branchprinter'},
208 $data->{'branchnotes'}, $data->{opac_info},
209 $data->{'branchreplyto'}, $data->{'branchreturnpath'}
211 return 1 if $dbh->err;
215 SET branchname=?,branchaddress1=?,
216 branchaddress2=?,branchaddress3=?,branchzip=?,
217 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
218 branchfax=?,branchemail=?,branchurl=?,branchip=?,
219 branchprinter=?,branchnotes=?,opac_info=?,
220 branchreplyto=?, branchreturnpath=?
223 my $sth = $dbh->prepare($query);
225 $data->{'branchname'},
226 $data->{'branchaddress1'}, $data->{'branchaddress2'},
227 $data->{'branchaddress3'}, $data->{'branchzip'},
228 $data->{'branchcity'}, $data->{'branchstate'},
229 $data->{'branchcountry'},
230 $data->{'branchphone'}, $data->{'branchfax'},
231 $data->{'branchemail'}, $data->{'branchurl'},
232 $data->{'branchip'}, $data->{'branchprinter'},
233 $data->{'branchnotes'}, $data->{opac_info},
234 $data->{'branchreplyto'}, $data->{'branchreturnpath'},
235 $data->{'branchcode'},
238 # sort out the categories....
240 my @cats = Koha::LibraryCategories->search;
241 foreach my $cat (@cats) {
242 my $code = $cat->categorycode;
243 if ( $data->{$code} ) {
244 push( @checkedcats, $code );
247 my $branchcode = uc( $data->{'branchcode'} );
248 my $branch = GetBranchInfo($branchcode);
249 $branch = $branch->[0];
250 my $branchcats = $branch->{'categories'};
253 foreach my $bcat (@$branchcats) {
255 unless ( grep { /^$bcat$/ } @checkedcats ) {
256 push( @removecats, $bcat );
259 foreach my $ccat (@checkedcats) {
260 unless ( grep { /^$ccat$/ } @$branchcats ) {
261 push( @addcats, $ccat );
264 foreach my $cat (@addcats) {
267 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
269 $sth->execute( $branchcode, $cat );
271 foreach my $cat (@removecats) {
274 "delete from branchrelations where branchcode=? and categorycode=?"
276 $sth->execute( $branchcode, $cat );
282 $branch = GetBranch( $query, $branches );
287 my ( $query, $branches ) = @_; # get branch for this query from branches
288 my $branch = $query->param('branch');
289 my %cookie = $query->cookie('userenv');
290 ($branch) || ($branch = $cookie{'branchname'});
291 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
297 $results = GetBranchInfo($branchcode);
299 returns C<$results>, a reference to an array of hashes containing branches.
300 if $branchcode, just this branch, with associated categories.
301 if ! $branchcode && $categorytype, all branches in the category.
306 my ($branchcode,$categorytype) = @_;
307 my $dbh = C4::Context->dbh;
314 "Select * from branches where branchcode = ? order by branchcode");
315 $sth->execute($branchcode);
318 $sth = $dbh->prepare("Select * from branches order by branchcode");
322 while ( my $data = $sth->fetchrow_hashref ) {
323 my @bind = ($data->{'branchcode'});
324 my $query= "select r.categorycode from branchrelations r";
325 $query .= ", branchcategories c " if($categorytype);
326 $query .= " where branchcode=? ";
328 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
329 push @bind, $categorytype;
331 my $nsth=$dbh->prepare($query);
332 $nsth->execute( @bind );
334 while ( my ($cat) = $nsth->fetchrow_array ) {
337 $data->{'categories'} = \@cats;
338 push( @results, $data );
348 Koha Development Team <http://koha-community.org/>