Bug 7941 : Fix version numbers in modules
[koha.git] / C4 / Members / Attributes.pm
index 88c6c09..e175915 100644 (file)
@@ -13,9 +13,9 @@ package C4::Members::Attributes;
 # 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;
@@ -29,10 +29,12 @@ our ($csv, $AttributeTypes);
 
 BEGIN {
     # set the version for version checking
-    $VERSION = 3.01;
+    $VERSION = 3.07.00.049;
     @ISA = qw(Exporter);
-    @EXPORT_OK = qw(GetBorrowerAttributes CheckUniqueness SetBorrowerAttributes
-                    extended_attributes_code_value_arrayref extended_attributes_merge);
+    @EXPORT_OK = qw(GetBorrowerAttributes GetBorrowerAttributeValue CheckUniqueness SetBorrowerAttributes
+                    DeleteBorrowerAttribute UpdateBorrowerAttribute
+                    extended_attributes_code_value_arrayref extended_attributes_merge
+                    SearchIdMatchingAttribute);
     %EXPORT_TAGS = ( all => \@EXPORT_OK );
 }
 
@@ -42,22 +44,14 @@ C4::Members::Attributes - manage extend patron attributes
 
 =head1 SYNOPSIS
 
-=over 4
-
-    use C4::Members::Attributes;
-    my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
-
-=back
+  use C4::Members::Attributes;
+  my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
 
 =head1 FUNCTIONS
 
 =head2 GetBorrowerAttributes
 
-=over 4
-
-my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber[, $opac_only]);
-
-=back
+  my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber[, $opac_only]);
 
 Retrieve an arrayref of extended attributes associated with the
 patron specified by C<$borrowernumber>.  Each entry in the arrayref
@@ -79,7 +73,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, password, 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)
@@ -93,21 +87,82 @@ sub GetBorrowerAttributes {
         push @results, {
             code              => $row->{'code'},
             description       => $row->{'description'},
-            value             => $row->{'attribute'},  
-            value_description => $row->{'lib'},  
+            value             => $row->{'attribute'},
+            value_description => $row->{'lib'},
             password          => $row->{'password'},
+            display_checkout  => $row->{'display_checkout'},
+            category_code     => $row->{'category_code'},
+            class             => $row->{'class'},
         }
     }
     return \@results;
 }
 
-=head2 CheckUniqueness
+=head2 GetAttributes
+
+  my $attributes = C4::Members::Attributes::GetAttributes([$opac_only]);
 
-=over 4
+Retrieve an arrayref of extended attribute codes
+
+=cut
 
-    my $ok = CheckUniqueness($code, $value[, $borrowernumber]);
+sub GetAttributes {
+    my ($opac_only) = @_;
 
-=back
+    my $dbh = C4::Context->dbh();
+    my $query = "SELECT code FROM borrower_attribute_types";
+    $query .= "\nWHERE opac_display = 1" if $opac_only;
+    $query .= "\nORDER BY code";
+    return $dbh->selectcol_arrayref($query);
+}
+
+=head2 GetBorrowerAttributeValue
+
+  my $value = C4::Members::Attributes::GetBorrowerAttributeValue($borrowernumber, $attribute_code);
+
+Retrieve the value of an extended attribute C<$attribute_code> associated with the
+patron specified by C<$borrowernumber>.
+
+=cut
+
+sub GetBorrowerAttributeValue {
+    my $borrowernumber = shift;
+    my $code = shift;
+
+    my $dbh = C4::Context->dbh();
+    my $query = "SELECT attribute
+                 FROM borrower_attributes
+                 WHERE borrowernumber = ?
+                 AND code = ?";
+    my $value = $dbh->selectrow_array($query, undef, $borrowernumber, $code);
+    return $value;
+}
+
+=head2 SearchIdMatchingAttribute
+
+  my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($filter);
+
+=cut
+
+sub SearchIdMatchingAttribute{
+    my $filter = shift;
+    $filter = [$filter] unless ref $filter;
+
+    my $dbh   = C4::Context->dbh();
+    my $query = qq{
+SELECT DISTINCT borrowernumber
+FROM borrower_attributes
+JOIN borrower_attribute_types USING (code)
+WHERE staff_searchable = 1
+AND (} . join (" OR ", map "attribute like ?", @$filter) .qq{)};
+    my $sth = $dbh->prepare_cached($query);
+    $sth->execute(map "%$_%", @$filter);
+    return [map $_->[0], @{ $sth->fetchall_arrayref }];
+}
+
+=head2 CheckUniqueness
+
+  my $ok = CheckUniqueness($code, $value[, $borrowernumber]);
 
 Given an attribute type and value, verify if would violate
 a unique_id restriction if added to the patron.  The
