Bug 10507: improve warning about duplicate patron attribute value
[koha.git] / C4 / Members / AttributeTypes.pm
index 93c67a5..1754099 100644 (file)
@@ -13,18 +13,19 @@ package C4::Members::AttributeTypes;
 # 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., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# 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.
 
 use strict;
+#use warnings; FIXME - Bug 2505
 use C4::Context;
 
 use vars qw($VERSION);
 
 BEGIN {
     # set the version for version checking
-    $VERSION = 3.00;
+    $VERSION = 3.07.00.049;
 }
 
 =head1 NAME
@@ -33,55 +34,59 @@ C4::Members::AttributeTypes - mananage extended patron attribute types
 
 =head1 SYNOPSIS
 
-=over 4
+  my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes();
 
-my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes();
+  my $attr_type = C4::Members::AttributeTypes->new($code, $description);
+  $attr_type->code($code);
+  $attr_type->description($description);
+  $attr_type->repeatable($repeatable);
+  $attr_type->unique_id($unique_id);
+  $attr_type->opac_display($opac_display);
+  $attr_type->password_allowed($password_allowed);
+  $attr_type->staff_searchable($staff_searchable);
+  $attr_type->authorised_value_category($authorised_value_category);
+  $attr_type->store();
+  $attr_type->delete();
 
-my $attr_type = C4::Members::AttributeTypes->new($code, $description);
-$attr_type->code($code);
-$attr_type->description($description);
-$attr_type->repeatable($repeatable);
-$attr_type->unique_id($unique_id);
-$attr_type->opac_display($opac_display);
-$attr_type->password_allowed($password_allowed);
-$attr_type->staff_searchable($staff_searchable);
-$attr_type->authorised_value_category($authorised_value_category);
-$attr_type->store();
-$attr_type->delete();
-
-my $attr_type = C4::Members::AttributeTypes->fetch($code);
-$attr_type = C4::Members::AttributeTypes->delete($code);
-
-=back
+  my $attr_type = C4::Members::AttributeTypes->fetch($code);
+  $attr_type = C4::Members::AttributeTypes->delete($code);
 
 =head1 FUNCTIONS
 
 =head2 GetAttributeTypes
 
-=over 4
-
-my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes($all_fields);
-
-=back
+  my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes($all_fields);
 
 Returns an array of hashrefs of each attribute type defined
 in the database.  The array is sorted by code.  Each hashref contains
 at least the following fields:
 
-code
-description
+ - code
+ - description
 
 If $all_fields is true, then each hashref also contains the other fields from borrower_attribute_types.
 
 =cut
 
 sub GetAttributeTypes {
-    my $all = @_ ? shift : 0;
-    my $select = $all ? '*' : 'code, description';
+    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;
 }
 
@@ -90,13 +95,25 @@ sub GetAttributeTypes_hashref {
     return \%hash;
 }
 
-=head1 METHODS 
+=head2 AttributeTypeExists
+
+  my $have_attr_xyz = C4::Members::AttributeTypes::AttributeTypeExists($code)
+
+Returns true if we have attribute type C<$code>
+in the database.
+
+=cut
 
-=over 4
+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;
+}
 
-my $attr_type = C4::Members::AttributeTypes->new($code, $description);
+=head1 METHODS 
 
-=back
+  my $attr_type = C4::Members::AttributeTypes->new($code, $description);
 
 Create a new attribute type.
 
