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;
39 &CheckBranchCategorycode
43 &GetBranchesInCategory
44 &ModBranchCategoryInfo
48 @EXPORT_OK = qw( &onlymine &mybranch );
53 C4::Branch - Koha branch module
61 The functions in this module deal with branches.
67 $branches = &GetBranches();
69 Returns informations about ALL branches, IndependentBranches Insensitive.
70 GetBranchInfo() returns the same information.
72 Create a branch selector with the following code.
76 my $branches = GetBranches;
78 foreach my $thisbranch (sort keys %$branches) {
79 my $selected = 1 if $thisbranch eq $branch;
80 my %row =(value => $thisbranch,
81 selected => $selected,
82 branchname => $branches->{$thisbranch}->{branchname},
84 push @branchloop, \%row;
89 <select name="branch" id="branch">
90 <option value=""></option>
91 [% FOREACH branchloo IN branchloop %]
92 [% IF ( branchloo.selected ) %]
93 <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
95 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
100 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
107 # returns a reference to a hash of references to ALL branches...
109 my $dbh = C4::Context->dbh;
111 my $query = "SELECT * FROM branches";
113 if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
114 $query .= ' WHERE branchcode = ? ';
115 push @bind_parameters, C4::Context->userenv->{branch};
117 $query .= " ORDER BY branchname";
118 $sth = $dbh->prepare($query);
119 $sth->execute(@bind_parameters);
122 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
123 $relations_sth->execute();
125 while ( my $rel = $relations_sth->fetchrow_hashref ) {
126 push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
129 while ( my $branch = $sth->fetchrow_hashref ) {
130 foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
131 $branch->{category}{$cat} = 1;
133 $branches{ $branch->{'branchcode'} } = $branch;
135 return ( \%branches );
140 C4::Context->preference('IndependentBranches')
141 && C4::Context->userenv
142 && !C4::Context->IsSuperLibrarian()
143 && C4::Context->userenv->{branch};
146 # always returns a string for OK comparison via "eq" or "ne"
148 C4::Context->userenv or return '';
149 return C4::Context->userenv->{branch} || '';
152 sub GetBranchesLoop { # since this is what most pages want anyway
153 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
154 my $onlymine = @_ ? shift : onlymine();
155 my $branches = GetBranches($onlymine);
157 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
159 value => $branchcode,
160 branchcode => $branchcode,
161 selected => ($branchcode eq $branch) ? 1 : 0,
162 branchname => $branches->{$branchcode}->{branchname},
173 my ($branchcode) = @_;
174 my $dbh = C4::Context->dbh;
176 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
177 $sth->execute($branchcode);
178 my $branchname = $sth->fetchrow_array;
179 return ($branchname);
184 $error = &ModBranch($newvalue);
186 This function modifies an existing branch
188 C<$newvalue> is a ref to an array which contains all the columns from branches table.
195 my $dbh = C4::Context->dbh;
199 (branchcode,branchname,branchaddress1,
200 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
201 branchcountry,branchphone,branchfax,branchemail,
202 branchurl,branchip,branchprinter,branchnotes,opac_info,
203 branchreplyto, branchreturnpath)
204 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
206 my $sth = $dbh->prepare($query);
208 $data->{'branchcode'}, $data->{'branchname'},
209 $data->{'branchaddress1'}, $data->{'branchaddress2'},
210 $data->{'branchaddress3'}, $data->{'branchzip'},
211 $data->{'branchcity'}, $data->{'branchstate'},
212 $data->{'branchcountry'},
213 $data->{'branchphone'}, $data->{'branchfax'},
214 $data->{'branchemail'}, $data->{'branchurl'},
215 $data->{'branchip'}, $data->{'branchprinter'},
216 $data->{'branchnotes'}, $data->{opac_info},
217 $data->{'branchreplyto'}, $data->{'branchreturnpath'}
219 return 1 if $dbh->err;
223 SET branchname=?,branchaddress1=?,
224 branchaddress2=?,branchaddress3=?,branchzip=?,
225 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
226 branchfax=?,branchemail=?,branchurl=?,branchip=?,
227 branchprinter=?,branchnotes=?,opac_info=?,
228 branchreplyto=?, branchreturnpath=?
231 my $sth = $dbh->prepare($query);
233 $data->{'branchname'},
234 $data->{'branchaddress1'}, $data->{'branchaddress2'},
235 $data->{'branchaddress3'}, $data->{'branchzip'},
236 $data->{'branchcity'}, $data->{'branchstate'},
237 $data->{'branchcountry'},
238 $data->{'branchphone'}, $data->{'branchfax'},
239 $data->{'branchemail'}, $data->{'branchurl'},
240 $data->{'branchip'}, $data->{'branchprinter'},
241 $data->{'branchnotes'}, $data->{opac_info},
242 $data->{'branchreplyto'}, $data->{'branchreturnpath'},
243 $data->{'branchcode'},
246 # sort out the categories....
248 my $cats = GetBranchCategories();
249 foreach my $cat (@$cats) {
250 my $code = $cat->{'categorycode'};
251 if ( $data->{$code} ) {
252 push( @checkedcats, $code );
255 my $branchcode = uc( $data->{'branchcode'} );
256 my $branch = GetBranchInfo($branchcode);
257 $branch = $branch->[0];
258 my $branchcats = $branch->{'categories'};
261 foreach my $bcat (@$branchcats) {
263 unless ( grep { /^$bcat$/ } @checkedcats ) {
264 push( @removecats, $bcat );
267 foreach my $ccat (@checkedcats) {
268 unless ( grep { /^$ccat$/ } @$branchcats ) {
269 push( @addcats, $ccat );
272 foreach my $cat (@addcats) {
275 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
277 $sth->execute( $branchcode, $cat );
279 foreach my $cat (@removecats) {
282 "delete from branchrelations where branchcode=? and categorycode=?"
284 $sth->execute( $branchcode, $cat );
288 =head2 GetBranchCategory
290 $results = GetBranchCategory($categorycode);
292 C<$results> is an hashref
296 sub GetBranchCategory {
298 return unless $catcode;
300 my $dbh = C4::Context->dbh;
303 $sth = $dbh->prepare(q{
305 FROM branchcategories
306 WHERE categorycode = ?
308 $sth->execute( $catcode );
309 return $sth->fetchrow_hashref;
312 =head2 GetBranchCategories
314 my $categories = GetBranchCategories($categorytype,$show_in_pulldown,$selected_in_pulldown);
316 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
317 i.e. categorydescription, categorytype, categoryname.
321 sub GetBranchCategories {
322 my ( $categorytype, $show_in_pulldown, $selected_in_pulldown ) = @_;
323 my $dbh = C4::Context->dbh();
325 my $query = "SELECT * FROM branchcategories ";
327 my ( @where, @bind );
328 if ( $categorytype ) {
329 push @where, " categorytype = ? ";
330 push @bind, $categorytype;
333 if ( defined( $show_in_pulldown ) ) {
334 push( @where, " show_in_pulldown = ? " );
335 push( @bind, $show_in_pulldown );
338 $query .= " WHERE " . join(" AND ", @where) if(@where);
339 $query .= " ORDER BY categorytype, categorycode";
340 my $sth=$dbh->prepare( $query);
341 $sth->execute(@bind);
343 my $branchcats = $sth->fetchall_arrayref({});
345 if ( $selected_in_pulldown ) {
346 foreach my $bc ( @$branchcats ) {
347 $bc->{selected} = 1 if $bc->{categorycode} eq $selected_in_pulldown;
354 =head2 GetCategoryTypes
356 $categorytypes = GetCategoryTypes;
357 returns a list of category types.
358 Currently these types are HARDCODED.
359 type: 'searchdomain' defines a group of agencies that the calling library may search in.
360 Other usage of agency categories falls under type: 'properties'.
361 to allow for other uses of categories.
362 The searchdomain bit may be better implemented as a separate module, but
363 the categories were already here, and minimally used.
367 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
368 sub GetCategoryTypes {
369 return ( 'searchdomain','properties');
374 $branch = GetBranch( $query, $branches );
379 my ( $query, $branches ) = @_; # get branch for this query from branches
380 my $branch = $query->param('branch');
381 my %cookie = $query->cookie('userenv');
382 ($branch) || ($branch = $cookie{'branchname'});
383 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
387 =head2 GetBranchDetail
389 $branch = &GetBranchDetail($branchcode);
391 Given the branch code, the function returns a
392 hashref for the corresponding row in the branches table.
396 sub GetBranchDetail {
397 my ($branchcode) = shift or return;
398 my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
399 $sth->execute($branchcode);
400 return $sth->fetchrow_hashref();
403 =head2 GetBranchesInCategory
405 my $branches = GetBranchesInCategory($categorycode);
407 Returns a href: keys %$branches eq (branchcode,branchname) .
411 sub GetBranchesInCategory {
412 my ($categorycode) = @_;
414 my $dbh = C4::Context->dbh();
415 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
416 where r.branchcode=b.branchcode and r.categorycode=?");
417 $sth->execute($categorycode);
418 while (my $branch = $sth->fetchrow) {
419 push @branches, $branch;
421 return( \@branches );
426 $results = GetBranchInfo($branchcode);
428 returns C<$results>, a reference to an array of hashes containing branches.
429 if $branchcode, just this branch, with associated categories.
430 if ! $branchcode && $categorytype, all branches in the category.
435 my ($branchcode,$categorytype) = @_;
436 my $dbh = C4::Context->dbh;
443 "Select * from branches where branchcode = ? order by branchcode");
444 $sth->execute($branchcode);
447 $sth = $dbh->prepare("Select * from branches order by branchcode");
451 while ( my $data = $sth->fetchrow_hashref ) {
452 my @bind = ($data->{'branchcode'});
453 my $query= "select r.categorycode from branchrelations r";
454 $query .= ", branchcategories c " if($categorytype);
455 $query .= " where branchcode=? ";
457 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
458 push @bind, $categorytype;
460 my $nsth=$dbh->prepare($query);
461 $nsth->execute( @bind );
463 while ( my ($cat) = $nsth->fetchrow_array ) {
466 $data->{'categories'} = \@cats;
467 push( @results, $data );
472 =head2 ModBranchCategoryInfo
474 &ModBranchCategoryInfo($data);
475 sets the data from the editbranch form, and writes to the database...
479 sub ModBranchCategoryInfo {
481 my $dbh = C4::Context->dbh;
483 # we are doing an insert
484 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype,show_in_pulldown) VALUES (?,?,?,?,?)");
485 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'} );
489 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=?,show_in_pulldown=? WHERE categorycode=?");
490 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'},uc( $data->{'categorycode'} ) );
494 =head2 CheckBranchCategorycode
496 $number_rows_affected = CheckBranchCategorycode($categorycode);
500 sub CheckBranchCategorycode {
502 # check to see if the branchcode is being used in the database somewhere....
503 my ($categorycode) = @_;
504 my $dbh = C4::Context->dbh;
507 "select count(*) from branchrelations where categorycode=?");
508 $sth->execute($categorycode);
509 my ($total) = $sth->fetchrow_array;
513 sub GetBranchesCount {
514 my $dbh = C4::Context->dbh();
515 my $query = "SELECT COUNT(*) AS branches_count FROM branches";
516 my $sth = $dbh->prepare( $query );
518 my $row = $sth->fetchrow_hashref();
519 return $row->{'branches_count'};
527 Koha Development Team <http://koha-community.org/>