Bug 17981: Add a preview mode for notice templates
[koha.git] / svc / letters / preview
1 #!/usr/bin/perl
2
3 # Copyright 2016 Koha Development Team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw( -utf8 );
22 use C4::Auth;
23 use C4::Context;
24 use C4::Output;
25 use C4::Circulation;
26 use C4::Letters;
27 use Koha::Checkouts;
28 use Koha::Items;
29 use Koha::Patrons;
30
31 my $input = new CGI;
32
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
34     {
35         template_name   => "tools/preview_letter.tt",
36         query           => $input,
37         type            => "intranet",
38         authnotrequired => 0,
39         flagsrequired   => { tools => 'edit_notices' },
40         debug           => 1,
41     }
42 );
43
44 my @messages;
45 my $code         = $input->param('code');
46 my $content      = $input->param('content');
47 my $title        = $input->param('title');
48 my $is_html      = $input->param('is_html');
49 my $data_preview = $input->param('data_preview');
50
51 unless ( $data_preview ) {
52     $template->param( messages => [{ code => 'no_data_for_preview', type => 'error' }]);
53     output_html_with_http_headers $input, $cookie, $template->output;
54     exit;
55 }
56
57 my $fake_letter = { content => $content, title => $title, is_html => $is_html };
58
59 my ( $tt_content, $fake_tt_letter );
60 if ( $content =~ m/[^\n]*<<.*>>[^\n]*/so ) {
61     $tt_content = $content;
62
63     my $table_mapping = {
64         biblio                 => 'biblio',
65         borrowers              => 'borrower',
66         branches               => 'branch',
67         items                  => 'item',
68         opac_news              => 'news',
69         aqorders               => 'orders',
70         reserves               => 'hold',
71         serial                 => 'serial',
72         subscription           => 'subscription',
73         suggestions            => 'suggestion',
74         issues                 => 'checkout',
75         old_issues             => 'old_checkout',
76         overdues               => 'overdue',
77         borrower_modifications => 'patron_modification',
78     };
79
80     # Today
81     $tt_content =~ s#<<today>>#[% today| \$KohaDates with_hours => 1 %]#sg;
82
83
84     for my $date_field ( qw(
85             borrowers.dateofbirth
86             borrowers.dateenrolled
87             borrowers.dateexpiry
88             borrowers.debarred
89             items.dateaccessioned
90             items.datelastborrowed
91             items.datelastseen
92             items.onloan
93             serials.planneddate
94             serials.publisheddate
95             serials.claimdate
96             reserves.reservedate
97             reserves.waitingdate
98             reserves.expirationdate
99             suggestions.suggesteddate
100             suggestions.manageddate
101             suggestions.accepteddate
102             suggestions.rejecteddate
103             aqorders.entrydate
104             aqorders.datereceived
105             aqorders.datecancellationprinted
106             aqorders.budgetdate
107             aqorders.claimed_date
108     ) ) {
109         my ( $table, $field ) = split '\.', $date_field;
110         my $new_field =
111           exists $table_mapping->{$table}
112           ? $table_mapping->{$table} . ".$field"
113           : "$table.$field";
114         $tt_content =~ s#<<$table\.$field>>#[% $new_field | \$KohaDates %]#sg;
115         $tt_content =~ s#<<$table\.$field\s*|\s*dateonly>>#[% $new_field | \$KohaDates %]#sg;
116     }
117
118     for my $datetime_field ( qw(
119             items.itemlost_on
120             items.withdrawn_on
121             issues.date_due
122             issues.returndate
123             issues.lastreneweddate
124             issues.issuedate
125             reserves.suspend_until
126     ) ) {
127         my ( $table, $field ) = split '\.', $datetime_field;
128         my $new_field =
129           exists $table_mapping->{$table}
130           ? $table_mapping->{$table} . ".$field"
131           : "$table.$field";
132         $tt_content =~ s#<<$table\.$field>>#[% $new_field | \$KohaDates with_hours => 1 %]#sg;
133         $tt_content =~ s#<<$table\.$field\s*|\s*dateonly>>#[% $new_field | \$KohaDates %]#sg;
134     }
135
136
137
138     while ( my ( $key, $value ) = each %$table_mapping ) {
139         $tt_content =~ s|<<$key\.|<<$value.|sg;
140     }
141
142     $tt_content =~ s|<<|[% |sg;
143     $tt_content =~ s|>>| %]|sg;
144     $fake_tt_letter =
145       { content => $tt_content, title => $title, is_html => $is_html };
146 }
147
148 my ( $rendered_message, $rendered_tt_message ) = (q||) x 2;
149 my $messages_are_similar;
150 my $letter_params = {};
151 if ( $code eq 'CHECKIN' ) {
152     my $item = Koha::Items->find( { barcode => $data_preview } );
153     my $checkout = Koha::Checkouts->find( { itemnumber => $item->itemnumber } );
154     if ($checkout) {
155         my $patron = Koha::Patrons->find( $checkout->borrowernumber );
156         my $branchcode =
157           C4::Circulation::_GetCircControlBranch( $item->unblessed,
158             $patron->unblessed );
159         $letter_params = {
160             tables => {
161                 issues      => $item->itemnumber,
162                 items       => $item->itemnumber,
163                 biblio      => $item->biblionumber,
164                 biblioitems => $item->biblionumber,
165                 issues      => $patron->borrowernumber,
166                 branches    => $branchcode,
167             }
168         };
169         push @messages, { code => 'not_checked_in_yet', type => 'message' };
170     }
171     else {
172         warn "No checkout";
173     }
174 }
175 elsif ( $code eq 'CHECKOUT' ) {
176     my ( $barcode, $borrowernumber ) = split '\|', $data_preview;
177     my $item = Koha::Items->find( { barcode => $barcode } );
178     my $patron = Koha::Patrons->find( $borrowernumber );
179     if ($item and $patron) {
180         my $branchcode =
181           C4::Circulation::_GetCircControlBranch( $item->unblessed,
182             $patron->unblessed );
183         $letter_params = {
184             tables => {
185                 issues      => $item->itemnumber,
186                 items       => $item->itemnumber,
187                 biblio      => $item->biblionumber,
188                 biblioitems => $item->biblionumber,
189                 issues      => $patron->borrowernumber,
190                 branches    => $branchcode,
191             }
192         };
193         push @messages, { code => 'not_checked_out_yet', type => 'message' };
194     }
195     else {
196         warn "No item or no patron";
197     }
198 }
199 elsif ( $code eq 'HOLD_SLIP' ) {
200     my ( $biblionumber, $borrowernumber ) = split '\|', $data_preview;
201     my $hold = Koha::Holds->find( { borrowernumber => $borrowernumber, biblionumber => $biblionumber } );
202     if ($hold) {
203         $letter_params = {
204             tables => {
205                 reserves    => $hold->unblessed,
206                 branches    => $hold->branchcode,
207                 borrowers   => $hold->borrowernumber,
208                 biblio      => $hold->biblionumber,
209                 biblioitems => $hold->biblionumber,
210                 items       => $hold->itemnumber,
211             }
212         };
213     }
214     else {
215         warn "No hold placed by this patron on this bibliographic record.";
216     }
217 }
218 else {
219     warn "Preview for letter code $code is not available";
220     push @messages, { type => 'alert', code => 'preview_not_available', letter_code => $code, };
221 }
222
223 if ( %$letter_params ) {
224     # FIXME Be case here GetPreparedLetter modify $fake_letter
225     $rendered_message = C4::Letters::GetPreparedLetter(
226         letter => $fake_letter,
227         %$letter_params,
228     );
229     if ($tt_content) {
230         $rendered_tt_message = C4::Letters::GetPreparedLetter(
231             letter => $fake_tt_letter,
232             %$letter_params,
233         );
234     }
235     $messages_are_similar =
236       $rendered_message->{content} eq $rendered_tt_message->{content};
237 }
238
239 $template->param(
240     original_content     => $content,
241     rendered_message     => $rendered_message,
242     tt_content           => $tt_content,
243     rendered_tt_message  => $rendered_tt_message,
244     messages_are_similar => $messages_are_similar,
245     messages             => \@messages,
246 );
247
248 output_html_with_http_headers $input, $cookie, $template->output;