Bug 20444: Remove sub GetAttributes
[koha.git] / C4 / Members / Attributes.pm
index 24c6f71..cf9051d 100644 (file)
@@ -4,18 +4,18 @@ package C4::Members::Attributes;
 #
 # 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;
@@ -24,16 +24,15 @@ use Text::CSV;      # Don't be tempted to use Text::CSV::Unicode -- even in bina
 use C4::Context;
 use C4::Members::AttributeTypes;
 
-use vars qw($VERSION @ISA @EXPORT_OK @EXPORT %EXPORT_TAGS);
+use vars qw(@ISA @EXPORT_OK @EXPORT %EXPORT_TAGS);
 our ($csv, $AttributeTypes);
 
 BEGIN {
-    # set the version for version checking
-    $VERSION = 3.01;
     @ISA = qw(Exporter);
     @EXPORT_OK = qw(GetBorrowerAttributes GetBorrowerAttributeValue CheckUniqueness SetBorrowerAttributes
+                    DeleteBorrowerAttribute UpdateBorrowerAttribute
                     extended_attributes_code_value_arrayref extended_attributes_merge
-                                       SearchIdMatchingAttribute);
+                    SearchIdMatchingAttribute);
     %EXPORT_TAGS = ( all => \@EXPORT_OK );
 }
 
@@ -60,7 +59,6 @@ code (attribute type code)
 description (attribute type description)
 value (attribute value)
 value_description (attribute value description (if associated with an authorised value))
