Bug 22043: (QA follow-up) Add parameter to control behavior
[koha.git] / C4 / Members / AttributeTypes.pm
index d8d5508..bfd1494 100644 (file)
@@ -4,29 +4,24 @@ package C4::Members::AttributeTypes;
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 #use warnings; FIXME - Bug 2505
 use C4::Context;
 
-use vars qw($VERSION);
 
-BEGIN {
-    # set the version for version checking
-    $VERSION = 3.00;
-}
 
 =head1 NAME
 
@@ -42,7 +37,7 @@ C4::Members::AttributeTypes - mananage extended patron attribute types
   $attr_type->repeatable($repeatable);
   $attr_type->unique_id($unique_id);
   $attr_type->opac_display($opac_display);
-  $attr_type->password_allowed($password_allowed);
+  $attr_type->opac_editable($opac_editable);
   $attr_type->staff_searchable($staff_searchable);
   $attr_type->authorised_value_category($authorised_value_category);
   $attr_type->store();
@@ -69,12 +64,24 @@ If $all_fields is true, then each hashref also contains the other fields from bo
 =cut
 
 sub GetAttributeTypes {
-    my ($all) = @_;
-    my $select = $all ? '*' : 'code, description, class';
+    my $all    = @_   ? shift : 0;
+    my $no_branch_limit = @_ ? shift : 0;
+    my $branch_limit = $no_branch_limit
+        ? 0
+        : C4::Context->userenv ? C4::Context->userenv->{"branch"} : 0;
+    my $select = $all ? '*'   : 'DISTINCT(code), description, class';
+
     my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("SELECT $select FROM borrower_attribute_types ORDER by code");
-    $sth->execute();
+    my $query = "SELECT $select FROM borrower_attribute_types";
+    $query .= qq{
+        LEFT JOIN borrower_attribute_types_branches ON bat_code = code
+        WHERE b_branchcode = ? OR b_branchcode IS NULL
+    } if $branch_limit;
+    $query .= " ORDER BY code";
+    my $sth    = $dbh->prepare($query);
+    $sth->execute( $branch_limit ? $branch_limit : () );
     my $results = $sth->fetchall_arrayref({});
+    $sth->finish;
     return @$results;
 }
 
@@ -83,22 +90,6 @@ sub GetAttributeTypes_hashref {
     return \%hash;
 }
 
-=head2 AttributeTypeExists
-
-  my $have_attr_xyz = C4::Members::AttributeTypes::AttributeTypeExists($code)
-
-Returns true if we have attribute type C<$code>
-in the database.
-
-=cut
-
-sub AttributeTypeExists {
-    my ($code) = @_;
-    my $dbh = C4::Context->dbh;
-    my $exists = $dbh->selectrow_array("SELECT code FROM borrower_attribute_types WHERE code = ?", undef, $code);
-    return $exists;
-}
-
 =head1 METHODS 
 
   my $attr_type = C4::Members::AttributeTypes->new($code, $description);