@@ -151,11 +206,7 @@ sub CheckUniqueness {
 
 =head2 SetBorrowerAttributes 
 
-=over 4
-
-    SetBorrowerAttributes($borrowernumber, [ { code => 'CODE', value => 'value', password => 'password' }, ... ] );
-
-=back
+  SetBorrowerAttributes($borrowernumber, [ { code => 'CODE', value => 'value', password => 'password' }, ... ] );
 
 Set patron attributes for the patron identified by C<$borrowernumber>,
 replacing any that existed previously.
@@ -175,17 +226,62 @@ sub SetBorrowerAttributes {
     foreach my $attr (@$attr_list) {
         $attr->{password} = undef unless exists $attr->{password};
         $sth->execute($borrowernumber, $attr->{code}, $attr->{value}, $attr->{password});
+        if ($sth->err) {
+            warn sprintf('Database returned the following error: %s', $sth->errstr);
+            return; # bail immediately on errors
+        }
     }
+    return 1; # borower attributes successfully set
 }
 
-=head2 extended_attributes_code_value_arrayref 
+=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} );
+}
 
-=over 4
+=head2 UpdateBorrowerAttribute
 
-    my $patron_attributes = "homeroom:1150605,grade:01,extradata:foobar";
-    my $aref = extended_attributes_code_value_arrayref($patron_attributes);
+  UpdateBorrowerAttribute($borrowernumber, $attribute );
 
-=back
+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 );
+    if ( defined $attribute->{password} ) {
+        $query .= ", password = ?";
+        push @params, $attribute->{password};
+    }
+    my $sth = $dbh->prepare( $query );
+
+    $sth->execute( @params );
+}
+
+
+=head2 extended_attributes_code_value_arrayref 
+
+   my $patron_attributes = "homeroom:1150605,grade:01,extradata:foobar";
+   my $aref = extended_attributes_code_value_arrayref($patron_attributes);
 
 Takes a comma-delimited CSV-style string argument and returns the kind of data structure that SetBorrowerAttributes wants, 
 namely a reference to array of hashrefs like:
@@ -211,16 +307,12 @@ sub extended_attributes_code_value_arrayref {
 
 =head2 extended_attributes_merge
 
-=over 4
-
-    my $old_attributes = extended_attributes_code_value_arrayref("homeroom:224,grade:04,deanslist:2007,deanslist:2008,somedata:xxx");
-    my $new_attributes = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2009,extradata:foobar");
-    my $merged = extended_attributes_merge($patron_attributes, $new_attributes, 1);
-
-    # assuming deanslist is a repeatable code, value same as:
-    # $merged = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2007,deanslist:2008,deanslist:2009,extradata:foobar,somedata:xxx");
+  my $old_attributes = extended_attributes_code_value_arrayref("homeroom:224,grade:04,deanslist:2007,deanslist:2008,somedata:xxx");
+  my $new_attributes = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2009,extradata:foobar");
+  my $merged = extended_attributes_merge($patron_attributes, $new_attributes, 1);
 
-=back
+  # assuming deanslist is a repeatable code, value same as:
+  # $merged = extended_attributes_code_value_arrayref("homeroom:115,grade:05,deanslist:2007,deanslist:2008,deanslist:2009,extradata:foobar,somedata:xxx");
 
 Takes three arguments.  The first two are references to array of hashrefs, each like:
  [ { code => 'CODE', value => 'value' }, { code => 'CODE2', value => 'othervalue' } ... ]
@@ -265,7 +357,7 @@ sub _sort_by_code {
 
 =head1 AUTHOR
 
-Koha Development Team <info@koha.org>
+Koha Development Team <http://koha-community.org/>
 
 Galen Charlton <galen.charlton@liblime.com>