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 &GetBranchesInCategory
41 &ModBranchCategoryInfo
44 @EXPORT_OK = qw( &onlymine &mybranch );
49 C4::Branch - Koha branch module
57 The functions in this module deal with branches.
63 $branches = &GetBranches();
65 Returns informations about ALL branches, IndependentBranches Insensitive.
66 GetBranchInfo() returns the same information.
68 Create a branch selector with the following code.
72 my $branches = GetBranches;
74 foreach my $thisbranch (sort keys %$branches) {
75 my $selected = 1 if $thisbranch eq $branch;
76 my %row =(value => $thisbranch,
77 selected => $selected,
78 branchname => $branches->{$thisbranch}->{branchname},
80 push @branchloop, \%row;
85 <select name="branch" id="branch">
86 <option value=""></option>
87 [% FOREACH branchloo IN branchloop %]
88 [% IF ( branchloo.selected ) %]
89 <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
91 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
96 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
103 # returns a reference to a hash of references to ALL branches...
105 my $dbh = C4::Context->dbh;
107 my $query = "SELECT * FROM branches";
109 if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
110 $query .= ' WHERE branchcode = ? ';
111 push @bind_parameters, C4::Context->userenv->{branch};
113 $query .= " ORDER BY branchname";
114 $sth = $dbh->prepare($query);
115 $sth->execute(@bind_parameters);
118 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
119 $relations_sth->execute();
121 while ( my $rel = $relations_sth->fetchrow_hashref ) {
122 push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
125 while ( my $branch = $sth->fetchrow_hashref ) {
126 foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
127 $branch->{category}{$cat} = 1;
129 $branches{ $branch->{'branchcode'} } = $branch;
131 return ( \%branches );
136 C4::Context->preference('IndependentBranches')
137 && C4::Context->userenv
138 && !C4::Context->IsSuperLibrarian()
139 && C4::Context->userenv->{branch};
142 # always returns a string for OK comparison via "eq" or "ne"
144 C4::Context->userenv or return '';
145 return C4::Context->userenv->{branch} || '';
148 sub GetBranchesLoop { # since this is what most pages want anyway
149 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
150 my $onlymine = @_ ? shift : onlymine();
151 my $branches = GetBranches($onlymine);
153 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
155 value => $branchcode,
156 branchcode => $branchcode,
157 selected => ($branchcode eq $branch) ? 1 : 0,
158 branchname => $branches->{$branchcode}->{branchname},
169 my ($branchcode) = @_;
170 my $dbh = C4::Context->dbh;
172 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
173 $sth->execute($branchcode);
174 my $branchname = $sth->fetchrow_array;
175 return ($branchname);
180 $error = &ModBranch($newvalue);
182 This function modifies an existing branch
184 C<$newvalue> is a ref to an array which contains all the columns from branches table.
191 my $dbh = C4::Context->dbh;
195 (branchcode,branchname,branchaddress1,
196 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
197 branchcountry,branchphone,branchfax,branchemail,
198 branchurl,branchip,branchprinter,branchnotes,opac_info,
199 branchreplyto, branchreturnpath)
200 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
202 my $sth = $dbh->prepare($query);
204 $data->{'branchcode'}, $data->{'branchname'},
205 $data->{'branchaddress1'}, $data->{'branchaddress2'},
206 $data->{'branchaddress3'}, $data->{'branchzip'},
207 $data->{'branchcity'}, $data->{'branchstate'},
208 $data->{'branchcountry'},
209 $data->{'branchphone'}, $data->{'branchfax'},
210 $data->{'branchemail'}, $data->{'branchurl'},
211 $data->{'branchip'}, $data->{'branchprinter'},
212 $data->{'branchnotes'}, $data->{opac_info},
213 $data->{'branchreplyto'}, $data->{'branchreturnpath'}
215 return 1 if $dbh->err;
219 SET branchname=?,branchaddress1=?,
220 branchaddress2=?,branchaddress3=?,branchzip=?,
221 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
222 branchfax=?,branchemail=?,branchurl=?,branchip=?,
223 branchprinter=?,branchnotes=?,opac_info=?,
224 branchreplyto=?, branchreturnpath=?
227 my $sth = $dbh->prepare($query);
229 $data->{'branchname'},
230 $data->{'branchaddress1'}, $data->{'branchaddress2'},
231 $data->{'branchaddress3'}, $data->{'branchzip'},
232 $data->{'branchcity'}, $data->{'branchstate'},
233 $data->{'branchcountry'},
234 $data->{'branchphone'}, $data->{'branchfax'},
235 $data->{'branchemail'}, $data->{'branchurl'},
236 $data->{'branchip'}, $data->{'branchprinter'},
237 $data->{'branchnotes'}, $data->{opac_info},
238 $data->{'branchreplyto'}, $data->{'branchreturnpath'},
239 $data->{'branchcode'},
242 # sort out the categories....
244 my @cats = Koha::LibraryCategories->search;
245 foreach my $cat (@cats) {
246 my $code = $cat->categorycode;
247 if ( $data->{$code} ) {
248 push( @checkedcats, $code );
251 my $branchcode = uc( $data->{'branchcode'} );
252 my $branch = GetBranchInfo($branchcode);
253 $branch = $branch->[0];
254 my $branchcats = $branch->{'categories'};
257 foreach my $bcat (@$branchcats) {
259 unless ( grep { /^$bcat$/ } @checkedcats ) {
260 push( @removecats, $bcat );
263 foreach my $ccat (@checkedcats) {
264 unless ( grep { /^$ccat$/ } @$branchcats ) {
265 push( @addcats, $ccat );
268 foreach my $cat (@addcats) {
271 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
273 $sth->execute( $branchcode, $cat );
275 foreach my $cat (@removecats) {
278 "delete from branchrelations where branchcode=? and categorycode=?"
280 $sth->execute( $branchcode, $cat );
286 $branch = GetBranch( $query, $branches );
291 my ( $query, $branches ) = @_; # get branch for this query from branches
292 my $branch = $query->param('branch');
293 my %cookie = $query->cookie('userenv');
294 ($branch) || ($branch = $cookie{'branchname'});
295 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
299 =head2 GetBranchDetail
301 $branch = &GetBranchDetail($branchcode);
303 Given the branch code, the function returns a
304 hashref for the corresponding row in the branches table.
308 sub GetBranchDetail {
309 my ($branchcode) = shift or return;
310 my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
311 $sth->execute($branchcode);
312 return $sth->fetchrow_hashref();
315 =head2 GetBranchesInCategory
317 my $branches = GetBranchesInCategory($categorycode);
319 Returns a href: keys %$branches eq (branchcode,branchname) .
323 sub GetBranchesInCategory {
324 my ($categorycode) = @_;
326 my $dbh = C4::Context->dbh();
327 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
328 where r.branchcode=b.branchcode and r.categorycode=?");
329 $sth->execute($categorycode);
330 while (my $branch = $sth->fetchrow) {
331 push @branches, $branch;
333 return( \@branches );
338 $results = GetBranchInfo($branchcode);
340 returns C<$results>, a reference to an array of hashes containing branches.
341 if $branchcode, just this branch, with associated categories.
342 if ! $branchcode && $categorytype, all branches in the category.
347 my ($branchcode,$categorytype) = @_;
348 my $dbh = C4::Context->dbh;
355 "Select * from branches where branchcode = ? order by branchcode");
356 $sth->execute($branchcode);
359 $sth = $dbh->prepare("Select * from branches order by branchcode");
363 while ( my $data = $sth->fetchrow_hashref ) {
364 my @bind = ($data->{'branchcode'});
365 my $query= "select r.categorycode from branchrelations r";
366 $query .= ", branchcategories c " if($categorytype);
367 $query .= " where branchcode=? ";
369 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
370 push @bind, $categorytype;
372 my $nsth=$dbh->prepare($query);
373 $nsth->execute( @bind );
375 while ( my ($cat) = $nsth->fetchrow_array ) {
378 $data->{'categories'} = \@cats;
379 push( @results, $data );
384 =head2 ModBranchCategoryInfo
386 &ModBranchCategoryInfo($data);
387 sets the data from the editbranch form, and writes to the database...
391 sub ModBranchCategoryInfo {
393 my $dbh = C4::Context->dbh;
395 # we are doing an insert
396 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype,show_in_pulldown) VALUES (?,?,?,?,?)");
397 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'} );
401 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=?,show_in_pulldown=? WHERE categorycode=?");
402 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'},uc( $data->{'categorycode'} ) );
410 Koha Development Team <http://koha-community.org/>