added autocomplete="off"
[koha.git] / C4 / Branch.pm
index f8edb6a..7bd0326 100644 (file)
@@ -17,14 +17,38 @@ package C4::Branch;
 
 
 use strict;
+#use warnings; FIXME - Bug 2505
 require Exporter;
 use C4::Context;
-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
+               &CheckCategoryUnique
+               &mybranch
+       );
+       @EXPORT_OK = qw( &onlymine &mybranch get_branch_code_from_name );
+}
 
 =head1 NAME
 
@@ -40,58 +64,39 @@ 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...
-  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
 
-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;
-}
+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 (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
-            <select name="branch">
-                <option value="">Default</option>
-            <!-- TMPL_LOOP name="branchloop" -->
-                <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
-            <!-- /TMPL_LOOP -->
-            </select>
+
+    <select name="branch">
+        <option value="">Default</option>
+        <!-- TMPL_LOOP name="branchloop" -->
+        <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="branchname" --></option>
+        <!-- /TMPL_LOOP -->
+    </select>
+
+=head4 Note that you often will want to just use GetBranchesLoop, for exactly the example above.
 
 =cut
 
@@ -101,20 +106,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 +140,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 { uc($branches->{$a}->{branchname}) cmp uc($branches->{$b}->{branchname}) } keys %$branches ) {
+        push @loop, {
+            value => $_,
+            selected => ($_ eq $branch) ? 1 : 0, 
+            branchname => $branches->{$_}->{branchname},
+        };
+    }
+    return \@loop;
+}
+
 =head2 GetBranchName
 
 =cut
@@ -149,9 +186,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.
 
@@ -165,33 +202,45 @@ sub ModBranch {
         my $query  = "
             INSERT INTO branches
             (branchcode,branchname,branchaddress1,
-            branchaddress2,branchaddress3,branchphone,
-            branchfax,branchemail,branchip,branchprinter)
-            VALUES (?,?,?,?,?,?,?,?,?,?)
+            branchaddress2,branchaddress3,branchzip,branchcity,branchstate,
+            branchcountry,branchphone,branchfax,branchemail,
+            branchurl,branchip,branchprinter,branchnotes)
+            VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
         ";
         my $sth    = $dbh->prepare($query);
         $sth->execute(
             $data->{'branchcode'},       $data->{'branchname'},
             $data->{'branchaddress1'},   $data->{'branchaddress2'},
-            $data->{'branchaddress3'},   $data->{'branchphone'},
-            $data->{'branchfax'},        $data->{'branchemail'},
+            $data->{'branchaddress3'},   $data->{'branchzip'},
+            $data->{'branchcity'},       $data->{'branchstate'},
+            $data->{'branchcountry'},
+            $data->{'branchphone'},      $data->{'branchfax'},
+            $data->{'branchemail'},      $data->{'branchurl'},
             $data->{'branchip'},         $data->{'branchprinter'},
+            $data->{'branchnotes'},
         );
+        return 1 if $dbh->err;
     } else {
         my $query  = "
             UPDATE branches
             SET branchname=?,branchaddress1=?,
-                branchaddress2=?,branchaddress3=?,branchphone=?,
-                branchfax=?,branchemail=?,branchip=?,branchprinter=?
+                branchaddress2=?,branchaddress3=?,branchzip=?,
+                branchcity=?,branchstate=?,branchcountry=?,branchphone=?,
+                branchfax=?,branchemail=?,branchurl=?,branchip=?,
+                branchprinter=?,branchnotes=?
             WHERE branchcode=?
         ";
         my $sth    = $dbh->prepare($query);
         $sth->execute(
             $data->{'branchname'},
             $data->{'branchaddress1'},   $data->{'branchaddress2'},
-            $data->{'branchaddress3'},   $data->{'branchphone'},
-            $data->{'branchfax'},        $data->{'branchemail'},
+            $data->{'branchaddress3'},   $data->{'branchzip'},
+            $data->{'branchcity'},       $data->{'branchstate'},       
+            $data->{'branchcountry'},
+            $data->{'branchphone'},      $data->{'branchfax'},
+            $data->{'branchemail'},      $data->{'branchurl'},
             $data->{'branchip'},         $data->{'branchprinter'},
+            $data->{'branchnotes'},
             $data->{'branchcode'},
         );
     }
@@ -344,51 +393,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;
-}
-
-=head2 get_branchinfos_of
-
-  my $branchinfos_of = get_branchinfos_of(@branchcodes);
-
-Associates a list of branchcodes to the information of the branch, taken in
-branches table.
-
-Returns a href where keys are branchcodes and values are href where keys are
-branch information key.
-
-  print 'branchname is ', $branchinfos_of->{$code}->{branchname};
-
-=cut
-
-sub get_branchinfos_of {
-    my @branchcodes = @_;
-
-    my $query = '
-    SELECT branchcode,
-       branchname
-    FROM branches
-    WHERE branchcode IN ('
-      . join( ',', map( { "'" . $_ . "'" } @branchcodes ) ) . ')
-';
-    return C4::Koha::get_infos_of( $query, 'branchcode' );
+    return $sth->fetchrow_hashref();
 }
 
-
 =head2 GetBranchesInCategory
 
   my $branches = GetBranchesInCategory($categorycode);
@@ -482,14 +500,44 @@ 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 CheckCategoryUnique
+
+if (CheckCategoryUnique($categorycode)){
+  # do something
+}
+
+=cut
+
+sub CheckCategoryUnique {
+    my $categorycode = shift;
+    my $dbh    = C4::Context->dbh;
+    my $sth = $dbh->prepare("SELECT categorycode FROM branchcategories WHERE categorycode = ?");
+    $sth->execute(uc( $categorycode) );
+    if (my $data = $sth->fetchrow_hashref){
+       return 0;
+    }
+    else {
+       return 1;
+    }
+}
+
+    
 =head2 DeleteBranchCategory
 
 DeleteBranchCategory($categorycode);
@@ -523,10 +571,20 @@ sub CheckBranchCategorycode {
     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;
+}
 
+1;
+__END__
 
 =head1 AUTHOR
 
-Koha Developement team <info@koha.org>
+Koha Development Team <http://koha-community.org/>
 
 =cut