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

(-)a/C4/Letters.pm (-2 / +102 lines)
Lines 31-36 use C4::SMS; Link Here
31
use C4::Debug;
31
use C4::Debug;
32
use Date::Calc qw( Add_Delta_Days );
32
use Date::Calc qw( Add_Delta_Days );
33
use Encode;
33
use Encode;
34
use Unicode::Normalize;
34
use Carp;
35
use Carp;
35
36
36
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
37
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Lines 116-121 sub GetLetters (;$) { Link Here
116
    return \%letters;
117
    return \%letters;
117
}
118
}
118
119
120
=head2 GetLetter( %params )
121
122
    retrieves the letter template
123
124
    %params hash:
125
      module => letter module, mandatory
126
      letter_code => letter code, mandatory
127
      branchcode => for letter selection, if missing default system letter taken
128
    Return value:
129
      letter fields hashref (title & content useful)
130
131
=cut
132
133
sub GetLetter {
134
    my %params = @_;
135
136
    my $module      = $params{module} or croak "No module";
137
    my $letter_code = $params{letter_code} or croak "No letter_code";
138
    my $branchcode  = $params{branchcode} || '';
139
140
    my $letter = getletter( $module, $letter_code, $branchcode )
141
        or warn( "No $module $letter_code letter"),
142
            return;
143
144
    return $letter;
145
}
146
119
my %letter;
147
my %letter;
120
sub getletter ($$$) {
148
sub getletter ($$$) {
121
    my ( $module, $code, $branchcode ) = @_;
149
    my ( $module, $code, $branchcode ) = @_;
Lines 409-414 sub SendAlerts { Link Here
409
437
410
=head2 GetPreparedLetter( %params )
438
=head2 GetPreparedLetter( %params )
411
439
440
    retrieves letter template and performs substituion processing
441
412
    %params hash:
442
    %params hash:
413
      module => letter module, mandatory
443
      module => letter module, mandatory
414
      letter_code => letter code, mandatory
444
      letter_code => letter code, mandatory
Lines 436-449 sub GetPreparedLetter { Link Here
436
    my $module      = $params{module} or croak "No module";
466
    my $module      = $params{module} or croak "No module";
437
    my $letter_code = $params{letter_code} or croak "No letter_code";
467
    my $letter_code = $params{letter_code} or croak "No letter_code";
438
    my $branchcode  = $params{branchcode} || '';
468
    my $branchcode  = $params{branchcode} || '';
469
    my $tables = $params{tables};
470
    my $substitute = $params{substitute};
471
    my $repeat = $params{repeat};
439
472
440
    my $letter = getletter( $module, $letter_code, $branchcode )
473
    my $letter = getletter( $module, $letter_code, $branchcode )
441
        or warn( "No $module $letter_code letter"),
474
        or warn( "No $module $letter_code letter"),
442
            return;
475
            return;
443
476
477
    my $prepared_letter = GetProcessedLetter(
478
        module => $module,
479
        letter_code => $letter_code,
480
        letter => $letter,
481
        branchcode => $branchcode,
482
        tables => $tables,
483
        substitute => $substitute,
484
        repeat => $repeat
485
    );
486
487
    return $prepared_letter;
488
}
489
490
=head2 GetProcessedLetter( %params )
491
492
    given a letter, with possible pre-processing do standard processing
493
    allows one to perform letter template processing beforehand
494
495
    %params hash:
496
      module => letter module, mandatory
497
      letter_code => letter code, mandatory
498
      letter => letter, mandatory
499
      branchcode => for letter selection, if missing default system letter taken
500
      tables => a hashref with table names as keys. Values are either:
501
        - a scalar - primary key value
502
        - an arrayref - primary key values
503
        - a hashref - full record
504
      substitute => custom substitution key/value pairs
505
      repeat => records to be substituted on consecutive lines:
506
        - an arrayref - tries to guess what needs substituting by
507
          taking remaining << >> tokensr; not recommended
508
        - a hashref token => @tables - replaces <token> << >> << >> </token>
509
          subtemplate for each @tables row; table is a hashref as above
510
      want_librarian => boolean,  if set to true triggers librarian details
511
        substitution from the userenv
512
    Return value:
513
      letter fields hashref (title & content useful)
514
515
=cut
516
517
sub GetProcessedLetter {
518
    my %params = @_;
519
520
    my $module      = $params{module} or croak "No module";
521
    my $letter_code = $params{letter_code} or croak "No letter_code";
522
    my $letter = $params{letter} or croak "No letter";
523
    my $branchcode  = $params{branchcode} || '';
444
    my $tables = $params{tables};
524
    my $tables = $params{tables};
445
    my $substitute = $params{substitute};
525
    my $substitute = $params{substitute};
446
    my $repeat = $params{repeat};
526
    my $repeat = $params{repeat};
527
447
    $tables || $substitute || $repeat
528
    $tables || $substitute || $repeat
448
      or carp( "ERROR: nothing to substitute - both 'tables' and 'substitute' are empty" ),
529
      or carp( "ERROR: nothing to substitute - both 'tables' and 'substitute' are empty" ),
449
         return;
530
         return;
Lines 675-680 sub EnqueueLetter ($) { Link Here
675
    return unless exists $params->{'borrowernumber'};
756
    return unless exists $params->{'borrowernumber'};
676
    return unless exists $params->{'message_transport_type'};
757
    return unless exists $params->{'message_transport_type'};
677
758
759
    my $content = $params->{letter}->{content};
760
    $content =~ s/\s+//g if(defined $content);
761
    if ( not defined $content or $content eq '' ) {
762
        warn "Trying to add an empty message to the message queue" if $debug;
763
        return;
764
    }
765
766
    # It was found that the some utf8 codes, cause the text to be truncated from that point onward when stored,
767
    # so we normalize utf8 with NFC so that mysql will store 'all' of the content in its TEXT column type
768
    # Note: It is also done in _add_attachments accordingly.
769
    $params->{'letter'}->{'title'} = NFC($params->{'letter'}->{'title'});     # subject
770
    $params->{'letter'}->{'content'} = NFC($params->{'letter'}->{'content'});
771
678
    # If we have any attachments we should encode then into the body.
772
    # If we have any attachments we should encode then into the body.
679
    if ( $params->{'attachments'} ) {
773
    if ( $params->{'attachments'} ) {
680
        $params->{'letter'} = _add_attachments(
774
        $params->{'letter'} = _add_attachments(
Lines 844-854 sub _add_attachments { Link Here
844
    $message->attach(
938
    $message->attach(
845
        Type => $letter->{'content-type'} || 'TEXT',
939
        Type => $letter->{'content-type'} || 'TEXT',
846
        Data => $letter->{'is_html'}
940
        Data => $letter->{'is_html'}
847
            ? _wrap_html($letter->{'content'}, $letter->{'title'})
941
            ? _wrap_html($letter->{'content'}, NFC($letter->{'title'}))
848
            : $letter->{'content'},
942
            : NFC($letter->{'content'}),
849
    );
943
    );
850
944
851
    foreach my $attachment ( @$attachments ) {
945
    foreach my $attachment ( @$attachments ) {
946
947
        if ($attachment->{'content'} =~ m/text/o) { # NFC normailze any "text" related  content-type attachments
948
            $attachment->{'content'} = NFC($attachment->{'content'});
949
        }
950
        $attachment->{'filename'} = NFC($attachment->{'filename'});
951
852
        $message->attach(
952
        $message->attach(
853
            Type     => $attachment->{'type'},
953
            Type     => $attachment->{'type'},
854
            Data     => $attachment->{'content'},
954
            Data     => $attachment->{'content'},
(-)a/misc/cronjobs/overdue_notices.pl (-5 / +13 lines)
Lines 419-424 END_SQL Link Here
419
                next PERIOD;
419
                next PERIOD;
420
            }
420
            }
421
421
422
            my $letter_template = C4::Letters::GetLetter (
423
                module => 'circulation',
424
                letter_code => $overdue_rules->{"letter$i"},
425
                branchcode => $branchcode
426
            );
427
422
            # $letter->{'content'} is the text of the mail that is sent.
428
            # $letter->{'content'} is the text of the mail that is sent.
423
            # this text contains fields that are replaced by their value. Those fields must be written between brackets
429
            # this text contains fields that are replaced by their value. Those fields must be written between brackets
424
            # The following fields are available :
430
            # The following fields are available :
Lines 490-495 END_SQL Link Here
490
496
491
                my $letter = parse_letter(
497
                my $letter = parse_letter(
492
                    {   letter_code     => $overdue_rules->{"letter$i"},
498
                    {   letter_code     => $overdue_rules->{"letter$i"},
499
                        letter          => $letter_template,
493
                        borrowernumber  => $borrowernumber,
500
                        borrowernumber  => $borrowernumber,
494
                        branchcode      => $branchcode,
501
                        branchcode      => $branchcode,
495
                        items           => \@items,
502
                        items           => \@items,
Lines 589-599 END_SQL Link Here
589
            
596
            
590
        my $attachment = {
597
        my $attachment = {
591
            filename => defined $csvfilename ? 'attachment.csv' : 'attachment.txt',
598
            filename => defined $csvfilename ? 'attachment.csv' : 'attachment.txt',
592
            type => 'text/plain',
599
            type => 'text/plain; charset="utf-8"',
593
            content => $content, 
600
            content => $content, 
594
        };
601
        };
595
602
596
        my $letter = {
603
        my $letter = {
604
            'content-type' => 'text/plain; charset="utf-8"',
597
            title   => 'Overdue Notices',
605
            title   => 'Overdue Notices',
598
            content => 'These messages were not sent directly to the patrons.',
606
            content => 'These messages were not sent directly to the patrons.',
599
        };
607
        };
Lines 658-664 sub parse_letter { Link Here
658
    my $currency_format;
666
    my $currency_format;
659
    if ($params->{'letter'}->{'content'} =~ m/<fine>(.*)<\/fine>/o) { # process any fine tags...
667
    if ($params->{'letter'}->{'content'} =~ m/<fine>(.*)<\/fine>/o) { # process any fine tags...
660
        $currency_format = $1;
668
        $currency_format = $1;
661
        $params->{'letter'}->{'content'} =~ s/<fine>.*<\/fine>/<<item.fine>>/o;
669
        $params->{'letter'}->{'content'} =~ s/<fine>.*<\/fine>/<<items.fine>>/o;
662
    }
670
    }
663
671
664
    my @item_tables;
672
    my @item_tables;
Lines 683-695 sub parse_letter { Link Here
683
        }
691
        }
684
    }
692
    }
685
693
686
    return C4::Letters::GetPreparedLetter (
694
    return C4::Letters::GetProcessedLetter (
687
        module => 'circulation',
695
        module => 'circulation',
688
        letter_code => $params->{'letter_code'},
696
        letter_code => $params->{'letter_code'},
697
        letter => $params->{'letter'},
689
        branchcode => $params->{'branchcode'},
698
        branchcode => $params->{'branchcode'},
690
        tables => \%tables,
699
        tables => \%tables,
691
        substitute => $substitute,
700
        substitute => $substitute,
692
        repeat => { item => \@item_tables },
701
        repeat => { item => \@item_tables }
693
    );
702
    );
694
}
703
}
695
704
696
- 

Return to bug 8378