+sub _process_tt {
+ my ( $params ) = @_;
+
+ my $content = $params->{content};
+ my $tables = $params->{tables};
+ my $loops = $params->{loops};
+ my $substitute = $params->{substitute} || {};
+
+ 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 ) }, %{ _get_tt_params( $loops, 'is_a_loop' ) }, %$substitute };
+
+ $content = add_tt_filters( $content );
+ $content = qq|[% USE KohaDates %][% USE Remove_MARC_punctuation %]$content|;
+
+ my $output;
+ $template->process( \$content, $tt_params, \$output ) || croak "ERROR PROCESSING TEMPLATE: " . $template->error();
+
+ return $output;
+}
+
+sub _get_tt_params {
+ my ($tables, $is_a_loop) = @_;
+
+ my $params;
+ $is_a_loop ||= 0;
+
+ my $config = {
+ article_requests => {
+ module => 'Koha::ArticleRequests',
+ singular => 'article_request',
+ plural => 'article_requests',
+ pk => 'id',
+ },
+ biblio => {
+ module => 'Koha::Biblios',
+ singular => 'biblio',
+ plural => 'biblios',
+ pk => 'biblionumber',
+ },
+ biblioitems => {
+ module => 'Koha::Biblioitems',
+ singular => 'biblioitem',
+ plural => 'biblioitems',
+ pk => 'biblioitemnumber',
+ },
+ 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',
+ },
+ aqorders => {
+ module => 'Koha::Acquisition::Orders',
+ singular => 'order',
+ plural => 'orders',
+ pk => 'ordernumber',
+ },
+ 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',
+ },
+ old_issues => {
+ module => 'Koha::Old::Checkouts',
+ singular => 'old_checkout',
+ plural => 'old_checkouts',
+ fk => 'itemnumber',
+ },
+ overdues => {
+ module => 'Koha::Checkouts',
+ singular => 'overdue',
+ plural => 'overdues',
+ 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 ( $is_a_loop ) {
+ my $values = $tables->{$table} || [];
+ unless ( ref( $values ) eq 'ARRAY' ) {
+ croak "ERROR processing table $table. Wrong API call.";
+ }
+ my $key = $pk ? $pk : $fk;
+ # $key does not come from user input
+ my $objects = $module->search(
+ { $key => $values },
+ {
+ # We want to retrieve the data in the same order
+ # FIXME MySQLism
+ # field is a MySQLism, but they are no other way to do it
+ # To be generic we could do it in perl, but we will need to fetch
+ # all the data then order them
+ @$values ? ( order_by => \[ "field($key, " . join( ', ', @$values ) . ")" ] ) : ()
+ }
+ );
+ $params->{ $config->{$table}->{plural} } = $objects;
+ }
+ elsif ( $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
+ if ( ref( $fk ) eq 'ARRAY' ) { # Foreign key is multi-column
+ my $search;
+ foreach my $key ( @$fk ) {
+ $search->{$key} = $id->{$key};
+ }
+ $object = $module->search( $search )->last();
+ } else { # Foreign key is single column
+ $object = $module->search( { $fk => $id } )->last();
+ }
+ } 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} } )->last();
+ }
+ else { # Params are mutliple foreign keys
+ croak "Multiple foreign keys (table $table) should be passed using an hashref";
+ }
+ $params->{ $config->{$table}->{singular} } = $object;
+ }
+ }
+ else {
+ croak "ERROR LOADING MODULE $module: $Module::Load::Conditional::ERROR";
+ }
+ }
+
+ $params->{today} = output_pref({ dt => dt_from_string, dateformat => 'iso' });
+
+ return $params;
+}
+
+=head3 add_tt_filters
+
+$content = add_tt_filters( $content );
+
+Add TT filters to some specific fields if needed.
+
+For now we only add the Remove_MARC_punctuation TT filter to biblio and biblioitem fields
+
+=cut
+
+sub add_tt_filters {
+ my ( $content ) = @_;
+ $content =~ s|\[%\s*biblio\.(.*?)\s*%\]|[% biblio.$1 \| \$Remove_MARC_punctuation %]|gxms;
+ $content =~ s|\[%\s*biblioitem\.(.*?)\s*%\]|[% biblioitem.$1 \| \$Remove_MARC_punctuation %]|gxms;
+ return $content;
+}
+
+=head2 get_item_content
+
+ my $item = Koha::Items->find(...)->unblessed;
+ my @item_content_fields = qw( date_due title barcode author itemnumber );
+ my $item_content = C4::Letters::get_item_content({
+ item => $item,
+ item_content_fields => \@item_content_fields
+ });
+
+This function generates a tab-separated list of values for the passed item. Dates
+are formatted following the current setup.
+
+=cut
+
+sub get_item_content {
+ my ( $params ) = @_;
+ my $item = $params->{item};
+ my $dateonly = $params->{dateonly} || 0;
+ my $item_content_fields = $params->{item_content_fields} || [];
+
+ return unless $item;
+
+ my @item_info = map {
+ $_ =~ /^date|date$/
+ ? eval {
+ output_pref(
+ { dt => dt_from_string( $item->{$_} ), dateonly => $dateonly } );
+ }
+ : $item->{$_}
+ || ''
+ } @$item_content_fields;
+ return join( "\t", @item_info ) . "\n";
+}