X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FBranch.pm;h=7dfd737245be623da417b25f4316f2a2728ea705;hb=cd14fd421c6908a73174d2cd0267779b9fd4e08f;hp=4a540be3f485e9764dcdd7f22c7b4e08bb2e7fb0;hpb=8af76042d82e2034061087e9b78df038e3b2a403;p=koha.git diff --git a/C4/Branch.pm b/C4/Branch.pm index 4a540be3f4..7dfd737245 100644 --- a/C4/Branch.pm +++ b/C4/Branch.pm @@ -15,7 +15,6 @@ package C4::Branch; # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, # Suite 330, Boston, MA 02111-1307 USA -# $Id$ use strict; require Exporter; @@ -24,8 +23,30 @@ use C4::Koha; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); -# set the version for version checking -$VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v).".".join( "_", map { sprintf "%03d", $_ } @v ); }; +BEGIN { + # set the version for version checking + $VERSION = 3.02; + @ISA = qw(Exporter); + @EXPORT = qw( + &GetBranchCategory + &GetBranchName + &GetBranch + &GetBranches + &GetBranchesLoop + &GetBranchDetail + &get_branchinfos_of + &ModBranch + &CheckBranchCategorycode + &GetBranchInfo + &GetCategoryTypes + &GetBranchCategories + &GetBranchesInCategory + &ModBranchCategoryInfo + &DelBranch + &DelBranchCategory + ); + @EXPORT_OK = qw( &onlymine &mybranch ); +} =head1 NAME @@ -41,76 +62,64 @@ The functions in this module deal with branches. =head1 FUNCTIONS -=cut - -@ISA = qw(Exporter); -@EXPORT = qw( - &GetBranchCategory - &GetBranchName - &GetBranch - &GetBranches - &GetBranchDetail - &get_branchinfos_of - &ModBranch - &CheckBranchCategorycode - &GetBranchInfo - &ModBranchCategoryInfo - &DelBranch - &DelBranchCategory -); - =head2 GetBranches $branches = &GetBranches(); - returns informations about ALL branches. - Create a branch selector with the following code - IndependantBranches Insensitive... + + Returns informations about ALL branches, IndependantBranches Insensitive. + GetBranchInfo() returns the same information without the problems of this function + (namespace collision, mainly). + Create a branch selector with the following code. =head3 in PERL SCRIPT -my $branches = GetBranches; -my @branchloop; -foreach my $thisbranch (keys %$branches) { - my $selected = 1 if $thisbranch eq $branch; - my %row =(value => $thisbranch, - selected => $selected, - branchname => $branches->{$thisbranch}->{'branchname'}, - ); - push @branchloop, \%row; -} - + my $branches = GetBranches; + my @branchloop; + foreach my $thisbranch (sort keys %$branches) { + my $selected = 1 if $thisbranch eq $branch; + my %row =(value => $thisbranch, + selected => $selected, + branchname => $branches->{$thisbranch}->{branchname}, + ); + push @branchloop, \%row; + } =head3 in TEMPLATE - + + + +=head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above. =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 =".$dbh->quote(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; + $sth->execute( @bind_parameters ); + + my $nsth = $dbh->prepare( + "SELECT categorycode FROM branchrelations WHERE branchcode = ?" + ); # prepare once, outside while loop + while ( my $branch = $sth->fetchrow_hashref ) { - my $nsth = - $dbh->prepare( - "select categorycode from branchrelations where branchcode = ?"); $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 @@ -120,13 +129,43 @@ sub GetBranches { # 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; + # $branch->{$cat} = 1; + $branch->{category}{$cat} = 1; } $branches{ $branch->{'branchcode'} } = $branch; } return ( \%branches ); } +sub onlymine { + return + C4::Context->preference('IndependantBranches') && + C4::Context->userenv && + C4::Context->userenv->{flags} %2 != 1 && + C4::Context->userenv->{branch} ; +} + +# always returns a string for OK comparison via "eq" or "ne" +sub mybranch { + C4::Context->userenv or return ''; + return C4::Context->userenv->{branch} || ''; +} + +sub GetBranchesLoop (;$$) { # since this is what most pages want anyway + my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted. + my $onlymine = @_ ? shift : onlymine(); + my $branches = GetBranches($onlymine); + my @loop; + foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) { + push @loop, { + value => $_, + selected => ($_ eq $branch) ? 1 : 0, + branchname => $branches->{$_}->{branchname}, + }; + } + return \@loop; +} + =head2 GetBranchName =cut @@ -144,9 +183,9 @@ sub GetBranchName { =head2 ModBranch -&ModBranch($newvalue); +$error = &ModBranch($newvalue); -This function modify an existing branches. +This function modify an existing branch C<$newvalue> is a ref to an array wich is containt all the column from branches table. @@ -172,6 +211,7 @@ sub ModBranch { $data->{'branchfax'}, $data->{'branchemail'}, $data->{'branchip'}, $data->{'branchprinter'}, ); + return 1 if $dbh->err; } else { my $query = " UPDATE branches @@ -270,6 +310,58 @@ sub GetBranchCategory { return \@results; } +=head2 GetBranchCategories + + my $categories = GetBranchCategories($branchcode,$categorytype); + +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. + +=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 ); +} + +=head2 GetCategoryTypes + +$categorytypes = GetCategoryTypes; +returns a list of category types. +Currently these types are HARDCODED. +type: 'searchdomain' defines a group of agencies that the calling library may search in. +Other usage of agency categories falls under type: 'properties'. + to allow for other uses of categories. +The searchdomain bit may be better implemented as a separate module, but +the categories were already here, and minimally used. +=cut + + #TODO manage category types. rename possibly to 'agency domains' ? as borrowergroups are called categories. +sub GetCategoryTypes() { + return ( 'searchdomain','properties'); +} + =head2 GetBranch $branch = GetBranch( $query, $branches ); @@ -287,24 +379,20 @@ sub GetBranch ($$) { =head2 GetBranchDetail - $branchname = &GetBranchDetail($branchcode); + $branch = &GetBranchDetail($branchcode); -Given the branch code, the function returns the corresponding -branch name for a comprehensive information display +Given the branch code, the function returns a +hashref for the corresponding row in the branches table. =cut sub GetBranchDetail { - my ($branchcode) = @_; - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("SELECT * FROM branches WHERE branchcode = ?"); + my ($branchcode) = shift or return; + my $sth = C4::Context->dbh->prepare("SELECT * FROM branches WHERE branchcode = ?"); $sth->execute($branchcode); - my $branchname = $sth->fetchrow_hashref(); - $sth->finish(); - return $branchname; + return $sth->fetchrow_hashref(); } - =head2 get_branchinfos_of my $branchinfos_of = get_branchinfos_of(@branchcodes); @@ -332,19 +420,45 @@ sub get_branchinfos_of { return C4::Koha::get_infos_of( $query, 'branchcode' ); } + +=head2 GetBranchesInCategory + + my $branches = GetBranchesInCategory($categorycode); + +Returns a href: keys %$branches eq (branchcode,branchname) . + +=cut + +sub GetBranchesInCategory($) { + my ($categorycode) = @_; + my @branches; + my $dbh = C4::Context->dbh(); + my $sth=$dbh->prepare( "SELECT b.branchcode FROM branchrelations r, branches b + where r.branchcode=b.branchcode and r.categorycode=?"); + $sth->execute($categorycode); + while (my $branch = $sth->fetchrow) { + push @branches, $branch; + } + $sth->finish(); + return( \@branches ); +} + =head2 GetBranchInfo $results = GetBranchInfo($branchcode); returns C<$results>, a reference to an array of hashes containing branches. - +if $branchcode, just this branch, with associated categories. +if ! $branchcode && $categorytype, all branches in the category. =cut sub GetBranchInfo { - my ($branchcode) = @_; + my ($branchcode,$categorytype) = @_; my $dbh = C4::Context->dbh; my $sth; - if ($branchcode) { + + + if ($branchcode) { $sth = $dbh->prepare( "Select * from branches where branchcode = ? order by branchcode"); @@ -356,10 +470,16 @@ sub GetBranchInfo { } my @results; while ( my $data = $sth->fetchrow_hashref ) { - my $nsth = - $dbh->prepare( - "select categorycode from branchrelations where branchcode = ?"); - $nsth->execute( $data->{'branchcode'} ); + my @bind = ($data->{'branchcode'}); + my $query= "select r.categorycode from branchrelations r"; + $query .= ", branchcategories c " if($categorytype); + $query .= " where branchcode=? "; + if($categorytype) { + $query .= " and c.categorytype=? and r.categorycode=c.categorycode"; + push @bind, $categorytype; + } + my $nsth=$dbh->prepare($query); + $nsth->execute( @bind ); my @cats = (); while ( my ($cat) = $nsth->fetchrow_array ) { push( @cats, $cat ); @@ -394,12 +514,20 @@ sets the data from the editbranch form, and writes to the database... =cut sub ModBranchCategoryInfo { - my ($data) = @_; my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("replace branchcategories (categorycode,categoryname,codedescription) values (?,?,?)"); - $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'} ); - $sth->finish; + 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(); + } + 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(); + } } =head2 DeleteBranchCategory @@ -435,7 +563,8 @@ sub CheckBranchCategorycode { return $total; } - +1; +__END__ =head1 AUTHOR