Bug 10663: restore ability of active hold requests to block renewal
[koha.git] / C4 / Branch.pm
index 7bd0326..8b76771 100644 (file)
@@ -25,7 +25,7 @@ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 
 BEGIN {
        # set the version for version checking
 
 BEGIN {
        # set the version for version checking
-       $VERSION = 3.02;
+    $VERSION = 3.07.00.049;
        @ISA    = qw(Exporter);
        @EXPORT = qw(
                &GetBranchCategory
        @ISA    = qw(Exporter);
        @EXPORT = qw(
                &GetBranchCategory
@@ -46,8 +46,9 @@ BEGIN {
                &DelBranchCategory
                &CheckCategoryUnique
                &mybranch
                &DelBranchCategory
                &CheckCategoryUnique
                &mybranch
+               &GetBranchesCount
        );
        );
-       @EXPORT_OK = qw( &onlymine &mybranch get_branch_code_from_name );
+    @EXPORT_OK = qw( &onlymine &mybranch );
 }
 
 =head1 NAME
 }
 
 =head1 NAME
@@ -68,7 +69,7 @@ The functions in this module deal with branches.
 
   $branches = &GetBranches();
 
 
   $branches = &GetBranches();
 
-Returns informations about ALL branches, IndependantBranches Insensitive.
+Returns informations about ALL branches, IndependentBranches Insensitive.
 GetBranchInfo() returns the same information without the problems of this function 
 (namespace collision, mainly).
 
 GetBranchInfo() returns the same information without the problems of this function 
 (namespace collision, mainly).
 
@@ -142,7 +143,7 @@ sub GetBranches {
 
 sub onlymine {
     return 
 
 sub onlymine {
     return 
-    C4::Context->preference('IndependantBranches') &&
+    C4::Context->preference('IndependentBranches') &&
     C4::Context->userenv                           &&
     C4::Context->userenv->{flags} %2 != 1          &&
     C4::Context->userenv->{branch}                 ;
     C4::Context->userenv                           &&
     C4::Context->userenv->{flags} %2 != 1          &&
     C4::Context->userenv->{branch}                 ;
@@ -154,16 +155,17 @@ sub mybranch {
     return C4::Context->userenv->{branch} || '';
 }
 
     return C4::Context->userenv->{branch} || '';
 }
 
-sub GetBranchesLoop (;$$) {  # since this is what most pages want anyway
+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;
     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 { 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, {
         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;
         };
     }
     return \@loop;
@@ -204,8 +206,8 @@ sub ModBranch {
             (branchcode,branchname,branchaddress1,
             branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
             branchcountry,branchphone,branchfax,branchemail,
             (branchcode,branchname,branchaddress1,
             branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
             branchcountry,branchphone,branchfax,branchemail,
-            branchurl,branchip,branchprinter,branchnotes)
-            VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+            branchurl,branchip,branchprinter,branchnotes,opac_info)
+            VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
         ";
         my $sth    = $dbh->prepare($query);
         $sth->execute(
         ";
         my $sth    = $dbh->prepare($query);
         $sth->execute(
@@ -217,7 +219,7 @@ sub ModBranch {
             $data->{'branchphone'},      $data->{'branchfax'},
             $data->{'branchemail'},      $data->{'branchurl'},
             $data->{'branchip'},         $data->{'branchprinter'},
             $data->{'branchphone'},      $data->{'branchfax'},
             $data->{'branchemail'},      $data->{'branchurl'},
             $data->{'branchip'},         $data->{'branchprinter'},
-            $data->{'branchnotes'},
+            $data->{'branchnotes'},      $data->{opac_info},
         );
         return 1 if $dbh->err;
     } else {
         );
         return 1 if $dbh->err;
     } else {
@@ -227,7 +229,7 @@ sub ModBranch {
                 branchaddress2=?,branchaddress3=?,branchzip=?,
                 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
                 branchfax=?,branchemail=?,branchurl=?,branchip=?,
                 branchaddress2=?,branchaddress3=?,branchzip=?,
                 branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
                 branchfax=?,branchemail=?,branchurl=?,branchip=?,
-                branchprinter=?,branchnotes=?
+                branchprinter=?,branchnotes=?,opac_info=?
             WHERE branchcode=?
         ";
         my $sth    = $dbh->prepare($query);
             WHERE branchcode=?
         ";
         my $sth    = $dbh->prepare($query);
@@ -240,13 +242,13 @@ sub ModBranch {
             $data->{'branchphone'},      $data->{'branchfax'},
             $data->{'branchemail'},      $data->{'branchurl'},
             $data->{'branchip'},         $data->{'branchprinter'},
             $data->{'branchphone'},      $data->{'branchfax'},
             $data->{'branchemail'},      $data->{'branchurl'},
             $data->{'branchip'},         $data->{'branchprinter'},
-            $data->{'branchnotes'},
+            $data->{'branchnotes'},      $data->{opac_info},
             $data->{'branchcode'},
         );
     }
     # sort out the categories....
     my @checkedcats;
             $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} ) {
     foreach my $cat (@$cats) {
         my $code = $cat->{'categorycode'};
         if ( $data->{$code} ) {
@@ -292,71 +294,66 @@ sub ModBranch {
 
 $results = GetBranchCategory($categorycode);
 
 
 $results = GetBranchCategory($categorycode);
 
-C<$results> is an ref to an array.
+C<$results> is an hashref
 
 =cut
 
 sub GetBranchCategory {
 
 =cut
 
 sub GetBranchCategory {
-
-    # returns a reference to an array of hashes containing branches,
     my ($catcode) = @_;
     my ($catcode) = @_;
+    return unless $catcode;
+
     my $dbh = C4::Context->dbh;
     my $sth;
 
     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
 
 }
 
 =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,
 
 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 {
 
 =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
 }
 
 =head2 GetCategoryTypes
@@ -372,7 +369,7 @@ the categories were already here, and minimally used.
 =cut
 
        #TODO  manage category types.  rename possibly to 'agency domains' ? as borrowergroups are called categories.
 =cut
 
        #TODO  manage category types.  rename possibly to 'agency domains' ? as borrowergroups are called categories.
-sub GetCategoryTypes() {
+sub GetCategoryTypes {
        return ( 'searchdomain','properties');
 }
 
        return ( 'searchdomain','properties');
 }
 
@@ -382,7 +379,7 @@ $branch = GetBranch( $query, $branches );
 
 =cut
 
 
 =cut
 
-sub GetBranch ($$) {
+sub GetBranch {
     my ( $query, $branches ) = @_;    # get branch for this query from branches
     my $branch = $query->param('branch');
     my %cookie = $query->cookie('userenv');
     my ( $query, $branches ) = @_;    # get branch for this query from branches
     my $branch = $query->param('branch');
     my %cookie = $query->cookie('userenv');
@@ -415,7 +412,7 @@ Returns a href:  keys %$branches eq (branchcode,branchname) .
 
 =cut
 
 
 =cut
 
-sub GetBranchesInCategory($) {
+sub GetBranchesInCategory {
     my ($categorycode) = @_;
        my @branches;
        my $dbh = C4::Context->dbh();
     my ($categorycode) = @_;
        my @branches;
        my $dbh = C4::Context->dbh();
@@ -504,14 +501,14 @@ sub ModBranchCategoryInfo {
     my $dbh    = C4::Context->dbh;
     if ($data->{'add'}){
        # we are doing an insert
     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'} );
+  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'} );
        $sth->finish();         
     }
     else {
        # modifying
        $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'} ) );
+        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'} ) );
        $sth->finish();
     }
 }
        $sth->finish();
     }
 }
@@ -571,13 +568,13 @@ sub CheckBranchCategorycode {
     return $total;
 }
 
     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";
+    my $sth = $dbh->prepare( $query );
+    $sth->execute();
+    my $row = $sth->fetchrow_hashref();
+    return $row->{'branches_count'};
 }
 
 1;
 }
 
 1;