Bug Fixing 1782 patch
[koha.git] / C4 / Branch.pm
index 615ca06..4755168 100644 (file)
@@ -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,28 @@ 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.01;
+       @ISA    = qw(Exporter);
+       @EXPORT = qw(
+               &GetBranchCategory
+               &GetBranchName
+               &GetBranch
+               &GetBranches
+               &GetBranchDetail
+               &get_branchinfos_of
+               &ModBranch
+               &CheckBranchCategorycode
+               &GetBranchInfo
+               &GetCategoryTypes
+               &GetBranchCategories
+               &GetBranchesInCategory
+               &ModBranchCategoryInfo
+               &DelBranch
+               &DelBranchCategory
+       );
+}
 
 =head1 NAME
 
@@ -41,29 +60,15 @@ 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
-);
-
 =head2 GetBranches
 
   $branches = &GetBranches();
   returns informations about ALL branches.
   Create a branch selector with the following code
   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.
   
 =head3 in PERL SCRIPT
 
@@ -78,7 +83,6 @@ foreach my $thisbranch (keys %$branches) {
     push @branchloop, \%row;
 }
 
-
 =head3 in TEMPLATE
             <select name="branch">
                 <option value="">Default</option>
@@ -90,23 +94,22 @@ foreach my $thisbranch (keys %$branches) {
 =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";
     if ($onlymine && C4::Context->userenv && C4::Context->userenv->{branch}){
       $query .= " WHERE branchcode =".$dbh->quote(C4::Context->userenv->{branch});
     }
-    $query.=" order by branchname";
+    $query.=" ORDER BY branchname";
     $sth = $dbh->prepare($query);
     $sth->execute;
     while ( my $branch = $sth->fetchrow_hashref ) {
         my $nsth =
           $dbh->prepare(
-            "select categorycode from branchrelations where branchcode = ?");
+            "SELECT categorycode FROM branchrelations WHERE branchcode = ?");
         $nsth->execute( $branch->{'branchcode'} );
         while ( my ($cat) = $nsth->fetchrow_array ) {
 
@@ -119,7 +122,8 @@ 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;
     }
@@ -269,6 +273,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 );
@@ -303,7 +359,6 @@ sub GetBranchDetail {
     return $branchname;
 }
 
-
 =head2 get_branchinfos_of
 
   my $branchinfos_of = get_branchinfos_of(@branchcodes);
@@ -331,19 +386,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");
@@ -355,10 +436,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 );
@@ -393,12 +480,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
@@ -407,7 +502,7 @@ DeleteBranchCategory($categorycode);
 
 =cut
 
-sub DeleteBranchCategory {
+sub DelBranchCategory {
     my ($categorycode) = @_;
     my $dbh = C4::Context->dbh;
     my $sth = $dbh->prepare("delete from branchcategories where categorycode = ?");
@@ -434,7 +529,8 @@ sub CheckBranchCategorycode {
     return $total;
 }
 
-
+1;
+__END__
 
 =head1 AUTHOR