3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
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
51 @EXPORT_OK = qw( &onlymine &mybranch );
56 C4::Branch - Koha branch module
64 The functions in this module deal with branches.
70 $branches = &GetBranches();
72 Returns informations about ALL branches, IndependentBranches Insensitive.
73 GetBranchInfo() returns the same information.
75 Create a branch selector with the following code.
79 my $branches = GetBranches;
81 foreach my $thisbranch (sort keys %$branches) {
82 my $selected = 1 if $thisbranch eq $branch;
83 my %row =(value => $thisbranch,
84 selected => $selected,
85 branchname => $branches->{$thisbranch}->{branchname},
87 push @branchloop, \%row;
92 <select name="branch" id="branch">
93 <option value=""></option>
94 [% FOREACH branchloo IN branchloop %]
95 [% IF ( branchloo.selected ) %]
96 <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
98 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
103 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
110 # returns a reference to a hash of references to ALL branches...
112 my $dbh = C4::Context->dbh;
114 my $query = "SELECT * FROM branches";
116 if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) {
117 $query .= ' WHERE branchcode = ? ';
118 push @bind_parameters, C4::Context->userenv->{branch};
120 $query .= " ORDER BY branchname";
121 $sth = $dbh->prepare($query);
122 $sth->execute(@bind_parameters);
125 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
126 $relations_sth->execute();
128 while ( my $rel = $relations_sth->fetchrow_hashref ) {
129 push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode};
132 while ( my $branch = $sth->fetchrow_hashref ) {
133 foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) {
134 $branch->{category}{$cat} = 1;
136 $branches{ $branch->{'branchcode'} } = $branch;
138 return ( \%branches );
143 C4::Context->preference('IndependentBranches')
144 && C4::Context->userenv
145 && !C4::Context->IsSuperLibrarian()
146 && C4::Context->userenv->{branch};
149 # always returns a string for OK comparison via "eq" or "ne"
151 C4::Context->userenv or return '';
152 return C4::Context->userenv->{branch} || '';
155 sub GetBranchesLoop { # since this is what most pages want anyway
156 my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted.
157 my $onlymine = @_ ? shift : onlymine();
158 my $branches = GetBranches($onlymine);
160 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
162 value => $branchcode,
163 branchcode => $branchcode,
164 selected => ($branchcode eq $branch) ? 1 : 0,
165 branchname => $branches->{$branchcode}->{branchname},
176 my ($branchcode) = @_;
177 my $dbh = C4::Context->dbh;
179 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
180 $sth->execute($branchcode);
181 my $branchname = $sth->fetchrow_array;
182 return ($branchname);
187 $error = &ModBranch($newvalue);
189 This function modify an existing branch
191 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
198 my $dbh = C4::Context->dbh;
202 (branchcode,branchname,branchaddress1,
203 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
204 branchcountry,branchphone,branchfax,branchemail,
205 branchurl,branchip,branchprinter,branchnotes,opac_info,
206 branchreplyto, branchreturnpath)
207 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
209 my $sth = $dbh->prepare($query);
211 $data->{'branchcode'}, $data->{'branchname'},
212 $data->{'branchaddress1'}, $data->{'branchaddress2'},
213 $data->{'branchaddress3'}, $data->{'branchzip'},
214 $data->{'branchcity'}, $data->{'branchstate'},
215 $data->{'branchcountry'},
216 $data->{'branchphone'}, $data->{'branchfax'},
217 $data->{'branchemail'}, $data->{'branchurl'},
218 $data->{'branchip'}, $data->{'branchprinter'},
219 $data->{'branchnotes'}, $data->{opac_info},
220 $data->{'branchreplyto'}, $data->{'branchreturnpath'}
222 return 1 if $dbh->err;
226 SET branchname=?,branchaddress1=?,
227 branchaddress2=?,branchaddress3=?,branchzip=?,
228 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
229 branchfax=?,branchemail=?,branchurl=?,branchip=?,
230 branchprinter=?,branchnotes=?,opac_info=?,
231 branchreplyto=?, branchreturnpath=?
234 my $sth = $dbh->prepare($query);
236 $data->{'branchname'},
237 $data->{'branchaddress1'}, $data->{'branchaddress2'},
238 $data->{'branchaddress3'}, $data->{'branchzip'},
239 $data->{'branchcity'}, $data->{'branchstate'},
240 $data->{'branchcountry'},
241 $data->{'branchphone'}, $data->{'branchfax'},
242 $data->{'branchemail'}, $data->{'branchurl'},
243 $data->{'branchip'}, $data->{'branchprinter'},
244 $data->{'branchnotes'}, $data->{opac_info},
245 $data->{'branchreplyto'}, $data->{'branchreturnpath'},
246 $data->{'branchcode'},
249 # sort out the categories....
251 my $cats = GetBranchCategories();
252 foreach my $cat (@$cats) {
253 my $code = $cat->{'categorycode'};
254 if ( $data->{$code} ) {
255 push( @checkedcats, $code );
258 my $branchcode = uc( $data->{'branchcode'} );
259 my $branch = GetBranchInfo($branchcode);
260 $branch = $branch->[0];
261 my $branchcats = $branch->{'categories'};
264 foreach my $bcat (@$branchcats) {
266 unless ( grep { /^$bcat$/ } @checkedcats ) {
267 push( @removecats, $bcat );
270 foreach my $ccat (@checkedcats) {
271 unless ( grep { /^$ccat$/ } @$branchcats ) {
272 push( @addcats, $ccat );
275 foreach my $cat (@addcats) {
278 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
280 $sth->execute( $branchcode, $cat );
282 foreach my $cat (@removecats) {
285 "delete from branchrelations where branchcode=? and categorycode=?"
287 $sth->execute( $branchcode, $cat );
291 =head2 GetBranchCategory
293 $results = GetBranchCategory($categorycode);
295 C<$results> is an hashref
299 sub GetBranchCategory {
301 return unless $catcode;
303 my $dbh = C4::Context->dbh;
306 $sth = $dbh->prepare(q{
308 FROM branchcategories
309 WHERE categorycode = ?
311 $sth->execute( $catcode );
312 return $sth->fetchrow_hashref;
315 =head2 GetBranchCategories
317 my $categories = GetBranchCategories($categorytype,$show_in_pulldown,$selected_in_pulldown);
319 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
320 i.e. categorydescription, categorytype, categoryname.
324 sub GetBranchCategories {
325 my ( $categorytype, $show_in_pulldown, $selected_in_pulldown ) = @_;
326 my $dbh = C4::Context->dbh();
328 my $query = "SELECT * FROM branchcategories ";
330 my ( @where, @bind );
331 if ( $categorytype ) {
332 push @where, " categorytype = ? ";
333 push @bind, $categorytype;
336 if ( defined( $show_in_pulldown ) ) {
337 push( @where, " show_in_pulldown = ? " );
338 push( @bind, $show_in_pulldown );
341 $query .= " WHERE " . join(" AND ", @where) if(@where);
342 $query .= " ORDER BY categorytype, categorycode";
343 my $sth=$dbh->prepare( $query);
344 $sth->execute(@bind);
346 my $branchcats = $sth->fetchall_arrayref({});
348 if ( $selected_in_pulldown ) {
349 foreach my $bc ( @$branchcats ) {
350 $bc->{selected} = 1 if $bc->{categorycode} eq $selected_in_pulldown;
357 =head2 GetCategoryTypes
359 $categorytypes = GetCategoryTypes;
360 returns a list of category types.
361 Currently these types are HARDCODED.
362 type: 'searchdomain' defines a group of agencies that the calling library may search in.
363 Other usage of agency categories falls under type: 'properties'.
364 to allow for other uses of categories.
365 The searchdomain bit may be better implemented as a separate module, but
366 the categories were already here, and minimally used.
369 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
370 sub GetCategoryTypes {
371 return ( 'searchdomain','properties');
376 $branch = GetBranch( $query, $branches );
381 my ( $query, $branches ) = @_; # get branch for this query from branches
382 my $branch = $query->param('branch');
383 my %cookie = $query->cookie('userenv');
384 ($branch) || ($branch = $cookie{'branchname'});
385 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
389 =head2 GetBranchDetail
391 $branch = &GetBranchDetail($branchcode);
393 Given the branch code, the function returns a
394 hashref for the corresponding row in the branches table.
398 sub GetBranchDetail {
399 my ($branchcode) = shift or return;
400 my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
401 $sth->execute($branchcode);
402 return $sth->fetchrow_hashref();
405 =head2 GetBranchesInCategory
407 my $branches = GetBranchesInCategory($categorycode);
409 Returns a href: keys %$branches eq (branchcode,branchname) .
413 sub GetBranchesInCategory {
414 my ($categorycode) = @_;
416 my $dbh = C4::Context->dbh();
417 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
418 where r.branchcode=b.branchcode and r.categorycode=?");
419 $sth->execute($categorycode);
420 while (my $branch = $sth->fetchrow) {
421 push @branches, $branch;
423 return( \@branches );
428 $results = GetBranchInfo($branchcode);
430 returns C<$results>, a reference to an array of hashes containing branches.
431 if $branchcode, just this branch, with associated categories.
432 if ! $branchcode && $categorytype, all branches in the category.
436 my ($branchcode,$categorytype) = @_;
437 my $dbh = C4::Context->dbh;
444 "Select * from branches where branchcode = ? order by branchcode");
445 $sth->execute($branchcode);
448 $sth = $dbh->prepare("Select * from branches order by branchcode");
452 while ( my $data = $sth->fetchrow_hashref ) {
453 my @bind = ($data->{'branchcode'});
454 my $query= "select r.categorycode from branchrelations r";
455 $query .= ", branchcategories c " if($categorytype);
456 $query .= " where branchcode=? ";
458 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
459 push @bind, $categorytype;
461 my $nsth=$dbh->prepare($query);
462 $nsth->execute( @bind );
464 while ( my ($cat) = $nsth->fetchrow_array ) {
467 $data->{'categories'} = \@cats;
468 push( @results, $data );
475 &DelBranch($branchcode);
480 my ($branchcode) = @_;
481 my $dbh = C4::Context->dbh;
482 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
483 $sth->execute($branchcode);
486 =head2 ModBranchCategoryInfo
488 &ModBranchCategoryInfo($data);
489 sets the data from the editbranch form, and writes to the database...
493 sub ModBranchCategoryInfo {
495 my $dbh = C4::Context->dbh;
497 # we are doing an insert
498 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype,show_in_pulldown) VALUES (?,?,?,?,?)");
499 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'} );
503 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=?,show_in_pulldown=? WHERE categorycode=?");
504 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'},uc( $data->{'categorycode'} ) );
508 =head2 CheckCategoryUnique
510 if (CheckCategoryUnique($categorycode)){
516 sub CheckCategoryUnique {
517 my $categorycode = shift;
518 my $dbh = C4::Context->dbh;
519 my $sth = $dbh->prepare("SELECT categorycode FROM branchcategories WHERE categorycode = ?");
520 $sth->execute(uc( $categorycode) );
521 if (my $data = $sth->fetchrow_hashref){
530 =head2 DeleteBranchCategory
532 DeleteBranchCategory($categorycode);
536 sub DelBranchCategory {
537 my ($categorycode) = @_;
538 my $dbh = C4::Context->dbh;
539 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
540 $sth->execute($categorycode);
543 =head2 CheckBranchCategorycode
545 $number_rows_affected = CheckBranchCategorycode($categorycode);
549 sub CheckBranchCategorycode {
551 # check to see if the branchcode is being used in the database somewhere....
552 my ($categorycode) = @_;
553 my $dbh = C4::Context->dbh;
556 "select count(*) from branchrelations where categorycode=?");
557 $sth->execute($categorycode);
558 my ($total) = $sth->fetchrow_array;
562 sub GetBranchesCount {
563 my $dbh = C4::Context->dbh();
564 my $query = "SELECT COUNT(*) AS branches_count FROM branches";
565 my $sth = $dbh->prepare( $query );
567 my $row = $sth->fetchrow_hashref();
568 return $row->{'branches_count'};
576 Koha Development Team <http://koha-community.org/>