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
24 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
27 # set the version for version checking
28 $VERSION = 3.07.00.049;
41 &GetBranchesInCategory
42 &ModBranchCategoryInfo
46 @EXPORT_OK = qw( &onlymine &mybranch );
51 C4::Branch - Koha branch module
59 The functions in this module deal with branches.
65 $branches = &GetBranches();
67 Returns informations about ALL branches, IndependentBranches Insensitive.
68 GetBranchInfo() returns the same information.
70 Create a branch selector with the following code.
74 my $branches = GetBranches;
76 foreach my $thisbranch (sort keys %$branches) {
77 my $selected = 1 if $thisbranch eq $branch;
78 my %row =(value => $thisbranch,
79 selected => $selected,
80 branchname => $branches->{$thisbranch}->{branchname},
82 push @branchloop, \%row;
87 <select name="branch" id="branch">
88 <option value=""></option>
89 [% FOREACH branchloo IN branchloop %]
90 [% IF ( branchloo.selected ) %]
91 <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
93 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
98 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
105 # returns a reference to a hash of references to ALL branches...
107 my $dbh = C4::Context->dbh;
109 my $query = "SELECT * FROM branches";
111 if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
112 $query .= ' WHERE branchcode = ? ';
113 push @bind_parameters, C4::Context->userenv->{branch};
115 $query .= " ORDER BY branchname";
116 $sth = $dbh->prepare($query);
117 $sth->execute(@bind_parameters);
120 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
121 $relations_sth->execute();
123 while ( my $rel = $relations_sth->fetchrow_hashref ) {
124 push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
127 while ( my $branch = $sth->fetchrow_hashref ) {
128 foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
129 $branch->{category}{$cat} = 1;
131 $branches{ $branch->{'branchcode'} } = $branch;
133 return ( \%branches );
138 C4::Context->preference('IndependentBranches')
139 && C4::Context->userenv
140 && !C4::Context->IsSuperLibrarian()
141 && C4::Context->userenv->{branch};
144 # always returns a string for OK comparison via "eq" or "ne"
146 C4::Context->userenv or return '';
147 return C4::Context->userenv->{branch} || '';
150 sub GetBranchesLoop { # since this is what most pages want anyway
151 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
152 my $onlymine = @_ ? shift : onlymine();
153 my $branches = GetBranches($onlymine);
155 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
157 value => $branchcode,
158 branchcode => $branchcode,
159 selected => ($branchcode eq $branch) ? 1 : 0,
160 branchname => $branches->{$branchcode}->{branchname},
171 my ($branchcode) = @_;
172 my $dbh = C4::Context->dbh;
174 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
175 $sth->execute($branchcode);
176 my $branchname = $sth->fetchrow_array;
177 return ($branchname);
182 $error = &ModBranch($newvalue);
184 This function modifies an existing branch
186 C<$newvalue> is a ref to an array which contains all the columns from branches table.
193 my $dbh = C4::Context->dbh;
197 (branchcode,branchname,branchaddress1,
198 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
199 branchcountry,branchphone,branchfax,branchemail,
200 branchurl,branchip,branchprinter,branchnotes,opac_info,
201 branchreplyto, branchreturnpath)
202 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
204 my $sth = $dbh->prepare($query);
206 $data->{'branchcode'}, $data->{'branchname'},
207 $data->{'branchaddress1'}, $data->{'branchaddress2'},
208 $data->{'branchaddress3'}, $data->{'branchzip'},
209 $data->{'branchcity'}, $data->{'branchstate'},
210 $data->{'branchcountry'},
211 $data->{'branchphone'}, $data->{'branchfax'},
212 $data->{'branchemail'}, $data->{'branchurl'},
213 $data->{'branchip'}, $data->{'branchprinter'},
214 $data->{'branchnotes'}, $data->{opac_info},
215 $data->{'branchreplyto'}, $data->{'branchreturnpath'}
217 return 1 if $dbh->err;
221 SET branchname=?,branchaddress1=?,
222 branchaddress2=?,branchaddress3=?,branchzip=?,
223 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
224 branchfax=?,branchemail=?,branchurl=?,branchip=?,
225 branchprinter=?,branchnotes=?,opac_info=?,
226 branchreplyto=?, branchreturnpath=?
229 my $sth = $dbh->prepare($query);
231 $data->{'branchname'},
232 $data->{'branchaddress1'}, $data->{'branchaddress2'},
233 $data->{'branchaddress3'}, $data->{'branchzip'},
234 $data->{'branchcity'}, $data->{'branchstate'},
235 $data->{'branchcountry'},
236 $data->{'branchphone'}, $data->{'branchfax'},
237 $data->{'branchemail'}, $data->{'branchurl'},
238 $data->{'branchip'}, $data->{'branchprinter'},
239 $data->{'branchnotes'}, $data->{opac_info},
240 $data->{'branchreplyto'}, $data->{'branchreturnpath'},
241 $data->{'branchcode'},
244 # sort out the categories....
246 my $cats = GetBranchCategories();
247 foreach my $cat (@$cats) {
248 my $code = $cat->{'categorycode'};
249 if ( $data->{$code} ) {
250 push( @checkedcats, $code );
253 my $branchcode = uc( $data->{'branchcode'} );
254 my $branch = GetBranchInfo($branchcode);
255 $branch = $branch->[0];
256 my $branchcats = $branch->{'categories'};
259 foreach my $bcat (@$branchcats) {
261 unless ( grep { /^$bcat$/ } @checkedcats ) {
262 push( @removecats, $bcat );
265 foreach my $ccat (@checkedcats) {
266 unless ( grep { /^$ccat$/ } @$branchcats ) {
267 push( @addcats, $ccat );
270 foreach my $cat (@addcats) {
273 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
275 $sth->execute( $branchcode, $cat );
277 foreach my $cat (@removecats) {
280 "delete from branchrelations where branchcode=? and categorycode=?"
282 $sth->execute( $branchcode, $cat );
286 =head2 GetBranchCategories
288 my $categories = GetBranchCategories($categorytype,$show_in_pulldown,$selected_in_pulldown);
290 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
291 i.e. categorydescription, categorytype, categoryname.
295 sub GetBranchCategories {
296 my ( $categorytype, $show_in_pulldown, $selected_in_pulldown ) = @_;
297 my $dbh = C4::Context->dbh();
299 my $query = "SELECT * FROM branchcategories ";
301 my ( @where, @bind );
302 if ( $categorytype ) {
303 push @where, " categorytype = ? ";
304 push @bind, $categorytype;
307 if ( defined( $show_in_pulldown ) ) {
308 push( @where, " show_in_pulldown = ? " );
309 push( @bind, $show_in_pulldown );
312 $query .= " WHERE " . join(" AND ", @where) if(@where);
313 $query .= " ORDER BY categorytype, categorycode";
314 my $sth=$dbh->prepare( $query);
315 $sth->execute(@bind);
317 my $branchcats = $sth->fetchall_arrayref({});
319 if ( $selected_in_pulldown ) {
320 foreach my $bc ( @$branchcats ) {
321 $bc->{selected} = 1 if $bc->{categorycode} eq $selected_in_pulldown;
328 =head2 GetCategoryTypes
330 $categorytypes = GetCategoryTypes;
331 returns a list of category types.
332 Currently these types are HARDCODED.
333 type: 'searchdomain' defines a group of agencies that the calling library may search in.
334 Other usage of agency categories falls under type: 'properties'.
335 to allow for other uses of categories.
336 The searchdomain bit may be better implemented as a separate module, but
337 the categories were already here, and minimally used.
341 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
342 sub GetCategoryTypes {
343 return ( 'searchdomain','properties');
348 $branch = GetBranch( $query, $branches );
353 my ( $query, $branches ) = @_; # get branch for this query from branches
354 my $branch = $query->param('branch');
355 my %cookie = $query->cookie('userenv');
356 ($branch) || ($branch = $cookie{'branchname'});
357 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
361 =head2 GetBranchDetail
363 $branch = &GetBranchDetail($branchcode);
365 Given the branch code, the function returns a
366 hashref for the corresponding row in the branches table.
370 sub GetBranchDetail {
371 my ($branchcode) = shift or return;
372 my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
373 $sth->execute($branchcode);
374 return $sth->fetchrow_hashref();
377 =head2 GetBranchesInCategory
379 my $branches = GetBranchesInCategory($categorycode);
381 Returns a href: keys %$branches eq (branchcode,branchname) .
385 sub GetBranchesInCategory {
386 my ($categorycode) = @_;
388 my $dbh = C4::Context->dbh();
389 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
390 where r.branchcode=b.branchcode and r.categorycode=?");
391 $sth->execute($categorycode);
392 while (my $branch = $sth->fetchrow) {
393 push @branches, $branch;
395 return( \@branches );
400 $results = GetBranchInfo($branchcode);
402 returns C<$results>, a reference to an array of hashes containing branches.
403 if $branchcode, just this branch, with associated categories.
404 if ! $branchcode && $categorytype, all branches in the category.
409 my ($branchcode,$categorytype) = @_;
410 my $dbh = C4::Context->dbh;
417 "Select * from branches where branchcode = ? order by branchcode");
418 $sth->execute($branchcode);
421 $sth = $dbh->prepare("Select * from branches order by branchcode");
425 while ( my $data = $sth->fetchrow_hashref ) {
426 my @bind = ($data->{'branchcode'});
427 my $query= "select r.categorycode from branchrelations r";
428 $query .= ", branchcategories c " if($categorytype);
429 $query .= " where branchcode=? ";
431 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
432 push @bind, $categorytype;
434 my $nsth=$dbh->prepare($query);
435 $nsth->execute( @bind );
437 while ( my ($cat) = $nsth->fetchrow_array ) {
440 $data->{'categories'} = \@cats;
441 push( @results, $data );
446 =head2 ModBranchCategoryInfo
448 &ModBranchCategoryInfo($data);
449 sets the data from the editbranch form, and writes to the database...
453 sub ModBranchCategoryInfo {
455 my $dbh = C4::Context->dbh;
457 # we are doing an insert
458 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype,show_in_pulldown) VALUES (?,?,?,?,?)");
459 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'} );
463 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=?,show_in_pulldown=? WHERE categorycode=?");
464 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'},uc( $data->{'categorycode'} ) );
467 sub GetBranchesCount {
468 my $dbh = C4::Context->dbh();
469 my $query = "SELECT COUNT(*) AS branches_count FROM branches";
470 my $sth = $dbh->prepare( $query );
472 my $row = $sth->fetchrow_hashref();
473 return $row->{'branches_count'};
481 Koha Development Team <http://koha-community.org/>