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

(-)a/C4/Biblio.pm (+83 lines)
Lines 95-100 use C4::Charset; Link Here
95
use C4::Linker;
95
use C4::Linker;
96
use C4::OAI::Sets;
96
use C4::OAI::Sets;
97
use C4::Debug;
97
use C4::Debug;
98
use C4::Ris;
98
99
99
use Koha::Caches;
100
use Koha::Caches;
100
use Koha::Authority::Types;
101
use Koha::Authority::Types;
Lines 3417-3422 sub RemoveAllNsb { Link Here
3417
    return $record;
3418
    return $record;
3418
}
3419
}
3419
3420
3421
=head2 BuildBiblioDataForExport
3422
3423
    my $data = BuildBiblioDataForExport({
3424
        biblionumbers => \@biblionumbers,
3425
        format => $format,
3426
        template => $template, # Only used for ISBD format
3427
        csv_profile_id => $csv_profile_id, # Only used for CSV format
3428
3429
Returns a hashref with the following keys:
3430
3431
=over
3432
3433
=item * records_file  A string containing the export file contents
3434
3435
=item * records_data  An array of hashrefs for all biblio records
3436
3437
=back
3438
3439
=cut
3440
3441
sub BuildBiblioDataForExport {
3442
    my ( $params ) = @_;
3443
    my $biblionumbers = $params->{biblionumbers};
3444
    my $format = $params->{format};
3445
    my $template = $params->{template}; # Only used for ISBD
3446
    my ( @records_data, $records_file );
3447
3448
    # CSV
3449
    if ($format eq 'csv' ) {
3450
3451
        # FIXME should be used at the beginning of this module
3452
        # But compilation explodes...
3453
        require C4::Record;
3454
        $records_file = C4::Record::marc2csv($biblionumbers, $params->{csv_profile_id});
3455
3456
    # Other formats
3457
    } else {
3458
        # retrieve biblios from shelf
3459
        my $marcflavour = C4::Context->preference('marcflavour');
3460
        foreach my $biblionumber (@$biblionumbers) {
3461
            my $dat              = GetBiblioData($biblionumber);
3462
            next unless $dat;
3463
            my $record           = GetMarcBiblio({ biblionumber => $biblionumber, embed_items => 1 });
3464
            my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
3465
            my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
3466
            my $fw               = GetFrameworkCode($biblionumber);
3467
3468
            my @items = C4::Items::GetItemsInfo( $biblionumber );
3469
3470
            my $hasauthors = 0;
3471
            if($dat->{'author'} || @$marcauthorsarray) {
3472
              $hasauthors = 1;
3473
            }
3474
3475
            $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
3476
            $dat->{MARCAUTHORS}    = $marcauthorsarray;
3477
            $dat->{HASAUTHORS}     = $hasauthors;
3478
            $dat->{biblionumber}   = $biblionumber;
3479
            $dat->{ITEM_RESULTS}   = \@items;
3480
3481
            if ($format eq 'iso2709') {
3482
                $records_file .= $record->as_usmarc();
3483
            }
3484
            elsif ($format eq 'ris') {
3485
                $records_file .= C4::Ris::marc2ris($record);
3486
            }
3487
            elsif ($format eq 'bibtex') {
3488
                require C4::Record;
3489
                $records_file .= C4::Record::marc2bibtex($record, $biblionumber);
3490
            }
3491
            elsif ( $format eq 'isbd' ) {
3492
                # Only called from the OPAC at the moment
3493
                $records_file   .= C4::Biblio::GetISBDView($biblionumber, $template);
3494
            }
3495
3496
            push( @records_data, $dat );
3497
        }
3498
    }
3499
3500
    return { records_file => $records_file, records_data => \@records_data };
3501
}
3502
3420
1;
3503
1;
3421
3504
3422
3505
(-)a/Koha/Email.pm (+106 lines)
Lines 20-25 package Koha::Email; Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
use Email::Valid;
21
use Email::Valid;
22
use Email::MessageID;
22
use Email::MessageID;
23
use Encode qw( encode );
24
use MIME::QuotedPrint;
25
use MIME::Base64;
23
26
24
use base qw(Class::Accessor);
27
use base qw(Class::Accessor);
25
use C4::Context;
28
use C4::Context;
Lines 39-44 Koha::Email Link Here
39
42
40
=head1 FUNCTIONS
43
=head1 FUNCTIONS
41
44
45
=head2 create_message_headers
46
47
    my %mail = $email->create_message_headers({
48
        from => $from,
49
        to => $to,
50
        charset => $charset,
51
        cc => $cc,
52
        bcc => $bcc,
53
        replyto => $replyto,
54
        sender => $sender,
55
        message => $message,
56
        subject => $subject,
57
        contenttype => $contenttype,
58
    });
59
60
    sendmail %mail or die "Error sending mail: $Mail::Sendmail::error";
61
42
=cut
62
=cut
43
63
44
sub create_message_headers {
64
sub create_message_headers {
Lines 73-78 sub create_message_headers { Link Here
73
    $mail{'Content-Type'} = $params->{contenttype} if $params->{contenttype};
93
    $mail{'Content-Type'} = $params->{contenttype} if $params->{contenttype};
74
    $mail{'X-Mailer'}     = "Koha";
94
    $mail{'X-Mailer'}     = "Koha";
75
    $mail{'Message-ID'}   = Email::MessageID->new->in_brackets;
95
    $mail{'Message-ID'}   = Email::MessageID->new->in_brackets;
96
76
    return %mail;
97
    return %mail;
77
}
98
}
99
100
=head2 fill_body_for_sending
101
102
    my %mail = $email->create_message_headers({
103
        to => $to,
104
    });
105
106
    %mail = (%mail, $email->fill_body_for_sending({
107
        template => $template,
108
        default_filename => $default_filename,
109
        attachment => $attachment,
110
    });
111
112
    sendmail %mail or die "Error sending mail: $Mail::Sendmail::error";
113
114
=cut
115
116
sub fill_body_for_sending {
117
    my ( $self, $params ) = @_;
118
    my $template          = $params->{template};
119
    my $default_filename  = $params->{default_filename};
120
    my $attachment        = $params->{attachment};
121
122
    my %mail;
123
124
    # Analysing information and getting mail properties
125
    if ( $template =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
126
        $mail{Subject} = $1;
127
        $mail{Subject} =~ s|\n?(.*)\n?|$1|;
128
        $mail{Subject} = Encode::encode("UTF-8", $mail{Subject});
129
    }
130
    else { $mail{Subject} = "no subject"; }
131
132
    my $email_header = "";
133
    if ( $template =~ /<HEADER>(.*)<END_HEADER>/s ) {
134
        $email_header = $1;
135
        $email_header =~ s|\n?(.*)\n?|$1|;
136
        $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
137
    }
138
139
    if ( $template =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
140
        $default_filename = $1;
141
        $default_filename =~ s|\n?(.*)\n?|$1|;
142
    }
143
144
    my $body;
145
    if ( $template =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
146
        $body = $1;
147
        $body =~ s|\n?(.*)\n?|$1|;
148
        $body = encode_qp(Encode::encode("UTF-8", $body));
149
    }
150
151
    my $boundary = "====" . time() . "====";
152
153
    # We set and put the multipart content
154
    $mail{'Content-Type'} = qq|multipart/mixed; boundary="$boundary"|;
155
156
    # If the attachment is a csv file, it's already encoded
157
    # See C4::Record::marc2csv which calls C4::Record::marcrecord2csv
158
    unless ( $default_filename =~ m|.csv$| ) {
159
        $attachment = Encode::encode( "UTF-8", $attachment )
160
    }
161
    my $file = encode_base64( $attachment );
162
    $boundary = '--' . $boundary;
163
164
    $mail{body} = <<END_OF_BODY;
165
$boundary
166
MIME-Version: 1.0
167
Content-Type: text/plain; charset="utf-8"
168
Content-Transfer-Encoding: quoted-printable
169
170
$email_header
171
$body
172
$boundary
173
Content-Type: application/octet-stream; name="$default_filename"
174
Content-Transfer-Encoding: base64
175
Content-Disposition: attachment; filename="$default_filename"
176
177
$file
178
$boundary--
179
END_OF_BODY
180
181
    return %mail;
182
}
183
78
1;
184
1;
(-)a/basket/basket.pl (-1 lines)
Lines 15-21 Link Here
15
# You should have received a copy of the GNU General Public License
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
17
18
19
use Modern::Perl;
18
use Modern::Perl;
20
use CGI qw ( -utf8 );
19
use CGI qw ( -utf8 );
21
use C4::Koha;
20
use C4::Koha;
(-)a/basket/downloadcart.pl (-1 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use CGI qw ( -utf8 );
22
use CGI qw ( -utf8 );
23
use Encode qw(encode);
24
23
25
use C4::Auth;
24
use C4::Auth;
26
use C4::Biblio;
25
use C4::Biblio;
(-)a/basket/sendbasket.pl (-86 / +23 lines)
Lines 18-28 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use CGI qw ( -utf8 );
20
use CGI qw ( -utf8 );
21
use Encode qw(encode);
22
use Carp;
21
use Carp;
23
use Mail::Sendmail;
22
use Mail::Sendmail;
24
use MIME::QuotedPrint;
25
use MIME::Base64;
26
23
27
use C4::Biblio;
24
use C4::Biblio;
28
use C4::Items;
25
use C4::Items;
Lines 31-36 use C4::Output; Link Here
31
use C4::Templates ();
28
use C4::Templates ();
32
use Koha::Email;
29
use Koha::Email;
33
use Koha::Token;
30
use Koha::Token;
31
use Koha::CsvProfiles;
34
32
35
my $query = new CGI;
33
my $query = new CGI;
36
34
Lines 46-51 my ( $template, $borrowernumber, $cookie ) = get_template_and_user ( Link Here
46
44
47
my $bib_list     = $query->param('bib_list') || '';
45
my $bib_list     = $query->param('bib_list') || '';
48
my $email_add    = $query->param('email_add');
46
my $email_add    = $query->param('email_add');
47
my $format       = $query->param('format') || 'iso2709';
49
48
50
my $dbh          = C4::Context->dbh;
49
my $dbh          = C4::Context->dbh;
51
50
Lines 65-162 if ( $email_add ) { Link Here
65
        'basket/sendbasket.tt', 'intranet', $query,
64
        'basket/sendbasket.tt', 'intranet', $query,
66
    );
65
    );
67
66
68
    my @bibs = split( /\//, $bib_list );
67
    my $csv_profile_id;
69
    my @results;
68
    if ( $format =~ m|^\d+$| ) {
70
    my $iso2709;
69
        $csv_profile_id = $format;
71
    my $marcflavour = C4::Context->preference('marcflavour');
70
        $format         = 'csv';
72
    foreach my $biblionumber (@bibs) {
73
        $template2->param( biblionumber => $biblionumber );
74
75
        my $dat              = GetBiblioData($biblionumber);
76
        next unless $dat;
77
        my $record           = GetMarcBiblio({
78
            biblionumber => $biblionumber,
79
            embed_items => 1 });
80
        my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
81
        my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
82
83
        my @items = GetItemsInfo( $biblionumber );
84
85
        my $hasauthors = 0;
86
        if($dat->{'author'} || @$marcauthorsarray) {
87
          $hasauthors = 1;
88
        }
89
	
90
91
        $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
92
        $dat->{MARCAUTHORS}    = $marcauthorsarray;
93
        $dat->{HASAUTHORS}     = $hasauthors;
94
        $dat->{'biblionumber'} = $biblionumber;
95
        $dat->{ITEM_RESULTS}   = \@items;
96
97
        $iso2709 .= $record->as_usmarc();
98
99
        push( @results, $dat );
100
    }
71
    }
72
    my @biblionumbers = split( /\//, $bib_list );
73
    my $data = C4::Biblio::BuildBiblioDataForExport(
74
        {
75
            biblionumbers  => \@biblionumbers,
76
            format         => $format,
77
            csv_profile_id => $csv_profile_id
78
        }
79
    );
80
    my $records_file = $data->{records_file};
81
    my $records_data = $data->{records_data};
101
82
102
    my $resultsarray = \@results;
103
    $template2->param(
83
    $template2->param(
104
        BIBLIO_RESULTS => $resultsarray,
84
        BIBLIO_RESULTS => $records_data,
105
        comment        => $comment
85
        comment        => $comment
106
    );
86
    );
107
87
108
    # Getting template result
88
    # Getting template result
109
    my $template_res = $template2->output();
89
    my $template_res = $template2->output();
110
    my $body;
111
112
    # Analysing information and getting mail properties
113
    if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
114
        $mail{subject} = $1;
115
        $mail{subject} =~ s|\n?(.*)\n?|$1|;
116
        $mail{subject} = encode('MIME-Header',$mail{subject});
117
    }
118
    else { $mail{'subject'} = "no subject"; }
119
120
    my $email_header = "";
121
    if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
122
        $email_header = $1;
123
        $email_header =~ s|\n?(.*)\n?|$1|;
124
        $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
125
    }
126
127
    my $email_file = "basket.txt";
128
    if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
129
        $email_file = $1;
130
        $email_file =~ s|\n?(.*)\n?|$1|;
131
    }
132
133
    if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
134
        $body = $1;
135
        $body =~ s|\n?(.*)\n?|$1|;
136
        $body = encode_qp(Encode::encode("UTF-8", $body));
137
    }
138
139
    my $boundary = "====" . time() . "====";
140
141
    # Writing mail
142
    $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
143
    my $isofile = encode_base64(encode("UTF-8", $iso2709));
144
    $boundary = '--' . $boundary;
145
    $mail{body} = <<END_OF_BODY;
146
$boundary
147
Content-Type: text/plain; charset="utf-8"
148
Content-Transfer-Encoding: quoted-printable
149
150
$email_header
151
$body
152
$boundary
153
Content-Type: application/octet-stream; name="basket.iso2709"
154
Content-Transfer-Encoding: base64
155
Content-Disposition: attachment; filename="basket.iso2709"
156
90
157
$isofile
91
    %mail = (%mail, $email->fill_body_for_sending({
158
$boundary--
92
        template => $template_res,
159
END_OF_BODY
93
        default_filename => "basket.$format",
94
        attachment => $records_file,
95
    }));
160
96
161
    # Sending mail
97
    # Sending mail
162
    if ( sendmail %mail ) {
98
    if ( sendmail %mail ) {
Lines 178-183 else { Link Here
178
        suggestion     => C4::Context->preference("suggestion"),
114
        suggestion     => C4::Context->preference("suggestion"),
179
        virtualshelves => C4::Context->preference("virtualshelves"),
115
        virtualshelves => C4::Context->preference("virtualshelves"),
180
        csrf_token     => Koha::Token->new->generate_csrf({ session_id => scalar $query->cookie('CGISESSID'), }),
116
        csrf_token     => Koha::Token->new->generate_csrf({ session_id => scalar $query->cookie('CGISESSID'), }),
117
        csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc' }) ],
181
    );
118
    );
182
    output_html_with_http_headers $query, $cookie, $template->output;
119
    output_html_with_http_headers $query, $cookie, $template->output;
183
}
120
}
(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/download-export-available-formats.inc (+9 lines)
Line 0 Link Here
1
<select name="format" id="dlformat">
2
    <option value="">-- Choose format --</option>
3
    <option value="ris">RIS (Zotero, EndNote, others)</option>
4
    <option value="bibtex">BibTeX</option>
5
    <option value="iso2709">MARC</option>
6
    [% FOREACH csv_profile IN csv_profiles %]
7
        <option value="[% csv_profile.export_format_id | html %]">CSV - [% csv_profile.profile | html %]</option>
8
    [% END %]
9
</select>
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/basket/downloadcart.tt (-12 / +5 lines)
Lines 9-26 Link Here
9
<form method="post" action="/cgi-bin/koha/basket/downloadcart.pl">
9
<form method="post" action="/cgi-bin/koha/basket/downloadcart.pl">
10
<fieldset class="rows">
10
<fieldset class="rows">
11
	<legend>Download cart</legend>
11
	<legend>Download cart</legend>
12
	<ol><li>
12
    <ol>
13
	<label for="format">Format:</label>
13
        <li>
14
        <select name="format" id="format">
14
            <label for="format">Format:</label>
15
        <option value="">-- Choose format --</option>
15
            [% INCLUDE 'download-export-available-formats.inc' %]
16
        <option value="iso2709">MARC</option>
16
        </li>
17
	    <option value="ris">RIS</option>
18
	    <option value="bibtex">BibTex</option>
19
	    [% FOREACH csv_profile IN csv_profiles %]
20
	    <option value="[% csv_profile.export_format_id | html %]">CSV - [% csv_profile.profile | html %]</option>
21
	    [% END %]
22
23
	</select></li>
24
	</ol>
17
	</ol>
25
	</fieldset>
18
	</fieldset>
26
	<fieldset class="action">
19
	<fieldset class="action">
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/basket/sendbasket.tt (-1 / +5 lines)
Lines 32-38 Your cart Link Here
32
        <li>
32
        <li>
33
            <span>
33
            <span>
34
                [% BIBLIO_RESULT.title | $raw %]
34
                [% BIBLIO_RESULT.title | $raw %]
35
                [% IF ( BIBLIO_RESULT.subtitle ) %] [% BIBLIO_RESULT.subtitle | $raw %][% END %]
35
                [% IF ( BIBLIO_RESULT.subtitle.size ) %]
36
                  [% FOREACH subtitle IN BIBLIO_RESULT.subtitle %]
37
                    [% subtitle.subfield | $raw %]
38
                  [% END %]
39
                [% END %]
36
                [% BIBLIO_RESULT.part_number | $raw %] [% BIBLIO_RESULT.part_name | $raw %]
40
                [% BIBLIO_RESULT.part_number | $raw %] [% BIBLIO_RESULT.part_name | $raw %]
37
            </span>
41
            </span>
38
42
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/basket/sendbasketform.tt (-12 / +16 lines)
Lines 18-35 Link Here
18
18
19
[% INCLUDE 'blocking_errors.inc' %]
19
[% INCLUDE 'blocking_errors.inc' %]
20
<form action="/cgi-bin/koha/basket/sendbasket.pl" method="post">
20
<form action="/cgi-bin/koha/basket/sendbasket.pl" method="post">
21
21
    <fieldset class="rows">
22
<fieldset class="rows"> 
22
        <legend>Sending your cart</legend>
23
<legend>Sending your cart</legend>
23
        <ol>
24
<ol>   <li>
24
            <li>
25
        <label for="email_add">Email address:</label>
25
                <label for="email_add">Email address:</label>
26
        <input type="text" id="email_add" name="email_add" size="43" class="focus" />
26
                <input type="text" id="email_add" name="email_add" size="43" class="focus" />
27
    </li>
27
            </li>
28
    <li>
28
            <li>
29
            <label for="comment">Comment:</label>
29
                <label for="comment">Comment:</label>
30
            <textarea id="comment" name="comment" rows="4" cols="40"></textarea>
30
                <textarea id="comment" name="comment" rows="4" cols="40"></textarea>
31
    </li>
31
            </li>
32
    </ol>
32
            <li>
33
                <label for="format">Format:</label>
34
                [% INCLUDE 'download-export-available-formats.inc' %]
35
            </li>
36
        </ol>
33
    </fieldset>
37
    </fieldset>
34
    <fieldset class="action"> <input type="submit" value="Send" /> <a class="cancel close" href="#">Cancel</a> </fieldset>
38
    <fieldset class="action"> <input type="submit" value="Send" /> <a class="cancel close" href="#">Cancel</a> </fieldset>
35
    <input type="hidden" name="bib_list" value="[% bib_list | html %]" />
39
    <input type="hidden" name="bib_list" value="[% bib_list | html %]" />
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/downloadshelf.tt (-12 / +6 lines)
Lines 26-43 Link Here
26
<form method="post" action="/cgi-bin/koha/virtualshelves/downloadshelf.pl">
26
<form method="post" action="/cgi-bin/koha/virtualshelves/downloadshelf.pl">
27
	<fieldset class="rows">
27
	<fieldset class="rows">
28
    <legend>Download list</legend>
28
    <legend>Download list</legend>
29
	<ol><li>
29
    <ol>
30
    <label for="format">Format: </label>
30
        <li>
31
        <select name="format" id="format">
31
            <label for="format">Format: </label>
32
        <option value="">-- Choose format --</option>
32
            [% INCLUDE 'download-export-available-formats.inc' %]
33
        <option value="iso2709">MARC</option>
33
        </li>
34
	    <option value="ris">RIS</option>
34
    </ol>
35
	    <option value="bibtex">BibTex</option>
36
	    [% FOREACH csv_profile IN csv_profiles %]
37
	    <option value="[% csv_profile.export_format_id | html %]">CSV - [% csv_profile.profile | html %]</option>
38
	    [% END %]
39
	</select>
40
	</li></ol>
41
	</fieldset>
35
	</fieldset>
42
	<fieldset class="action"><input type="hidden" name="shelfid" value="[% shelfid | html %]" />
36
	<fieldset class="action"><input type="hidden" name="shelfid" value="[% shelfid | html %]" />
43
	<input type="submit" name="save" value="Save" />  <a class="cancel close" href="#">Cancel</a>
37
	<input type="submit" name="save" value="Save" />  <a class="cancel close" href="#">Cancel</a>
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/virtualshelves/sendshelfform.tt (+4 lines)
Lines 27-32 Link Here
27
            <label for="comment">Comment:</label>
27
            <label for="comment">Comment:</label>
28
            <textarea id="comment" name="comment" rows="4" cols="40"></textarea>
28
            <textarea id="comment" name="comment" rows="4" cols="40"></textarea>
29
    </li>
29
    </li>
30
    <li>
31
            <label for="comment">Format:</label>
32
            [% INCLUDE 'download-export-available-formats.inc' %]
33
    </li>
30
    <li>
34
    <li>
31
        <input type="hidden" name="shelfid" value="[% shelfid | html %]" />
35
        <input type="hidden" name="shelfid" value="[% shelfid | html %]" />
32
    </li></ol></fieldset>
36
    </li></ol></fieldset>
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/includes/download-export-available-formats.inc (+10 lines)
Line 0 Link Here
1
<select name="format" id="dlformat">
2
    <option value="">-- Choose format --</option>
3
    <option value="ris">RIS (Zotero, EndNote, others)</option>
4
    <option value="bibtex">BibTeX</option>
5
    <option value="isbd">ISBD</option>
6
    <option value="iso2709">MARC</option>
7
    [% FOREACH csv_profile IN csv_profiles %]
8
        <option value="[% csv_profile.export_format_id | html %]">CSV - [% csv_profile.profile | html %]</option>
9
    [% END %]
10
</select>
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-downloadcart.tt (-10 / +1 lines)
Lines 11-26 Link Here
11
                    <div id="userdownloadcart">
11
                    <div id="userdownloadcart">
12
                        <h1>Download cart</h1>
12
                        <h1>Download cart</h1>
13
                        <form method="post" action="/cgi-bin/koha/opac-downloadcart.pl">
13
                        <form method="post" action="/cgi-bin/koha/opac-downloadcart.pl">
14
                            <select name="format" id="format">
14
                            [% INCLUDE 'download-export-available-formats.inc' %]
15
                                <option value="">-- Choose format --</option>
16
                                <option value="ris">RIS (Zotero, EndNote, others)</option>
17
                                <option value="bibtex">BibTeX</option>
18
                                <option value="isbd">ISBD</option>
19
                                <option value="iso2709">MARC</option>
20
                                [% FOREACH csv_profile IN csv_profiles %]
21
                                    <option value="[% csv_profile.export_format_id | html %]">CSV - [% csv_profile.profile | html %]</option>
22
                                [% END %]
23
                            </select>
24
                            <fieldset class="action">
15
                            <fieldset class="action">
25
                                <input type="hidden" name="bib_list" value="[% bib_list | html %]" />
16
                                <input type="hidden" name="bib_list" value="[% bib_list | html %]" />
26
                                <input type="submit" name="save" value="Go" />
17
                                <input type="submit" name="save" value="Go" />
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-downloadshelf.tt (-10 / +1 lines)
Lines 50-65 Link Here
50
                                <h1>Download list <i>[% shelf.shelfname | html %]</i></h1>
50
                                <h1>Download list <i>[% shelf.shelfname | html %]</i></h1>
51
                                <form method="post" action="/cgi-bin/koha/opac-downloadshelf.pl">
51
                                <form method="post" action="/cgi-bin/koha/opac-downloadshelf.pl">
52
                                    <fieldset>
52
                                    <fieldset>
53
                                        <select name="format" id="dlformat" required="required">
53
                                        [% INCLUDE 'download-export-available-formats.inc' %]
54
                                            <option value="">-- Choose format --</option>
55
                                            <option value="ris">RIS (Zotero, EndNote, others)</option>
56
                                            <option value="bibtex">BibTeX</option>
57
                                            <option value="isbd">ISBD</option>
58
                                            <option value="iso2709">MARC</option>
59
                                            [% FOREACH csv_profile IN csv_profiles %]
60
                                            <option value="[% csv_profile.export_format_id | html %]">CSV - [% csv_profile.profile | html %]</option>
61
                                            [% END %]
62
                                        </select>
63
                                        <span class="required">Required</span>
54
                                        <span class="required">Required</span>
64
                                    </fieldset>
55
                                    </fieldset>
65
56
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-sendbasketform.tt (+2 lines)
Lines 36-41 Link Here
36
                                    <textarea id="comment" name="comment" rows="4" cols="40"></textarea>
36
                                    <textarea id="comment" name="comment" rows="4" cols="40"></textarea>
37
                                    <input type="hidden" name="bib_list" value="[% bib_list | html %]" />
37
                                    <input type="hidden" name="bib_list" value="[% bib_list | html %]" />
38
                                    <input type="hidden" name="csrf_token" value="[% csrf_token | html %]" />
38
                                    <input type="hidden" name="csrf_token" value="[% csrf_token | html %]" />
39
                                    <label for="format">Format:</label>
40
                                    [% INCLUDE 'download-export-available-formats.inc' %]
39
                                </fieldset>
41
                                </fieldset>
40
                                [% IF Koha.Preference('RequestOnOpac') || Koha.Preference('OpacRenewalAllowed') %]<p id="donotrequestbymail">Please do not use this mail to request or renew books.</p>[% END %]
42
                                [% IF Koha.Preference('RequestOnOpac') || Koha.Preference('OpacRenewalAllowed') %]<p id="donotrequestbymail">Please do not use this mail to request or renew books.</p>[% END %]
41
                                <fieldset class="action">
43
                                <fieldset class="action">
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-sendshelfform.tt (+3 lines)
Lines 39-44 Link Here
39
                                        <label for="comment">Comment:</label>
39
                                        <label for="comment">Comment:</label>
40
                                        <textarea id="comment" name="comment" rows="4" cols="40"></textarea>
40
                                        <textarea id="comment" name="comment" rows="4" cols="40"></textarea>
41
41
42
                                        <label for="format">Format:</label>
43
                                        [% INCLUDE 'download-export-available-formats.inc' %]
44
42
                                        <input type="hidden" name="shelfid" value="[% shelfid | html %]" />
45
                                        <input type="hidden" name="shelfid" value="[% shelfid | html %]" />
43
                                    </fieldset>
46
                                    </fieldset>
44
                                    [% IF Koha.Preference('RequestOnOpac') || Koha.Preference('OpacRenewalAllowed') %]<p id="donotrequestbymail">Please do not use this mail to request or renew books.</p>[% END %]
47
                                    [% IF Koha.Preference('RequestOnOpac') || Koha.Preference('OpacRenewalAllowed') %]<p id="donotrequestbymail">Please do not use this mail to request or renew books.</p>[% END %]
(-)a/opac/opac-downloadcart.pl (-6 / +3 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use CGI qw ( -utf8 );
22
use CGI qw ( -utf8 );
23
use Encode qw(encode);
24
23
25
use C4::Auth;
24
use C4::Auth;
26
use C4::Biblio;
25
use C4::Biblio;
Lines 63-74 if ($bib_list && $format) { Link Here
63
    my $extension;
62
    my $extension;
64
    my $type;
63
    my $type;
65
64
66
    # CSV   
65
    # CSV
67
    if ($format =~ /^\d+$/) {
66
    if ($format =~ /^\d+$/) {
68
67
69
        $output = marc2csv(\@bibs, $format);
68
        $output = marc2csv(\@bibs, $format);
69
        $format = 'csv';
70
70
71
        # Other formats
71
    # Other formats
72
    } else {
72
    } else {
73
        my $record_processor = Koha::RecordProcessor->new({
73
        my $record_processor = Koha::RecordProcessor->new({
74
            filters => 'ViewPolicy'
74
            filters => 'ViewPolicy'
Lines 111-119 if ($bib_list && $format) { Link Here
111
        }
111
        }
112
    }
112
    }
113
113
114
    # If it was a CSV export we change the format after the export so the file extension is fine
115
    $format = "csv" if ($format =~ m/^\d+$/);
116
117
    print $query->header(
114
    print $query->header(
118
                               -type => ($type) ? $type : 'application/octet-stream',
115
                               -type => ($type) ? $type : 'application/octet-stream',
119
        -'Content-Transfer-Encoding' => 'binary',
116
        -'Content-Transfer-Encoding' => 'binary',
(-)a/opac/opac-downloadshelf.pl (-4 / +3 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use CGI qw ( -utf8 );
22
use CGI qw ( -utf8 );
23
use Encode qw(encode);
24
23
25
use C4::Auth;
24
use C4::Auth;
26
use C4::Biblio;
25
use C4::Biblio;
Lines 80-85 if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) { Link Here
80
                push @biblios, $content->biblionumber;
79
                push @biblios, $content->biblionumber;
81
            }
80
            }
82
            $output = marc2csv(\@biblios, $format);
81
            $output = marc2csv(\@biblios, $format);
82
83
            $format = 'csv';
84
83
        # Other formats
85
        # Other formats
84
        } else {
86
        } else {
85
            my $record_processor = Koha::RecordProcessor->new({
87
            my $record_processor = Koha::RecordProcessor->new({
Lines 122-130 if ( $shelf and $shelf->can_be_viewed( $borrowernumber ) ) { Link Here
122
            }
124
            }
123
        }
125
        }
124
126
125
        # If it was a CSV export we change the format after the export so the file extension is fine
126
        $format = "csv" if ($format =~ m/^\d+$/);
127
128
        print $query->header(
127
        print $query->header(
129
                                   -type => ($type) ? $type : 'application/octet-stream',
128
                                   -type => ($type) ? $type : 'application/octet-stream',
130
            -'Content-Transfer-Encoding' => 'binary',
129
            -'Content-Transfer-Encoding' => 'binary',
(-)a/opac/opac-sendbasket.pl (-94 / +26 lines)
Lines 20-30 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use CGI qw ( -utf8 );
22
use CGI qw ( -utf8 );
23
use Encode qw(encode);
24
use Carp;
23
use Carp;
25
use Mail::Sendmail;
24
use Mail::Sendmail;
26
use MIME::QuotedPrint;
27
use MIME::Base64;
28
25
29
use C4::Biblio;
26
use C4::Biblio;
30
use C4::Items;
27
use C4::Items;
Lines 35-40 use C4::Templates (); Link Here
35
use Koha::Email;
32
use Koha::Email;
36
use Koha::Patrons;
33
use Koha::Patrons;
37
use Koha::Token;
34
use Koha::Token;
35
use Koha::CsvProfiles;
38
36
39
my $query = new CGI;
37
my $query = new CGI;
40
38
Lines 49-54 my ( $template, $borrowernumber, $cookie ) = get_template_and_user ( Link Here
49
47
50
my $bib_list     = $query->param('bib_list') || '';
48
my $bib_list     = $query->param('bib_list') || '';
51
my $email_add    = $query->param('email_add');
49
my $email_add    = $query->param('email_add');
50
my $format       = $query->param('format') || 'iso2709';
52
51
53
my $dbh          = C4::Context->dbh;
52
my $dbh          = C4::Context->dbh;
54
53
Lines 79-124 if ( $email_add ) { Link Here
79
        'opac-sendbasket.tt', 'opac', $query,
78
        'opac-sendbasket.tt', 'opac', $query,
80
    );
79
    );
81
80
82
    my @bibs = split( /\//, $bib_list );
81
    my $csv_profile_id;
83
    my @results;
82
    if ( $format =~ m|^\d+$| ) {
84
    my $iso2709;
83
        $csv_profile_id = $format;
85
    my $marcflavour = C4::Context->preference('marcflavour');
84
        $format         = 'csv';
86
    foreach my $biblionumber (@bibs) {
87
        $template2->param( biblionumber => $biblionumber );
88
89
        my $dat              = GetBiblioData($biblionumber);
90
        next unless $dat;
91
        my $record           = GetMarcBiblio({
92
            biblionumber => $biblionumber,
93
            embed_items  => 1,
94
            opac         => 1,
95
            borcat       => $borcat });
96
        my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
97
        my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
98
99
        my @items = GetItemsInfo( $biblionumber );
100
101
        my $hasauthors = 0;
102
        if($dat->{'author'} || @$marcauthorsarray) {
103
          $hasauthors = 1;
104
        }
105
	
106
107
        $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
108
        $dat->{MARCAUTHORS}    = $marcauthorsarray;
109
        $dat->{HASAUTHORS}     = $hasauthors;
110
        $dat->{'biblionumber'} = $biblionumber;
111
        $dat->{ITEM_RESULTS}   = \@items;
112
113
        $iso2709 .= $record->as_usmarc();
114
115
        push( @results, $dat );
116
    }
85
    }
86
    my @biblionumbers = split( /\//, $bib_list );
87
    my $data = C4::Biblio::BuildBiblioDataForExport(
88
        {
89
            biblionumbers  => \@biblionumbers,
90
            format         => $format,
91
            csv_profile_id => $csv_profile_id,
92
            template       => 'opac',
93
        }
94
    );
95
    my $records_file = $data->{records_file};
96
    my $records_data = $data->{records_data};
117
97
118
    my $resultsarray = \@results;
119
    
120
    $template2->param(
98
    $template2->param(
121
        BIBLIO_RESULTS => $resultsarray,
99
        BIBLIO_RESULTS => $records_data,
122
        comment        => $comment,
100
        comment        => $comment,
123
        firstname      => $patron->firstname,
101
        firstname      => $patron->firstname,
124
        surname        => $patron->surname,
102
        surname        => $patron->surname,
Lines 126-193 if ( $email_add ) { Link Here
126
104
127
    # Getting template result
105
    # Getting template result
128
    my $template_res = $template2->output();
106
    my $template_res = $template2->output();
129
    my $body;
130
131
    # Analysing information and getting mail properties
132
133
    if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
134
        $mail{subject} = $1;
135
        $mail{subject} =~ s|\n?(.*)\n?|$1|;
136
        $mail{subject} = encode('MIME-Header',$mail{subject});
137
    }
138
    else { $mail{'subject'} = "no subject"; }
139
140
    my $email_header = "";
141
    if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
142
        $email_header = $1;
143
        $email_header =~ s|\n?(.*)\n?|$1|;
144
        $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
145
    }
146
147
    my $email_file = "basket.txt";
148
    if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
149
        $email_file = $1;
150
        $email_file =~ s|\n?(.*)\n?|$1|;
151
    }
152
153
    if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
154
        $body = $1;
155
        $body =~ s|\n?(.*)\n?|$1|;
156
        $body = encode_qp(Encode::encode("UTF-8", $body));
157
    }
158
159
    $mail{body} = $body;
160
161
    my $boundary = "====" . time() . "====";
162
163
    $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
164
    my $isofile = encode_base64(encode("UTF-8", $iso2709));
165
    $boundary = '--' . $boundary;
166
    $mail{body} = <<END_OF_BODY;
167
$boundary
168
MIME-Version: 1.0
169
Content-Type: text/plain; charset="UTF-8"
170
Content-Transfer-Encoding: quoted-printable
171
172
$email_header
173
$body
174
$boundary
175
Content-Type: application/octet-stream; name="basket.iso2709"
176
Content-Transfer-Encoding: base64
177
Content-Disposition: attachment; filename="basket.iso2709"
178
107
179
$isofile
108
    %mail = (%mail, $email->fill_body_for_sending({
180
$boundary--
109
        template => $template_res,
181
END_OF_BODY
110
        default_filename => "basket.$format",
111
        attachment => $records_file,
112
    }));
182
113
183
    # Sending mail (if not empty basket)
114
    # Sending mail (if not empty basket)
184
    if ( defined($iso2709) && sendmail %mail ) {
115
    if ( defined($records_file) && sendmail %mail ) {
185
    # do something if it works....
116
    # do something if it works....
186
        $template->param( SENT      => "1" );
117
        $template->param( SENT      => "1" );
187
    }
118
    }
188
    else {
119
    else {
189
        # do something if it doesn't work....
120
        # do something if it doesn't work....
190
    carp "Error sending mail: empty basket" if !defined($iso2709);
121
        carp "Error sending mail: empty basket" unless defined($records_file);
191
        carp "Error sending mail: $Mail::Sendmail::error" if $Mail::Sendmail::error;
122
        carp "Error sending mail: $Mail::Sendmail::error" if $Mail::Sendmail::error;
192
        $template->param( error => 1 );
123
        $template->param( error => 1 );
193
    }
124
    }
Lines 203-208 else { Link Here
203
        virtualshelves => C4::Context->preference("virtualshelves"),
134
        virtualshelves => C4::Context->preference("virtualshelves"),
204
        csrf_token => Koha::Token->new->generate_csrf(
135
        csrf_token => Koha::Token->new->generate_csrf(
205
            { session_id => $new_session_id, } ),
136
            { session_id => $new_session_id, } ),
137
        csv_profiles   => [ Koha::CsvProfiles->search({ type => 'marc' }) ],
206
    );
138
    );
207
    output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
139
    output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
208
}
140
}
(-)a/opac/opac-sendshelf.pl (-83 / +29 lines)
Lines 20-31 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use CGI qw ( -utf8 );
22
use CGI qw ( -utf8 );
23
use Encode qw( encode );
24
use Carp;
23
use Carp;
25
24
26
use Mail::Sendmail;
25
use Mail::Sendmail;
27
use MIME::QuotedPrint;
28
use MIME::Base64;
29
use C4::Auth;
26
use C4::Auth;
30
use C4::Biblio;
27
use C4::Biblio;
31
use C4::Items;
28
use C4::Items;
Lines 34-39 use C4::Members; Link Here
34
use Koha::Email;
31
use Koha::Email;
35
use Koha::Patrons;
32
use Koha::Patrons;
36
use Koha::Virtualshelves;
33
use Koha::Virtualshelves;
34
use Koha::CsvProfiles;
37
35
38
my $query = new CGI;
36
my $query = new CGI;
39
37
Lines 54-59 my ( $template, $borrowernumber, $cookie ) = get_template_and_user ( Link Here
54
52
55
my $shelfid = $query->param('shelfid');
53
my $shelfid = $query->param('shelfid');
56
my $email   = $query->param('email');
54
my $email   = $query->param('email');
55
my $format  = $query->param('format') || 'iso2709';
57
56
58
my $dbh          = C4::Context->dbh;
57
my $dbh          = C4::Context->dbh;
59
58
Lines 84-123 if ( $email ) { Link Here
84
83
85
    my $shelf = Koha::Virtualshelves->find( $shelfid );
84
    my $shelf = Koha::Virtualshelves->find( $shelfid );
86
    my $contents = $shelf->get_contents;
85
    my $contents = $shelf->get_contents;
87
    my $marcflavour         = C4::Context->preference('marcflavour');
88
    my $iso2709;
89
    my @results;
90
86
87
    my @biblionumbers;
91
    while ( my $content = $contents->next ) {
88
    while ( my $content = $contents->next ) {
92
        my $biblionumber = $content->biblionumber;
89
        my $biblionumber = $content->biblionumber;
93
        my $record           = GetMarcBiblio({
90
        push @biblionumbers, $biblionumber;
94
            biblionumber => $biblionumber,
95
            embed_items  => 1,
96
            opac         => 1,
97
            borcat       => $borcat });
98
        next unless $record;
99
        my $fw               = GetFrameworkCode($biblionumber);
100
        my $dat              = GetBiblioData($biblionumber);
101
102
        my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
103
        my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
104
105
        my @items = GetItemsInfo( $biblionumber );
106
107
        $dat->{ISBN}           = GetMarcISBN($record, $marcflavour);
108
        $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
109
        $dat->{MARCAUTHORS}    = $marcauthorsarray;
110
        $dat->{'biblionumber'} = $biblionumber;
111
        $dat->{ITEM_RESULTS}   = \@items;
112
        $dat->{HASAUTHORS}     = $dat->{'author'} || @$marcauthorsarray;
113
114
        $iso2709 .= $record->as_usmarc();
115
116
        push( @results, $dat );
117
    }
91
    }
118
92
93
    my $csv_profile_id;
94
    if ( $format =~ m|^\d+$| ) {
95
        $csv_profile_id = $format;
96
        $format         = 'csv';
97
    }
98
    my $data = C4::Biblio::BuildBiblioDataForExport(
99
        {
100
            biblionumbers  => \@biblionumbers,
101
            format         => $format,
102
            csv_profile_id => $csv_profile_id,
103
            template       => 'opac',
104
        }
105
    );
106
    my $records_file = $data->{records_file};
107
    my $records_data = $data->{records_data};
108
109
    my $user = GetMember(borrowernumber => $borrowernumber);
110
119
    $template2->param(
111
    $template2->param(
120
        BIBLIO_RESULTS => \@results,
112
        BIBLIO_RESULTS => $records_data,
121
        comment        => $comment,
113
        comment        => $comment,
122
        shelfname      => $shelf->shelfname,
114
        shelfname      => $shelf->shelfname,
123
        firstname      => $patron->firstname,
115
        firstname      => $patron->firstname,
Lines 126-184 if ( $email ) { Link Here
126
118
127
    # Getting template result
119
    # Getting template result
128
    my $template_res = $template2->output();
120
    my $template_res = $template2->output();
129
    my $body;
130
131
    # Analysing information and getting mail properties
132
    if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
133
        $mail{subject} = $1;
134
        $mail{subject} =~ s|\n?(.*)\n?|$1|;
135
    }
136
    else { $mail{'subject'} = "no subject"; }
137
    $mail{subject} = encode('MIME-Header', $mail{subject});
138
139
    my $email_header = "";
140
    if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
141
        $email_header = $1;
142
        $email_header =~ s|\n?(.*)\n?|$1|;
143
        $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
144
    }
145
146
    my $email_file = "list.txt";
147
    if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
148
        $email_file = $1;
149
        $email_file =~ s|\n?(.*)\n?|$1|;
150
    }
151
152
    if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
153
        $body = $1;
154
        $body =~ s|\n?(.*)\n?|$1|;
155
        $body = encode_qp(Encode::encode("UTF-8", $body));
156
    }
157
158
    my $boundary = "====" . time() . "====";
159
160
    # We set and put the multipart content
161
    $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
162
163
    my $isofile = encode_base64(encode("UTF-8", $iso2709));
164
    $boundary = '--' . $boundary;
165
166
    $mail{body} = <<END_OF_BODY;
167
$boundary
168
MIME-Version: 1.0
169
Content-Type: text/plain; charset="utf-8"
170
Content-Transfer-Encoding: quoted-printable
171
172
$email_header
173
$body
174
$boundary
175
Content-Type: application/octet-stream; name="list.iso2709"
176
Content-Transfer-Encoding: base64
177
Content-Disposition: attachment; filename="list.iso2709"
178
121
179
$isofile
122
    %mail = (%mail, $message->fill_body_for_sending({
180
$boundary--
123
        template => $template_res,
181
END_OF_BODY
124
        default_filename => "list.$format",
125
        attachment => $records_file,
126
    }));
182
127
183
    # Sending mail
128
    # Sending mail
184
    if ( sendmail %mail ) {
129
    if ( sendmail %mail ) {
Lines 201-206 END_OF_BODY Link Here
201
}else{
146
}else{
202
    $template->param( shelfid => $shelfid,
147
    $template->param( shelfid => $shelfid,
203
                      url     => "/cgi-bin/koha/opac-sendshelf.pl",
148
                      url     => "/cgi-bin/koha/opac-sendshelf.pl",
149
                      csv_profiles   => [ Koha::CsvProfiles->search({ type => 'marc' }) ],
204
                    );
150
                    );
205
    output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
151
    output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };
206
}
152
}
(-)a/t/Koha_Email.t (-3 / +70 lines)
Lines 1-7 Link Here
1
use Modern::Perl;
1
use Modern::Perl;
2
2
3
use MIME::QuotedPrint;
4
use MIME::Base64;
5
use Template;
6
3
use t::lib::Mocks;
7
use t::lib::Mocks;
4
use Test::More tests => 4;                      # last test to print
8
use Test::More tests => 6;
9
10
use FindBin qw( $Bin );
5
11
6
use_ok('Koha::Email');
12
use_ok('Koha::Email');
7
13
Lines 9-16 my $from = 'chrisc@catalyst.net.nz'; Link Here
9
t::lib::Mocks::mock_preference('ReplytoDefault', $from);
15
t::lib::Mocks::mock_preference('ReplytoDefault', $from);
10
t::lib::Mocks::mock_preference('ReturnpathDefault', $from);
16
t::lib::Mocks::mock_preference('ReturnpathDefault', $from);
11
17
12
13
14
ok( my $email = Koha::Email->new(), 'Create a Koha::Email Object');
18
ok( my $email = Koha::Email->new(), 'Create a Koha::Email Object');
15
ok( my %mail = $email->create_message_headers({from => $from}),'Set headers');
19
ok( my %mail = $email->create_message_headers({from => $from}),'Set headers');
16
is ($mail{'From'}, $from, 'Set correctly');
20
is ($mail{'From'}, $from, 'Set correctly');
21
22
23
my $subject = q|My subject μΣ会é|;
24
my $header = q|My header μΣ会é|;
25
my $message = q|My Message μΣ会é|;
26
27
my $template_filepath = $Bin . '/data/templates/send_by_email.tt';
28
my $tt = Template->new( ENCODING => 'UTF-8', ABSOLUTE => 1 );
29
30
subtest 'csv' => sub {
31
    plan tests => 5;
32
    my $csv_attachment = q|title
33
    "La Recherche"
34
    "Problèmes politiques et sociaux"
35
    "Sud Provence Côte d'Azur"
36
    "The art of computer programming μΣ会é"
37
    |;
38
    # the csv is generated by C4::Record::marc2csv and is already encoded
39
    $csv_attachment = Encode::encode('UTF-8', $csv_attachment);
40
41
    my $output;
42
    $tt->process( $template_filepath, { test => 'pouet' }, \$output );
43
44
    %mail = (%mail, $email->fill_body_for_sending({
45
        template => $output,
46
        default_filename => 'myfile.csv',
47
        attachment => $csv_attachment,
48
    }));
49
50
    is ($mail{From}, $from, 'From is still set correctly');
51
    like( $mail{Subject}, qr{$subject}, );
52
53
    my $encoded_header = encode_qp($header);
54
    my $encoded_message = encode_qp($header);
55
    my $encoded_csv_attachment = encode_base64($csv_attachment);
56
    like( $mail{body}, qr{$encoded_header}, );
57
    like( $mail{body}, qr{$encoded_message}, );
58
    like( $mail{body}, qr{$encoded_csv_attachment}, );
59
};
60
61
subtest 'iso2709' => sub {
62
    plan tests => 5;
63
    my $ris_attachment = qq|TY  - BOOK\r\nAU  - Knuth, Donald Ervin\r\nTI  - The art of computer programming μΣ会é\r\nKW  - Computer programming\r\nKW  - Computer algorithms\r\nER  - \r\n|;
64
65
    my $output;
66
    $tt->process( $template_filepath, { test => 'pouet' }, \$output );
67
68
    %mail = (%mail, $email->fill_body_for_sending({
69
        template => $output,
70
        default_filename => 'myfile.ris',
71
        attachment => $ris_attachment,
72
    }));
73
74
    is ($mail{From}, $from, 'From is still set correctly');
75
    like( $mail{Subject}, qr{$subject}, );
76
77
    my $encoded_header = encode_qp($header);
78
    my $encoded_message = encode_qp($header);
79
    my $encoded_ris_attachment = encode_base64(Encode::encode('UTF-8', $ris_attachment));
80
    like( $mail{body}, qr{$encoded_header}, );
81
    like( $mail{body}, qr{$encoded_message}, );
82
    like( $mail{body}, qr{$encoded_ris_attachment}, );
83
};
(-)a/t/data/templates/send_by_email.tt (+11 lines)
Line 0 Link Here
1
<SUBJECT>
2
My subject μΣ会é
3
<END_SUBJECT>
4
5
<HEADER>
6
My header μΣ会é
7
<END_HEADER>
8
9
<MESSAGE>
10
My Message μΣ会é
11
<END_MESSAGE>
(-)a/t/db_dependent/Biblio/BuildBiblioDataForExport.t (+139 lines)
Line 0 Link Here
1
use Modern::Perl;
2
use Test::More tests => 4;
3
4
use Test::MockModule;
5
use MARC::Record;
6
use MARC::Field;
7
8
use C4::Biblio;
9
use C4::Context;
10
use C4::Items;
11
use C4::Record;
12
use Koha::DateUtils;
13
14
my $dbh = C4::Context->dbh;
15
$dbh->{AutoCommit} = 0;
16
$dbh->{RaiseError} = 1;
17
18
my $module_biblio = Test::MockModule->new('C4::Biblio');
19
20
#my $title = q|The art of computer programming μΣ会é|;
21
my $title = q|The art of computer programming|;
22
my $record = new_record();
23
my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, q|| );
24
AddItem({ homebranch => 'CPL', holdingbranch => 'CPL', itype => 'CANNOT', barcode => 'my new barcode' }, $biblionumber);
25
26
subtest 'csv' => sub {
27
    plan tests => 1;
28
    my $csv_content = q(Title=245$a|Author=245$c|Subject=650$a);
29
    my $csv_profile_id = insert_csv_profile({ csv_content => $csv_content });
30
31
    my $csv_data = C4::Biblio::BuildBiblioDataForExport({
32
        biblionumbers => [ $biblionumber ],
33
        format => 'csv',
34
        csv_profile_id => $csv_profile_id,
35
    });
36
37
    is_deeply( $csv_data, { records_data => [], records_file => qq{Title|Author|Subject
38
"$title,The art of another title"|"Donald E. Knuth.,Donald E. Knuth. II"|"Computer programming.,Computer algorithms."
39
}}, 'BuildBiblioDataForExport should return correct values in csv' );
40
};
41
42
subtest 'iso2709' => sub {
43
    plan tests => 7;
44
    my $iso2709_data = C4::Biblio::BuildBiblioDataForExport({
45
        biblionumbers => [ $biblionumber ],
46
        format => 'iso2709',
47
    });
48
49
    my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1});
50
51
    like( $iso2709_data->{records_file}, qr{00467nam a2200121 a \d+140211b xxu||||| |||| 00| 0 eng d1 aKnuth, Donald Ervind193814a$titlecDonald E. Knuth.9014aThe art of another titlecDonald E. Knuth. II91 1aComputer programming.9462 0aComputer algorithms.9499  c57d57  00104070910aCPLbCPLd$todaypmy new barcoder$todayw$todayyCANNOT}, 'BuildBiblioDataForExport should return a correct records file in iso2709' );
52
53
    my $first_biblio_data = @{ $iso2709_data->{records_data} }[0];
54
    is( $first_biblio_data->{HASAUTHORS}, 1 );
55
    is( scalar( @{ $first_biblio_data->{MARCSUBJCTS} } ), 2, '499 and 462' );
56
    is( $first_biblio_data->{title}, qq{$title | The art of another title}, );
57
    is( scalar( @{ $first_biblio_data->{ITEM_RESULTS} } ), 1, '1 item' );
58
    is( scalar( @{ $first_biblio_data->{MARCAUTHORS} } ), 0, '0 author' ); # Could be fixed if needed
59
    is( $first_biblio_data->{biblionumber}, $biblionumber, 'bibionumber is correct' );
60
};
61
62
subtest 'ris' => sub {
63
    plan tests => 1;
64
    my $ris_data = C4::Biblio::BuildBiblioDataForExport({
65
        biblionumbers => [ $biblionumber ],
66
        format => 'ris',
67
    });
68
    is( $ris_data->{records_file}, qq|TY  - BOOK\r\nAU  - Knuth, Donald Ervin\r\nTI  - $title\r\nKW  - Computer programming\r\nKW  - Computer algorithms\r\nER  - \r\n|,);
69
};
70
71
subtest 'bibtex' => sub {
72
    plan tests => 1;
73
    my $bibtex_data = C4::Biblio::BuildBiblioDataForExport({
74
        biblionumbers => [ $biblionumber ],
75
        format => 'bibtex',
76
    });
77
    is( $bibtex_data->{records_file}, qq|\@book{$biblionumber,
78
    author = {Knuth, Donald Ervin},
79
    title = {The art of computer programming}
80
}
81
|);
82
};
83
84
sub insert_csv_profile {
85
    my ( $params ) = @_;
86
    $dbh->do(q|
87
        INSERT INTO export_format(profile, description, content, csv_separator, field_separator, subfield_separator, encoding, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
88
        |, {}, ("TEST_PROFILE4", "my desc", $params->{csv_content}, '|', ';', ',', 'utf8', 'marc')
89
    );
90
    return $dbh->last_insert_id( undef, undef, 'export_format', undef );
91
}
92
93
sub new_record {
94
    my $record = MARC::Record->new;
95
    $record->leader('03174nam a2200445 a 4500');
96
    my @fields = (
97
        MARC::Field->new(
98
            '008', "140211b xxu||||| |||| 00| 0 eng d"
99
        ),
100
        MARC::Field->new(
101
            100, '1', ' ',
102
            a => 'Knuth, Donald Ervin',
103
            d => '1938',
104
        ),
105
        MARC::Field->new(
106
            245, '1', '4',
107
            a => $title,
108
            c => 'Donald E. Knuth.',
109
            9 => '0',
110
        ),
111
        MARC::Field->new(
112
            245, '1', '4',
113
            a => 'The art of another title',
114
            c => 'Donald E. Knuth. II',
115
            9 => '1',
116
        ),
117
        MARC::Field->new(
118
            650, ' ', '1',
119
            a => 'Computer programming.',
120
            9 => '462',
121
        ),
122
        MARC::Field->new(
123
            650, ' ', '0',
124
            a => 'Computer algorithms.',
125
            9 => '499',
126
        ),
127
        MARC::Field->new(
128
            952, ' ', ' ',
129
            p => '3010023917',
130
            y => 'BK',
131
            c => 'GEN',
132
            d => '2001-06-25',
133
        ),
134
    );
135
    $record->append_fields(@fields);
136
    return $record;
137
}
138
139
done_testing;
(-)a/virtualshelves/downloadshelf.pl (-1 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use CGI qw ( -utf8 );
22
use CGI qw ( -utf8 );
23
use Encode qw(encode);
24
23
25
use C4::Auth;
24
use C4::Auth;
26
use C4::Biblio;
25
use C4::Biblio;
(-)a/virtualshelves/sendshelf.pl (-77 / +25 lines)
Lines 20-37 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use CGI qw ( -utf8 );
22
use CGI qw ( -utf8 );
23
use Encode qw( encode );
24
use Carp;
23
use Carp;
25
24
26
use Mail::Sendmail;
25
use Mail::Sendmail;
27
use MIME::QuotedPrint;
28
use MIME::Base64;
29
use C4::Auth;
26
use C4::Auth;
30
use C4::Biblio;
27
use C4::Biblio;
31
use C4::Items;
28
use C4::Items;
32
use C4::Output;
29
use C4::Output;
33
use Koha::Email;
30
use Koha::Email;
34
use Koha::Virtualshelves;
31
use Koha::Virtualshelves;
32
use Koha::CsvProfiles;
35
33
36
my $query = new CGI;
34
my $query = new CGI;
37
35
Lines 47-52 my ( $template, $borrowernumber, $cookie ) = get_template_and_user( Link Here
47
45
48
my $shelfid = $query->param('shelfid');
46
my $shelfid = $query->param('shelfid');
49
my $email   = $query->param('email');
47
my $email   = $query->param('email');
48
my $format  = $query->param('format') || 'iso2709';
50
49
51
my $dbh = C4::Context->dbh;
50
my $dbh = C4::Context->dbh;
52
51
Lines 71-163 if ($email) { Link Here
71
70
72
    my $shelf = Koha::Virtualshelves->find( $shelfid );
71
    my $shelf = Koha::Virtualshelves->find( $shelfid );
73
    my $contents = $shelf->get_contents;
72
    my $contents = $shelf->get_contents;
74
    my $marcflavour = C4::Context->preference('marcflavour');
75
    my $iso2709;
76
    my @results;
77
73
74
    my @biblionumbers;
78
    while ( my $content = $contents->next ) {
75
    while ( my $content = $contents->next ) {
79
        my $biblionumber     = $content->biblionumber;
76
        my $biblionumber     = $content->biblionumber;
80
        my $dat              = GetBiblioData($biblionumber);
77
        push @biblionumbers, $biblionumber;
81
        my $record           = GetMarcBiblio({
78
    }
82
            biblionumber => $biblionumber,
83
            embed_items  => 1 });
84
        my $marcauthorsarray = GetMarcAuthors( $record, $marcflavour );
85
        my $marcsubjctsarray = GetMarcSubjects( $record, $marcflavour );
86
87
        my @items = GetItemsInfo($biblionumber);
88
89
        $dat->{ISBN}           = GetMarcISBN($record, $marcflavour);
90
        $dat->{MARCSUBJCTS}    = $marcsubjctsarray;
91
        $dat->{MARCAUTHORS}    = $marcauthorsarray;
92
        $dat->{'biblionumber'} = $biblionumber;
93
        $dat->{ITEM_RESULTS}   = \@items;
94
        $dat->{HASAUTHORS}     = $dat->{'author'} || @$marcauthorsarray;
95
96
        $iso2709 .= $record->as_usmarc();
97
79
98
        push( @results, $dat );
80
    my $csv_profile_id;
81
    if ( $format =~ m|^\d+$| ) {
82
        $csv_profile_id = $format;
83
        $format         = 'csv';
99
    }
84
    }
85
    my $data = C4::Biblio::BuildBiblioDataForExport(
86
        {
87
            biblionumbers  => \@biblionumbers,
88
            format         => $format,
89
            csv_profile_id => $csv_profile_id
90
        }
91
    );
92
    my $records_file = $data->{records_file};
93
    my $records_data = $data->{records_data};
100
94
101
    $template2->param(
95
    $template2->param(
102
        BIBLIO_RESULTS => \@results,
96
        BIBLIO_RESULTS => $records_data,
103
        comment        => $comment,
97
        comment        => $comment,
104
        shelfname      => $shelf->shelfname,
98
        shelfname      => $shelf->shelfname,
105
    );
99
    );
106
100
107
    # Getting template result
101
    # Getting template result
108
    my $template_res = $template2->output();
102
    my $template_res = $template2->output();
109
    my $body;
110
111
    # Analysing information and getting mail properties
112
    if ( $template_res =~ /<SUBJECT>(.*)<END_SUBJECT>/s ) {
113
        $mail{subject} = $1;
114
        $mail{subject} =~ s|\n?(.*)\n?|$1|;
115
    }
116
    else { $mail{'subject'} = "no subject"; }
117
    $mail{subject} = encode( 'MIME-Header', $mail{subject} );
118
119
    my $email_header = "";
120
    if ( $template_res =~ /<HEADER>(.*)<END_HEADER>/s ) {
121
        $email_header = $1;
122
        $email_header =~ s|\n?(.*)\n?|$1|;
123
        $email_header = encode_qp(Encode::encode("UTF-8", $email_header));
124
    }
125
126
    my $email_file = "list.txt";
127
    if ( $template_res =~ /<FILENAME>(.*)<END_FILENAME>/s ) {
128
        $email_file = $1;
129
        $email_file =~ s|\n?(.*)\n?|$1|;
130
    }
131
132
    if ( $template_res =~ /<MESSAGE>(.*)<END_MESSAGE>/s ) {
133
        $body = $1;
134
        $body =~ s|\n?(.*)\n?|$1|;
135
        $body = encode_qp(Encode::encode("UTF-8", $body));
136
    }
137
138
    my $boundary = "====" . time() . "====";
139
140
    # We set and put the multipart content
141
    $mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";
142
143
    my $isofile = encode_base64( encode( "UTF-8", $iso2709 ) );
144
    $boundary = '--' . $boundary;
145
146
    $mail{body} = <<END_OF_BODY;
147
$boundary
148
Content-Type: text/plain; charset="utf-8"
149
Content-Transfer-Encoding: quoted-printable
150
151
$email_header
152
$body
153
$boundary
154
Content-Type: application/octet-stream; name="shelf.iso2709"
155
Content-Transfer-Encoding: base64
156
Content-Disposition: attachment; filename="shelf.iso2709"
157
103
158
$isofile
104
    %mail = (%mail, $message->fill_body_for_sending({
159
$boundary--
105
        template => $template_res,
160
END_OF_BODY
106
        default_filename => "list.$format",
107
        attachment => $records_file,
108
    }));
161
109
162
    # Sending mail
110
    # Sending mail
163
    if ( sendmail %mail ) {
111
    if ( sendmail %mail ) {
Lines 179-184 else { Link Here
179
    $template->param(
127
    $template->param(
180
        shelfid => $shelfid,
128
        shelfid => $shelfid,
181
        url     => "/cgi-bin/koha/virtualshelves/sendshelf.pl",
129
        url     => "/cgi-bin/koha/virtualshelves/sendshelf.pl",
130
        csv_profiles   => [ Koha::CsvProfiles->search({ type => 'marc' }) ],
182
    );
131
    );
183
    output_html_with_http_headers $query, $cookie, $template->output;
132
    output_html_with_http_headers $query, $cookie, $template->output;
184
}
133
}
185
- 

Return to bug 13345