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
24 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
27 # set the version for version checking
39 &CheckBranchCategorycode
43 &GetBranchesInCategory
44 &ModBranchCategoryInfo
52 C4::Branch - Koha branch module
60 The functions in this module deal with branches.
66 $branches = &GetBranches();
67 returns informations about ALL branches.
68 Create a branch selector with the following code
69 IndependantBranches Insensitive...
70 GetBranchInfo() returns the same information without the problems of this function
71 (namespace collision, mainly). You should probably use that, and replace GetBranches()
72 with GetBranchInfo() where you see it in the code.
76 my $branches = GetBranches;
78 foreach my $thisbranch (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;
88 <select name="branch">
89 <option value="">Default</option>
90 <!-- TMPL_LOOP name="branchloop" -->
91 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
99 # returns a reference to a hash of references to ALL branches...
101 my $dbh = C4::Context->dbh;
103 my $query="SELECT * FROM branches";
105 if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
106 $query .= ' WHERE branchcode = ? ';
107 push @bind_parameters, C4::Context->userenv->{branch};
109 $query.=" ORDER BY branchname";
110 $sth = $dbh->prepare($query);
111 $sth->execute( @bind_parameters );
112 while ( my $branch = $sth->fetchrow_hashref ) {
115 "SELECT categorycode FROM branchrelations WHERE branchcode = ?");
116 $nsth->execute( $branch->{'branchcode'} );
117 while ( my ($cat) = $nsth->fetchrow_array ) {
119 # FIXME - This seems wrong. It ought to be
120 # $branch->{categorycodes}{$cat} = 1;
121 # otherwise, there's a namespace collision if there's a
122 # category with the same name as a field in the 'branches'
123 # table (i.e., don't create a category called "issuing").
124 # In addition, the current structure doesn't really allow
125 # you to list the categories that a branch belongs to:
126 # you'd have to list keys %$branch, and remove those keys
127 # that aren't fields in the "branches" table.
128 # $branch->{$cat} = 1;
129 $branch->{category}{$cat} = 1;
131 $branches{ $branch->{'branchcode'} } = $branch;
133 return ( \%branches );
136 sub GetBranchesLoop ($$) { # since this is what most pages want anyway
137 my $branch = shift or return undef;
138 my $onlymine = shift || 0;
139 my $branches = GetBranches($onlymine);
141 foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
144 selected => ($_ eq $branch) ? 1 : 0,
145 branchname => $branches->{$_}->{branchname},
156 my ($branchcode) = @_;
157 my $dbh = C4::Context->dbh;
159 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
160 $sth->execute($branchcode);
161 my $branchname = $sth->fetchrow_array;
163 return ($branchname);
168 &ModBranch($newvalue);
170 This function modify an existing branches.
172 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
179 my $dbh = C4::Context->dbh;
183 (branchcode,branchname,branchaddress1,
184 branchaddress2,branchaddress3,branchphone,
185 branchfax,branchemail,branchip,branchprinter)
186 VALUES (?,?,?,?,?,?,?,?,?,?)
188 my $sth = $dbh->prepare($query);
190 $data->{'branchcode'}, $data->{'branchname'},
191 $data->{'branchaddress1'}, $data->{'branchaddress2'},
192 $data->{'branchaddress3'}, $data->{'branchphone'},
193 $data->{'branchfax'}, $data->{'branchemail'},
194 $data->{'branchip'}, $data->{'branchprinter'},
199 SET branchname=?,branchaddress1=?,
200 branchaddress2=?,branchaddress3=?,branchphone=?,
201 branchfax=?,branchemail=?,branchip=?,branchprinter=?
204 my $sth = $dbh->prepare($query);
206 $data->{'branchname'},
207 $data->{'branchaddress1'}, $data->{'branchaddress2'},
208 $data->{'branchaddress3'}, $data->{'branchphone'},
209 $data->{'branchfax'}, $data->{'branchemail'},
210 $data->{'branchip'}, $data->{'branchprinter'},
211 $data->{'branchcode'},
214 # sort out the categories....
216 my $cats = GetBranchCategory();
217 foreach my $cat (@$cats) {
218 my $code = $cat->{'categorycode'};
219 if ( $data->{$code} ) {
220 push( @checkedcats, $code );
223 my $branchcode = uc( $data->{'branchcode'} );
224 my $branch = GetBranchInfo($branchcode);
225 $branch = $branch->[0];
226 my $branchcats = $branch->{'categories'};
229 foreach my $bcat (@$branchcats) {
231 unless ( grep { /^$bcat$/ } @checkedcats ) {
232 push( @removecats, $bcat );
235 foreach my $ccat (@checkedcats) {
236 unless ( grep { /^$ccat$/ } @$branchcats ) {
237 push( @addcats, $ccat );
240 foreach my $cat (@addcats) {
243 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
245 $sth->execute( $branchcode, $cat );
248 foreach my $cat (@removecats) {
251 "delete from branchrelations where branchcode=? and categorycode=?"
253 $sth->execute( $branchcode, $cat );
258 =head2 GetBranchCategory
260 $results = GetBranchCategory($categorycode);
262 C<$results> is an ref to an array.
266 sub GetBranchCategory {
268 # returns a reference to an array of hashes containing branches,
270 my $dbh = C4::Context->dbh;
273 # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
277 "select * from branchcategories where categorycode = ?");
278 $sth->execute($catcode);
281 $sth = $dbh->prepare("Select * from branchcategories");
285 while ( my $data = $sth->fetchrow_hashref ) {
286 push( @results, $data );
290 # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
294 =head2 GetBranchCategories
296 my $categories = GetBranchCategories($branchcode,$categorytype);
298 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
299 i.e. categorycode, categorydescription, categorytype, categoryname.
300 if $branchcode and/or $categorytype are passed, limit set to categories that
301 $branchcode is a member of , and to $categorytype.
305 sub GetBranchCategories {
306 my ($branchcode,$categorytype) = @_;
307 my $dbh = C4::Context->dbh();
308 my $query = "SELECT c.* FROM branchcategories c";
311 $query .= ",branchrelations r, branches b ";
312 push @where, "c.categorycode=r.categorycode and r.branchcode=? ";
313 push @bind , $branchcode;
316 push @where, " c.categorytype=? ";
317 push @bind, $categorytype;
319 $query .= " where " . join(" and ", @where) if(@where);
320 $query .= " order by categorytype,c.categorycode";
321 my $sth=$dbh->prepare( $query);
322 $sth->execute(@bind);
324 my $branchcats = $sth->fetchall_arrayref({});
326 return( $branchcats );
329 =head2 GetCategoryTypes
331 $categorytypes = GetCategoryTypes;
332 returns a list of category types.
333 Currently these types are HARDCODED.
334 type: 'searchdomain' defines a group of agencies that the calling library may search in.
335 Other usage of agency categories falls under type: 'properties'.
336 to allow for other uses of categories.
337 The searchdomain bit may be better implemented as a separate module, but
338 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 $branchname = &GetBranchDetail($branchcode);
365 Given the branch code, the function returns the corresponding
366 branch name for a comprehensive information display
370 sub GetBranchDetail {
371 my ($branchcode) = @_;
372 my $dbh = C4::Context->dbh;
373 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
374 $sth->execute($branchcode);
375 my $branchname = $sth->fetchrow_hashref();
380 =head2 get_branchinfos_of
382 my $branchinfos_of = get_branchinfos_of(@branchcodes);
384 Associates a list of branchcodes to the information of the branch, taken in
387 Returns a href where keys are branchcodes and values are href where keys are
388 branch information key.
390 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
394 sub get_branchinfos_of {
395 my @branchcodes = @_;
401 WHERE branchcode IN ('
402 . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
404 return C4::Koha::get_infos_of( $query, 'branchcode' );
408 =head2 GetBranchesInCategory
410 my $branches = GetBranchesInCategory($categorycode);
412 Returns a href: keys %$branches eq (branchcode,branchname) .
416 sub GetBranchesInCategory($) {
417 my ($categorycode) = @_;
419 my $dbh = C4::Context->dbh();
420 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
421 where r.branchcode=b.branchcode and r.categorycode=?");
422 $sth->execute($categorycode);
423 while (my $branch = $sth->fetchrow) {
424 push @branches, $branch;
427 return( \@branches );
432 $results = GetBranchInfo($branchcode);
434 returns C<$results>, a reference to an array of hashes containing branches.
435 if $branchcode, just this branch, with associated categories.
436 if ! $branchcode && $categorytype, all branches in the category.
440 my ($branchcode,$categorytype) = @_;
441 my $dbh = C4::Context->dbh;
448 "Select * from branches where branchcode = ? order by branchcode");
449 $sth->execute($branchcode);
452 $sth = $dbh->prepare("Select * from branches order by branchcode");
456 while ( my $data = $sth->fetchrow_hashref ) {
457 my @bind = ($data->{'branchcode'});
458 my $query= "select r.categorycode from branchrelations r";
459 $query .= ", branchcategories c " if($categorytype);
460 $query .= " where branchcode=? ";
462 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
463 push @bind, $categorytype;
465 my $nsth=$dbh->prepare($query);
466 $nsth->execute( @bind );
468 while ( my ($cat) = $nsth->fetchrow_array ) {
472 $data->{'categories'} = \@cats;
473 push( @results, $data );
481 &DelBranch($branchcode);
486 my ($branchcode) = @_;
487 my $dbh = C4::Context->dbh;
488 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
489 $sth->execute($branchcode);
493 =head2 ModBranchCategoryInfo
495 &ModBranchCategoryInfo($data);
496 sets the data from the editbranch form, and writes to the database...
500 sub ModBranchCategoryInfo {
502 my $dbh = C4::Context->dbh;
504 # we are doing an insert
505 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
506 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
511 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
512 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
517 =head2 DeleteBranchCategory
519 DeleteBranchCategory($categorycode);
523 sub DelBranchCategory {
524 my ($categorycode) = @_;
525 my $dbh = C4::Context->dbh;
526 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
527 $sth->execute($categorycode);
531 =head2 CheckBranchCategorycode
533 $number_rows_affected = CheckBranchCategorycode($categorycode);
537 sub CheckBranchCategorycode {
539 # check to see if the branchcode is being used in the database somewhere....
540 my ($categorycode) = @_;
541 my $dbh = C4::Context->dbh;
544 "select count(*) from branchrelations where categorycode=?");
545 $sth->execute($categorycode);
546 my ($total) = $sth->fetchrow_array;
555 Koha Developement team <info@koha.org>