X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FBranch.pm;h=7dfd737245be623da417b25f4316f2a2728ea705;hb=f33e65499b0d00637dc7627c6ead4f6849cc2a96;hp=f8edb6a99dffc5708d9acb1c361673f1c7147915;hpb=3d04be488e6e73840d5701f0290defa1f88c8943;p=koha.git
diff --git a/C4/Branch.pm b/C4/Branch.pm
index f8edb6a99d..7dfd737245 100644
--- a/C4/Branch.pm
+++ b/C4/Branch.pm
@@ -23,8 +23,30 @@ use C4::Koha;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
-# set the version for version checking
-$VERSION = 3.00;
+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
@@ -40,58 +62,38 @@ 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
- &GetCategoryTypes
- &GetBranchCategories
- &GetBranchesInCategory
- &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). You should probably use that, and replace GetBranches()
- with GetBranchInfo() where you see it in the code.
+ (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
@@ -101,20 +103,23 @@ sub GetBranches {
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
@@ -132,6 +137,35 @@ sub GetBranches {
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
@@ -149,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.
@@ -177,6 +211,7 @@ sub ModBranch {
$data->{'branchfax'}, $data->{'branchemail'},
$data->{'branchip'}, $data->{'branchprinter'},
);
+ return 1 if $dbh->err;
} else {
my $query = "
UPDATE branches
@@ -344,21 +379,18 @@ 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
@@ -482,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,categorytype) values (?,?,?,?)");
- $sth->execute(uc( $data->{'categorycode'} ),$data->{'categoryname'}, $data->{'codedescription'},$data->{'categorytype'} );
- $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
@@ -523,7 +563,8 @@ sub CheckBranchCategorycode {
return $total;
}
-
+1;
+__END__
=head1 AUTHOR