Reimplement fix for bug 5557 - Link to logs for user with view_system_logs permission
[koha.git] / C4 / Branch.pm
blobdb7a87f2ffa88f5395145617277ca65cfe2f0b47
1 package C4::Branch;
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
8 # version.
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
19 use strict;
20 #use warnings; FIXME - Bug 2505
21 require Exporter;
22 use C4::Context;
23 use C4::Koha;
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
27 BEGIN {
28 # set the version for version checking
29 $VERSION = 3.02;
30 @ISA = qw(Exporter);
31 @EXPORT = qw(
32 &GetBranchCategory
33 &GetBranchName
34 &GetBranch
35 &GetBranches
36 &GetBranchesLoop
37 &GetBranchDetail
38 &get_branchinfos_of
39 &ModBranch
40 &CheckBranchCategorycode
41 &GetBranchInfo
42 &GetCategoryTypes
43 &GetBranchCategories
44 &GetBranchesInCategory
45 &ModBranchCategoryInfo
46 &DelBranch
47 &DelBranchCategory
48 &CheckCategoryUnique
49 &mybranch
51 @EXPORT_OK = qw( &onlymine &mybranch get_branch_code_from_name );
54 =head1 NAME
56 C4::Branch - Koha branch module
58 =head1 SYNOPSIS
60 use C4::Branch;
62 =head1 DESCRIPTION
64 The functions in this module deal with branches.
66 =head1 FUNCTIONS
68 =head2 GetBranches
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.
78 =head3 in PERL SCRIPT
80 my $branches = GetBranches;
81 my @branchloop;
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;
91 =head3 in TEMPLATE
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>
97 <!-- /TMPL_LOOP -->
98 </select>
100 =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
102 =cut
104 sub GetBranches {
105 my ($onlymine)=@_;
106 # returns a reference to a hash of references to ALL branches...
107 my %branches;
108 my $dbh = C4::Context->dbh;
109 my $sth;
110 my $query="SELECT * FROM branches";
111 my @bind_parameters;
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 );
144 sub onlymine {
145 return
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"
153 sub mybranch {
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);
162 my @loop;
163 foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
164 push @loop, {
165 value => $_,
166 selected => ($_ eq $branch) ? 1 : 0,
167 branchname => $branches->{$_}->{branchname},
170 return \@loop;
173 =head2 GetBranchName
175 =cut
177 sub GetBranchName {
178 my ($branchcode) = @_;
179 my $dbh = C4::Context->dbh;
180 my $sth;
181 $sth = $dbh->prepare("Select branchname from branches where branchcode=?");
182 $sth->execute($branchcode);
183 my $branchname = $sth->fetchrow_array;
184 $sth->finish;
185 return ($branchname);
188 =head2 ModBranch
190 $error = &ModBranch($newvalue);
192 This function modify an existing branch
194 C<$newvalue> is a ref to an array wich is containt all the column from branches table.
196 =cut
198 sub ModBranch {
199 my ($data) = @_;
201 my $dbh = C4::Context->dbh;
202 if ($data->{add}) {
203 my $query = "
204 INSERT INTO branches
205 (branchcode,branchname,branchaddress1,
206 branchaddress2,branchaddress3,branchzip,branchcity,
207 branchcountry,branchphone,branchfax,branchemail,
208 branchurl,branchip,branchprinter,branchnotes)
209 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
211 my $sth = $dbh->prepare($query);
212 $sth->execute(
213 $data->{'branchcode'}, $data->{'branchname'},
214 $data->{'branchaddress1'}, $data->{'branchaddress2'},
215 $data->{'branchaddress3'}, $data->{'branchzip'},
216 $data->{'branchcity'}, $data->{'branchcountry'},
217 $data->{'branchphone'}, $data->{'branchfax'},
218 $data->{'branchemail'}, $data->{'branchurl'},
219 $data->{'branchip'}, $data->{'branchprinter'},
220 $data->{'branchnotes'},
222 return 1 if $dbh->err;
223 } else {
224 my $query = "
225 UPDATE branches
226 SET branchname=?,branchaddress1=?,
227 branchaddress2=?,branchaddress3=?,branchzip=?,
228 branchcity=?,branchcountry=?,branchphone=?,
229 branchfax=?,branchemail=?,branchurl=?,branchip=?,
230 branchprinter=?,branchnotes=?
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->{'branchcountry'},
239 $data->{'branchphone'}, $data->{'branchfax'},
240 $data->{'branchemail'}, $data->{'branchurl'},
241 $data->{'branchip'}, $data->{'branchprinter'},
242 $data->{'branchnotes'},
243 $data->{'branchcode'},
246 # sort out the categories....
247 my @checkedcats;
248 my $cats = GetBranchCategory();
249 foreach my $cat (@$cats) {
250 my $code = $cat->{'categorycode'};
251 if ( $data->{$code} ) {
252 push( @checkedcats, $code );
255 my $branchcode = uc( $data->{'branchcode'} );
256 my $branch = GetBranchInfo($branchcode);
257 $branch = $branch->[0];
258 my $branchcats = $branch->{'categories'};
259 my @addcats;
260 my @removecats;
261 foreach my $bcat (@$branchcats) {
263 unless ( grep { /^$bcat$/ } @checkedcats ) {
264 push( @removecats, $bcat );
267 foreach my $ccat (@checkedcats) {
268 unless ( grep { /^$ccat$/ } @$branchcats ) {
269 push( @addcats, $ccat );
272 foreach my $cat (@addcats) {
273 my $sth =
274 $dbh->prepare(
275 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
277 $sth->execute( $branchcode, $cat );
278 $sth->finish;
280 foreach my $cat (@removecats) {
281 my $sth =
282 $dbh->prepare(
283 "delete from branchrelations where branchcode=? and categorycode=?"
285 $sth->execute( $branchcode, $cat );
286 $sth->finish;
290 =head2 GetBranchCategory
292 $results = GetBranchCategory($categorycode);
294 C<$results> is an ref to an array.
296 =cut
298 sub GetBranchCategory {
300 # returns a reference to an array of hashes containing branches,
301 my ($catcode) = @_;
302 my $dbh = C4::Context->dbh;
303 my $sth;
305 # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n";
306 if ($catcode) {
307 $sth =
308 $dbh->prepare(
309 "select * from branchcategories where categorycode = ?");
310 $sth->execute($catcode);
312 else {
313 $sth = $dbh->prepare("Select * from branchcategories");
314 $sth->execute();
316 my @results;
317 while ( my $data = $sth->fetchrow_hashref ) {
318 push( @results, $data );
320 $sth->finish;
322 # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n";
323 return \@results;
326 =head2 GetBranchCategories
328 my $categories = GetBranchCategories($branchcode,$categorytype);
330 Returns a list ref of anon hashrefs with keys eq columns of branchcategories table,
331 i.e. categorycode, categorydescription, categorytype, categoryname.
332 if $branchcode and/or $categorytype are passed, limit set to categories that
333 $branchcode is a member of , and to $categorytype.
335 =cut
337 sub GetBranchCategories {
338 my ($branchcode,$categorytype) = @_;
339 my $dbh = C4::Context->dbh();
340 my $query = "SELECT c.* FROM branchcategories c";
341 my (@where, @bind);
342 if($branchcode) {
343 $query .= ",branchrelations r, branches b ";
344 push @where, "c.categorycode=r.categorycode and r.branchcode=? ";
345 push @bind , $branchcode;
347 if ($categorytype) {
348 push @where, " c.categorytype=? ";
349 push @bind, $categorytype;
351 $query .= " where " . join(" and ", @where) if(@where);
352 $query .= " order by categorytype,c.categorycode";
353 my $sth=$dbh->prepare( $query);
354 $sth->execute(@bind);
356 my $branchcats = $sth->fetchall_arrayref({});
357 $sth->finish();
358 return( $branchcats );
361 =head2 GetCategoryTypes
363 $categorytypes = GetCategoryTypes;
364 returns a list of category types.
365 Currently these types are HARDCODED.
366 type: 'searchdomain' defines a group of agencies that the calling library may search in.
367 Other usage of agency categories falls under type: 'properties'.
368 to allow for other uses of categories.
369 The searchdomain bit may be better implemented as a separate module, but
370 the categories were already here, and minimally used.
371 =cut
373 #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories.
374 sub GetCategoryTypes() {
375 return ( 'searchdomain','properties');
378 =head2 GetBranch
380 $branch = GetBranch( $query, $branches );
382 =cut
384 sub GetBranch ($$) {
385 my ( $query, $branches ) = @_; # get branch for this query from branches
386 my $branch = $query->param('branch');
387 my %cookie = $query->cookie('userenv');
388 ($branch) || ($branch = $cookie{'branchname'});
389 ( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] );
390 return $branch;
393 =head2 GetBranchDetail
395 $branch = &GetBranchDetail($branchcode);
397 Given the branch code, the function returns a
398 hashref for the corresponding row in the branches table.
400 =cut
402 sub GetBranchDetail {
403 my ($branchcode) = shift or return;
404 my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?");
405 $sth->execute($branchcode);
406 return $sth->fetchrow_hashref();
409 =head2 get_branchinfos_of
411 my $branchinfos_of = get_branchinfos_of(@branchcodes);
413 Associates a list of branchcodes to the information of the branch, taken in
414 branches table.
416 Returns a href where keys are branchcodes and values are href where keys are
417 branch information key.
419 print 'branchname is ', $branchinfos_of->{$code}->{branchname};
421 =cut
423 sub get_branchinfos_of {
424 my @branchcodes = @_;
426 my $query = '
427 SELECT branchcode,
428 branchname
429 FROM branches
430 WHERE branchcode IN ('
431 . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
433 return C4::Koha::get_infos_of( $query, 'branchcode' );
437 =head2 GetBranchesInCategory
439 my $branches = GetBranchesInCategory($categorycode);
441 Returns a href: keys %$branches eq (branchcode,branchname) .
443 =cut
445 sub GetBranchesInCategory($) {
446 my ($categorycode) = @_;
447 my @branches;
448 my $dbh = C4::Context->dbh();
449 my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b
450 where r.branchcode=b.branchcode and r.categorycode=?");
451 $sth->execute($categorycode);
452 while (my $branch = $sth->fetchrow) {
453 push @branches, $branch;
455 $sth->finish();
456 return( \@branches );
459 =head2 GetBranchInfo
461 $results = GetBranchInfo($branchcode);
463 returns C<$results>, a reference to an array of hashes containing branches.
464 if $branchcode, just this branch, with associated categories.
465 if ! $branchcode && $categorytype, all branches in the category.
466 =cut
468 sub GetBranchInfo {
469 my ($branchcode,$categorytype) = @_;
470 my $dbh = C4::Context->dbh;
471 my $sth;
474 if ($branchcode) {
475 $sth =
476 $dbh->prepare(
477 "Select * from branches where branchcode = ? order by branchcode");
478 $sth->execute($branchcode);
480 else {
481 $sth = $dbh->prepare("Select * from branches order by branchcode");
482 $sth->execute();
484 my @results;
485 while ( my $data = $sth->fetchrow_hashref ) {
486 my @bind = ($data->{'branchcode'});
487 my $query= "select r.categorycode from branchrelations r";
488 $query .= ", branchcategories c " if($categorytype);
489 $query .= " where branchcode=? ";
490 if($categorytype) {
491 $query .= " and c.categorytype=? and r.categorycode=c.categorycode";
492 push @bind, $categorytype;
494 my $nsth=$dbh->prepare($query);
495 $nsth->execute( @bind );
496 my @cats = ();
497 while ( my ($cat) = $nsth->fetchrow_array ) {
498 push( @cats, $cat );
500 $nsth->finish;
501 $data->{'categories'} = \@cats;
502 push( @results, $data );
504 $sth->finish;
505 return \@results;
508 =head2 DelBranch
510 &DelBranch($branchcode);
512 =cut
514 sub DelBranch {
515 my ($branchcode) = @_;
516 my $dbh = C4::Context->dbh;
517 my $sth = $dbh->prepare("delete from branches where branchcode = ?");
518 $sth->execute($branchcode);
519 $sth->finish;
522 =head2 ModBranchCategoryInfo
524 &ModBranchCategoryInfo($data);
525 sets the data from the editbranch form, and writes to the database...
527 =cut
529 sub ModBranchCategoryInfo {
530 my ($data) = @_;
531 my $dbh = C4::Context->dbh;
532 if ($data->{'add'}){
533 # we are doing an insert
534 my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)");
535 $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
536 $sth->finish();
538 else {
539 # modifying
540 my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?");
541 $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) );
542 $sth->finish();
546 =head2 CheckCategoryUnique
548 if (CheckCategoryUnique($categorycode)){
549 # do something
552 =cut
554 sub CheckCategoryUnique {
555 my $categorycode = shift;
556 my $dbh = C4::Context->dbh;
557 my $sth = $dbh->prepare("SELECT categorycode FROM branchcategories WHERE categorycode = ?");
558 $sth->execute(uc( $categorycode) );
559 if (my $data = $sth->fetchrow_hashref){
560 return 0;
562 else {
563 return 1;
568 =head2 DeleteBranchCategory
570 DeleteBranchCategory($categorycode);
572 =cut
574 sub DelBranchCategory {
575 my ($categorycode) = @_;
576 my $dbh = C4::Context->dbh;
577 my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
578 $sth->execute($categorycode);
579 $sth->finish;
582 =head2 CheckBranchCategorycode
584 $number_rows_affected = CheckBranchCategorycode($categorycode);
586 =cut
588 sub CheckBranchCategorycode {
590 # check to see if the branchcode is being used in the database somewhere....
591 my ($categorycode) = @_;
592 my $dbh = C4::Context->dbh;
593 my $sth =
594 $dbh->prepare(
595 "select count(*) from branchrelations where categorycode=?");
596 $sth->execute($categorycode);
597 my ($total) = $sth->fetchrow_array;
598 return $total;
601 sub get_branch_code_from_name {
602 my @branch_name = @_;
603 my $query = "SELECT branchcode FROM branches WHERE branchname=?;";
604 my $dbh = C4::Context->dbh();
605 my $sth = $dbh->prepare($query);
606 $sth->execute(@branch_name);
607 return $sth->fetchrow_array;
611 __END__
613 =head1 AUTHOR
615 Koha Development Team <http://koha-community.org/>
617 =cut