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

(-)a/C4/Letters.pm (-2 / +95 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 682-687 sub EnqueueLetter { Link Here
682
        return;
763
        return;
683
    }
764
    }
684
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
685
    # 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.
686
    if ( $params->{'attachments'} ) {
773
    if ( $params->{'attachments'} ) {
687
        $params->{'letter'} = _add_attachments(
774
        $params->{'letter'} = _add_attachments(
Lines 852-862 sub _add_attachments { Link Here
852
    $message->attach(
939
    $message->attach(
853
        Type => $letter->{'content-type'} || 'TEXT',
940
        Type => $letter->{'content-type'} || 'TEXT',
854
        Data => $letter->{'is_html'}
941
        Data => $letter->{'is_html'}
855
            ? _wrap_html($letter->{'content'}, $letter->{'title'})
942
            ? _wrap_html($letter->{'content'}, NFC($letter->{'title'}))
856
            : $letter->{'content'},
943
            : NFC($letter->{'content'}),
857
    );
944
    );
858
945
859
    foreach my $attachment ( @$attachments ) {
946
    foreach my $attachment ( @$attachments ) {
947
948
        if ($attachment->{'content'} =~ m/text/o) { # NFC normailze any "text" related  content-type attachments
949
            $attachment->{'content'} = NFC($attachment->{'content'});
950
        }
951
        $attachment->{'filename'} = NFC($attachment->{'filename'});
952
860
        $message->attach(
953
        $message->attach(
861
            Type     => $attachment->{'type'},
954
            Type     => $attachment->{'type'},
862
            Data     => $attachment->{'content'},
955
            Data     => $attachment->{'content'},
(-)a/misc/cronjobs/overdue_notices.pl (-5 / +13 lines)
Lines 432-437 END_SQL Link Here
432
                next PERIOD;
432
                next PERIOD;
433
            }
433
            }
434
434
435
            my $letter_template = C4::Letters::GetLetter (
436
                module => 'circulation',
437
                letter_code => $overdue_rules->{"letter$i"},
438
                branchcode => $branchcode
439
            );
440
435
            # $letter->{'content'} is the text of the mail that is sent.
441
            # $letter->{'content'} is the text of the mail that is sent.
436
            # this text contains fields that are replaced by their value. Those fields must be written between brackets
442
            # this text contains fields that are replaced by their value. Those fields must be written between brackets
437
            # The following fields are available :
443
            # The following fields are available :
Lines 503-508 END_SQL Link Here
503
509
504
                my $letter = parse_letter(
510
                my $letter = parse_letter(
505
                    {   letter_code     => $overdue_rules->{"letter$i"},
511
                    {   letter_code     => $overdue_rules->{"letter$i"},
512
                        letter          => $letter_template,
506
                        borrowernumber  => $borrowernumber,
513
                        borrowernumber  => $borrowernumber,
507
                        branchcode      => $branchcode,
514
                        branchcode      => $branchcode,
508
                        items           => \@items,
515
                        items           => \@items,
Lines 602-612 END_SQL Link Here
602
            
609
            
603
        my $attachment = {
610
        my $attachment = {
604
            filename => defined $csvfilename ? 'attachment.csv' : 'attachment.txt',
611
            filename => defined $csvfilename ? 'attachment.csv' : 'attachment.txt',
605
            type => 'text/plain',
612
            type => 'text/plain; charset="utf-8"',
606
            content => $content, 
613
            content => $content, 
607
        };
614
        };
608
615
609
        my $letter = {
616
        my $letter = {
617
            'content-type' => 'text/plain; charset="utf-8"',
610
            title   => 'Overdue Notices',
618
            title   => 'Overdue Notices',
611
            content => 'These messages were not sent directly to the patrons.',
619
            content => 'These messages were not sent directly to the patrons.',
612
        };
620
        };
Lines 671-677 sub parse_letter { Link Here
671
    my $currency_format;
679
    my $currency_format;
672
    if ($params->{'letter'}->{'content'} =~ m/<fine>(.*)<\/fine>/o) { # process any fine tags...
680
    if ($params->{'letter'}->{'content'} =~ m/<fine>(.*)<\/fine>/o) { # process any fine tags...
673
        $currency_format = $1;
681
        $currency_format = $1;
674
        $params->{'letter'}->{'content'} =~ s/<fine>.*<\/fine>/<<item.fine>>/o;
682
        $params->{'letter'}->{'content'} =~ s/<fine>.*<\/fine>/<<items.fine>>/o;
675
    }
683
    }
676
684
677
    my @item_tables;
685
    my @item_tables;
Lines 696-708 sub parse_letter { Link Here
696
        }
704
        }
697
    }
705
    }
698
706
699
    return C4::Letters::GetPreparedLetter (
707
    return C4::Letters::GetProcessedLetter (
700
        module => 'circulation',
708
        module => 'circulation',
701
        letter_code => $params->{'letter_code'},
709
        letter_code => $params->{'letter_code'},
710
        letter => $params->{'letter'},
702
        branchcode => $params->{'branchcode'},
711
        branchcode => $params->{'branchcode'},
703
        tables => \%tables,
712
        tables => \%tables,
704
        substitute => $substitute,
713
        substitute => $substitute,
705
        repeat => { item => \@item_tables },
714
        repeat => { item => \@item_tables }
706
    );
715
    );
707
}
716
}
708
717
709
- 

Return to bug 8378