View | Details | Raw Unified | Return to bug 33360
Collapse All | Expand All

(-)a/C4/Letters.pm (-30 / +46 lines)
Lines 954-975 ENDSQL Link Here
954
=head2 SendQueuedMessages ([$hashref]) 
954
=head2 SendQueuedMessages ([$hashref]) 
955
955
956
    my $sent = SendQueuedMessages({
956
    my $sent = SendQueuedMessages({
957
        letter_code => $letter_code,
957
        message_id => $id,
958
        borrowernumber => $who_letter_is_for,
958
        borrowernumber => $who_letter_is_for,
959
        letter_code => $letter_code, # can be scalar or arrayref
960
        type => $type, # can be scalar or arrayref
959
        limit => 50,
961
        limit => 50,
960
        verbose => 1,
962
        verbose => 1,
961
        type => 'sms',
963
        where => $where,
962
    });
964
    });
963
965
964
Sends all of the 'pending' items in the message queue, unless
966
Sends 'pending' messages from the queue, based on parameters.
965
parameters are passed.
966
967
967
The letter_code, borrowernumber and limit parameters are used
968
The (optional) message_id, borrowernumber, letter_code, type and where
968
to build a parameter set for _get_unsent_messages, thus limiting
969
parameter are used to select which pending messages will be processed. The
969
which pending messages will be processed. They are all optional.
970
limit parameter determines the volume of results, i.e. sent messages.
970
971
971
The verbose parameter can be used to generate debugging output.
972
The optional verbose parameter can be used to generate debugging output.
972
It is also optional.
973
973
974
Returns number of messages sent.
974
Returns number of messages sent.
975
975
Lines 977-995 Returns number of messages sent. Link Here
977
977
978
sub SendQueuedMessages {
978
sub SendQueuedMessages {
979
    my $params = shift;
979
    my $params = shift;
980
    my $limit = $params->{limit};
981
    my $where = $params->{where};
982
983
    my $count_messages = 0;
984
    my $unsent_messages = Koha::Notice::Messages->search({
985
        status  => 'pending',
986
        $params->{message_id} ? ( message_id => $params->{message_id} ) : (),
987
        $params->{borrowernumber} ? ( borrowernumber => $params->{borrowernumber} ) : (),
988
        # Check for scalar or array in letter_code and type
989
        ref($params->{letter_code}) && @{$params->{letter_code}} ? ( letter_code => $params->{letter_code} ) : (),
990
        !ref($params->{letter_code}) && $params->{letter_code} ? ( letter_code => $params->{letter_code} ) : (),
991
        ref($params->{type}) && @{$params->{type}} ? ( message_transport_type => $params->{type} ) : (), #TODO Existing inconsistency
992
        !ref($params->{type}) && $params->{type} ? ( message_transport_type => $params->{type} ) : (), #TODO Existing inconsistency
993
    });
994
    $unsent_messages = $unsent_messages->search( \$where ) if $where;
980
995
981
    my $which_unsent_messages = {
982
        'message_id'             => $params->{'message_id'},
983
        'limit'                  => $params->{'limit'} // 0,
984
        'borrowernumber'         => $params->{'borrowernumber'} // q{},
985
        'letter_code'            => $params->{'letter_code'} // q{},
986
        'message_transport_type' => $params->{'type'} // q{},
987
        'where'                  => $params->{'where'} // q{},
988
    };
989
    my $unsent_messages = _get_unsent_messages( $which_unsent_messages );
990
    $domain_limits = Koha::Notice::Util->load_domain_limits; # (re)initialize per run
996
    $domain_limits = Koha::Notice::Util->load_domain_limits; # (re)initialize per run
991
    MESSAGE: foreach my $message ( @$unsent_messages ) {
997
    while( ( my $message_object = $unsent_messages->next ) && ( !$limit || $count_messages < $limit ) ) {
992
        my $message_object = Koha::Notice::Messages->find( $message->{message_id} );
998
        my $message = $message_object->unblessed;
999
993
        # If this fails the database is unwritable and we won't manage to send a message that continues to be marked 'pending'
1000
        # If this fails the database is unwritable and we won't manage to send a message that continues to be marked 'pending'
994
        $message_object->make_column_dirty('status');
1001
        $message_object->make_column_dirty('status');
995
        return unless $message_object->store;
1002
        return unless $message_object->store;
Lines 1000-1008 sub SendQueuedMessages { Link Here
1000
                      $message->{'borrowernumber'} || 'Admin' )
1007
                      $message->{'borrowernumber'} || 'Admin' )
1001
          if $params->{'verbose'};
1008
          if $params->{'verbose'};
1002
        # This is just begging for subclassing
1009
        # This is just begging for subclassing
1003
        next MESSAGE if ( lc($message->{'message_transport_type'}) eq 'rss' );
1010
        next if ( lc($message->{'message_transport_type'}) eq 'rss' );
1004
        if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
1011
        if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
1005
            _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1012
            my $rv = _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1013
            $count_messages++ if $rv;
1006
        }
1014
        }
1007
        elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
1015
        elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
1008
            if ( C4::Context->preference('SMSSendDriver') eq 'Email' ) {
1016
            if ( C4::Context->preference('SMSSendDriver') eq 'Email' ) {
Lines 1011-1022 sub SendQueuedMessages { Link Here
1011
                unless ( $sms_provider ) {
1019
                unless ( $sms_provider ) {
1012
                    warn sprintf( "Patron %s has no sms provider id set!", $message->{'borrowernumber'} ) if $params->{'verbose'};
1020
                    warn sprintf( "Patron %s has no sms provider id set!", $message->{'borrowernumber'} ) if $params->{'verbose'};
1013
                    _set_message_status( { message_id => $message->{'message_id'}, status => 'failed' } );
1021
                    _set_message_status( { message_id => $message->{'message_id'}, status => 'failed' } );
1014
                    next MESSAGE;
1022
                    next;
1015
                }
1023
                }
1016
                unless ( $patron->smsalertnumber ) {
1024
                unless ( $patron->smsalertnumber ) {
1017
                    _set_message_status( { message_id => $message->{'message_id'}, status => 'failed' } );
1025
                    _set_message_status( { message_id => $message->{'message_id'}, status => 'failed' } );
1018
                    warn sprintf( "No smsalertnumber found for patron %s!", $message->{'borrowernumber'} ) if $params->{'verbose'};
1026
                    warn sprintf( "No smsalertnumber found for patron %s!", $message->{'borrowernumber'} ) if $params->{'verbose'};
1019
                    next MESSAGE;
1027
                    next;
1020
                }
1028
                }
1021
                $message->{to_address}  = $patron->smsalertnumber; #Sometime this is set to email - sms should always use smsalertnumber
1029
                $message->{to_address}  = $patron->smsalertnumber; #Sometime this is set to email - sms should always use smsalertnumber
1022
                $message->{to_address} .= '@' . $sms_provider->domain();
1030
                $message->{to_address} .= '@' . $sms_provider->domain();
Lines 1029-1041 sub SendQueuedMessages { Link Here
1029
                }
1037
                }
1030
1038
1031
                _update_message_to_address($message->{'message_id'}, $message->{to_address});
1039
                _update_message_to_address($message->{'message_id'}, $message->{to_address});
1032
                _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1040
                my $rv = _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1041
                $count_messages++ if $rv;
1033
            } else {
1042
            } else {
1034
                _send_message_by_sms( $message );
1043
                my $rv = _send_message_by_sms( $message );
1044
                $count_messages++ if $rv;
1035
            }
1045
            }
1036
        }
