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
25 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28 # set the version for version checking
40 &CheckBranchCategorycode
44 &GetBranchesInCategory
45 &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).
74 Create a branch selector with the following code.
78 my $branches = GetBranches;
80 foreach my $thisbranch (sort keys %$branches) {
81 my $selected = 1 if $thisbranch eq $branch;
82 my %row =(value => $thisbranch,
83 selected => $selected,
84 branchname => $branches->{$thisbranch}->{branchname},
86 push @branchloop, \%row;
91 <select name="branch">
92 <option value="">Default</option>
93 <!-- TMPL_LOOP name="branchloop" -->
94 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
98 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
104 # returns a reference to a hash of references to ALL branches...
106 my $dbh = C4
::Context
->dbh;
108 my $query="SELECT * FROM branches";
110 if ($onlymine && C4
::Context
->userenv && C4
::Context
->userenv->{branch
}){
111 $query .= ' WHERE branchcode = ? ';
112 push @bind_parameters, C4
::Context
->userenv->{branch
};
114 $query.=" ORDER BY branchname";
115 $sth = $dbh->prepare($query);
116 $sth->execute( @bind_parameters );
118 my $nsth = $dbh->prepare(
119 "SELECT categorycode FROM branchrelations WHERE branchcode = ?"
120 ); # prepare once, outside while loop
122 while ( my $branch = $sth->fetchrow_hashref ) {
123 $nsth->execute( $branch->{'branchcode'} );
124 while ( my ($cat) = $nsth->fetchrow_array ) {
125 # FIXME - This seems wrong. It ought to be
126 # $branch->{categorycodes}{$cat} = 1;
127 # otherwise, there's a namespace collision if there's a
128 # category with the same name as a field in the 'branches'
129 # table (i.e., don't create a category called "issuing").
130 # In addition, the current structure doesn't really allow
131 # you to list the categories that a branch belongs to:
132 # you'd have to list keys %$branch, and remove those keys
133 # that aren't fields in the "branches" table.
134 # $branch->{$cat} = 1;
135 $branch->{category
}{$cat} = 1;
137 $branches{ $branch->{'branchcode'} } = $branch;
139 return ( \
%branches );
144 C4
::Context
->preference('IndependantBranches') &&
145 C4
::Context
->userenv &&
146 C4
::Context
->userenv->{flags
} %2 != 1 &&
147 C4
::Context
->userenv->{branch
} ;
150 # always returns a string for OK comparison via "eq" or "ne"
152 C4
::Context
->userenv or return '';
153 return C4
::Context
->userenv->{branch
} || '';
156 sub GetBranchesLoop
(;$$) { # since this is what most pages want anyway
157 my $branch = @_ ?
shift : mybranch
(); # optional first argument is branchcode of "my branch", if preselection is wanted.
158 my $onlymine = @_ ?
shift : onlymine
();
159 my $branches = GetBranches
($onlymine);
161 foreach (sort { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} } keys %$branches) {
164 selected
=> ($_ eq $branch) ?
1 : 0,
165 branchname
=> $branches->{$_}->{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;
183 return ($branchname);
188 $error = &ModBranch($newvalue);
190 This function modify an existing branch
192 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
199 my $dbh = C4
::Context
->dbh;
203 (branchcode,branchname,branchaddress1,
204 branchaddress2,branchaddress3,branchzip,branchcity,
205 branchcountry,branchphone,branchfax,branchemail,
206 branchurl,branchip,branchprinter,branchnotes)
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->{'branchcountry'},
215 $data->{'branchphone'}, $data->{'branchfax'},
216 $data->{'branchemail'}, $data->{'branchurl'},
217 $data->{'branchip'}, $data->{'branchprinter'},
218 $data->{'branchnotes'},
220 return 1 if $dbh->err;
224 SET branchname=?,branchaddress1=?,
225 branchaddress2=?,branchaddress3=?,branchzip=?,
226 branchcity=?,branchcountry=?,branchphone=?,
227 branchfax=?,branchemail=?,branchurl=?,branchip=?,
228 branchprinter=?,branchnotes=?
231 my $sth = $dbh->prepare($query);
233 $data->{'branchname'},
234 $data->{'branchaddress1'}, $data->{'branchaddress2'},
235 $data->{'branchaddress3'}, $data->{'branchzip'},
236 $data->{'branchcity'}, $data->{'branchcountry'},
237 $data->{'branchphone'}, $data->{'branchfax'},
238 $data->{'branchemail'}, $data->{'branchurl'},
239 $data->{'branchip'}, $data->{'branchprinter'},
240 $data->{'branchnotes'},
241 $data->{'branchcode'},
244 # sort out the categories....
246 my $cats = GetBranchCategory
();
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 );
278 foreach my $cat (@removecats) {
281 "delete from branchrelations where branchcode=? and categorycode=?"
283 $sth->execute( $branchcode, $cat );
288 =head2 GetBranchCategory
290 $results = GetBranchCategory($categorycode);
292 C<$results> is an ref to an array.
296 sub GetBranchCategory
{
298 # returns a reference to an array of hashes containing branches,
300 my $dbh = C4
::Context
->dbh;
303 # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
307 "select * from branchcategories where categorycode = ?");
308 $sth->execute($catcode);
311 $sth = $dbh->prepare("Select * from branchcategories");
315 while ( my $data = $sth->fetchrow_hashref ) {
316 push( @results, $data );
320 # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
324 =head2 GetBranchCategories
326 my $categories = GetBranchCategories($branchcode,$categorytype);
328 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
329 i.e. categorycode, categorydescription, categorytype, categoryname.
330 if $branchcode and/or $categorytype are passed, limit set to categories that
331 $branchcode is a member of , and to $categorytype.
335 sub GetBranchCategories
{
336 my ($branchcode,$categorytype) = @_;
337 my $dbh = C4
::Context
->dbh();
338 my $query = "SELECT c.* FROM branchcategories c";
341 $query .= ",branchrelations r, branches b ";
342 push @where, "c.categorycode=r.categorycode and r.branchcode=? ";
343 push @bind , $branchcode;
346 push @where, " c.categorytype=? ";
347 push @bind, $categorytype;
349 $query .= " where " . join(" and ", @where) if(@where);
350 $query .= " order by categorytype,c.categorycode";
351 my $sth=$dbh->prepare( $query);
352 $sth->execute(@bind);
354 my $branchcats = $sth->fetchall_arrayref({});
356 return( $branchcats );
359 =head2 GetCategoryTypes
361 $categorytypes = GetCategoryTypes;
362 returns a list of category types.
363 Currently these types are HARDCODED.
364 type: 'searchdomain' defines a group of agencies that the calling library may search in.
365 Other usage of agency categories falls under type: 'properties'.
366 to allow for other uses of categories.
367 The searchdomain bit may be better implemented as a separate module, but
368 the categories were already here, and minimally used.
371 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
372 sub GetCategoryTypes
() {
373 return ( 'searchdomain','properties');
378 $branch = GetBranch( $query, $branches );
383 my ( $query, $branches ) = @_; # get branch for this query from branches
384 my $branch = $query->param('branch');
385 my %cookie = $query->cookie('userenv');
386 ($branch) || ($branch = $cookie{'branchname'});
387 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
391 =head2 GetBranchDetail
393 $branch = &GetBranchDetail($branchcode);
395 Given the branch code, the function returns a
396 hashref for the corresponding row in the branches table.
400 sub GetBranchDetail
{
401 my ($branchcode) = shift or return;
402 my $sth = C4
::Context
->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
403 $sth->execute($branchcode);
404 return $sth->fetchrow_hashref();
407 =head2 get_branchinfos_of
409 my $branchinfos_of = get_branchinfos_of(@branchcodes);
411 Associates a list of branchcodes to the information of the branch, taken in
414 Returns a href where keys are branchcodes and values are href where keys are
415 branch information key.
417 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
421 sub get_branchinfos_of
{
422 my @branchcodes = @_;
428 WHERE branchcode IN ('
429 . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
431 return C4
::Koha
::get_infos_of
( $query, 'branchcode' );
435 =head2 GetBranchesInCategory
437 my $branches = GetBranchesInCategory($categorycode);
439 Returns a href: keys %$branches eq (branchcode,branchname) .
443 sub GetBranchesInCategory
($) {
444 my ($categorycode) = @_;
446 my $dbh = C4
::Context
->dbh();
447 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
448 where r.branchcode=b.branchcode and r.categorycode=?");
449 $sth->execute($categorycode);
450 while (my $branch = $sth->fetchrow) {
451 push @branches, $branch;
454 return( \
@branches );
459 $results = GetBranchInfo($branchcode);
461 returns C<$results>, a reference to an array of hashes containing branches.
462 if $branchcode, just this branch, with associated categories.
463 if ! $branchcode && $categorytype, all branches in the category.
467 my ($branchcode,$categorytype) = @_;
468 my $dbh = C4
::Context
->dbh;
475 "Select * from branches where branchcode = ? order by branchcode");
476 $sth->execute($branchcode);
479 $sth = $dbh->prepare("Select * from branches order by branchcode");
483 while ( my $data = $sth->fetchrow_hashref ) {
484 my @bind = ($data->{'branchcode'});
485 my $query= "select r.categorycode from branchrelations r";
486 $query .= ", branchcategories c " if($categorytype);
487 $query .= " where branchcode=? ";
489 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
490 push @bind, $categorytype;
492 my $nsth=$dbh->prepare($query);
493 $nsth->execute( @bind );
495 while ( my ($cat) = $nsth->fetchrow_array ) {
499 $data->{'categories'} = \
@cats;
500 push( @results, $data );
508 &DelBranch($branchcode);
513 my ($branchcode) = @_;
514 my $dbh = C4
::Context
->dbh;
515 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
516 $sth->execute($branchcode);
520 =head2 ModBranchCategoryInfo
522 &ModBranchCategoryInfo($data);
523 sets the data from the editbranch form, and writes to the database...
527 sub ModBranchCategoryInfo
{
529 my $dbh = C4
::Context
->dbh;
531 # we are doing an insert
532 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
533 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
538 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
539 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
544 =head2 CheckCategoryUnique
546 if (CheckCategoryUnique($categorycode)){
552 sub CheckCategoryUnique
{
553 my $categorycode = shift;
554 my $dbh = C4
::Context
->dbh;
555 my $sth = $dbh->prepare("SELECT categorycode FROM branchcategories WHERE categorycode = ?");
556 $sth->execute(uc( $categorycode) );
557 if (my $data = $sth->fetchrow_hashref){
566 =head2 DeleteBranchCategory
568 DeleteBranchCategory($categorycode);
572 sub DelBranchCategory
{
573 my ($categorycode) = @_;
574 my $dbh = C4
::Context
->dbh;
575 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
576 $sth->execute($categorycode);
580 =head2 CheckBranchCategorycode
582 $number_rows_affected = CheckBranchCategorycode($categorycode);
586 sub CheckBranchCategorycode
{
588 # check to see if the branchcode is being used in the database somewhere....
589 my ($categorycode) = @_;
590 my $dbh = C4
::Context
->dbh;
593 "select count(*) from branchrelations where categorycode=?");
594 $sth->execute($categorycode);
595 my ($total) = $sth->fetchrow_array;
599 sub get_branch_code_from_name
{
600 my @branch_name = @_;
601 my $query = "SELECT branchcode FROM branches WHERE branchname=?;";
602 my $dbh = C4
::Context
->dbh();
603 my $sth = $dbh->prepare($query);
604 $sth->execute(@branch_name);
605 return $sth->fetchrow_array;
613 Koha Developement team <info@koha.org>