Bug 15411: More changes
[koha.git] / C4 / Branch.pm
blobbdf4b4b99cdc22b87793b36b2301af8120f9b949
1 package C4::Branch;
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>.
19 use strict;
20 #use warnings; FIXME - Bug 2505
21 require Exporter;
22 use C4::Context;
24 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26 BEGIN {
27 # set the version for version checking
28 $VERSION = 3.07.00.049;
29 @ISA = qw(Exporter);
30 @EXPORT = qw(
31 &GetBranchCategory
32 &GetBranchName
33 &GetBranch
34 &GetBranches
35 &GetBranchesLoop
36 &GetBranchDetail
37 &ModBranch
38 &CheckBranchCategorycode
39 &GetBranchInfo
40 &GetCategoryTypes
41 &GetBranchCategories
42 &GetBranchesInCategory
43 &ModBranchCategoryInfo
44 &DelBranch
45 &DelBranchCategory
46 &CheckCategoryUnique
47 &mybranch
48 &GetBranchesCount
50 @EXPORT_OK = qw( &onlymine &mybranch );
53 =head1 NAME
55 C4::Branch - Koha branch module
57 =head1 SYNOPSIS
59 use C4::Branch;
61 =head1 DESCRIPTION
63 The functions in this module deal with branches.
65 =head1 FUNCTIONS
67 =head2 GetBranches
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.
76 =head3 in PERL SCRIPT
78 my $branches = GetBranches;
79 my @branchloop;
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;
89 =head3 in TEMPLATE
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>
96 [% ELSE %]
97 <option value="[% branchloo.value %]" >[% branchloo.branchname %]</option>
98 [% END %]
99 [% END %]
100 </select>
102 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
104 =cut
106 sub GetBranches {
107 my ($onlymine) = @_;
109 # returns a reference to a hash of references to ALL branches...
110 my %branches;
111 my $dbh = C4::Context->dbh;
112 my $sth;
113 my $query = "SELECT * FROM branches";
114 my @bind_parameters;
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);
123 my $relations_sth =
124 $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations");
125 $relations_sth->execute();
126 my %relations;
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 );
140 sub onlymine {
141 return
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"
149 sub mybranch {
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);
158 my @loop;
159 foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
160 push @loop, {
161 value => $branchcode,
162 branchcode => $branchcode,
163 selected => ($branchcode eq $branch) ? 1 : 0,
164 branchname => $branches->{$branchcode}->{branchname},
167 return \@loop;
170 =head2 GetBranchName
172 =cut
174 sub GetBranchName {
175 my ($branchcode) = @_;
176 my $dbh = C4::Context->dbh;
177 my $sth;
178 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
179 $sth->execute($branchcode);
180 my $branchname = $sth->fetchrow_array;
181 return ($branchname);
184 =head2 ModBranch
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.
192 =cut
194 sub ModBranch {
195 my ($data) = @_;
197 my $dbh = C4::Context->dbh;
198 if ($data->{add}) {
199 my $query = "
200 INSERT INTO branches
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);
209 $sth->execute(
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;
222 } else {
223 my $query = "
224 UPDATE branches
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=?
231 WHERE branchcode=?
233 my $sth = $dbh->prepare($query);
234 $sth->execute(
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....
249 my @checkedcats;
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'};
261 my @addcats;
262 my @removecats;
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) {
275 my $sth =
276 $dbh->prepare(
277 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
279 $sth->execute( $branchcode, $cat );
281 foreach my $cat (@removecats) {
282 my $sth =
283 $dbh->prepare(
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
296 =cut
298 sub GetBranchCategory {
299 my ($catcode) = @_;
300 return unless $catcode;
302 my $dbh = C4::Context->dbh;
303 my $sth;
305 $sth = $dbh->prepare(q{
306 SELECT *
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.
321 =cut
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;
353 return $branchcats;
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.
366 =cut
368 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
369 sub GetCategoryTypes {
370 return ( 'searchdomain','properties');
373 =head2 GetBranch
375 $branch = GetBranch( $query, $branches );
377 =cut
379 sub GetBranch {
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] );
385 return $branch;
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.
395 =cut
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) .
410 =cut
412 sub GetBranchesInCategory {
413 my ($categorycode) = @_;
414 my @branches;
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 );
425 =head2 GetBranchInfo
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.
432 =cut
434 sub GetBranchInfo {
435 my ($branchcode,$categorytype) = @_;
436 my $dbh = C4::Context->dbh;
437 my $sth;
440 if ($branchcode) {
441 $sth =
442 $dbh->prepare(
443 "Select * from branches where branchcode = ? order by branchcode");
444 $sth->execute($branchcode);
446 else {
447 $sth = $dbh->prepare("Select * from branches order by branchcode");
448 $sth->execute();
450 my @results;
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=? ";
456 if($categorytype) {
457 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
458 push @bind, $categorytype;
460 my $nsth=$dbh->prepare($query);
461 $nsth->execute( @bind );
462 my @cats = ();
463 while ( my ($cat) = $nsth->fetchrow_array ) {
464 push( @cats, $cat );
466 $data->{'categories'} = \@cats;
467 push( @results, $data );
469 return \@results;
472 =head2 DelBranch
474 &DelBranch($branchcode);
476 =cut
478 sub DelBranch {
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...
490 =cut
492 sub ModBranchCategoryInfo {
493 my ($data) = @_;
494 my $dbh = C4::Context->dbh;
495 if ($data->{'add'}){
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'} );
500 else {
501 # modifying
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)){
510 # do something
513 =cut
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){
521 return 0;
523 else {
524 return 1;
529 =head2 DeleteBranchCategory
531 DeleteBranchCategory($categorycode);
533 =cut
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);
546 =cut
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;
553 my $sth =
554 $dbh->prepare(
555 "select count(*) from branchrelations where categorycode=?");
556 $sth->execute($categorycode);
557 my ($total) = $sth->fetchrow_array;
558 return $total;
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 );
565 $sth->execute();
566 my $row = $sth->fetchrow_hashref();
567 return $row->{'branches_count'};
571 __END__
573 =head1 AUTHOR
575 Koha Development Team <http://koha-community.org/>
577 =cut