-password (password, if any, associated with attribute
 
 If the C<$opac_only> parameter is present and has a true value, only the attributes
 marked for OPAC display are returned.
@@ -72,7 +70,7 @@ sub GetBorrowerAttributes {
     my $opac_only = @_ ? shift : 0;
 
     my $dbh = C4::Context->dbh();
-    my $query = "SELECT code, description, attribute, lib, password
+    my $query = "SELECT code, description, attribute, lib, display_checkout, category_code, class
                  FROM borrower_attributes
                  JOIN borrower_attribute_types USING (code)
                  LEFT JOIN authorised_values ON (category = authorised_value_category AND attribute = authorised_value)
@@ -86,11 +84,14 @@ sub GetBorrowerAttributes {
         push @results, {
             code              => $row->{'code'},
             description       => $row->{'description'},
-            value             => $row->{'attribute'},  
-            value_description => $row->{'lib'},  
-            password          => $row->{'password'},
+            value             => $row->{'attribute'},
+            value_description => $row->{'lib'},
+            display_checkout  => $row->{'display_checkout'},
+            category_code     => $row->{'category_code'},
+            class             => $row->{'class'},
         }
     }
+    $sth->finish;
     return \@results;
 }
 
@@ -118,23 +119,24 @@ sub GetBorrowerAttributeValue {
 
 =head2 SearchIdMatchingAttribute
 
-  my $matching_records = C4::Members::Attributes::SearchIdMatchingAttribute($filter);
+  my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($filter);
 
 =cut
 
 sub SearchIdMatchingAttribute{
     my $filter = shift;
-    my $finalfilter=$filter->[0];
+    $filter = [$filter] unless ref $filter;
+
     my $dbh   = C4::Context->dbh();
     my $query = qq{
-SELECT borrowernumber
+SELECT DISTINCT borrowernumber
 FROM borrower_attributes
 JOIN borrower_attribute_types USING (code)
 WHERE staff_searchable = 1
-AND attribute like ?};
+AND (} . join (" OR ", map "attribute like ?", @$filter) .qq{)};
     my $sth = $dbh->prepare_cached($query);
-    $sth->execute("%$finalfilter%");
-    return $sth->fetchall_arrayref;
+    $sth->execute(map "%$_%", @$filter);
+    return [map $_->[0], @{ $sth->fetchall_arrayref }];
 }
 
 =head2 CheckUniqueness
@@ -183,7 +185,7 @@ sub CheckUniqueness {
 
 =head2 SetBorrowerAttributes 
 
-  SetBorrowerAttributes($borrowernumber, [ { code => 'CODE', value => 'value', password => 'password' }, ... ] );
+  SetBorrowerAttributes($borrowernumber, [ { code => 'CODE', value => 'value' }, ... ] );
 
 Set patron attributes for the patron identified by C<$borrowernumber>,
 replacing any that existed previously.
@@ -193,19 +195,99 @@ replacing any that existed previously.
 sub SetBorrowerAttributes {
     my $borrowernumber = shift;
     my $attr_list = shift;
+    my $no_branch_limit = shift // 0;
 
     my $dbh = C4::Context->dbh;
-    my $delsth = $dbh->prepare("DELETE FROM borrower_attributes WHERE borrowernumber = ?");
-    $delsth->execute($borrowernumber);
 
-    my $sth = $dbh->prepare("INSERT INTO borrower_attributes (borrowernumber, code, attribute, password)
-                             VALUES (?, ?, ?, ?)");
+    DeleteBorrowerAttributes( $borrowernumber, $no_branch_limit );
+
+    my $sth = $dbh->prepare("INSERT INTO borrower_attributes (borrowernumber, code, attribute)
+                             VALUES (?, ?, ?)");
     foreach my $attr (@$attr_list) {
-        $attr->{password} = undef unless exists $attr->{password};
-        $sth->execute($borrowernumber, $attr->{code}, $attr->{value}, $attr->{password});
+        $sth->execute($borrowernumber, $attr->{code}, $attr->{value});
+        if ($sth->err) {
+            warn sprintf('Database returned the following error: %s', $sth->errstr);
+            return; # bail immediately on errors
+        }
     }
+    return 1; # borrower attributes successfully set
+}
+
+=head2 DeleteBorrowerAttributes
+
+  DeleteBorrowerAttributes($borrowernumber);
+
+Delete borrower attributes for the patron identified by C<$borrowernumber>.
+
+=cut
+
+sub DeleteBorrowerAttributes {
+    my $borrowernumber = shift;
+    my $no_branch_limit = @_ ? shift : 0;
+    my $branch_limit = $no_branch_limit
+        ? 0
+        : C4::Context->userenv ? C4::Context->userenv->{"branch"} : 0;
+
+    my $dbh = C4::Context->dbh;
+    my $query = q{
+        DELETE borrower_attributes FROM borrower_attributes
+        };
+
+    $query .= $branch_limit
+        ? q{
+            LEFT JOIN borrower_attribute_types_branches ON bat_code = code
+            WHERE ( b_branchcode = ? OR b_branchcode IS NULL )
+                AND borrowernumber = ?
+        }
+        : q{
+            WHERE borrowernumber = ?
+        };
+
+    $dbh->do( $query, undef, $branch_limit ? $branch_limit : (), $borrowernumber );
+}
+
+=head2 DeleteBorrowerAttribute
+
+  DeleteBorrowerAttribute($borrowernumber, $attribute);
+
+Delete a borrower attribute for the patron identified by C<$borrowernumber> and the attribute code of C<$attribute>
+
+=cut
+
+sub DeleteBorrowerAttribute {
+    my ( $borrowernumber, $attribute ) = @_;
+
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare(qq{
+        DELETE FROM borrower_attributes
+            WHERE borrowernumber = ?
+            AND code = ?
+    } );
+    $sth->execute( $borrowernumber, $attribute->{code} );
+}
+
+=head2 UpdateBorrowerAttribute
+
+  UpdateBorrowerAttribute($borrowernumber, $attribute );
+
+Update a borrower attribute C<$attribute> for the patron identified by C<$borrowernumber>,
+
+=cut
+
+sub UpdateBorrowerAttribute {
+    my ( $borrowernumber, $attribute ) = @_;
+
+    DeleteBorrowerAttribute $borrowernumber, $attribute;
+
+    my $dbh = C4::Context->dbh;
+    my $query = "INSERT INTO borrower_attributes SET attribute = ?, code = ?, borrowernumber = ?";
+    my @params = ( $attribute->{attribute}, $attribute->{code}, $borrowernumber );
+    my $sth = $dbh->prepare( $query );
+
+    $sth->execute( @params );
 }
 
+
 =head2 extended_attributes_code_value_arrayref 
 
    my $patron_attributes = "homeroom:1150605,grade:01,extradata:foobar";