Bug 33039: Add ability to specify a template for serial subscription "Published on (text)" field

Some libraries would like to have the text version of the serials
"published on" field auto-generated from a template. This template
should be definable at the subscription level.

Test Plan:
1) Apply this patch
2) Run updatedatabase.pl
3) Restart all the things!
4) Create or edit a new subscription
5) Edit the "Publication date template", create a template toolkit
   template.
   Keys available are the Koha::Subscription object as 'subscription'
   and the following serial table columns as keys:
    serialseq
    serialseq_x
    serialseq_y
    serialseq_z
    subscriptionid
    biblionumber
    status
    planneddate
    publisheddate
    publisheddateext
    notes
    routingnotes
   So your example template could be "[% subscription.subscriptionid %] [% biblionumber %]"
6) Generate the next serial
7) Note the next issue has a "Date published (text)" field based on the
   template you set!

Signed-off-by: Laura Escamilla <laura.escamilla@bywatersolutions.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Kyle Hall 2023-02-22 07:03:39 -05:00 committed by Tomas Cohen Arazi
parent 930eb65c94
commit cc478eaaa9
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
6 changed files with 107 additions and 15 deletions

View File

@ -20,9 +20,7 @@ package C4::Serials;
use Modern::Perl;
use C4::Auth qw( haspermission );
use C4::Context;
use DateTime;
use Carp qw( croak );
use Date::Calc qw(
Add_Delta_Days
Add_Delta_YM
@ -31,19 +29,24 @@ use Date::Calc qw(
N_Delta_YMD
Today
);
use DateTime;
use POSIX qw( strftime );
use Scalar::Util qw( looks_like_number );
use Try::Tiny;
use C4::Auth qw( haspermission );
use C4::Biblio qw( GetMarcFromKohaField ModBiblio );
use C4::Context;
use C4::Log qw( logaction ); # logaction
use C4::Serials::Frequency qw( GetSubscriptionFrequency );
use C4::Serials::Numberpattern;
use Koha::AdditionalFieldValues;
use Koha::Biblios;
use Koha::Serial;
use Koha::Subscriptions;
use Koha::Subscription::Histories;
use Koha::Suggestions;
use Koha::SharedContent;
use Scalar::Util qw( looks_like_number );
use Koha::Subscription::Histories;
use Koha::Subscriptions;
use Koha::Suggestions;
# Define statuses
use constant {
@ -894,6 +897,7 @@ sub GetNextSeq {
my $newlastvalue3string = _numeration( $newlastvalue3, $pattern->{numbering3}, $locale );
$calculated =~ s/\{Z\}/$newlastvalue3string/g;
}
}
return ($calculated,
@ -1287,7 +1291,7 @@ sub ModSubscription {
$biblionumber, $callnumber, $notes, $letter, $manualhistory,
$internalnotes, $serialsadditems, $staffdisplaycount, $opacdisplaycount,
$graceperiod, $location, $enddate, $subscriptionid, $skip_serialseq,
$itemtype, $previousitemtype, $mana_id, $ccode
$itemtype, $previousitemtype, $mana_id, $ccode, $published_on_template
) = @_;
my $subscription = Koha::Subscriptions->find($subscriptionid);
@ -1331,6 +1335,7 @@ sub ModSubscription {
previousitemtype => $previousitemtype,
mana_id => $mana_id,
ccode => $ccode,
published_on_template => $published_on_template,
}
)->store;
# FIXME Must be $subscription->serials
@ -1368,7 +1373,8 @@ sub NewSubscription {
$innerloop3, $status, $notes, $letter, $firstacquidate, $irregularity,
$numberpattern, $locale, $callnumber, $manualhistory, $internalnotes,
$serialsadditems, $staffdisplaycount, $opacdisplaycount, $graceperiod,
$location, $enddate, $skip_serialseq, $itemtype, $previousitemtype, $mana_id, $ccode
$location, $enddate, $skip_serialseq, $itemtype, $previousitemtype, $mana_id, $ccode,
$published_on_template,
) = @_;
my $dbh = C4::Context->dbh;
@ -1411,7 +1417,8 @@ sub NewSubscription {
itemtype => $itemtype,
previousitemtype => $previousitemtype,
mana_id => $mana_id,
ccode => $ccode
ccode => $ccode,
published_on_template => $published_on_template,
}
)->store;
$subscription->discard_changes;
@ -1587,6 +1594,61 @@ sub NewIssue {
my $subscription = Koha::Subscriptions->find( $subscriptionid );
if ( my $template = $subscription->published_on_template ) {
# If we detect a TT opening tag, run string through Template Toolkit Processor
if ( index( $template, '[%' ) != -1 ) { # Much faster than regex
my $use_template_cache = C4::Context->config('template_cache_dir')
&& defined $ENV{GATEWAY_INTERFACE};
my $tt = 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 $schema = Koha::Database->new->schema;
$schema->txn_begin;
try {
my $text;
$tt->process(
\$template,
{
subscription => $subscription,
serialseq => $serialseq,
serialseq_x => $subscription->lastvalue1(),
serialseq_y => $subscription->lastvalue2(),
serialseq_z => $subscription->lastvalue3(),
subscriptionid => $subscriptionid,
biblionumber => $biblionumber,
status => $status,
planneddate => $planneddate,
publisheddate => $publisheddate,
publisheddatetext => $publisheddatetext,
notes => $notes,
routingnotes => $routingnotes,
},
\$text
);
$publisheddatetext = $text;
}
catch {
croak "ERROR PROCESSING TEMPLATE: $_ :: " . $template->error();
}
finally {
$schema->txn_rollback;
};
} else {
$publisheddatetext = $template;
}
}
my $serial = Koha::Serial->new(
{
serialseq => $serialseq,
@ -1600,7 +1662,7 @@ sub NewIssue {
publisheddate => $publisheddate,
publisheddatetext => $publisheddatetext,
notes => $notes,
routingnotes => $routingnotes
routingnotes => $routingnotes,
}
)->store();

View File

@ -0,0 +1,18 @@
use Modern::Perl;
return {
bug_number => "33039",
description => "Add published on template to serial subscriptions table",
up => sub {
my ($args) = @_;
my ($dbh, $out) = @$args{qw(dbh out)};
if( !column_exists( 'subscription', 'published_on_template' ) ) {
$dbh->do(q{
ALTER TABLE subscription
ADD COLUMN `published_on_template` TEXT DEFAULT NULL COMMENT 'Template Toolkit syntax to generate the default "Published on (text)" field when receiving an issue this serial'
AFTER `ccode`
});
}
},
};

View File

@ -5516,6 +5516,7 @@ CREATE TABLE `subscription` (
`previousitemtype` varchar(10) DEFAULT NULL,
`mana_id` int(11) DEFAULT NULL,
`ccode` varchar(80) DEFAULT NULL COMMENT 'collection code to assign to serial items',
`published_on_template` TEXT DEFAULT NULL COMMENT 'Template Toolkit syntax to generate the default "Published on (text)" field when receiving an issue this serial',
PRIMARY KEY (`subscriptionid`),
KEY `subscription_ibfk_1` (`periodicity`),
KEY `subscription_ibfk_2` (`numberpattern`),

View File

@ -373,6 +373,10 @@ fieldset.rows table { clear: none; margin: 0; }
</select>
<span class="required">Required</span>
</li>
<li>
<label for="published_on_template">Publication date template:</label>
<textarea id="published_on_template" name="published_on_template">[% published_on_template | html %]</textarea>
</li>
<li>
<label for="locale">Locale:</label>
<select id="locale" name="locale">

View File

@ -330,6 +330,7 @@ sub redirect_add_subscription {
my $previousitemtype = $query->param('previousitemtype');
my $skip_serialseq = $query->param('skip_serialseq');
my $ccode = $query->param('ccode');
my $published_on_template = $query->param('published_on_template');
my $mana_id;
if ( $query->param('mana_id') ne "" ) {
@ -356,7 +357,7 @@ sub redirect_add_subscription {
join(";",@irregularity), $numberpattern, $locale, $callnumber,
$manualhistory, $internalnotes, $serialsadditems,
$staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate,
$skip_serialseq, $itemtype, $previousitemtype, $mana_id, $ccode
$skip_serialseq, $itemtype, $previousitemtype, $mana_id, $ccode, $published_on_template
);
if ( (C4::Context->preference('Mana') == 1) and ( grep { $_ eq "subscription" } split(/,/, C4::Context->preference('AutoShareWithMana'))) ){
my $result = Koha::SharedContent::send_entity( $query->param('mana_language') || '', $loggedinuser, $subscriptionid, 'subscription');
@ -438,6 +439,7 @@ sub redirect_mod_subscription {
my $previousitemtype = $query->param('previousitemtype');
my $skip_serialseq = $query->param('skip_serialseq');
my $ccode = $query->param('ccode');
my $published_on_template = $query->param('published_on_template');
my $mana_id;
if ( $query->param('mana_id') ne "" ) {
@ -473,7 +475,7 @@ sub redirect_mod_subscription {
$status, $biblionumber, $callnumber, $notes, $letter,
$manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
$opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
$skip_serialseq, $itemtype, $previousitemtype, $mana_id, $ccode
$skip_serialseq, $itemtype, $previousitemtype, $mana_id, $ccode, $published_on_template
);
my @additional_fields;

View File

@ -1775,7 +1775,7 @@ subtest 'move_to_biblio() tests' => sub {
subtest 'search_ordered' => sub {
plan tests => 6;
plan tests => 9;
$schema->storage->txn_begin;
@ -1837,7 +1837,8 @@ subtest 'search_ordered' => sub {
value => {
biblionumber => $biblio->biblionumber,
periodicity => $sub_freq->{id},
numberpattern => $sub_np->{id}
numberpattern => $sub_np->{id},
published_on_template => "[% publisheddatetext %] [% biblionumber %]",
}
}
);
@ -1892,6 +1893,10 @@ subtest 'search_ordered' => sub {
[ $item3->itemnumber, $item2->itemnumber, $item1->itemnumber ],
"serial - order by enumchron" );
is( $serial1->publisheddatetext, "publisheddatetext " . $biblio->biblionumber, "Column publisheddatetext rendered correctly from template for serial1" );
is( $serial2->publisheddatetext, "publisheddatetext " . $biblio->biblionumber, "Column publisheddatetext rendered correctly from template for serial2" );
is( $serial3->publisheddatetext, "publisheddatetext " . $biblio->biblionumber, "Column publisheddatetext rendered correctly from template for serial3" );
}
$schema->storage->txn_rollback;