From 7276a38c2b67b7ef48b85f544527648b358efa6d Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Wed, 2 Dec 2015 13:32:44 +0000 Subject: [PATCH] Bug 14757 - Allow the use of Template Toolkit syntax for slips and notices This bug is the beginning of a conversion from our current bespoke syntax for slips and notices to Template Toolkit syntax. This patch is the initial seed which will evolve over time. With this addition, we can take advantage of our Koha Objects to greatly simplify the processing of Slips and Notices over time. Test Plan: 1) Apply this patch 2) Ensure you have the default CHECKOUT notice 3) Check out and return an item for a patron 4) Note the text of the CHECKOUT notice 5) Replace your CHECKOUT notice with the following: The following items have been checked out: ---- [% biblio.title %] ---- Thank you for visiting [% branch.branchname %]. 6) Repeat step 3 7) Note the CHECKOUT notice text matches the previous CHECKOUT notice text Signed-off-by: Bernardo Gonzalez Kriegel New notice syntax works, no koha-qa errors Signed-off-by: Sean McGarvey Bug 14757 [QA Followup] - Change method type() to _type() for Koha objects Signed-off-by: Sean McGarvey Bug 14757 [QA Followup] - Change all references to Koha::Borrower to Koha::Patron Signed-off-by: Sean McGarvey Signed-off-by: Jonathan Druart Signed-off-by: Kyle M Hall --- C4/Letters.pm | 163 +++++++++++++++++++++++++++++++++-- Koha/Checkout.pm | 52 +++++++++++ Koha/Checkouts.pm | 62 +++++++++++++ Koha/News.pm | 62 +++++++++++++ Koha/NewsItem.pm | 54 ++++++++++++ Koha/Patron/Modification.pm | 52 +++++++++++ Koha/Patron/Modifications.pm | 24 +++++- Koha/Suggestion.pm | 52 +++++++++++ Koha/Suggestions.pm | 62 +++++++++++++ tools/letter.pl | 1 + 10 files changed, 577 insertions(+), 7 deletions(-) create mode 100644 Koha/Checkout.pm create mode 100644 Koha/Checkouts.pm create mode 100644 Koha/News.pm create mode 100644 Koha/NewsItem.pm create mode 100644 Koha/Patron/Modification.pm create mode 100644 Koha/Suggestion.pm create mode 100644 Koha/Suggestions.pm diff --git a/C4/Letters.pm b/C4/Letters.pm index 192f3539b3..a25aaf2a6f 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -22,6 +22,11 @@ use warnings; use MIME::Lite; use Mail::Sendmail; +use Date::Calc qw( Add_Delta_Days ); +use Encode; +use Carp; +use Template; +use Module::Load::Conditional qw(can_load); use C4::Koha qw(GetAuthorisedValueByCode); use C4::Members; @@ -33,11 +38,8 @@ use C4::Debug; use Koha::DateUtils; use Koha::SMS::Providers; -use Date::Calc qw( Add_Delta_Days ); -use Encode; -use Carp; use Koha::Email; -use Koha::DateUtils qw( format_sqldatetime ); +use Koha::DateUtils qw( format_sqldatetime dt_from_string ); use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); @@ -704,8 +706,14 @@ sub GetPreparedLetter { } } + $letter->{content} = _process_tt( + { + content => $letter->{content}, + tables => $tables, + } + ); + $letter->{content} =~ s/<<\S*>>//go; #remove any stragglers -# $letter->{content} =~ s/<<[^>]*>>//go; return $letter; } @@ -1363,6 +1371,151 @@ sub _set_message_status { return $result; } +sub _process_tt { + my ( $params ) = @_; + + my $content = $params->{content}; + my $tables = $params->{tables}; + + my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE}; + my $template = Template->new( + { + EVAL_PERL => 1, + ABSOLUTE => 1, + PLUGIN_BASE => 'Koha::Template::Plugin', + COMPILE_EXT => $use_template_cache ? '.ttc' : '', + COMPILE_DIR => $use_template_cache ? C4::Context->config('template_cache_dir') : '', + FILTERS => {}, + ENCODING => 'UTF-8', + } + ) or die Template->error(); + + my $tt_params = _get_tt_params( $tables ); + + my $output; + $template->process( \$content, $tt_params, \$output ) || croak "ERROR PROCESSING TEMPLATE: " . $template->error(); + + return $output; +} + +sub _get_tt_params { + my ($tables) = @_; + + my $params; + + my $config = { + biblio => { + module => 'Koha::Biblios', + singular => 'biblio', + plural => 'biblios', + pk => 'biblionumber', + }, + borrowers => { + module => 'Koha::Patrons', + singular => 'borrower', + plural => 'borrowers', + pk => 'borrowernumber', + }, + branches => { + module => 'Koha::Libraries', + singular => 'branch', + plural => 'branches', + pk => 'branchcode', + }, + items => { + module => 'Koha::Items', + singular => 'item', + plural => 'items', + pk => 'itemnumber', + }, + opac_news => { + module => 'Koha::News', + singular => 'news', + plural => 'news', + pk => 'idnew', + }, + reserves => { + module => 'Koha::Holds', + singular => 'hold', + plural => 'holds', + fk => [ 'borrowernumber', 'biblionumber' ], + }, + serial => { + module => 'Koha::Serials', + singular => 'serial', + plural => 'serials', + pk => 'serialid', + }, + subscription => { + module => 'Koha::Subscriptions', + singular => 'subscription', + plural => 'subscriptions', + pk => 'subscriptionid', + }, + suggestions => { + module => 'Koha::Suggestions', + singular => 'suggestion', + plural => 'suggestions', + pk => 'suggestionid', + }, + issues => { + module => 'Koha::Checkouts', + singular => 'checkout', + plural => 'checkouts', + fk => 'itemnumber', + }, + borrower_modifications => { + module => 'Koha::Patron::Modifications', + singular => 'patron_modification', + plural => 'patron_modifications', + fk => 'verification_token', + }, + }; + + foreach my $table ( keys %$tables ) { + next unless $config->{$table}; + + my $ref = ref( $tables->{$table} ) || q{}; + my $module = $config->{$table}->{module}; + + if ( can_load( modules => { $module => undef } ) ) { + my $pk = $config->{$table}->{pk}; + my $fk = $config->{$table}->{fk}; + + if ( $ref eq q{} || $ref eq 'HASH' ) { + my $id = ref $ref eq 'HASH' ? $tables->{$table}->{$pk} : $tables->{$table}; + my $object; + if ( $fk ) { # Using a foreign key for lookup + $object = $module->search( { $fk => $id } )->next(); + } else { # using the table's primary key for lookup + $object = $module->find($id); + } + $params->{ $config->{$table}->{singular} } = $object; + } + else { # $ref eq 'ARRAY' + my $object; + if ( @{ $tables->{$table} } == 1 ) { # Param is a single key + $object = $module->search( { $pk => $tables->{$table} } )->next(); + } + else { # Params are mutliple foreign keys + my @values = @{ $tables->{$table} }; + my @keys = @{ $config->{$table}->{fk} }; + my %params = map { $_ => shift(@values) } @keys; + $object = $module->search( \%params )->next(); + } + $params->{ $config->{$table}->{singular} } = $object; + } + } + else { + croak "ERROR LOADING MODULE $module: $Module::Load::Conditional::ERROR"; + } + } + + $params->{today} = dt_from_string(); + + return $params; +} + 1; __END__ diff --git a/Koha/Checkout.pm b/Koha/Checkout.pm new file mode 100644 index 0000000000..358f41c356 --- /dev/null +++ b/Koha/Checkout.pm @@ -0,0 +1,52 @@ +package Koha::Checkout; + +# Copyright ByWater Solutions 2015 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::Checkout - Koha Checkout object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'Issue'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; diff --git a/Koha/Checkouts.pm b/Koha/Checkouts.pm new file mode 100644 index 0000000000..374cfb1742 --- /dev/null +++ b/Koha/Checkouts.pm @@ -0,0 +1,62 @@ +package Koha::Checkouts; + +# Copyright ByWater Solutions 2015 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use Koha::Checkout; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Checkouts - Koha Checkout object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'Issue'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Checkout'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; diff --git a/Koha/News.pm b/Koha/News.pm new file mode 100644 index 0000000000..4a3d759278 --- /dev/null +++ b/Koha/News.pm @@ -0,0 +1,62 @@ +package Koha::News; + +# Copyright ByWater Solutions 2015 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use Koha::NewsItem; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::News - Koha News object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'OpacNews'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::NewsItem'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; diff --git a/Koha/NewsItem.pm b/Koha/NewsItem.pm new file mode 100644 index 0000000000..9d0dd63f9a --- /dev/null +++ b/Koha/NewsItem.pm @@ -0,0 +1,54 @@ +package Koha::NewsItem; + +# Copyright ByWater Solutions 2015 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::NewsItem - Koha News Item object class + +Koha::NewsItem represents a single piece of news from the opac_news table + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'OpacNews'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; diff --git a/Koha/Patron/Modification.pm b/Koha/Patron/Modification.pm new file mode 100644 index 0000000000..a06aec99a0 --- /dev/null +++ b/Koha/Patron/Modification.pm @@ -0,0 +1,52 @@ +package Koha::Patron::Modification; + +# Copyright ByWater Solutions 2014 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::Item - Koha Item object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'BorrowerModification'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; diff --git a/Koha/Patron/Modifications.pm b/Koha/Patron/Modifications.pm index 3d9dfdc22d..31fd61bcea 100644 --- a/Koha/Patron/Modifications.pm +++ b/Koha/Patron/Modifications.pm @@ -27,10 +27,18 @@ use Modern::Perl; use C4::Context; use C4::Debug; +use base qw(Koha::Objects); + sub new { - my ( $class, %args ) = @_; + my ( $self, %args ) = @_; + + $self = $self->SUPER::new(@_); + + foreach my $key ( keys %args ) { + $self->{$key} = $args{$key}; + } - return bless( \%args, $class ); + return $self; } =head2 AddModifications @@ -304,4 +312,16 @@ sub GetModifications { return $data; } +sub _type { + return 'BorrowerModification'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Patron::Modification'; +} + 1; diff --git a/Koha/Suggestion.pm b/Koha/Suggestion.pm new file mode 100644 index 0000000000..a091f43f2a --- /dev/null +++ b/Koha/Suggestion.pm @@ -0,0 +1,52 @@ +package Koha::Suggestion; + +# Copyright ByWater Solutions 2015 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::Suggestion - Koha Suggestion object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'Suggestion'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; diff --git a/Koha/Suggestions.pm b/Koha/Suggestions.pm new file mode 100644 index 0000000000..73ad28dc50 --- /dev/null +++ b/Koha/Suggestions.pm @@ -0,0 +1,62 @@ +package Koha::Suggestions; + +# Copyright ByWater Solutions 2015 +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use Koha::Suggestion; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Suggestions - Koha Suggestion object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'Suggestion'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Suggestion'; +} + +=head1 AUTHOR + +Kyle M Hall + +=cut + +1; diff --git a/tools/letter.pl b/tools/letter.pl index 9f8ac95429..35c89974cd 100755 --- a/tools/letter.pl +++ b/tools/letter.pl @@ -259,6 +259,7 @@ sub add_validate { my @content = $input->multi_param('content'); for my $mtt ( @mtt ) { my $is_html = $input->param("is_html_$mtt"); + my $is_tt = $input->param("is_tt_$mtt") ? 1 : 0; my $title = shift @title; my $content = shift @content; my $letter = C4::Letters::getletter( $oldmodule, $code, $branchcode, $mtt); -- 2.39.5