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
38 &CheckBranchCategorycode
42 &GetBranchesInCategory
43 &ModBranchCategoryInfo
51 C4::Branch - Koha branch module
59 The functions in this module deal with branches.
65 $branches = &GetBranches();
66 returns informations about ALL branches.
67 Create a branch selector with the following code
68 IndependantBranches Insensitive...
69 GetBranchInfo() returns the same information without the problems of this function
70 (namespace collision, mainly). You should probably use that, and replace GetBranches()
71 with GetBranchInfo() where you see it in the code.
75 my $branches = GetBranches;
77 foreach my $thisbranch (keys %$branches) {
78 my $selected = 1 if $thisbranch eq $branch;
79 my %row =(value => $thisbranch,
80 selected => $selected,
81 branchname => $branches->{$thisbranch}->{'branchname'},
83 push @branchloop, \%row;
87 <select name="branch">
88 <option value="">Default</option>
89 <!-- TMPL_LOOP name="branchloop" -->
90 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
98 # returns a reference to a hash of references to ALL branches...
100 my $dbh = C4
::Context
->dbh;
102 my $query="SELECT * FROM branches";
103 if ($onlymine && C4
::Context
->userenv && C4
::Context
->userenv->{branch
}){
104 $query .= " WHERE branchcode =".$dbh->quote(C4
::Context
->userenv->{branch
});
106 $query.=" ORDER BY branchname";
107 $sth = $dbh->prepare($query);
109 while ( my $branch = $sth->fetchrow_hashref ) {
112 "SELECT categorycode FROM branchrelations WHERE branchcode = ?");
113 $nsth->execute( $branch->{'branchcode'} );
114 while ( my ($cat) = $nsth->fetchrow_array ) {
116 # FIXME - This seems wrong. It ought to be
117 # $branch->{categorycodes}{$cat} = 1;
118 # otherwise, there's a namespace collision if there's a
119 # category with the same name as a field in the 'branches'
120 # table (i.e., don't create a category called "issuing").
121 # In addition, the current structure doesn't really allow
122 # you to list the categories that a branch belongs to:
123 # you'd have to list keys %$branch, and remove those keys
124 # that aren't fields in the "branches" table.
125 # $branch->{$cat} = 1;
126 $branch->{category
}{$cat} = 1;
128 $branches{ $branch->{'branchcode'} } = $branch;
130 return ( \
%branches );
138 my ($branchcode) = @_;
139 my $dbh = C4
::Context
->dbh;
141 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
142 $sth->execute($branchcode);
143 my $branchname = $sth->fetchrow_array;
145 return ($branchname);
150 &ModBranch($newvalue);
152 This function modify an existing branches.
154 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
161 my $dbh = C4
::Context
->dbh;
165 (branchcode,branchname,branchaddress1,
166 branchaddress2,branchaddress3,branchphone,
167 branchfax,branchemail,branchip,branchprinter)
168 VALUES (?,?,?,?,?,?,?,?,?,?)
170 my $sth = $dbh->prepare($query);
172 $data->{'branchcode'}, $data->{'branchname'},
173 $data->{'branchaddress1'}, $data->{'branchaddress2'},
174 $data->{'branchaddress3'}, $data->{'branchphone'},
175 $data->{'branchfax'}, $data->{'branchemail'},
176 $data->{'branchip'}, $data->{'branchprinter'},
181 SET branchname=?,branchaddress1=?,
182 branchaddress2=?,branchaddress3=?,branchphone=?,
183 branchfax=?,branchemail=?,branchip=?,branchprinter=?
186 my $sth = $dbh->prepare($query);
188 $data->{'branchname'},
189 $data->{'branchaddress1'}, $data->{'branchaddress2'},
190 $data->{'branchaddress3'}, $data->{'branchphone'},
191 $data->{'branchfax'}, $data->{'branchemail'},
192 $data->{'branchip'}, $data->{'branchprinter'},
193 $data->{'branchcode'},
196 # sort out the categories....
198 my $cats = GetBranchCategory
();
199 foreach my $cat (@
$cats) {
200 my $code = $cat->{'categorycode'};
201 if ( $data->{$code} ) {
202 push( @checkedcats, $code );
205 my $branchcode = uc( $data->{'branchcode'} );
206 my $branch = GetBranchInfo
($branchcode);
207 $branch = $branch->[0];
208 my $branchcats = $branch->{'categories'};
211 foreach my $bcat (@
$branchcats) {
213 unless ( grep { /^$bcat$/ } @checkedcats ) {
214 push( @removecats, $bcat );
217 foreach my $ccat (@checkedcats) {
218 unless ( grep { /^$ccat$/ } @
$branchcats ) {
219 push( @addcats, $ccat );
222 foreach my $cat (@addcats) {
225 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
227 $sth->execute( $branchcode, $cat );
230 foreach my $cat (@removecats) {
233 "delete from branchrelations where branchcode=? and categorycode=?"
235 $sth->execute( $branchcode, $cat );
240 =head2 GetBranchCategory
242 $results = GetBranchCategory($categorycode);
244 C<$results> is an ref to an array.
248 sub GetBranchCategory
{
250 # returns a reference to an array of hashes containing branches,
252 my $dbh = C4
::Context
->dbh;
255 # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
259 "select * from branchcategories where categorycode = ?");
260 $sth->execute($catcode);
263 $sth = $dbh->prepare("Select * from branchcategories");
267 while ( my $data = $sth->fetchrow_hashref ) {
268 push( @results, $data );
272 # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
276 =head2 GetBranchCategories
278 my $categories = GetBranchCategories($branchcode,$categorytype);
280 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
281 i.e. categorycode, categorydescription, categorytype, categoryname.
282 if $branchcode and/or $categorytype are passed, limit set to categories that
283 $branchcode is a member of , and to $categorytype.
287 sub GetBranchCategories
{
288 my ($branchcode,$categorytype) = @_;
289 my $dbh = C4
::Context
->dbh();
290 my $query = "SELECT c.* FROM branchcategories c";
293 $query .= ",branchrelations r, branches b ";
294 push @where, "c.categorycode=r.categorycode and r.branchcode=? ";
295 push @bind , $branchcode;
298 push @where, " c.categorytype=? ";
299 push @bind, $categorytype;
301 $query .= " where " . join(" and ", @where) if(@where);
302 $query .= " order by categorytype,c.categorycode";
303 my $sth=$dbh->prepare( $query);
304 $sth->execute(@bind);
306 my $branchcats = $sth->fetchall_arrayref({});
308 return( $branchcats );
311 =head2 GetCategoryTypes
313 $categorytypes = GetCategoryTypes;
314 returns a list of category types.
315 Currently these types are HARDCODED.
316 type: 'searchdomain' defines a group of agencies that the calling library may search in.
317 Other usage of agency categories falls under type: 'properties'.
318 to allow for other uses of categories.
319 The searchdomain bit may be better implemented as a separate module, but
320 the categories were already here, and minimally used.
323 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
324 sub GetCategoryTypes
() {
325 return ( 'searchdomain','properties');
330 $branch = GetBranch( $query, $branches );
335 my ( $query, $branches ) = @_; # get branch for this query from branches
336 my $branch = $query->param('branch');
337 my %cookie = $query->cookie('userenv');
338 ($branch) || ($branch = $cookie{'branchname'});
339 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
343 =head2 GetBranchDetail
345 $branchname = &GetBranchDetail($branchcode);
347 Given the branch code, the function returns the corresponding
348 branch name for a comprehensive information display
352 sub GetBranchDetail
{
353 my ($branchcode) = @_;
354 my $dbh = C4
::Context
->dbh;
355 my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
356 $sth->execute($branchcode);
357 my $branchname = $sth->fetchrow_hashref();
362 =head2 get_branchinfos_of
364 my $branchinfos_of = get_branchinfos_of(@branchcodes);
366 Associates a list of branchcodes to the information of the branch, taken in
369 Returns a href where keys are branchcodes and values are href where keys are
370 branch information key.
372 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
376 sub get_branchinfos_of
{
377 my @branchcodes = @_;
383 WHERE branchcode IN ('
384 . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
386 return C4
::Koha
::get_infos_of
( $query, 'branchcode' );
390 =head2 GetBranchesInCategory
392 my $branches = GetBranchesInCategory($categorycode);
394 Returns a href: keys %$branches eq (branchcode,branchname) .
398 sub GetBranchesInCategory
($) {
399 my ($categorycode) = @_;
401 my $dbh = C4
::Context
->dbh();
402 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
403 where r.branchcode=b.branchcode and r.categorycode=?");
404 $sth->execute($categorycode);
405 while (my $branch = $sth->fetchrow) {
406 push @branches, $branch;
409 return( \
@branches );
414 $results = GetBranchInfo($branchcode);
416 returns C<$results>, a reference to an array of hashes containing branches.
417 if $branchcode, just this branch, with associated categories.
418 if ! $branchcode && $categorytype, all branches in the category.
422 my ($branchcode,$categorytype) = @_;
423 my $dbh = C4
::Context
->dbh;
430 "Select * from branches where branchcode = ? order by branchcode");
431 $sth->execute($branchcode);
434 $sth = $dbh->prepare("Select * from branches order by branchcode");
438 while ( my $data = $sth->fetchrow_hashref ) {
439 my @bind = ($data->{'branchcode'});
440 my $query= "select r.categorycode from branchrelations r";
441 $query .= ", branchcategories c " if($categorytype);
442 $query .= " where branchcode=? ";
444 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
445 push @bind, $categorytype;
447 my $nsth=$dbh->prepare($query);
448 $nsth->execute( @bind );
450 while ( my ($cat) = $nsth->fetchrow_array ) {
454 $data->{'categories'} = \
@cats;
455 push( @results, $data );
463 &DelBranch($branchcode);
468 my ($branchcode) = @_;
469 my $dbh = C4
::Context
->dbh;
470 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
471 $sth->execute($branchcode);
475 =head2 ModBranchCategoryInfo
477 &ModBranchCategoryInfo($data);
478 sets the data from the editbranch form, and writes to the database...
482 sub ModBranchCategoryInfo
{
484 my $dbh = C4
::Context
->dbh;
486 # we are doing an insert
487 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
488 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
493 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
494 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
499 =head2 DeleteBranchCategory
501 DeleteBranchCategory($categorycode);
505 sub DelBranchCategory
{
506 my ($categorycode) = @_;
507 my $dbh = C4
::Context
->dbh;
508 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
509 $sth->execute($categorycode);
513 =head2 CheckBranchCategorycode
515 $number_rows_affected = CheckBranchCategorycode($categorycode);
519 sub CheckBranchCategorycode
{
521 # check to see if the branchcode is being used in the database somewhere....
522 my ($categorycode) = @_;
523 my $dbh = C4
::Context
->dbh;
526 "select count(*) from branchrelations where categorycode=?");
527 $sth->execute($categorycode);
528 my ($total) = $sth->fetchrow_array;
537 Koha Developement team <info@koha.org>