3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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;
38 &CheckBranchCategorycode
42 &GetBranchesInCategory
43 &ModBranchCategoryInfo
50 @EXPORT_OK = qw( &onlymine &mybranch );
55 C4::Branch - Koha branch module
63 The functions in this module deal with branches.
69 $branches = &GetBranches();
71 Returns informations about ALL branches, IndependentBranches Insensitive.
72 GetBranchInfo() returns the same information.
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" id="branch">
92 <option value=""></option>
93 [% FOREACH branchloo IN branchloop %]
94 [% IF ( branchloo.selected ) %]
95 <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>
97 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
102 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
109 # returns a reference to a hash of references to ALL branches...
111 my $dbh = C4
::Context
->dbh;
113 my $query = "SELECT * FROM branches";
115 if ( $onlymine && C4
::Context
->userenv && C4
::Context
->userenv->{branch
} ) {
116 $query .= ' WHERE branchcode = ? ';
117 push @bind_parameters, C4
::Context
->userenv->{branch
};
119 $query .= " ORDER BY branchname";
120 $sth = $dbh->prepare($query);
121 $sth->execute(@bind_parameters);
124 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
125 $relations_sth->execute();
127 while ( my $rel = $relations_sth->fetchrow_hashref ) {
128 push @
{ $relations{ $rel->{branchcode
} } }, $rel->{categorycode
};
131 while ( my $branch = $sth->fetchrow_hashref ) {
132 foreach my $cat ( @
{ $relations{ $branch->{branchcode
} } } ) {
133 $branch->{category
}{$cat} = 1;
135 $branches{ $branch->{'branchcode'} } = $branch;
137 return ( \
%branches );
142 C4
::Context
->preference('IndependentBranches')
143 && C4
::Context
->userenv
144 && !C4
::Context
->IsSuperLibrarian()
145 && C4
::Context
->userenv->{branch
};
148 # always returns a string for OK comparison via "eq" or "ne"
150 C4
::Context
->userenv or return '';
151 return C4
::Context
->userenv->{branch
} || '';
154 sub GetBranchesLoop
{ # since this is what most pages want anyway
155 my $branch = @_ ?
shift : mybranch
(); # optional first argument is branchcode of "my branch", if preselection is wanted.
156 my $onlymine = @_ ?
shift : onlymine
();
157 my $branches = GetBranches
($onlymine);
159 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname
}) cmp uc($branches->{$b}->{branchname
}) } keys %$branches ) {
161 value
=> $branchcode,
162 branchcode
=> $branchcode,
163 selected
=> ($branchcode eq $branch) ?
1 : 0,
164 branchname
=> $branches->{$branchcode}->{branchname
},
175 my ($branchcode) = @_;
176 my $dbh = C4
::Context
->dbh;
178 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
179 $sth->execute($branchcode);
180 my $branchname = $sth->fetchrow_array;
181 return ($branchname);
186 $error = &ModBranch($newvalue);
188 This function modify an existing branch
190 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
197 my $dbh = C4
::Context
->dbh;
201 (branchcode,branchname,branchaddress1,
202 branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
203 branchcountry,branchphone,branchfax,branchemail,
204 branchurl,branchip,branchprinter,branchnotes,opac_info,
205 branchreplyto, branchreturnpath)
206 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
208 my $sth = $dbh->prepare($query);
210 $data->{'branchcode'}, $data->{'branchname'},
211 $data->{'branchaddress1'}, $data->{'branchaddress2'},
212 $data->{'branchaddress3'}, $data->{'branchzip'},
213 $data->{'branchcity'}, $data->{'branchstate'},
214 $data->{'branchcountry'},
215 $data->{'branchphone'}, $data->{'branchfax'},
216 $data->{'branchemail'}, $data->{'branchurl'},
217 $data->{'branchip'}, $data->{'branchprinter'},
218 $data->{'branchnotes'}, $data->{opac_info
},
219 $data->{'branchreplyto'}, $data->{'branchreturnpath'}
221 return 1 if $dbh->err;
225 SET branchname=?,branchaddress1=?,
226 branchaddress2=?,branchaddress3=?,branchzip=?,
227 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
228 branchfax=?,branchemail=?,branchurl=?,branchip=?,
229 branchprinter=?,branchnotes=?,opac_info=?,
230 branchreplyto=?, branchreturnpath=?
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->{'branchreplyto'}, $data->{'branchreturnpath'},
245 $data->{'branchcode'},
248 # sort out the categories....
250 my $cats = GetBranchCategories
();
251 foreach my $cat (@
$cats) {
252 my $code = $cat->{'categorycode'};
253 if ( $data->{$code} ) {
254 push( @checkedcats, $code );
257 my $branchcode = uc( $data->{'branchcode'} );
258 my $branch = GetBranchInfo
($branchcode);
259 $branch = $branch->[0];
260 my $branchcats = $branch->{'categories'};
263 foreach my $bcat (@
$branchcats) {
265 unless ( grep { /^$bcat$/ } @checkedcats ) {
266 push( @removecats, $bcat );
269 foreach my $ccat (@checkedcats) {
270 unless ( grep { /^$ccat$/ } @
$branchcats ) {
271 push( @addcats, $ccat );
274 foreach my $cat (@addcats) {
277 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
279 $sth->execute( $branchcode, $cat );
281 foreach my $cat (@removecats) {
284 "delete from branchrelations where branchcode=? and categorycode=?"
286 $sth->execute( $branchcode, $cat );
290 =head2 GetBranchCategory
292 $results = GetBranchCategory($categorycode);
294 C<$results> is an hashref
298 sub GetBranchCategory
{
300 return unless $catcode;
302 my $dbh = C4
::Context
->dbh;
305 $sth = $dbh->prepare(q{
307 FROM branchcategories
308 WHERE categorycode = ?
310 $sth->execute( $catcode );
311 return $sth->fetchrow_hashref;
314 =head2 GetBranchCategories
316 my $categories = GetBranchCategories($categorytype,$show_in_pulldown,$selected_in_pulldown);
318 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
319 i.e. categorydescription, categorytype, categoryname.
323 sub GetBranchCategories
{
324 my ( $categorytype, $show_in_pulldown, $selected_in_pulldown ) = @_;
325 my $dbh = C4
::Context
->dbh();
327 my $query = "SELECT * FROM branchcategories ";
329 my ( @where, @bind );
330 if ( $categorytype ) {
331 push @where, " categorytype = ? ";
332 push @bind, $categorytype;
335 if ( defined( $show_in_pulldown ) ) {
336 push( @where, " show_in_pulldown = ? " );
337 push( @bind, $show_in_pulldown );
340 $query .= " WHERE " . join(" AND ", @where) if(@where);
341 $query .= " ORDER BY categorytype, categorycode";
342 my $sth=$dbh->prepare( $query);
343 $sth->execute(@bind);
345 my $branchcats = $sth->fetchall_arrayref({});
347 if ( $selected_in_pulldown ) {
348 foreach my $bc ( @
$branchcats ) {
349 $bc->{selected
} = 1 if $bc->{categorycode
} eq $selected_in_pulldown;
356 =head2 GetCategoryTypes
358 $categorytypes = GetCategoryTypes;
359 returns a list of category types.
360 Currently these types are HARDCODED.
361 type: 'searchdomain' defines a group of agencies that the calling library may search in.
362 Other usage of agency categories falls under type: 'properties'.
363 to allow for other uses of categories.
364 The searchdomain bit may be better implemented as a separate module, but
365 the categories were already here, and minimally used.
368 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
369 sub GetCategoryTypes
{
370 return ( 'searchdomain','properties');
375 $branch = GetBranch( $query, $branches );
380 my ( $query, $branches ) = @_; # get branch for this query from branches
381 my $branch = $query->param('branch');
382 my %cookie = $query->cookie('userenv');
383 ($branch) || ($branch = $cookie{'branchname'});
384 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
388 =head2 GetBranchDetail
390 $branch = &GetBranchDetail($branchcode);
392 Given the branch code, the function returns a
393 hashref for the corresponding row in the branches table.
397 sub GetBranchDetail
{
398 my ($branchcode) = shift or return;
399 my $sth = C4
::Context
->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
400 $sth->execute($branchcode);
401 return $sth->fetchrow_hashref();
404 =head2 GetBranchesInCategory
406 my $branches = GetBranchesInCategory($categorycode);
408 Returns a href: keys %$branches eq (branchcode,branchname) .
412 sub GetBranchesInCategory
{
413 my ($categorycode) = @_;
415 my $dbh = C4
::Context
->dbh();
416 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
417 where r.branchcode=b.branchcode and r.categorycode=?");
418 $sth->execute($categorycode);
419 while (my $branch = $sth->fetchrow) {
420 push @branches, $branch;
422 return( \
@branches );
427 $results = GetBranchInfo($branchcode);
429 returns C<$results>, a reference to an array of hashes containing branches.
430 if $branchcode, just this branch, with associated categories.
431 if ! $branchcode && $categorytype, all branches in the category.
435 my ($branchcode,$categorytype) = @_;
436 my $dbh = C4
::Context
->dbh;
443 "Select * from branches where branchcode = ? order by branchcode");
444 $sth->execute($branchcode);
447 $sth = $dbh->prepare("Select * from branches order by branchcode");
451 while ( my $data = $sth->fetchrow_hashref ) {
452 my @bind = ($data->{'branchcode'});
453 my $query= "select r.categorycode from branchrelations r";
454 $query .= ", branchcategories c " if($categorytype);
455 $query .= " where branchcode=? ";
457 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
458 push @bind, $categorytype;
460 my $nsth=$dbh->prepare($query);
461 $nsth->execute( @bind );
463 while ( my ($cat) = $nsth->fetchrow_array ) {
466 $data->{'categories'} = \
@cats;
467 push( @results, $data );
474 &DelBranch($branchcode);
479 my ($branchcode) = @_;
480 my $dbh = C4
::Context
->dbh;
481 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
482 $sth->execute($branchcode);
485 =head2 ModBranchCategoryInfo
487 &ModBranchCategoryInfo($data);
488 sets the data from the editbranch form, and writes to the database...
492 sub ModBranchCategoryInfo
{
494 my $dbh = C4
::Context
->dbh;
496 # we are doing an insert
497 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype,show_in_pulldown) VALUES (?,?,?,?,?)");
498 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'} );
502 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=?,show_in_pulldown=? WHERE categorycode=?");
503 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'},uc( $data->{'categorycode'} ) );
507 =head2 CheckCategoryUnique
509 if (CheckCategoryUnique($categorycode)){
515 sub CheckCategoryUnique
{
516 my $categorycode = shift;
517 my $dbh = C4
::Context
->dbh;
518 my $sth = $dbh->prepare("SELECT categorycode FROM branchcategories WHERE categorycode = ?");
519 $sth->execute(uc( $categorycode) );
520 if (my $data = $sth->fetchrow_hashref){
529 =head2 DeleteBranchCategory
531 DeleteBranchCategory($categorycode);
535 sub DelBranchCategory
{
536 my ($categorycode) = @_;
537 my $dbh = C4
::Context
->dbh;
538 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
539 $sth->execute($categorycode);
542 =head2 CheckBranchCategorycode
544 $number_rows_affected = CheckBranchCategorycode($categorycode);
548 sub CheckBranchCategorycode
{
550 # check to see if the branchcode is being used in the database somewhere....
551 my ($categorycode) = @_;
552 my $dbh = C4
::Context
->dbh;
555 "select count(*) from branchrelations where categorycode=?");
556 $sth->execute($categorycode);
557 my ($total) = $sth->fetchrow_array;
561 sub GetBranchesCount
{
562 my $dbh = C4
::Context
->dbh();
563 my $query = "SELECT COUNT(*) AS branches_count FROM branches";
564 my $sth = $dbh->prepare( $query );
566 my $row = $sth->fetchrow_hashref();
567 return $row->{'branches_count'};
575 Koha Development Team <http://koha-community.org/>