@@ -116,7 +107,7 @@ sub new {
     $self->{'repeatable'} = 0;
     $self->{'unique_id'} = 0;
     $self->{'opac_display'} = 0;
-    $self->{'password_allowed'} = 0;
+    $self->{'opac_editable'} = 0;
     $self->{'staff_searchable'} = 0;
     $self->{'display_checkout'} = 0;
     $self->{'authorised_value_category'} = '';
@@ -158,7 +149,7 @@ sub fetch {
     $self->{'repeatable'}                = $row->{'repeatable'};
     $self->{'unique_id'}                 = $row->{'unique_id'};
     $self->{'opac_display'}              = $row->{'opac_display'};
-    $self->{'password_allowed'}          = $row->{'password_allowed'};
+    $self->{'opac_editable'}             = $row->{'opac_editable'};
     $self->{'staff_searchable'}          = $row->{'staff_searchable'};
     $self->{'display_checkout'}          = $row->{'display_checkout'};
     $self->{'authorised_value_category'} = $row->{'authorised_value_category'};
@@ -166,6 +157,13 @@ sub fetch {
     $self->{'category_description'}      = $row->{'category_description'};
     $self->{'class'}                     = $row->{'class'};
 
+    $sth = $dbh->prepare("SELECT branchcode, branchname FROM borrower_attribute_types_branches, branches WHERE b_branchcode = branchcode AND bat_code = ?;");
+    $sth->execute( $code );
+    while ( my $data = $sth->fetchrow_hashref ) {
+        push @{ $self->{branches} }, $data;
+    }
+    $sth->finish();
+
     bless $self, $class;
     return $self;
 }
@@ -192,7 +190,7 @@ sub store {
                                          repeatable = ?,
                                          unique_id = ?,
                                          opac_display = ?,
-                                         password_allowed = ?,
+                                         opac_editable = ?,
                                          staff_searchable = ?,
                                          authorised_value_category = ?,
                                          display_checkout = ?,
@@ -201,24 +199,51 @@ sub store {
                                      WHERE code = ?");
     } else {
         $sth = $dbh->prepare_cached("INSERT INTO borrower_attribute_types 
-                                        (description, repeatable, unique_id, opac_display, password_allowed,
-                                         staff_searchable, authorised_value_category, display_checkout, category_code, class, code)
-                                        VALUES (?, ?, ?, ?, ?,
-                                                ?, ?, ?, ?, ?, ?)");
+                                        ( description,
+                                          repeatable,
+                                          unique_id,
+                                          opac_display,
+                                          opac_editable,
+                                          staff_searchable,
+                                          authorised_value_category,
+                                          display_checkout,
+                                          category_code,
+                                          class,
+                                          code
+                                        )
+                                        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
     }
-    $sth->bind_param(1, $self->{'description'});
-    $sth->bind_param(2, $self->{'repeatable'});
-    $sth->bind_param(3, $self->{'unique_id'});
-    $sth->bind_param(4, $self->{'opac_display'});
-    $sth->bind_param(5, $self->{'password_allowed'});
-    $sth->bind_param(6, $self->{'staff_searchable'});
-    $sth->bind_param(7, $self->{'authorised_value_category'});
-    $sth->bind_param(8, $self->{'display_checkout'});
-    $sth->bind_param(9, $self->{'category_code'} || undef);
-    $sth->bind_param(10, $self->{'class'});
-    $sth->bind_param(11, $self->{'code'});
-    $sth->execute;
 
+    $sth->execute(
+        $self->{'description'},
+        $self->{'repeatable'},
+        $self->{'unique_id'},
+        $self->{'opac_display'},
+        $self->{'opac_editable'},
+        $self->{'staff_searchable'} || 0,
+        $self->{'authorised_value_category'},
+        $self->{'display_checkout'},
+        $self->{'category_code'} || undef,
+        $self->{'class'},
+        $self->{'code'}
+    );
+
+    if ( defined $$self{branches} ) {
+        $sth = $dbh->prepare("DELETE FROM borrower_attribute_types_branches WHERE bat_code = ?");
+        $sth->execute( $$self{code} );
+        $sth = $dbh->prepare(
+            "INSERT INTO borrower_attribute_types_branches
+                        ( bat_code, b_branchcode )
+                        VALUES ( ?, ? )"
+        );
+        for my $branchcode ( @{$$self{branches}} ) {
+            next if not $branchcode;
+            $sth->bind_param( 1, $$self{code} );
+            $sth->bind_param( 2, $branchcode );
+            $sth->execute;
+        }
+    }
+    $sth->finish;
 }
 
 =head2 code
@@ -250,6 +275,20 @@ sub description {
     @_ ? $self->{'description'} = shift : $self->{'description'};
 }
 
+=head2 branches
+
+my $branches = $attr_type->branches();
+$attr_type->branches($branches);
+
+Accessor.
+
+=cut
+
+sub branches {
+    my $self = shift;
+    @_ ? $self->{branches} = shift : $self->{branches};
+}
+
 =head2 repeatable
 
   my $repeatable = $attr_type->repeatable();
@@ -279,6 +318,7 @@ sub unique_id {
     my $self = shift;
     @_ ? $self->{'unique_id'} = ((shift) ? 1 : 0) : $self->{'unique_id'};
 }
+
 =head2 opac_display
 
   my $opac_display = $attr_type->opac_display();
@@ -293,20 +333,22 @@ sub opac_display {
     my $self = shift;
     @_ ? $self->{'opac_display'} = ((shift) ? 1 : 0) : $self->{'opac_display'};
 }
-=head2 password_allowed
 
-  my $password_allowed = $attr_type->password_allowed();
-  $attr_type->password_allowed($password_allowed);
+=head2 opac_editable
 
-Accessor.  The C<$password_allowed> argument
+  my $opac_editable = $attr_type->opac_editable();
+  $attr_type->opac_editable($opac_editable);
+
+Accessor.  The C<$opac_editable> argument
 is interpreted as a Perl boolean.
 
 =cut
 
-sub password_allowed {
+sub opac_editable {
     my $self = shift;
-    @_ ? $self->{'password_allowed'} = ((shift) ? 1 : 0) : $self->{'password_allowed'};
+    @_ ? $self->{'opac_editable'} = ((shift) ? 1 : 0) : $self->{'opac_editable'};
 }
+
 =head2 staff_searchable
 
   my $staff_searchable = $attr_type->staff_searchable();
@@ -324,13 +366,9 @@ sub staff_searchable {
 
 =head2 display_checkout
 
-=over 4
-
 my $display_checkout = $attr_type->display_checkout();
 $attr_type->display_checkout($display_checkout);
 
-=back
-
 Accessor.  The C<$display_checkout> argument
 is interpreted as a Perl boolean.
 
@@ -357,13 +395,9 @@ sub authorised_value_category {
 
 =head2 category_code
 
-=over 4
-
 my $category_code = $attr_type->category_code();
 $attr_type->category_code($category_code);
 
-=back
-
 Accessor.
 
 =cut
@@ -375,13 +409,9 @@ sub category_code {
 
 =head2 category_description
 
-=over 4
-
 my $category_description = $attr_type->category_description();
 $attr_type->category_description($category_description);
 
-=back
-
 Accessor.
 
 =cut
@@ -393,13 +423,9 @@ sub category_description {
 
 =head2 class
 
-=over 4
-
 my $class = $attr_type->class();
 $attr_type->class($class);
 
-=back
-
 Accessor.
 
 =cut
@@ -432,6 +458,7 @@ sub delete {
     my $dbh = C4::Context->dbh;
     my $sth = $dbh->prepare_cached("DELETE FROM borrower_attribute_types WHERE code = ?");
     $sth->execute($code);
+    $sth->finish;
 }
 
 =head2 num_patrons