Bug 16048 [Follow-up] Making notices actions buttons
[koha.git] / C4 / Members.pm
index 2616598..884ee3d 100644 (file)
@@ -36,14 +36,19 @@ use C4::NewsChannels; #get slip news
 use DateTime;
 use Koha::Database;
 use Koha::DateUtils;
-use Koha::Borrower::Debarments qw(IsDebarred);
+use Koha::Patron::Debarments qw(IsDebarred);
 use Text::Unaccent qw( unac_string );
 use Koha::AuthUtils qw(hash_password);
 use Koha::Database;
-require Koha::NorwegianPatronDB;
 
 our ($VERSION,@ISA,@EXPORT,@EXPORT_OK,$debug);
 
+use Module::Load::Conditional qw( can_load );
+if ( ! can_load( modules => { 'Koha::NorwegianPatronDB' => undef } ) ) {
+   $debug && warn "Unable to load Koha::NorwegianPatronDB";
+}
+
+
 BEGIN {
     $VERSION = 3.07.00.049;
     $debug = $ENV{DEBUG} || 0;
@@ -62,21 +67,13 @@ BEGIN {
         &GetPendingIssues
         &GetAllIssues
 
-        &getzipnamecity
-        &getidcity
-
         &GetFirstValidEmailAddress
         &GetNoticeEmailAddress
 
         &GetAge
-        &GetCities
         &GetSortDetails
         &GetTitles
 
-        &GetPatronImage
-        &PutPatronImage
-        &RmPatronImage
-
         &GetHideLostItemsPreference
 
         &IsMemberBlocked
@@ -95,11 +92,6 @@ BEGIN {
         &GetExpiryDate
         &GetUpcomingMembershipExpires
 
-        &AddMessage
-        &DeleteMessage
-        &GetMessages
-        &GetMessagesCount
-
         &IssueSlip
         GetBorrowersWithEmail
 
@@ -111,7 +103,6 @@ BEGIN {
     push @EXPORT, qw(
         &ModMember
         &changepassword
-         &ModPrivacy
     );
 
     #Delete data
@@ -238,16 +229,6 @@ sub GetMemberDetails {
     $borrower->{'flags'}     = $flags;
     $borrower->{'authflags'} = $accessflagshash;
 
-    # For the purposes of making templates easier, we'll define a
-    # 'showname' which is the alternate form the user's first name if 
-    # 'other name' is defined.
-    if ($borrower->{category_type} eq 'I') {
-        $borrower->{'showname'} = $borrower->{'othernames'};
-        $borrower->{'showname'} .= " $borrower->{'firstname'}" if $borrower->{'firstname'};
-    } else {
-        $borrower->{'showname'} = $borrower->{'firstname'};
-    }
-
     # Handle setting the true behavior for BlockExpiredPatronOpacActions
     $borrower->{'BlockExpiredPatronOpacActions'} =
       C4::Context->preference('BlockExpiredPatronOpacActions')
@@ -454,7 +435,7 @@ sub GetMember {
     }
     $debug && warn $select, " ",values %information;
     my $sth = $dbh->prepare("$select");
-    $sth->execute(map{$information{$_}} keys %information);
+    $sth->execute(@values);
     my $data = $sth->fetchall_arrayref({});
     #FIXME interface to this routine now allows generation of a result set
     #so whole array should be returned but bowhere in the current code expects this
@@ -531,7 +512,7 @@ sub IsMemberBlocked {
     my $borrowernumber = shift;
     my $dbh            = C4::Context->dbh;
 
-    my $blockeddate = Koha::Borrower::Debarments::IsDebarred($borrowernumber);
+    my $blockeddate = Koha::Patron::Debarments::IsDebarred($borrowernumber);
 
     return ( 1, $blockeddate ) if $blockeddate;
 
@@ -652,6 +633,7 @@ sub ModMember {
             $data{password} = hash_password($data{password});
         }
     }
+
     my $old_categorycode = GetBorrowerCategorycode( $data{borrowernumber} );
 
     # get only the columns of a borrower
@@ -660,15 +642,19 @@ sub ModMember {
     my $new_borrower = { map { join(' ', @columns) =~ /$_/ ? ( $_ => $data{$_} ) : () } keys(%data) };
     delete $new_borrower->{flags};
 
-    $new_borrower->{dateofbirth}  ||= undef if exists $new_borrower->{dateofbirth};
-    $new_borrower->{dateenrolled} ||= undef if exists $new_borrower->{dateenrolled};
-    $new_borrower->{dateexpiry}   ||= undef if exists $new_borrower->{dateexpiry};
-    $new_borrower->{debarred}     ||= undef if exists $new_borrower->{debarred};
+    $new_borrower->{dateofbirth}     ||= undef if exists $new_borrower->{dateofbirth};
+    $new_borrower->{dateenrolled}    ||= undef if exists $new_borrower->{dateenrolled};
+    $new_borrower->{dateexpiry}      ||= undef if exists $new_borrower->{dateexpiry};
+    $new_borrower->{debarred}        ||= undef if exists $new_borrower->{debarred};
+    $new_borrower->{sms_provider_id} ||= undef if exists $new_borrower->{sms_provider_id};
+
     my $rs = $schema->resultset('Borrower')->search({
         borrowernumber => $new_borrower->{borrowernumber},
      });
+
     my $execute_success = $rs->update($new_borrower);
     if ($execute_success ne '0E0') { # only proceed if the update was a success
+
         # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
         # so when we update information for an adult we should check for guarantees and update the relevant part
         # of their records, ie addresses and phone numbers
@@ -751,6 +737,9 @@ sub AddMember {
       : $patron_category->default_privacy() eq 'never'   ? 2
       : $patron_category->default_privacy() eq 'forever' ? 0
       :                                                    undef;
+
+    $data{'privacy_guarantor_checkouts'} = 0 unless defined( $data{'privacy_guarantor_checkouts'} );
+
     # Make a copy of the plain text password for later use
     my $plain_text_password = $data{'password'};
 
@@ -758,8 +747,9 @@ sub AddMember {
     $data{'password'} = ($data{'password'})? hash_password($data{'password'}) : '!';
 
     # we don't want invalid dates in the db (mysql has a bad habit of inserting 0000-00-00
-    $data{'dateofbirth'} = undef if( not $data{'dateofbirth'} );
-    $data{'debarred'} = undef if ( not $data{'debarred'} );
+    $data{'dateofbirth'}     = undef if ( not $data{'dateofbirth'} );
+    $data{'debarred'}        = undef if ( not $data{'debarred'} );
+    $data{'sms_provider_id'} = undef if ( not $data{'sms_provider_id'} );
 
     # get only the columns of Borrower
     my @columns = $schema->source('Borrower')->columns;
@@ -1006,6 +996,7 @@ sub UpdateGuarantees {
         $sth->execute($data{'address'},$data{'fax'},$data{'B_city'},$data{'mobile'},$data{'city'},$data{'phone'},$guarantee->{'borrowernumber'});
     }
 }
+
 =head2 GetPendingIssues
 
   my $issues = &GetPendingIssues(@borrowernumber);
@@ -1019,7 +1010,6 @@ The keys include C<biblioitems> fields except marc and marcxml.
 
 =cut
 
-#'
 sub GetPendingIssues {
     my @borrowernumbers = @_;
 
@@ -1364,40 +1354,6 @@ sub get_cardnumber_length {
     return ( $min, $max );
 }
 
-=head2 getzipnamecity (OUEST-PROVENCE)
-
-take all info from table city for the fields city and  zip
-check for the name and the zip code of the city selected
-
-=cut
-
-sub getzipnamecity {
-    my ($cityid) = @_;
-    my $dbh      = C4::Context->dbh;
-    my $sth      =
-      $dbh->prepare(
-        "select city_name,city_state,city_zipcode,city_country from cities where cityid=? ");
-    $sth->execute($cityid);
-    my @data = $sth->fetchrow;
-    return $data[0], $data[1], $data[2], $data[3];
-}
-
-
-=head2 getdcity (OUEST-PROVENCE)
-
-recover cityid  with city_name condition
-
-=cut
-
-sub getidcity {
-    my ($city_name) = @_;
-    my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare("select cityid from cities where city_name=? ");
-    $sth->execute($city_name);
-    my $data = $sth->fetchrow;
-    return $data;
-}
-
 =head2 GetFirstValidEmailAddress
 
   $email = GetFirstValidEmailAddress($borrowernumber);
@@ -1704,35 +1660,6 @@ sub SetAge{
     return $borrower;
 }    # sub SetAge
 
-=head2 GetCities
-
-  $cityarrayref = GetCities();
-
-  Returns an array_ref of the entries in the cities table
-  If there are entries in the table an empty row is returned
-  This is currently only used to populate a popup in memberentry
-
-=cut
-
-sub GetCities {
-
-    my $dbh   = C4::Context->dbh;
-    my $city_arr = $dbh->selectall_arrayref(
-        q|SELECT cityid,city_zipcode,city_name,city_state,city_country FROM cities ORDER BY city_name|,
-        { Slice => {} });
-    if ( @{$city_arr} ) {
-        unshift @{$city_arr}, {
-            city_zipcode => q{},
-            city_name    => q{},
-            cityid       => q{},
-            city_state   => q{},
-            city_country => q{},
-        };
-    }
-
-    return  $city_arr;
-}
-
 =head2 GetSortDetails (OUEST-PROVENCE)
 
   ($lib) = &GetSortDetails($category,$sortvalue);
@@ -1899,66 +1826,6 @@ sub GetTitles {
     }
 }
 
-=head2 GetPatronImage
-
-    my ($imagedata, $dberror) = GetPatronImage($borrowernumber);
-
-Returns the mimetype and binary image data of the image for the patron with the supplied borrowernumber.
-
-=cut
-
-sub GetPatronImage {
-    my ($borrowernumber) = @_;
-    warn "Borrowernumber passed to GetPatronImage is $borrowernumber" if $debug;
-    my $dbh = C4::Context->dbh;
-    my $query = 'SELECT mimetype, imagefile FROM patronimage WHERE borrowernumber = ?';
-    my $sth = $dbh->prepare($query);
-    $sth->execute($borrowernumber);
-    my $imagedata = $sth->fetchrow_hashref;
-    warn "Database error!" if $sth->errstr;
-    return $imagedata, $sth->errstr;
-}
-
-=head2 PutPatronImage
-
-    PutPatronImage($cardnumber, $mimetype, $imgfile);
-
-Stores patron binary image data and mimetype in database.
-NOTE: This function is good for updating images as well as inserting new images in the database.
-
-=cut
-
-sub PutPatronImage {
-    my ($cardnumber, $mimetype, $imgfile) = @_;
-    warn "Parameters passed in: Cardnumber=$cardnumber, Mimetype=$mimetype, " . ($imgfile ? "Imagefile" : "No Imagefile") if $debug;
-    my $dbh = C4::Context->dbh;
-    my $query = "INSERT INTO patronimage (borrowernumber, mimetype, imagefile) VALUES ( ( SELECT borrowernumber from borrowers WHERE cardnumber = ? ),?,?) ON DUPLICATE KEY UPDATE imagefile = ?;";
-    my $sth = $dbh->prepare($query);
-    $sth->execute($cardnumber,$mimetype,$imgfile,$imgfile);
-    warn "Error returned inserting $cardnumber.$mimetype." if $sth->errstr;
-    return $sth->errstr;
-}
-
-=head2 RmPatronImage
-
-    my ($dberror) = RmPatronImage($borrowernumber);
-
-Removes the image for the patron with the supplied borrowernumber.
-
-=cut
-
-sub RmPatronImage {
-    my ($borrowernumber) = @_;
-    warn "Borrowernumber passed to GetPatronImage is $borrowernumber" if $debug;
-    my $dbh = C4::Context->dbh;
-    my $query = "DELETE FROM patronimage WHERE borrowernumber = ?;";
-    my $sth = $dbh->prepare($query);
-    $sth->execute($borrowernumber);
-    my $dberror = $sth->errstr;
-    warn "Database error!" if $sth->errstr;
-    return $dberror;
-}
-
 =head2 GetHideLostItemsPreference
 
   $hidelostitemspref = &GetHideLostItemsPreference($borrowernumber);
@@ -2198,130 +2065,6 @@ sub ModPrivacy {
                       privacy        => $privacy );
 }
 
-=head2 AddMessage
-
-  AddMessage( $borrowernumber, $message_type, $message, $branchcode );
-
-Adds a message to the messages table for the given borrower.
-
-Returns:
-  True on success
-  False on failure
-
-=cut
-
-sub AddMessage {
-    my ( $borrowernumber, $message_type, $message, $branchcode ) = @_;
-
-    my $dbh  = C4::Context->dbh;
-
-    if ( ! ( $borrowernumber && $message_type && $message && $branchcode ) ) {
-      return;
-    }
-
-    my $query = "INSERT INTO messages ( borrowernumber, branchcode, message_type, message ) VALUES ( ?, ?, ?, ? )";
-    my $sth = $dbh->prepare($query);
-    $sth->execute( $borrowernumber, $branchcode, $message_type, $message );
-    logaction("MEMBERS", "ADDCIRCMESSAGE", $borrowernumber, $message) if C4::Context->preference("BorrowersLog");
-    return 1;
-}
-
-=head2 GetMessages
-
-  GetMessages( $borrowernumber, $type );
-
-$type is message type, B for borrower, or L for Librarian.
-Empty type returns all messages of any type.
-
-Returns all messages for the given borrowernumber
-
-=cut
-
-sub GetMessages {
-    my ( $borrowernumber, $type, $branchcode ) = @_;
-
-    if ( ! $type ) {
-      $type = '%';
-    }
-
-    my $dbh  = C4::Context->dbh;
-
-    my $query = "SELECT
-                  branches.branchname,
-                  messages.*,
-                  message_date,
-                  messages.branchcode LIKE '$branchcode' AS can_delete
-                  FROM messages, branches
-                  WHERE borrowernumber = ?
-                  AND message_type LIKE ?
-                  AND messages.branchcode = branches.branchcode
-                  ORDER BY message_date DESC";
-    my $sth = $dbh->prepare($query);
-    $sth->execute( $borrowernumber, $type ) ;
-    my @results;
-
-    while ( my $data = $sth->fetchrow_hashref ) {
-        $data->{message_date_formatted} = output_pref( { dt => dt_from_string( $data->{message_date} ), dateonly => 1, dateformat => 'iso' } );
-        push @results, $data;
-    }
-    return \@results;
-
-}
-
-=head2 GetMessages
-
-  GetMessagesCount( $borrowernumber, $type );
-
-$type is message type, B for borrower, or L for Librarian.
-Empty type returns all messages of any type.
-
-Returns the number of messages for the given borrowernumber
-
-=cut
-
-sub GetMessagesCount {
-    my ( $borrowernumber, $type, $branchcode ) = @_;
-
-    if ( ! $type ) {
-      $type = '%';
-    }
-
-    my $dbh  = C4::Context->dbh;
-
-    my $query = "SELECT COUNT(*) as MsgCount FROM messages WHERE borrowernumber = ? AND message_type LIKE ?";
-    my $sth = $dbh->prepare($query);
-    $sth->execute( $borrowernumber, $type ) ;
-    my @results;
-
-    my $data = $sth->fetchrow_hashref;
-    my $count = $data->{'MsgCount'};
-
-    return $count;
-}
-
-
-
-=head2 DeleteMessage
-
-  DeleteMessage( $message_id );
-
-=cut
-
-sub DeleteMessage {
-    my ( $message_id ) = @_;
-
-    my $dbh = C4::Context->dbh;
-    my $query = "SELECT * FROM messages WHERE message_id = ?";
-    my $sth = $dbh->prepare($query);
-    $sth->execute( $message_id );
-    my $message = $sth->fetchrow_hashref();
-
-    $query = "DELETE FROM messages WHERE message_id = ?";
-    $sth = $dbh->prepare($query);
-    $sth->execute( $message_id );
-    logaction("MEMBERS", "DELCIRCMESSAGE", $message->{'borrowernumber'}, $message->{'message'}) if C4::Context->preference("BorrowersLog");
-}
-
 =head2 IssueSlip
 
   IssueSlip($branchcode, $borrowernumber, $quickslip)
@@ -2478,18 +2221,19 @@ sub GetBorrowersWithEmail {
 sub AddMember_Opac {
     my ( %borrower ) = @_;
 
-    $borrower{'categorycode'} = C4::Context->preference('PatronSelfRegistrationDefaultCategory');
-
-    my $sr = new String::Random;
-    $sr->{'A'} = [ 'A'..'Z', 'a'..'z' ];
-    my $password = $sr->randpattern("AAAAAAAAAA");
-    $borrower{'password'} = $password;
+    $borrower{'categorycode'} //= C4::Context->preference('PatronSelfRegistrationDefaultCategory');
+    if (not defined $borrower{'password'}){
+        my $sr = new String::Random;
+        $sr->{'A'} = [ 'A'..'Z', 'a'..'z' ];
+        my $password = $sr->randpattern("AAAAAAAAAA");
+        $borrower{'password'} = $password;
+    }
 
-    $borrower{'cardnumber'} = fixup_cardnumber();
+    $borrower{'cardnumber'} = fixup_cardnumber( $borrower{'cardnumber'} );
 
     my $borrowernumber = AddMember(%borrower);
 
-    return ( $borrowernumber, $password );
+    return ( $borrowernumber, $borrower{'password'} );
 }
 
 =head2 AddEnrolmentFeeIfNeeded