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
39 &CheckBranchCategorycode
43 &GetBranchesInCategory
44 &ModBranchCategoryInfo
50 @EXPORT_OK = qw( &onlymine &mybranch get_branch_code_from_name );
55 C4::Branch - Koha branch module
63 The functions in this module deal with branches.
69 $branches = &GetBranches();
71 Returns informations about ALL branches, IndependantBranches Insensitive.
72 GetBranchInfo() returns the same information without the problems of this function
73 (namespace collision, mainly).
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">
93 <option value="">Default</option>
94 <!-- TMPL_LOOP name="branchloop" -->
95 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
99 =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 );
119 my $nsth = $dbh->prepare(
120 "SELECT categorycode FROM branchrelations WHERE branchcode = ?"
121 ); # prepare once, outside while loop
123 while ( my $branch = $sth->fetchrow_hashref ) {
124 $nsth->execute( $branch->{'branchcode'} );
125 while ( my ($cat) = $nsth->fetchrow_array ) {
126 # FIXME - This seems wrong. It ought to be
127 # $branch->{categorycodes}{$cat} = 1;
128 # otherwise, there's a namespace collision if there's a
129 # category with the same name as a field in the 'branches'
130 # table (i.e., don't create a category called "issuing").
131 # In addition, the current structure doesn't really allow
132 # you to list the categories that a branch belongs to:
133 # you'd have to list keys %$branch, and remove those keys
134 # that aren't fields in the "branches" table.
135 # $branch->{$cat} = 1;
136 $branch->{category
}{$cat} = 1;
138 $branches{ $branch->{'branchcode'} } = $branch;
140 return ( \
%branches );
145 C4
::Context
->preference('IndependantBranches') &&
146 C4
::Context
->userenv &&
147 C4
::Context
->userenv->{flags
} %2 != 1 &&
148 C4
::Context
->userenv->{branch
} ;
151 # always returns a string for OK comparison via "eq" or "ne"
153 C4
::Context
->userenv or return '';
154 return C4
::Context
->userenv->{branch
} || '';
157 sub GetBranchesLoop
(;$$) { # since this is what most pages want anyway
158 my $branch = @_ ?
shift : mybranch
(); # optional first argument is branchcode of "my branch", if preselection is wanted.
159 my $onlymine = @_ ?
shift : onlymine
();
160 my $branches = GetBranches
($onlymine);
162 foreach ( sort { uc($branches->{$a}->{branchname
}) cmp uc($branches->{$b}->{branchname
}) } keys %$branches ) {
165 selected
=> ($_ eq $branch) ?
1 : 0,
166 branchname
=> $branches->{$_}->{branchname
},
177 my ($branchcode) = @_;
178 my $dbh = C4
::Context
->dbh;
180 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
181 $sth->execute($branchcode);
182 my $branchname = $sth->fetchrow_array;
184 return ($branchname);
189 $error = &ModBranch($newvalue);
191 This function modify an existing branch
193 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
200 my $dbh = C4
::Context
->dbh;
204 (branchcode,branchname,branchaddress1,
205 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
206 branchcountry,branchphone,branchfax,branchemail,
207 branchurl,branchip,branchprinter,branchnotes,opac_info)
208 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
210 my $sth = $dbh->prepare($query);
212 $data->{'branchcode'}, $data->{'branchname'},
213 $data->{'branchaddress1'}, $data->{'branchaddress2'},
214 $data->{'branchaddress3'}, $data->{'branchzip'},
215 $data->{'branchcity'}, $data->{'branchstate'},
216 $data->{'branchcountry'},
217 $data->{'branchphone'}, $data->{'branchfax'},
218 $data->{'branchemail'}, $data->{'branchurl'},
219 $data->{'branchip'}, $data->{'branchprinter'},
220 $data->{'branchnotes'}, $data->{opac_info
},
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=?
233 my $sth = $dbh->prepare($query);
235 $data->{'branchname'},
236 $data->{'branchaddress1'}, $data->{'branchaddress2'},
237 $data->{'branchaddress3'}, $data->{'branchzip'},
238 $data->{'branchcity'}, $data->{'branchstate'},
239 $data->{'branchcountry'},
240 $data->{'branchphone'}, $data->{'branchfax'},
241 $data->{'branchemail'}, $data->{'branchurl'},
242 $data->{'branchip'}, $data->{'branchprinter'},
243 $data->{'branchnotes'}, $data->{opac_info
},
244 $data->{'branchcode'},
247 # sort out the categories....
249 my $cats = GetBranchCategory
();
250 foreach my $cat (@
$cats) {
251 my $code = $cat->{'categorycode'};
252 if ( $data->{$code} ) {
253 push( @checkedcats, $code );
256 my $branchcode = uc( $data->{'branchcode'} );
257 my $branch = GetBranchInfo
($branchcode);
258 $branch = $branch->[0];
259 my $branchcats = $branch->{'categories'};
262 foreach my $bcat (@
$branchcats) {
264 unless ( grep { /^$bcat$/ } @checkedcats ) {
265 push( @removecats, $bcat );
268 foreach my $ccat (@checkedcats) {
269 unless ( grep { /^$ccat$/ } @
$branchcats ) {
270 push( @addcats, $ccat );
273 foreach my $cat (@addcats) {
276 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
278 $sth->execute( $branchcode, $cat );
281 foreach my $cat (@removecats) {
284 "delete from branchrelations where branchcode=? and categorycode=?"
286 $sth->execute( $branchcode, $cat );
291 =head2 GetBranchCategory
293 $results = GetBranchCategory($categorycode);
295 C<$results> is an ref to an array.
299 sub GetBranchCategory
{
301 # returns a reference to an array of hashes containing branches,
303 my $dbh = C4
::Context
->dbh;
306 # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
310 "select * from branchcategories where categorycode = ?");
311 $sth->execute($catcode);
314 $sth = $dbh->prepare("Select * from branchcategories");
318 while ( my $data = $sth->fetchrow_hashref ) {
319 push( @results, $data );
323 # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
327 =head2 GetBranchCategories
329 my $categories = GetBranchCategories($branchcode,$categorytype);
331 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
332 i.e. categorycode, categorydescription, categorytype, categoryname.
333 if $branchcode and/or $categorytype are passed, limit set to categories that
334 $branchcode is a member of , and to $categorytype.
338 sub GetBranchCategories
{
339 my ($branchcode,$categorytype) = @_;
340 my $dbh = C4
::Context
->dbh();
341 my $query = "SELECT c.* FROM branchcategories c";
344 $query .= ",branchrelations r, branches b ";
345 push @where, "c.categorycode=r.categorycode and r.branchcode=? ";
346 push @bind , $branchcode;
349 push @where, " c.categorytype=? ";
350 push @bind, $categorytype;
352 $query .= " where " . join(" and ", @where) if(@where);
353 $query .= " order by categorytype,c.categorycode";
354 my $sth=$dbh->prepare( $query);
355 $sth->execute(@bind);
357 my $branchcats = $sth->fetchall_arrayref({});
359 return( $branchcats );
362 =head2 GetCategoryTypes
364 $categorytypes = GetCategoryTypes;
365 returns a list of category types.
366 Currently these types are HARDCODED.
367 type: 'searchdomain' defines a group of agencies that the calling library may search in.
368 Other usage of agency categories falls under type: 'properties'.
369 to allow for other uses of categories.
370 The searchdomain bit may be better implemented as a separate module, but
371 the categories were already here, and minimally used.
374 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
375 sub GetCategoryTypes
() {
376 return ( 'searchdomain','properties');
381 $branch = GetBranch( $query, $branches );
386 my ( $query, $branches ) = @_; # get branch for this query from branches
387 my $branch = $query->param('branch');
388 my %cookie = $query->cookie('userenv');
389 ($branch) || ($branch = $cookie{'branchname'});
390 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
394 =head2 GetBranchDetail
396 $branch = &GetBranchDetail($branchcode);
398 Given the branch code, the function returns a
399 hashref for the corresponding row in the branches table.
403 sub GetBranchDetail
{
404 my ($branchcode) = shift or return;
405 my $sth = C4
::Context
->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
406 $sth->execute($branchcode);
407 return $sth->fetchrow_hashref();
410 =head2 GetBranchesInCategory
412 my $branches = GetBranchesInCategory($categorycode);
414 Returns a href: keys %$branches eq (branchcode,branchname) .
418 sub GetBranchesInCategory
($) {
419 my ($categorycode) = @_;
421 my $dbh = C4
::Context
->dbh();
422 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
423 where r.branchcode=b.branchcode and r.categorycode=?");
424 $sth->execute($categorycode);
425 while (my $branch = $sth->fetchrow) {
426 push @branches, $branch;
429 return( \
@branches );
434 $results = GetBranchInfo($branchcode);
436 returns C<$results>, a reference to an array of hashes containing branches.
437 if $branchcode, just this branch, with associated categories.
438 if ! $branchcode && $categorytype, all branches in the category.
442 my ($branchcode,$categorytype) = @_;
443 my $dbh = C4
::Context
->dbh;
450 "Select * from branches where branchcode = ? order by branchcode");
451 $sth->execute($branchcode);
454 $sth = $dbh->prepare("Select * from branches order by branchcode");
458 while ( my $data = $sth->fetchrow_hashref ) {
459 my @bind = ($data->{'branchcode'});
460 my $query= "select r.categorycode from branchrelations r";
461 $query .= ", branchcategories c " if($categorytype);
462 $query .= " where branchcode=? ";
464 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
465 push @bind, $categorytype;
467 my $nsth=$dbh->prepare($query);
468 $nsth->execute( @bind );
470 while ( my ($cat) = $nsth->fetchrow_array ) {
474 $data->{'categories'} = \
@cats;
475 push( @results, $data );
483 &DelBranch($branchcode);
488 my ($branchcode) = @_;
489 my $dbh = C4
::Context
->dbh;
490 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
491 $sth->execute($branchcode);
495 =head2 ModBranchCategoryInfo
497 &ModBranchCategoryInfo($data);
498 sets the data from the editbranch form, and writes to the database...
502 sub ModBranchCategoryInfo
{
504 my $dbh = C4
::Context
->dbh;
506 # we are doing an insert
507 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
508 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
513 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
514 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
519 =head2 CheckCategoryUnique
521 if (CheckCategoryUnique($categorycode)){
527 sub CheckCategoryUnique
{
528 my $categorycode = shift;
529 my $dbh = C4
::Context
->dbh;
530 my $sth = $dbh->prepare("SELECT categorycode FROM branchcategories WHERE categorycode = ?");
531 $sth->execute(uc( $categorycode) );
532 if (my $data = $sth->fetchrow_hashref){
541 =head2 DeleteBranchCategory
543 DeleteBranchCategory($categorycode);
547 sub DelBranchCategory
{
548 my ($categorycode) = @_;
549 my $dbh = C4
::Context
->dbh;
550 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
551 $sth->execute($categorycode);
555 =head2 CheckBranchCategorycode
557 $number_rows_affected = CheckBranchCategorycode($categorycode);
561 sub CheckBranchCategorycode
{
563 # check to see if the branchcode is being used in the database somewhere....
564 my ($categorycode) = @_;
565 my $dbh = C4
::Context
->dbh;
568 "select count(*) from branchrelations where categorycode=?");
569 $sth->execute($categorycode);
570 my ($total) = $sth->fetchrow_array;
574 sub get_branch_code_from_name
{
575 my @branch_name = @_;
576 my $query = "SELECT branchcode FROM branches WHERE branchname=?;";
577 my $dbh = C4
::Context
->dbh();
578 my $sth = $dbh->prepare($query);
579 $sth->execute(@branch_name);
580 return $sth->fetchrow_array;
588 Koha Development Team <http://koha-community.org/>