X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FBranch.pm;h=a78bef60fe6bd6d79e37ebe350a8936187fb945b;hb=0db45ce3b881cfd085b7222680b574079ba268a8;hp=d765c992388430879a17adabf3bc1a243c12d2a3;hpb=e5a24bbbdd0d0e9ff09bfaadfe4c4bb37312e7ec;p=koha.git diff --git a/C4/Branch.pm b/C4/Branch.pm index d765c99238..a78bef60fe 100644 --- a/C4/Branch.pm +++ b/C4/Branch.pm @@ -2,18 +2,18 @@ package C4::Branch; # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along with -# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, -# Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . use strict; @@ -48,7 +48,7 @@ BEGIN { &mybranch &GetBranchesCount ); - @EXPORT_OK = qw( &onlymine &mybranch get_branch_code_from_name ); + @EXPORT_OK = qw( &onlymine &mybranch ); } =head1 NAME @@ -69,9 +69,8 @@ The functions in this module deal with branches. $branches = &GetBranches(); -Returns informations about ALL branches, IndependantBranches Insensitive. -GetBranchInfo() returns the same information without the problems of this function -(namespace collision, mainly). +Returns informations about ALL branches, IndependentBranches Insensitive. +GetBranchInfo() returns the same information. Create a branch selector with the following code. @@ -90,11 +89,15 @@ Create a branch selector with the following code. =head3 in TEMPLATE - + + [% FOREACH branchloo IN branchloop %] + [% IF ( branchloo.selected ) %] + + [% ELSE %] + + [% END %] + [% END %] =head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above. @@ -102,38 +105,32 @@ Create a branch selector with the following code. =cut sub GetBranches { - my ($onlymine)=@_; + my ($onlymine) = @_; + # returns a reference to a hash of references to ALL branches... my %branches; my $dbh = C4::Context->dbh; my $sth; - my $query="SELECT * FROM branches"; + my $query = "SELECT * FROM branches"; my @bind_parameters; - if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){ - $query .= ' WHERE branchcode = ? '; - push @bind_parameters, C4::Context->userenv->{branch}; + if ( $onlymine && C4::Context->userenv && C4::Context->userenv->{branch} ) { + $query .= ' WHERE branchcode = ? '; + push @bind_parameters, C4::Context->userenv->{branch}; } - $query.=" ORDER BY branchname"; + $query .= " ORDER BY branchname"; $sth = $dbh->prepare($query); - $sth->execute( @bind_parameters ); - - my $nsth = $dbh->prepare( - "SELECT categorycode FROM branchrelations WHERE branchcode = ?" - ); # prepare once, outside while loop + $sth->execute(@bind_parameters); + + my $relations_sth = + $dbh->prepare("SELECT branchcode,categorycode FROM branchrelations"); + $relations_sth->execute(); + my %relations; + while ( my $rel = $relations_sth->fetchrow_hashref ) { + push @{ $relations{ $rel->{branchcode} } }, $rel->{categorycode}; + } while ( my $branch = $sth->fetchrow_hashref ) { - $nsth->execute( $branch->{'branchcode'} ); - while ( my ($cat) = $nsth->fetchrow_array ) { - # FIXME - This seems wrong. It ought to be - # $branch->{categorycodes}{$cat} = 1; - # otherwise, there's a namespace collision if there's a - # category with the same name as a field in the 'branches' - # table (i.e., don't create a category called "issuing"). - # In addition, the current structure doesn't really allow - # you to list the categories that a branch belongs to: - # you'd have to list keys %$branch, and remove those keys - # that aren't fields in the "branches" table. - # $branch->{$cat} = 1; + foreach my $cat ( @{ $relations{ $branch->{branchcode} } } ) { $branch->{category}{$cat} = 1; } $branches{ $branch->{'branchcode'} } = $branch; @@ -142,11 +139,11 @@ sub GetBranches { } sub onlymine { - return - C4::Context->preference('IndependantBranches') && - C4::Context->userenv && - C4::Context->userenv->{flags} %2 != 1 && - C4::Context->userenv->{branch} ; + return + C4::Context->preference('IndependentBranches') + && C4::Context->userenv + && !C4::Context->IsSuperLibrarian() + && C4::Context->userenv->{branch}; } # always returns a string for OK comparison via "eq" or "ne" @@ -160,11 +157,12 @@ sub GetBranchesLoop { # since this is what most pages want anyway my $onlymine = @_ ? shift : onlymine(); my $branches = GetBranches($onlymine); my @loop; - foreach ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) { + foreach my $branchcode ( sort { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) { push @loop, { - value => $_, - selected => ($_ eq $branch) ? 1 : 0, - branchname => $branches->{$_}->{branchname}, + value => $branchcode, + branchcode => $branchcode, + selected => ($branchcode eq $branch) ? 1 : 0, + branchname => $branches->{$branchcode}->{branchname}, }; } return \@loop; @@ -181,7 +179,6 @@ sub GetBranchName { $sth = $dbh->prepare("Select branchname from branches where branchcode=?"); $sth->execute($branchcode); my $branchname = $sth->fetchrow_array; - $sth->finish; return ($branchname); } @@ -205,8 +202,9 @@ sub ModBranch { (branchcode,branchname,branchaddress1, branchaddress2,branchaddress3,branchzip,branchcity,branchstate, branchcountry,branchphone,branchfax,branchemail, - branchurl,branchip,branchprinter,branchnotes,opac_info) - VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) + branchurl,branchip,branchprinter,branchnotes,opac_info, + branchreplyto, branchreturnpath) + VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) "; my $sth = $dbh->prepare($query); $sth->execute( @@ -219,6 +217,7 @@ sub ModBranch { $data->{'branchemail'}, $data->{'branchurl'}, $data->{'branchip'}, $data->{'branchprinter'}, $data->{'branchnotes'}, $data->{opac_info}, + $data->{'branchreplyto'}, $data->{'branchreturnpath'} ); return 1 if $dbh->err; } else { @@ -228,7 +227,8 @@ sub ModBranch { branchaddress2=?,branchaddress3=?,branchzip=?, branchcity=?,branchstate=?,branchcountry=?,branchphone=?, branchfax=?,branchemail=?,branchurl=?,branchip=?, - branchprinter=?,branchnotes=?,opac_info=? + branchprinter=?,branchnotes=?,opac_info=?, + branchreplyto=?, branchreturnpath=? WHERE branchcode=? "; my $sth = $dbh->prepare($query); @@ -242,12 +242,13 @@ sub ModBranch { $data->{'branchemail'}, $data->{'branchurl'}, $data->{'branchip'}, $data->{'branchprinter'}, $data->{'branchnotes'}, $data->{opac_info}, + $data->{'branchreplyto'}, $data->{'branchreturnpath'}, $data->{'branchcode'}, ); } # sort out the categories.... my @checkedcats; - my $cats = GetBranchCategory(); + my $cats = GetBranchCategories(); foreach my $cat (@$cats) { my $code = $cat->{'categorycode'}; if ( $data->{$code} ) { @@ -277,7 +278,6 @@ sub ModBranch { "insert into branchrelations (branchcode, categorycode) values(?, ?)" ); $sth->execute( $branchcode, $cat ); - $sth->finish; } foreach my $cat (@removecats) { my $sth = @@ -285,7 +285,6 @@ sub ModBranch { "delete from branchrelations where branchcode=? and categorycode=?" ); $sth->execute( $branchcode, $cat ); - $sth->finish; } } @@ -293,71 +292,66 @@ sub ModBranch { $results = GetBranchCategory($categorycode); -C<$results> is an ref to an array. +C<$results> is an hashref =cut sub GetBranchCategory { - - # returns a reference to an array of hashes containing branches, my ($catcode) = @_; + return unless $catcode; + my $dbh = C4::Context->dbh; my $sth; - # print DEBUG "GetBranchCategory: entry: catcode=".cvs($catcode)."\n"; - if ($catcode) { - $sth = - $dbh->prepare( - "select * from branchcategories where categorycode = ?"); - $sth->execute($catcode); - } - else { - $sth = $dbh->prepare("Select * from branchcategories"); - $sth->execute(); - } - my @results; - while ( my $data = $sth->fetchrow_hashref ) { - push( @results, $data ); - } - $sth->finish; - - # print DEBUG "GetBranchCategory: exit: returning ".cvs(\@results)."\n"; - return \@results; + $sth = $dbh->prepare(q{ + SELECT * + FROM branchcategories + WHERE categorycode = ? + }); + $sth->execute( $catcode ); + return $sth->fetchrow_hashref; } =head2 GetBranchCategories - my $categories = GetBranchCategories($branchcode,$categorytype); + my $categories = GetBranchCategories($categorytype,$show_in_pulldown,$selected_in_pulldown); Returns a list ref of anon hashrefs with keys eq columns of branchcategories table, -i.e. categorycode, categorydescription, categorytype, categoryname. -if $branchcode and/or $categorytype are passed, limit set to categories that -$branchcode is a member of , and to $categorytype. +i.e. categorydescription, categorytype, categoryname. =cut sub GetBranchCategories { - my ($branchcode,$categorytype) = @_; - my $dbh = C4::Context->dbh(); - my $query = "SELECT c.* FROM branchcategories c"; - my (@where, @bind); - if($branchcode) { - $query .= ",branchrelations r, branches b "; - push @where, "c.categorycode=r.categorycode and r.branchcode=? "; - push @bind , $branchcode; - } - if ($categorytype) { - push @where, " c.categorytype=? "; - push @bind, $categorytype; - } - $query .= " where " . join(" and ", @where) if(@where); - $query .= " order by categorytype,c.categorycode"; - my $sth=$dbh->prepare( $query); - $sth->execute(@bind); - - my $branchcats = $sth->fetchall_arrayref({}); - $sth->finish(); - return( $branchcats ); + my ( $categorytype, $show_in_pulldown, $selected_in_pulldown ) = @_; + my $dbh = C4::Context->dbh(); + + my $query = "SELECT * FROM branchcategories "; + + my ( @where, @bind ); + if ( $categorytype ) { + push @where, " categorytype = ? "; + push @bind, $categorytype; + } + + if ( defined( $show_in_pulldown ) ) { + push( @where, " show_in_pulldown = ? " ); + push( @bind, $show_in_pulldown ); + } + + $query .= " WHERE " . join(" AND ", @where) if(@where); + $query .= " ORDER BY categorytype, categorycode"; + my $sth=$dbh->prepare( $query); + $sth->execute(@bind); + + my $branchcats = $sth->fetchall_arrayref({}); + + if ( $selected_in_pulldown ) { + foreach my $bc ( @$branchcats ) { + $bc->{selected} = 1 if $bc->{categorycode} eq $selected_in_pulldown; + } + } + + return $branchcats; } =head2 GetCategoryTypes @@ -426,7 +420,6 @@ sub GetBranchesInCategory { while (my $branch = $sth->fetchrow) { push @branches, $branch; } - $sth->finish(); return( \@branches ); } @@ -471,11 +464,9 @@ sub GetBranchInfo { while ( my ($cat) = $nsth->fetchrow_array ) { push( @cats, $cat ); } - $nsth->finish; $data->{'categories'} = \@cats; push( @results, $data ); } - $sth->finish; return \@results; } @@ -490,7 +481,6 @@ sub DelBranch { my $dbh = C4::Context->dbh; my $sth = $dbh->prepare("delete from branches where branchcode = ?"); $sth->execute($branchcode); - $sth->finish; } =head2 ModBranchCategoryInfo @@ -505,15 +495,13 @@ sub ModBranchCategoryInfo { my $dbh = C4::Context->dbh; if ($data->{'add'}){ # we are doing an insert - my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype) VALUES (?,?,?,?)"); - $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} ); - $sth->finish(); + my $sth = $dbh->prepare("INSERT INTO branchcategories (categorycode,categoryname,codedescription,categorytype,show_in_pulldown) VALUES (?,?,?,?,?)"); + $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'} ); } else { # modifying - my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=? WHERE categorycode=?"); - $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},uc( $data->{'categorycode'} ) ); - $sth->finish(); + my $sth = $dbh->prepare("UPDATE branchcategories SET categoryname=?,codedescription=?,categorytype=?,show_in_pulldown=? WHERE categorycode=?"); + $sth->execute($data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'},$data->{'show_in_pulldown'},uc( $data->{'categorycode'} ) ); } } @@ -550,7 +538,6 @@ sub DelBranchCategory { my $dbh = C4::Context->dbh; my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?"); $sth->execute($categorycode); - $sth->finish; } =head2 CheckBranchCategorycode @@ -572,15 +559,6 @@ sub CheckBranchCategorycode { return $total; } -sub get_branch_code_from_name { - my @branch_name = @_; - my $query = "SELECT branchcode FROM branches WHERE branchname=?;"; - my $dbh = C4::Context->dbh(); - my $sth = $dbh->prepare($query); - $sth->execute(@branch_name); - return $sth->fetchrow_array; -} - sub GetBranchesCount { my $dbh = C4::Context->dbh(); my $query = "SELECT COUNT(*) AS branches_count FROM branches";