3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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>.
20 =head1 tools/letter.pl
23 this script use an $op to know what to do.
24 if $op is empty or none of the values listed below,
25 - the default screen is built (with all or filtered (if search string is set) records).
26 - the user can click on add, modify or delete record.
27 - filtering is done on the code field
29 - if primary key (module + code) exists, this is a modification,so we read the required record
30 - builds the add/modify form
32 - the user has just send data, so we create/modify the record
34 - we show the record selected and ask for confirmation
36 - we delete the designated record
40 # TODO This script drives the CRUD operations on the letter table
41 # The DB interaction should be handled by calls to C4/Letters.pm
47 use C4::Auth qw( get_template_and_user );
49 use C4::Output qw( output_html_with_http_headers );
50 use C4::Letters qw( GetMessageTransportTypes );
51 use C4::Log qw( logaction );
53 use Koha::Notice::Templates;
54 use Koha::Patron::Attribute::Types;
56 # $protected_letters = protected_letters()
57 # - return a hashref of letter_codes representing letters that should never be deleted
58 sub protected_letters {
59 my $dbh = C4::Context->dbh;
60 my $codes = $dbh->selectall_arrayref(q{SELECT DISTINCT letter_code FROM message_transports});
61 return { map { $_->[0] => 1 } @{$codes} };
64 our $input = CGI->new;
65 my $searchfield = $input->param('searchfield');
66 my $script_name = '/cgi-bin/koha/tools/letter.pl';
67 our $branchcode = $input->param('branchcode');
68 $branchcode = '' if defined $branchcode and $branchcode eq '*';
69 my $code = $input->param('code');
70 my $module = $input->param('module') || '';
71 my $content = $input->param('content');
72 my $op = $input->param('op') || '';
73 my $redirect = $input->param('redirect');
74 my $section = $input->param('section');
75 my $langtab = $input->param('langtab');
77 my $dbh = C4::Context->dbh;
79 our ( $template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
81 template_name => 'tools/letter.tt',
84 flagsrequired => { tools => 'edit_notices' },
88 our $my_branch = C4::Context->preference("IndependentBranches") && !$staffflags->{'superlibrarian'}
89 ? C4::Context->userenv()->{'branch'}
91 # we show only the TMPL_VAR names $op
94 independant_branch => $my_branch,
95 script_name => $script_name,
96 searchfield => $searchfield,
97 branchcode => $branchcode,
100 action => $script_name
103 if ( $op eq 'add_validate' or $op eq 'copy_validate' ) {
105 if( $redirect eq "just_save" ){
106 print $input->redirect("/cgi-bin/koha/tools/letter.pl?op=add_form&branchcode=$branchcode&module=$module&code=$code&redirect=done§ion=$section&langtab=$langtab");
109 $op = q{}; # we return to the default screen for the next operation
112 if ($op eq 'copy_form') {
113 my $oldbranchcode = $input->param('oldbranchcode') || q||;
114 my $branchcode = $input->param('branchcode');
115 add_form($oldbranchcode, $module, $code);
117 oldbranchcode => $oldbranchcode,
118 branchcode => $branchcode,
123 elsif ( $op eq 'add_form' ) {
124 add_form($branchcode, $module, $code);
126 elsif ( $op eq 'delete_confirm' ) {
127 delete_confirm($branchcode, $module, $code);
129 elsif ( $op eq 'delete_confirmed' ) {
130 delete_confirmed($branchcode, $module, $code);
131 $op = q{}; # next operation is to return to default screen
134 default_display($branchcode,$searchfield);
137 # Do this last as delete_confirmed resets
139 $template->param($op => 1);
141 $template->param(no_op_set => 1);
144 output_html_with_http_headers $input, $cookie, $template->output;
147 my ( $branchcode,$module, $code ) = @_;
150 # if code has been passed we can identify letter and its an update action
152 $letters = C4::Letters::GetLetterTemplates(
154 branchcode => $branchcode,
161 my $message_transport_types = GetMessageTransportTypes();
162 my $templates = { map { $_ => { message_transport_type => $_ } } sort @$message_transport_types };
163 my %letters = ( default => { templates => $templates } );
165 if ( C4::Context->preference('TranslateNotices') ) {
166 my $translated_languages =
167 C4::Languages::getTranslatedLanguages( 'opac',
168 C4::Context->preference('template') );
169 for my $language (@$translated_languages) {
170 for my $sublanguage( @{ $language->{sublanguages_loop} } ) {
171 if ( $language->{plural} ) {
172 $letters{ $sublanguage->{rfc4646_subtag} } = {
173 description => $sublanguage->{native_description}
175 . $sublanguage->{region_description} . ' ('
176 . $sublanguage->{rfc4646_subtag} . ')',
177 templates => { %$templates },
181 $letters{ $sublanguage->{rfc4646_subtag} } = {
182 description => $sublanguage->{native_description}
184 . $sublanguage->{rfc4646_subtag} . ')',
185 templates => { %$templates },
190 $template->param( languages => $translated_languages );
197 my $first_flag_name = 1;
200 # The letter name is contained into each mtt row.
201 # So we can only sent the first one to the template.
202 for my $letter ( @$letters ) {
204 if ( $first_flag_name and $letter->{name} ) {
206 letter_name=> $letter->{name},
208 $first_flag_name = 0;
211 # Catch template issues
213 C4::Letters::_process_tt($letter);
216 $error =~ s/^ERROR PROCESSING TEMPLATE: //;
217 $error =~ s/at .*\/tools\/letter\.pl line 213\.$//;
218 $letter->{tt_error} = $error;
221 my $lang = $letter->{lang};
222 my $mtt = $letter->{message_transport_type};
223 $letters{ $lang }{templates}{$mtt} = {
224 message_transport_type => $letter->{message_transport_type},
225 is_html => $letter->{is_html},
226 updated_on => $letter->{updated_on},
227 title => $letter->{title},
228 content => $letter->{content} // '',
229 tt_error => $letter->{tt_error},
234 $template->param( adding => 1 );
238 letters => \%letters,
242 push @{$field_selection}, add_fields('branches');
243 if ($module eq 'reserves') {
244 push @{$field_selection}, add_fields('borrowers', 'reserves', 'biblio', 'biblioitems', 'items');
246 elsif ( $module eq 'acquisition' ) {
247 push @{$field_selection}, add_fields('aqbooksellers', 'aqorders', 'biblio', 'items');
249 elsif ($module eq 'claimacquisition' || $module eq 'orderacquisition') {
250 push @{$field_selection}, add_fields('aqbooksellers', 'aqbasket', 'aqorders', 'biblio', 'biblioitems');
252 elsif ($module eq 'claimissues') {
253 push @{$field_selection}, add_fields('aqbooksellers', 'serial', 'subscription', 'biblio', 'biblioitems');
255 elsif ($module eq 'patron_slip') {
256 push @{$field_selection}, add_fields('borrowers');
258 elsif ($module eq 'serial') {
259 push @{$field_selection}, add_fields('branches', 'biblio', 'biblioitems', 'borrowers', 'subscription', 'serial');
261 elsif ($module eq 'suggestions') {
262 push @{$field_selection}, add_fields('suggestions', 'borrowers', 'biblio');
265 push @{$field_selection}, add_fields('biblio','biblioitems'),
267 {value => 'items.content', text => 'items.content'},
268 {value => 'items.fine', text => 'items.fine'},
269 add_fields('borrowers');
270 if ($module eq 'circulation') {
271 push @{$field_selection}, add_fields('additional_contents', 'recalls');
275 if ( $module eq 'circulation' and $code and ( $code eq "CHECKIN" or $code eq "CHECKINSLIP" ) ) {
276 push @{$field_selection}, add_fields('old_issues');
278 push @{$field_selection}, add_fields('issues');
281 if ( $module eq 'circulation' and $code and $code =~ /^AR_/ ) {
282 push @{$field_selection}, add_fields('article_requests');
285 if ( $module eq 'members' and $code and $code eq 'PROBLEM_REPORT' ) {
286 push @{$field_selection}, add_fields('problem_reports');
289 if ( $module eq 'ill' ) {
290 push @{$field_selection}, add_fields('illrequests');
294 my $preview_is_available = 0;
297 $preview_is_available = grep {$_ eq $code } qw( CHECKIN CHECKOUT HOLD_SLIP );
302 SQLfieldnames => $field_selection,
303 branchcode => $branchcode,
304 preview_is_available => $preview_is_available,
310 my $dbh = C4::Context->dbh;
311 my $branchcode = $input->param('branchcode');
312 my $module = $input->param('module');
313 my $oldmodule = $input->param('oldmodule');
314 my $code = $input->param('code');
315 my $name = $input->param('name');
316 my @mtt = $input->multi_param('message_transport_type');
317 my @title = $input->multi_param('title');
318 my @content = $input->multi_param('content');
319 my @lang = $input->multi_param('lang');
320 for my $mtt ( @mtt ) {
321 my $lang = shift @lang;
322 my $is_html = $input->param("is_html_$mtt\_$lang");
323 my $title = shift @title;
324 my $content = shift @content;
325 my $letter = Koha::Notice::Templates->find(
327 module => $oldmodule,
329 branchcode => $branchcode,
330 message_transport_type => $mtt,
335 unless ( $title and $content ) {
336 # Delete this mtt if no title or content given
337 delete_confirmed( $branchcode, $oldmodule, $code, $mtt, $lang );
341 logaction( 'NOTICES', 'MODIFY', $letter->id, $content,
343 if ( C4::Context->preference("NoticesLog")
344 && $content ne $letter->content );
348 branchcode => $branchcode || '',
351 is_html => $is_html || 0,
359 my $letter = Koha::Notice::Template->new(
361 branchcode => $branchcode,
368 message_transport_type => $mtt,
372 logaction( 'NOTICES', 'CREATE', $letter->id, $letter->content,
374 if C4::Context->preference("NoticesLog");
377 # set up default display
378 default_display($branchcode);
383 my ($branchcode, $module, $code) = @_;
384 my $dbh = C4::Context->dbh;
385 my $letter = Koha::Notice::Templates->search(
386 { module => $module, code => $code, branchcode => $branchcode } );
388 letter => $letter ? $letter->next : undef,
393 sub delete_confirmed {
394 my ( $branchcode, $module, $code, $mtt, $lang ) = @_;
395 my $letters = Koha::Notice::Templates->search(
397 branchcode => $branchcode || '',
400 ( $mtt ? ( message_transport_type => $mtt ) : () ),
401 ( $lang ? ( lang => $lang ) : () ),
404 while ( my $letter = $letters->next ) {
405 logaction( 'NOTICES', 'DELETE', $letter->id, $letter->content,
407 if C4::Context->preference("NoticesLog");
411 # setup default display for screen
412 default_display($branchcode);
416 sub retrieve_letters {
417 my ($branchcode, $searchstring) = @_;
419 $branchcode = $my_branch if $branchcode && $my_branch;
421 my $dbh = C4::Context->dbh;
422 my ($sql, @where, @args);
423 $sql = "SELECT branchcode, module, code, name, branchname, MAX(updated_on) as updated_on
425 LEFT OUTER JOIN branches USING (branchcode)
427 if ($searchstring && $searchstring=~m/(\S+)/) {
428 $searchstring = $1 . q{%};
429 push @where, 'code LIKE ?';
430 push @args, $searchstring;
432 elsif ($branchcode) {
433 push @where, 'branchcode = ?';
434 push @args, $branchcode || '';
437 push @where, "(branchcode = ? OR branchcode = '')";
438 push @args, $my_branch;
441 $sql .= " WHERE ".join(" AND ", @where) if @where;
442 $sql .= " GROUP BY branchcode,module,code,name,branchname";
444 $sql .= " ORDER BY module, code, branchcode";
446 return $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
449 sub default_display {
450 my ($branchcode, $searchfield) = @_;
452 unless ( defined $branchcode ) {
453 if ( C4::Context->preference('DefaultToLoggedInLibraryNoticesSlips') ) {
454 $branchcode = C4::Context::mybranch();
458 if ( $searchfield ) {
459 $template->param( search => 1 );
461 my $results = retrieve_letters($branchcode,$searchfield);
464 my $protected_letters = protected_letters();
466 foreach my $row (@{$results}) {
467 $row->{protected} = !$row->{branchcode} && $protected_letters->{ $row->{code} };
468 push @{$loop_data}, $row;
473 letter => $loop_data,
474 branchcode => $branchcode,
482 for my $table (@tables) {
483 push @fields, get_columns_for($table);
489 sub get_columns_for {
491 # FIXME untranslatable
493 aqbooksellers => '---BOOKSELLERS---',
494 aqorders => '---ORDERS---',
495 serial => '---SERIALS---',
496 reserves => '---HOLDS---',
497 suggestions => '---SUGGESTIONS---',
500 if (exists $column_map{$table} ) {
503 text => $column_map{$table} ,
507 my $tlabel = '---' . uc $table;
515 my $sql = "SHOW COLUMNS FROM $table";# TODO not db agnostic
516 my $table_prefix = $table . q|.|;
517 my $rows = C4::Context->dbh->selectall_arrayref($sql, { Slice => {} });
518 for my $row (@{$rows}) {
519 next if $row->{'Field'} eq 'timestamp'; # this is really an irrelevant field and there may be other common fields that should be excluded from the list
520 next if $row->{'Field'} eq 'password'; # passwords can no longer be shown in notices so the password field should be removed as a template option
522 value => $table_prefix . $row->{Field},
523 text => $table_prefix . $row->{Field},
526 if ($table eq 'borrowers') {
527 my $attribute_types = Koha::Patron::Attribute::Types->search(
529 { order_by => 'code' },
531 while ( my $at = $attribute_types->next ) {
533 value => "borrower-attribute:" . $at->code,
534 text => "attribute:" . $at->code,