Bug 12571 - Add ability to customize SIP2 screen messages
[koha-ffzg.git] / C4 / Branch.pm
index 108f24f..ff571dc 100644 (file)
@@ -70,8 +70,7 @@ The functions in this module deal with branches.
   $branches = &GetBranches();
 
 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.
 
 Create a branch selector with the following code.
 
@@ -106,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;
@@ -146,11 +139,11 @@ sub GetBranches {
 }
 
 sub onlymine {
-    return 
-    C4::Context->preference('IndependentBranches') &&
-    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"
@@ -186,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);
 }
 
@@ -210,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(
@@ -224,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 {
@@ -233,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);
@@ -247,6 +242,7 @@ sub ModBranch {
             $data->{'branchemail'},      $data->{'branchurl'},
             $data->{'branchip'},         $data->{'branchprinter'},
             $data->{'branchnotes'},      $data->{opac_info},
+            $data->{'branchreplyto'},    $data->{'branchreturnpath'},
             $data->{'branchcode'},
         );
     }
@@ -282,7 +278,6 @@ sub ModBranch {
 "insert into branchrelations (branchcode, categorycode) values(?, ?)"
           );
         $sth->execute( $branchcode, $cat );
-        $sth->finish;
     }
     foreach my $cat (@removecats) {
         my $sth =
@@ -290,7 +285,6 @@ sub ModBranch {
             "delete from branchrelations where branchcode=? and categorycode=?"
           );
         $sth->execute( $branchcode, $cat );
-        $sth->finish;
     }
 }
 
@@ -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
@@ -507,13 +497,11 @@ sub ModBranchCategoryInfo {
        # we are doing an insert
   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
         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();
     }
 }
 
@@ -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