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 get_branch_code_from_name );
56 C4::Branch - Koha branch module
64 The functions in this module deal with branches.
70 $branches = &GetBranches();
72 Returns informations about ALL branches, IndependantBranches Insensitive.
73 GetBranchInfo() returns the same information without the problems of this function
74 (namespace collision, mainly).
76 Create a branch selector with the following code.
80 my $branches = GetBranches;
82 foreach my $thisbranch (sort keys %$branches) {
83 my $selected = 1 if $thisbranch eq $branch;
84 my %row =(value => $thisbranch,
85 selected => $selected,
86 branchname => $branches->{$thisbranch}->{branchname},
88 push @branchloop, \%row;
93 <select name="branch">
94 <option value="">Default</option>
95 <!-- TMPL_LOOP name="branchloop" -->
96 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
100 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
106 # returns a reference to a hash of references to ALL branches...
108 my $dbh = C4
::Context
->dbh;
110 my $query="SELECT * FROM branches";
112 if ($onlymine && C4
::Context
->userenv && C4
::Context
->userenv->{branch
}){
113 $query .= ' WHERE branchcode = ? ';
114 push @bind_parameters, C4
::Context
->userenv->{branch
};
116 $query.=" ORDER BY branchname";
117 $sth = $dbh->prepare($query);
118 $sth->execute( @bind_parameters );
120 my $nsth = $dbh->prepare(
121 "SELECT categorycode FROM branchrelations WHERE branchcode = ?"
122 ); # prepare once, outside while loop
124 while ( my $branch = $sth->fetchrow_hashref ) {
125 $nsth->execute( $branch->{'branchcode'} );
126 while ( my ($cat) = $nsth->fetchrow_array ) {
127 # FIXME - This seems wrong. It ought to be
128 # $branch->{categorycodes}{$cat} = 1;
129 # otherwise, there's a namespace collision if there's a
130 # category with the same name as a field in the 'branches'
131 # table (i.e., don't create a category called "issuing").
132 # In addition, the current structure doesn't really allow
133 # you to list the categories that a branch belongs to:
134 # you'd have to list keys %$branch, and remove those keys
135 # that aren't fields in the "branches" table.
136 # $branch->{$cat} = 1;
137 $branch->{category
}{$cat} = 1;
139 $branches{ $branch->{'branchcode'} } = $branch;
141 return ( \
%branches );
146 C4
::Context
->preference('IndependantBranches') &&
147 C4
::Context
->userenv &&
148 C4
::Context
->userenv->{flags
} %2 != 1 &&
149 C4
::Context
->userenv->{branch
} ;
152 # always returns a string for OK comparison via "eq" or "ne"
154 C4
::Context
->userenv or return '';
155 return C4
::Context
->userenv->{branch
} || '';
158 sub GetBranchesLoop
{ # since this is what most pages want anyway
159 my $branch = @_ ?
shift : mybranch
(); # optional first argument is branchcode of "my branch", if preselection is wanted.
160 my $onlymine = @_ ?
shift : onlymine
();
161 my $branches = GetBranches
($onlymine);
163 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname
}) cmp uc($branches->{$b}->{branchname
}) } keys %$branches ) {
165 value
=> $branchcode,
166 branchcode
=> $branchcode,
167 selected
=> ($branchcode eq $branch) ?
1 : 0,
168 branchname
=> $branches->{$branchcode}->{branchname
},
179 my ($branchcode) = @_;
180 my $dbh = C4
::Context
->dbh;
182 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
183 $sth->execute($branchcode);
184 my $branchname = $sth->fetchrow_array;
186 return ($branchname);
191 $error = &ModBranch($newvalue);
193 This function modify an existing branch
195 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
202 my $dbh = C4
::Context
->dbh;
206 (branchcode,branchname,branchaddress1,
207 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
208 branchcountry,branchphone,branchfax,branchemail,
209 branchurl,branchip,branchprinter,branchnotes,opac_info)
210 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
212 my $sth = $dbh->prepare($query);
214 $data->{'branchcode'}, $data->{'branchname'},
215 $data->{'branchaddress1'}, $data->{'branchaddress2'},
216 $data->{'branchaddress3'}, $data->{'branchzip'},
217 $data->{'branchcity'}, $data->{'branchstate'},
218 $data->{'branchcountry'},
219 $data->{'branchphone'}, $data->{'branchfax'},
220 $data->{'branchemail'}, $data->{'branchurl'},
221 $data->{'branchip'}, $data->{'branchprinter'},
222 $data->{'branchnotes'}, $data->{opac_info
},
224 return 1 if $dbh->err;
228 SET branchname=?,branchaddress1=?,
229 branchaddress2=?,branchaddress3=?,branchzip=?,
230 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
231 branchfax=?,branchemail=?,branchurl=?,branchip=?,
232 branchprinter=?,branchnotes=?,opac_info=?
235 my $sth = $dbh->prepare($query);
237 $data->{'branchname'},
238 $data->{'branchaddress1'}, $data->{'branchaddress2'},
239 $data->{'branchaddress3'}, $data->{'branchzip'},
240 $data->{'branchcity'}, $data->{'branchstate'},
241 $data->{'branchcountry'},
242 $data->{'branchphone'}, $data->{'branchfax'},
243 $data->{'branchemail'}, $data->{'branchurl'},
244 $data->{'branchip'}, $data->{'branchprinter'},
245 $data->{'branchnotes'}, $data->{opac_info
},
246 $data->{'branchcode'},
249 # sort out the categories....
251 my $cats = GetBranchCategory
();
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 );
283 foreach my $cat (@removecats) {
286 "delete from branchrelations where branchcode=? and categorycode=?"
288 $sth->execute( $branchcode, $cat );
293 =head2 GetBranchCategory
295 $results = GetBranchCategory($categorycode);
297 C<$results> is an ref to an array.
301 sub GetBranchCategory
{
303 # returns a reference to an array of hashes containing branches,
305 my $dbh = C4
::Context
->dbh;
308 # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
312 "select * from branchcategories where categorycode = ?");
313 $sth->execute($catcode);
316 $sth = $dbh->prepare("Select * from branchcategories");
320 while ( my $data = $sth->fetchrow_hashref ) {
321 push( @results, $data );
325 # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
329 =head2 GetBranchCategories
331 my $categories = GetBranchCategories($branchcode,$categorytype,$show_in_pulldown,$selected_in_pulldown);
333 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
334 i.e. categorycode, categorydescription, categorytype, categoryname.
335 if $branchcode and/or $categorytype are passed, limit set to categories that
336 $branchcode is a member of , and to $categorytype.
340 sub GetBranchCategories
{
341 my ( $branchcode, $categorytype, $show_in_pulldown, $selected_in_pulldown ) = @_;
342 my $dbh = C4
::Context
->dbh();
344 my $query = "SELECT c.* FROM branchcategories c";
345 my ( @where, @bind );
348 $query .= ",branchrelations r, branches b ";
349 push @where, "c.categorycode = r.categorycode AND r.branchcode = ? ";
350 push @bind , $branchcode;
353 if ( $categorytype ) {
354 push @where, " c.categorytype = ? ";
355 push @bind, $categorytype;
358 if ( defined( $show_in_pulldown ) ) {
359 push( @where, " c.show_in_pulldown = ? " );
360 push( @bind, $show_in_pulldown );
363 $query .= " WHERE " . join(" AND ", @where) if(@where);
364 $query .= " ORDER BY categorytype,c.categorycode";
365 my $sth=$dbh->prepare( $query);
366 $sth->execute(@bind);
368 my $branchcats = $sth->fetchall_arrayref({});
371 if ( $selected_in_pulldown ) {
372 foreach my $bc ( @
$branchcats ) {
373 $bc->{'selected'} = 1 if ( $bc->{'categorycode'} eq $selected_in_pulldown );
377 return( $branchcats );
380 =head2 GetCategoryTypes
382 $categorytypes = GetCategoryTypes;
383 returns a list of category types.
384 Currently these types are HARDCODED.
385 type: 'searchdomain' defines a group of agencies that the calling library may search in.
386 Other usage of agency categories falls under type: 'properties'.
387 to allow for other uses of categories.
388 The searchdomain bit may be better implemented as a separate module, but
389 the categories were already here, and minimally used.
392 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
393 sub GetCategoryTypes
{
394 return ( 'searchdomain','properties');
399 $branch = GetBranch( $query, $branches );
404 my ( $query, $branches ) = @_; # get branch for this query from branches
405 my $branch = $query->param('branch');
406 my %cookie = $query->cookie('userenv');
407 ($branch) || ($branch = $cookie{'branchname'});
408 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
412 =head2 GetBranchDetail
414 $branch = &GetBranchDetail($branchcode);
416 Given the branch code, the function returns a
417 hashref for the corresponding row in the branches table.
421 sub GetBranchDetail
{
422 my ($branchcode) = shift or return;
423 my $sth = C4
::Context
->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
424 $sth->execute($branchcode);
425 return $sth->fetchrow_hashref();
428 =head2 GetBranchesInCategory
430 my $branches = GetBranchesInCategory($categorycode);
432 Returns a href: keys %$branches eq (branchcode,branchname) .
436 sub GetBranchesInCategory
{
437 my ($categorycode) = @_;
439 my $dbh = C4
::Context
->dbh();
440 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
441 where r.branchcode=b.branchcode and r.categorycode=?");
442 $sth->execute($categorycode);
443 while (my $branch = $sth->fetchrow) {
444 push @branches, $branch;
447 return( \
@branches );
452 $results = GetBranchInfo($branchcode);
454 returns C<$results>, a reference to an array of hashes containing branches.
455 if $branchcode, just this branch, with associated categories.
456 if ! $branchcode && $categorytype, all branches in the category.
460 my ($branchcode,$categorytype) = @_;
461 my $dbh = C4
::Context
->dbh;
468 "Select * from branches where branchcode = ? order by branchcode");
469 $sth->execute($branchcode);
472 $sth = $dbh->prepare("Select * from branches order by branchcode");
476 while ( my $data = $sth->fetchrow_hashref ) {
477 my @bind = ($data->{'branchcode'});
478 my $query= "select r.categorycode from branchrelations r";
479 $query .= ", branchcategories c " if($categorytype);
480 $query .= " where branchcode=? ";
482 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
483 push @bind, $categorytype;
485 my $nsth=$dbh->prepare($query);
486 $nsth->execute( @bind );
488 while ( my ($cat) = $nsth->fetchrow_array ) {
492 $data->{'categories'} = \
@cats;
493 push( @results, $data );
501 &DelBranch($branchcode);
506 my ($branchcode) = @_;
507 my $dbh = C4
::Context
->dbh;
508 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
509 $sth->execute($branchcode);
513 =head2 ModBranchCategoryInfo
515 &ModBranchCategoryInfo($data);
516 sets the data from the editbranch form, and writes to the database...
520 sub ModBranchCategoryInfo
{
522 my $dbh = C4
::Context
->dbh;
524 # we are doing an insert
525 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype,show_in_pulldown) VALUES (?,?,?,?,?)");
526 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'} );
531 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=?,show_in_pulldown=? WHERE categorycode=?");
532 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'},uc( $data->{'categorycode'} ) );
537 =head2 CheckCategoryUnique
539 if (CheckCategoryUnique($categorycode)){
545 sub CheckCategoryUnique
{
546 my $categorycode = shift;
547 my $dbh = C4
::Context
->dbh;
548 my $sth = $dbh->prepare("SELECT categorycode FROM branchcategories WHERE categorycode = ?");
549 $sth->execute(uc( $categorycode) );
550 if (my $data = $sth->fetchrow_hashref){
559 =head2 DeleteBranchCategory
561 DeleteBranchCategory($categorycode);
565 sub DelBranchCategory
{
566 my ($categorycode) = @_;
567 my $dbh = C4
::Context
->dbh;
568 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
569 $sth->execute($categorycode);
573 =head2 CheckBranchCategorycode
575 $number_rows_affected = CheckBranchCategorycode($categorycode);
579 sub CheckBranchCategorycode
{
581 # check to see if the branchcode is being used in the database somewhere....
582 my ($categorycode) = @_;
583 my $dbh = C4
::Context
->dbh;
586 "select count(*) from branchrelations where categorycode=?");
587 $sth->execute($categorycode);
588 my ($total) = $sth->fetchrow_array;
592 sub get_branch_code_from_name
{
593 my @branch_name = @_;
594 my $query = "SELECT branchcode FROM branches WHERE branchname=?;";
595 my $dbh = C4
::Context
->dbh();
596 my $sth = $dbh->prepare($query);
597 $sth->execute(@branch_name);
598 return $sth->fetchrow_array;
601 sub GetBranchesCount
{
602 my $dbh = C4
::Context
->dbh();
603 my $query = "SELECT COUNT(*) AS branches_count FROM branches";
604 my $sth = $dbh->prepare( $query );
606 my $row = $sth->fetchrow_hashref();
607 return $row->{'branches_count'};
615 Koha Development Team <http://koha-community.org/>