@@ -113,7 +130,11 @@ sub new {
     $self->{'opac_display'} = 0;
     $self->{'password_allowed'} = 0;
     $self->{'staff_searchable'} = 0;
+    $self->{'display_checkout'} = 0;
     $self->{'authorised_value_category'} = '';
+    $self->{'category_code'} = '';
+    $self->{'category_description'} = '';
+    $self->{'class'} = '';
 
     bless $self, $class;
     return $self;
@@ -121,11 +142,7 @@ sub new {
 
 =head2 fetch
 
-=over 4
-
-my $attr_type = C4::Members::AttributeTypes->fetch($code);
-
-=back
+  my $attr_type = C4::Members::AttributeTypes->fetch($code);
 
 Fetches an attribute type from the database.  If no
 type with the given C<$code> exists, returns undef.
@@ -138,11 +155,15 @@ sub fetch {
     my $self = {};
     my $dbh = C4::Context->dbh();
 
-    my $sth = $dbh->prepare_cached("SELECT * FROM borrower_attribute_types WHERE code = ?");
+    my $sth = $dbh->prepare_cached("
+        SELECT borrower_attribute_types.*, categories.description AS category_description
+        FROM borrower_attribute_types
+        LEFT JOIN categories ON borrower_attribute_types.category_code=categories.categorycode
+        WHERE code =?");
     $sth->execute($code);
     my $row = $sth->fetchrow_hashref;
     $sth->finish();
-    return undef unless defined $row;    
+    return unless defined $row;
 
     $self->{'code'}                      = $row->{'code'};
     $self->{'description'}               = $row->{'description'};
@@ -151,7 +172,18 @@ sub fetch {
     $self->{'opac_display'}              = $row->{'opac_display'};
     $self->{'password_allowed'}          = $row->{'password_allowed'};
     $self->{'staff_searchable'}          = $row->{'staff_searchable'};
+    $self->{'display_checkout'}          = $row->{'display_checkout'};
     $self->{'authorised_value_category'} = $row->{'authorised_value_category'};
+    $self->{'category_code'}             = $row->{'category_code'};
+    $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;
@@ -159,11 +191,7 @@ sub fetch {
 
 =head2 store
 
-=over 4
-
-$attr_type->store();
-
-=back
+  $attr_type->store();
 
 Stores attribute type in the database.  If the type
 previously retrieved from the database via the fetch()
@@ -185,14 +213,17 @@ sub store {
                                          opac_display = ?,
                                          password_allowed = ?,
                                          staff_searchable = ?,
-                                         authorised_value_category = ?
+                                         authorised_value_category = ?,
+                                         display_checkout = ?,
+                                         category_code = ?,
+                                         class = ?
                                      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, code)
+                                         staff_searchable, authorised_value_category, display_checkout, category_code, class, code)
                                         VALUES (?, ?, ?, ?, ?,
-                                                ?, ?, ?)");
+                                                ?, ?, ?, ?, ?, ?)");
     }
     $sth->bind_param(1, $self->{'description'});
     $sth->bind_param(2, $self->{'repeatable'});
@@ -201,19 +232,34 @@ sub store {
     $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->{'code'});
+    $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;
 
+    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
 
-=over 4
-
-my $code = $attr_type->code();
-$attr_type->code($code);
-
-=back
+  my $code = $attr_type->code();
+  $attr_type->code($code);
 
 Accessor.  Note that the code is immutable once
 a type is created or fetched from the database.
@@ -227,12 +273,8 @@ sub code {
 
 =head2 description
 
-=over 4
-
-my $description = $attr_type->description();
-$attr_type->description($description);
-
-=back
+  my $description = $attr_type->description();
+  $attr_type->description($description);
 
 Accessor.
 
@@ -243,14 +285,24 @@ sub description {
     @_ ? $self->{'description'} = shift : $self->{'description'};
 }
 
-=head2 repeatable
+=head2 branches
 
-=over 4
+my $branches = $attr_type->branches();
+$attr_type->branches($branches);
 
-my $repeatable = $attr_type->repeatable();
-$attr_type->repeatable($repeatable);
+Accessor.
 
-=back
+=cut
+
+sub branches {
+    my $self = shift;
+    @_ ? $self->{branches} = shift : $self->{branches};
+}
+
+=head2 repeatable
+
+  my $repeatable = $attr_type->repeatable();
+  $attr_type->repeatable($repeatable);
 
 Accessor.  The C<$repeatable> argument
 is interpreted as a Perl boolean.
@@ -264,12 +316,8 @@ sub repeatable {
 
 =head2 unique_id
 
-=over 4
-
-my $unique_id = $attr_type->unique_id();
-$attr_type->unique_id($unique_id);
-
-=back
+  my $unique_id = $attr_type->unique_id();
+  $attr_type->unique_id($unique_id);
 
 Accessor.  The C<$unique_id> argument
 is interpreted as a Perl boolean.
@@ -282,12 +330,8 @@ sub unique_id {
 }
 =head2 opac_display
 
-=over 4
-
-my $opac_display = $attr_type->opac_display();
-$attr_type->opac_display($opac_display);
-
-=back
+  my $opac_display = $attr_type->opac_display();
+  $attr_type->opac_display($opac_display);
 
 Accessor.  The C<$opac_display> argument
 is interpreted as a Perl boolean.
@@ -300,12 +344,8 @@ sub opac_display {
 }
 =head2 password_allowed
 
-=over 4
-
-my $password_allowed = $attr_type->password_allowed();
-$attr_type->password_allowed($password_allowed);
-
-=back
+  my $password_allowed = $attr_type->password_allowed();
+  $attr_type->password_allowed($password_allowed);
 
 Accessor.  The C<$password_allowed> argument
 is interpreted as a Perl boolean.
@@ -318,12 +358,8 @@ sub password_allowed {
 }
 =head2 staff_searchable
 
-=over 4
-
-my $staff_searchable = $attr_type->staff_searchable();
-$attr_type->staff_searchable($staff_searchable);
-
-=back
+  my $staff_searchable = $attr_type->staff_searchable();
+  $attr_type->staff_searchable($staff_searchable);
 
 Accessor.  The C<$staff_searchable> argument
 is interpreted as a Perl boolean.
@@ -335,14 +371,25 @@ sub staff_searchable {
     @_ ? $self->{'staff_searchable'} = ((shift) ? 1 : 0) : $self->{'staff_searchable'};
 }
 
-=head2 authorised_value_category
+=head2 display_checkout
+
+my $display_checkout = $attr_type->display_checkout();
+$attr_type->display_checkout($display_checkout);
 
-=over 4
+Accessor.  The C<$display_checkout> argument
+is interpreted as a Perl boolean.
 
-my $authorised_value_category = $attr_type->authorised_value_category();
-$attr_type->authorised_value_category($authorised_value_category);
+=cut
 
-=back
+sub display_checkout {
+    my $self = shift;
+    @_ ? $self->{'display_checkout'} = ((shift) ? 1 : 0) : $self->{'display_checkout'};
+}
+
+=head2 authorised_value_category
+
+  my $authorised_value_category = $attr_type->authorised_value_category();
+  $attr_type->authorised_value_category($authorised_value_category);
 
 Accessor.
 
@@ -353,14 +400,53 @@ sub authorised_value_category {
     @_ ? $self->{'authorised_value_category'} = shift : $self->{'authorised_value_category'};
 }
 
-=head2 delete
+=head2 category_code
+
+my $category_code = $attr_type->category_code();
+$attr_type->category_code($category_code);
+
+Accessor.
+
+=cut
+
+sub category_code {
+    my $self = shift;
+    @_ ? $self->{'category_code'} = shift : $self->{'category_code'};
+}
+
+=head2 category_description
+
+my $category_description = $attr_type->category_description();
+$attr_type->category_description($category_description);
+
+Accessor.
+
+=cut
 
-=over 4
+sub category_description {
+    my $self = shift;
+    @_ ? $self->{'category_description'} = shift : $self->{'category_description'};
+}
 
-$attr_type->delete();
-C4::Members::AttributeTypes->delete($code);
+=head2 class
 
-=back
+my $class = $attr_type->class();
+$attr_type->class($class);
+
+Accessor.
+
+=cut
+
+sub class {
+    my $self = shift;
+    @_ ? $self->{'class'} = shift : $self->{'class'};
+}
+
+
+=head2 delete
+
+  $attr_type->delete();
+  C4::Members::AttributeTypes->delete($code);
 
 Delete an attribute type from the database.  The attribute
 type may be specified either by an object or by a code.
@@ -379,15 +465,12 @@ 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
 
-=over 4
-
-my $count = $attr_type->num_patrons();
-
-=back
+  my $count = $attr_type->num_patrons();
 
 Returns the number of patron records that use
 this attribute type.
@@ -409,11 +492,7 @@ sub num_patrons {
 
 =head2 get_patrons
 
-=over 4
-
-my @borrowernumbers = $attr_type->get_patrons($attribute);
-
-=back
+  my @borrowernumbers = $attr_type->get_patrons($attribute);
 
 Returns the borrowernumber of the patron records that
 have an attribute with the specifie value.
@@ -439,7 +518,7 @@ sub get_patrons {
 
 =head1 AUTHOR
 
-Koha Development Team <info@koha.org>
+Koha Development Team <http://koha-community.org/>
 
 Galen Charlton <galen.charlton@liblime.com>