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
45 @EXPORT_OK = qw( &onlymine &mybranch );
50 C4::Branch - Koha branch module
58 The functions in this module deal with branches.
64 $branches = &GetBranches();
66 Returns informations about ALL branches, IndependentBranches Insensitive.
67 GetBranchInfo() returns the same information.
69 Create a branch selector with the following code.
73 my $branches = GetBranches;
75 foreach my $thisbranch (sort keys %$branches) {
76 my $selected = 1 if $thisbranch eq $branch;
77 my %row =(value => $thisbranch,
78 selected => $selected,
79 branchname => $branches->{$thisbranch}->{branchname},
81 push @branchloop, \%row;
86 <select name="branch" id="branch">
87 <option value=""></option>
88 [% FOREACH branchloo IN branchloop %]
89 [% IF ( branchloo.selected ) %]
90 <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
92 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
97 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
104 # returns a reference to a hash of references to ALL branches...
106 my $dbh = C4::Context->dbh;
108 my $query = "SELECT * FROM branches";
110 if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
111 $query .= ' WHERE branchcode = ? ';
112 push @bind_parameters, C4::Context->userenv->{branch};
114 $query .= " ORDER BY branchname";
115 $sth = $dbh->prepare($query);
116 $sth->execute(@bind_parameters);
119 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
120 $relations_sth->execute();
122 while ( my $rel = $relations_sth->fetchrow_hashref ) {
123 push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
126 while ( my $branch = $sth->fetchrow_hashref ) {
127 foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
128 $branch->{category}{$cat} = 1;
130 $branches{ $branch->{'branchcode'} } = $branch;
132 return ( \%branches );
137 C4::Context->preference('IndependentBranches')
138 && C4::Context->userenv
139 && !C4::Context->IsSuperLibrarian()
140 && C4::Context->userenv->{branch};
143 # always returns a string for OK comparison via "eq" or "ne"
145 C4::Context->userenv or return '';
146 return C4::Context->userenv->{branch} || '';
149 sub GetBranchesLoop { # since this is what most pages want anyway
150 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
151 my $onlymine = @_ ? shift : onlymine();
152 my $branches = GetBranches($onlymine);
154 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
156 value => $branchcode,
157 branchcode => $branchcode,
158 selected => ($branchcode eq $branch) ? 1 : 0,
159 branchname => $branches->{$branchcode}->{branchname},
170 my ($branchcode) = @_;
171 my $dbh = C4::Context->dbh;
173 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
174 $sth->execute($branchcode);
175 my $branchname = $sth->fetchrow_array;
176 return ($branchname);
181 $error = &ModBranch($newvalue);
183 This function modifies an existing branch
185 C<$newvalue> is a ref to an array which contains all the columns from branches table.
192 my $dbh = C4::Context->dbh;
196 (branchcode,branchname,branchaddress1,
197 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
198 branchcountry,branchphone,branchfax,branchemail,
199 branchurl,branchip,branchprinter,branchnotes,opac_info,
200 branchreplyto, branchreturnpath)
201 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
203 my $sth = $dbh->prepare($query);
205 $data->{'branchcode'}, $data->{'branchname'},
206 $data->{'branchaddress1'}, $data->{'branchaddress2'},
207 $data->{'branchaddress3'}, $data->{'branchzip'},
208 $data->{'branchcity'}, $data->{'branchstate'},
209 $data->{'branchcountry'},
210 $data->{'branchphone'}, $data->{'branchfax'},
211 $data->{'branchemail'}, $data->{'branchurl'},
212 $data->{'branchip'}, $data->{'branchprinter'},
213 $data->{'branchnotes'}, $data->{opac_info},
214 $data->{'branchreplyto'}, $data->{'branchreturnpath'}
216 return 1 if $dbh->err;
220 SET branchname=?,branchaddress1=?,
221 branchaddress2=?,branchaddress3=?,branchzip=?,
222 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
223 branchfax=?,branchemail=?,branchurl=?,branchip=?,
224 branchprinter=?,branchnotes=?,opac_info=?,
225 branchreplyto=?, branchreturnpath=?
228 my $sth = $dbh->prepare($query);
230 $data->{'branchname'},
231 $data->{'branchaddress1'}, $data->{'branchaddress2'},
232 $data->{'branchaddress3'}, $data->{'branchzip'},
233 $data->{'branchcity'}, $data->{'branchstate'},
234 $data->{'branchcountry'},
235 $data->{'branchphone'}, $data->{'branchfax'},
236 $data->{'branchemail'}, $data->{'branchurl'},
237 $data->{'branchip'}, $data->{'branchprinter'},
238 $data->{'branchnotes'}, $data->{opac_info},
239 $data->{'branchreplyto'}, $data->{'branchreturnpath'},
240 $data->{'branchcode'},
243 # sort out the categories....
245 my @cats = Koha::LibraryCategories->search;
246 foreach my $cat (@cats) {
247 my $code = $cat->categorycode;
248 if ( $data->{$code} ) {
249 push( @checkedcats, $code );
252 my $branchcode = uc( $data->{'branchcode'} );
253 my $branch = GetBranchInfo($branchcode);
254 $branch = $branch->[0];
255 my $branchcats = $branch->{'categories'};
258 foreach my $bcat (@$branchcats) {
260 unless ( grep { /^$bcat$/ } @checkedcats ) {
261 push( @removecats, $bcat );
264 foreach my $ccat (@checkedcats) {
265 unless ( grep { /^$ccat$/ } @$branchcats ) {
266 push( @addcats, $ccat );
269 foreach my $cat (@addcats) {
272 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
274 $sth->execute( $branchcode, $cat );
276 foreach my $cat (@removecats) {
279 "delete from branchrelations where branchcode=? and categorycode=?"
281 $sth->execute( $branchcode, $cat );
287 $branch = GetBranch( $query, $branches );
292 my ( $query, $branches ) = @_; # get branch for this query from branches
293 my $branch = $query->param('branch');
294 my %cookie = $query->cookie('userenv');
295 ($branch) || ($branch = $cookie{'branchname'});
296 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
300 =head2 GetBranchDetail
302 $branch = &GetBranchDetail($branchcode);
304 Given the branch code, the function returns a
305 hashref for the corresponding row in the branches table.
309 sub GetBranchDetail {
310 my ($branchcode) = shift or return;
311 my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
312 $sth->execute($branchcode);
313 return $sth->fetchrow_hashref();
316 =head2 GetBranchesInCategory
318 my $branches = GetBranchesInCategory($categorycode);
320 Returns a href: keys %$branches eq (branchcode,branchname) .
324 sub GetBranchesInCategory {
325 my ($categorycode) = @_;
327 my $dbh = C4::Context->dbh();
328 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
329 where r.branchcode=b.branchcode and r.categorycode=?");
330 $sth->execute($categorycode);
331 while (my $branch = $sth->fetchrow) {
332 push @branches, $branch;
334 return( \@branches );
339 $results = GetBranchInfo($branchcode);
341 returns C<$results>, a reference to an array of hashes containing branches.
342 if $branchcode, just this branch, with associated categories.
343 if ! $branchcode && $categorytype, all branches in the category.
348 my ($branchcode,$categorytype) = @_;
349 my $dbh = C4::Context->dbh;
356 "Select * from branches where branchcode = ? order by branchcode");
357 $sth->execute($branchcode);
360 $sth = $dbh->prepare("Select * from branches order by branchcode");
364 while ( my $data = $sth->fetchrow_hashref ) {
365 my @bind = ($data->{'branchcode'});
366 my $query= "select r.categorycode from branchrelations r";
367 $query .= ", branchcategories c " if($categorytype);
368 $query .= " where branchcode=? ";
370 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
371 push @bind, $categorytype;
373 my $nsth=$dbh->prepare($query);
374 $nsth->execute( @bind );
376 while ( my ($cat) = $nsth->fetchrow_array ) {
379 $data->{'categories'} = \@cats;
380 push( @results, $data );
385 =head2 ModBranchCategoryInfo
387 &ModBranchCategoryInfo($data);
388 sets the data from the editbranch form, and writes to the database...
392 sub ModBranchCategoryInfo {
394 my $dbh = C4::Context->dbh;
396 # we are doing an insert
397 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype,show_in_pulldown) VALUES (?,?,?,?,?)");
398 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'} );
402 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=?,show_in_pulldown=? WHERE categorycode=?");
403 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'},uc( $data->{'categorycode'} ) );
406 sub GetBranchesCount {
407 my $dbh = C4::Context->dbh();
408 my $query = "SELECT COUNT(*) AS branches_count FROM branches";
409 my $sth = $dbh->prepare( $query );
411 my $row = $sth->fetchrow_hashref();
412 return $row->{'branches_count'};
420 Koha Development Team <http://koha-community.org/>