1046
        }
1037
    }
1047
    }
1038
    return scalar( @$unsent_messages );
1048
    return $count_messages;
1039
}
1049
}
1040
1050
1041
=head2 GetRSSMessages
1051
=head2 GetRSSMessages
Lines 1307-1315 sub _send_message_by_email { Link Here
1307
    my $message = shift or return;
1317
    my $message = shift or return;
1308
    my ($username, $password, $method) = @_;
1318
    my ($username, $password, $method) = @_;
1309
1319
1310
    my $patron = Koha::Patrons->find( $message->{borrowernumber} );
1320
    my $patron;
1311
    my $to_address = $message->{'to_address'};
1321
    my $to_address = $message->{'to_address'};
1312
    unless ($to_address) {
1322
    unless ($to_address) {
1323
        $patron = Koha::Patrons->find( $message->{borrowernumber} );
1313
        unless ($patron) {
1324
        unless ($patron) {
1314
            warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1325
            warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1315
            _set_message_status(
1326
            _set_message_status(
Lines 1337-1343 sub _send_message_by_email { Link Here
1337
    }
1348
    }
1338
1349
1339
    # Skip this message if we exceed domain limits in this run
1350
    # Skip this message if we exceed domain limits in this run
1340
    return if Koha::Notice::Util->exceeds_limit({ to => $to_address, limits => $domain_limits });
1351
    if( Koha::Notice::Util->exceeds_limit({ to => $to_address, limits => $domain_limits }) ) {
1352
        # Save the to_address if you delay the message so that we dont need to look it up again
1353
        _update_message_to_address( $message->{'message_id'}, $to_address )
1354
            if !$message->{to_address};
1355
        return;
1356
    }
1341
1357
1342
    my $subject = $message->{'subject'};
1358
    my $subject = $message->{'subject'};
1343
1359
Lines 1350-1355 sub _send_message_by_email { Link Here
1350
    my $branch_returnpath = undef;
1366
    my $branch_returnpath = undef;
1351
    my $library;
1367
    my $library;
1352
1368
1369
    $patron //= Koha::Patrons->find( $message->{borrowernumber} ); # we might already found him
1353
    if ($patron) {
1370
    if ($patron) {
1354
        $library           = $patron->library;
1371
        $library           = $patron->library;
1355
        $branch_email      = $library->from_email_address;
1372
        $branch_email      = $library->from_email_address;
1356
- 

Return to bug 33360