From 06d4f506dfd8d67b9014ca5041879acd72ce0e3d Mon Sep 17 00:00:00 2001 From: Emily-Rose Francoeur Date: Tue, 19 Dec 2023 08:49:23 -0500 Subject: [PATCH] Bug 35590: Translate the seasons when testing the numbering pattern with a selected locale MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch allows you to test a numbering pattern that uses seasons or their abbreviations with the locale of your choice. Since DateTime() cannot handle translations for seasons, the patch relies on the translations in the .po files. Therefore, you need to have the .po file installed if you want to test this patch. TEST PLAN Prequisites: Install the .po files for at least one language other than the one used in your staff interface (these files should be added to the misc/translator/po/ folder). Ensure that translations for seasons (Spring, Summer, Fall, and Winter) and their abbreviations (Spr, Sum, Fal, and Win) exist (these should be in the *-staff-prog.po). In my case, I use the .po files for French Canadian translations (fr-CA) because the translations for seasons and their abbreviations already exist. 1) Apply the patch. 2) Navigate to "Serials > Manage numbering patterns" and click on the "Edit" button for the "Seasonal" pattern. 3) Under "Test prediction pattern": - Set "First issue publication date" to today's date. - Set "Subscription length" to "issues" and input "4". - Choose the language you previously installed for "Locale" (in my case, it's "Français (French)"). - Set "Begins with" to 0 (for X) and 2023 (for Y). 4) Click on the "Test pattern" button. - Observe that the season names are displayed in the other language ("Printemps Été Automne Hiver" for me). 5) In the first section of the page, within the table, change "Formatting" to "Name of season (abbreviated)". 6) Click on the "Test pattern" button. - Observe that the season abbreviated names are displayed in the other language ("Pri Été Aut Hiv" for me). 7) Navigate to a record detail page and click on "+ New" and then "New subscription". 8) Click on the "Next ->" button and then "OK". 9) Fill in the following fields: - Set "First issue publication date" to today's date. - Set "Frequency" to your preference. - Set "Subscription length" to "issues" and input "4". - Set "Subscription start date" to today's date. - Select "Seasonal" for "Numbering pattern". - Choose a language different from the one used in the staff interface for "Locale" (any language except English). - Set "Begins with" to 0 (for Season) and 2023 (for Year). 10) Click on the "Test pattern" button. - Observe that the season names are displayed in the other language ("Printemps Été Automne Hiver" for me). 11) Run the unit tests for Serials and GetNextSeq - prove ./t/db_dependent/Serials.t - prove ./t/db_dependent/Serials/GetNextSeq.t --- C4/Serials.pm | 182 +++++++++--- .../modules/serials/showpredictionpattern.tt | 34 ++- .../en/modules/serials/subscription-add.tt | 258 ++++++++-------- .../serials/subscription-numberpatterns.tt | 130 ++++---- .../intranet-tmpl/prog/js/subscription-add.js | 49 ++- serials/showpredictionpattern.pl | 21 +- serials/subscription-add.pl | 1 + serials/subscription-numberpatterns.pl | 1 + t/db_dependent/Serials.t | 82 ++++- t/db_dependent/Serials/GetNextSeq.t | 1 + t/db_dependent/Serials/GetNextSeq.t.tdy | 279 ++++++++++++++++++ 11 files changed, 760 insertions(+), 278 deletions(-) create mode 100755 t/db_dependent/Serials/GetNextSeq.t.tdy diff --git a/C4/Serials.pm b/C4/Serials.pm index 39853a878b..d0269ff3c7 100644 --- a/C4/Serials.pm +++ b/C4/Serials.pm @@ -50,6 +50,8 @@ use Koha::Subscription::Routinglists; use Koha::Subscriptions; use Koha::Suggestions; use Koha::TemplateUtils qw( process_tt ); +use List::Util qw( first ); +use Locale::PO; # Define statuses use constant { @@ -108,6 +110,7 @@ BEGIN { findSerialsByStatus + GetSeasonsTranslations ); } @@ -319,22 +322,22 @@ sub GetFullSubscription { my $query = qq| SELECT serial.serialid, serial.serialseq, - serial.planneddate, - serial.publisheddate, + serial.planneddate, + serial.publisheddate, serial.publisheddatetext, - serial.status, + serial.status, serial.notes as notes, year(IF(serial.publisheddate IS NULL,serial.planneddate,serial.publisheddate)) as year, aqbooksellers.name as aqbooksellername, biblio.title as bibliotitle, subscription.branchcode AS branchcode, subscription.subscriptionid AS subscriptionid - FROM serial - LEFT JOIN subscription ON + FROM serial + LEFT JOIN subscription ON (serial.subscriptionid=subscription.subscriptionid ) - LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id - LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber - WHERE serial.subscriptionid = ? + LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id + LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber + WHERE serial.subscriptionid = ? ORDER BY year DESC, IF(serial.publisheddate IS NULL,serial.planneddate,serial.publisheddate) DESC, serial.subscriptionid @@ -460,22 +463,22 @@ sub GetFullSubscriptionsFromBiblionumber { my $query = qq| SELECT serial.serialid, serial.serialseq, - serial.planneddate, - serial.publisheddate, + serial.planneddate, + serial.publisheddate, serial.publisheddatetext, - serial.status, + serial.status, serial.notes as notes, year(IF(serial.publisheddate IS NULL,serial.planneddate,serial.publisheddate)) as year, biblio.title as bibliotitle, subscription.branchcode AS branchcode, subscription.subscriptionid AS subscriptionid, subscription.location AS location - FROM serial - LEFT JOIN subscription ON + FROM serial + LEFT JOIN subscription ON (serial.subscriptionid=subscription.subscriptionid) - LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id - LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber - WHERE subscription.biblionumber = ? + LEFT JOIN aqbooksellers on subscription.aqbooksellerid=aqbooksellers.id + LEFT JOIN biblio on biblio.biblionumber=subscription.biblionumber + WHERE subscription.biblionumber = ? ORDER BY year DESC, IF(serial.publisheddate IS NULL,serial.planneddate,serial.publisheddate) DESC, serial.subscriptionid @@ -726,7 +729,7 @@ sub GetSerials2 { my $query = q| SELECT serialid,serialseq, status, planneddate, publisheddate, publisheddatetext, notes, routingnotes - FROM serial + FROM serial WHERE subscriptionid=? | . q| AND status IN (| . join( ",", ('?') x @$statuses ) . q|)| . q| @@ -861,6 +864,7 @@ sub GetNextSeq { $newinnerloop1 = $subscription->{innerloop1} || 0; $newinnerloop2 = $subscription->{innerloop2} || 0; $newinnerloop3 = $subscription->{innerloop3} || 0; + my $translations = $subscription->{translations}; my %calc; foreach (qw/X Y Z/) { @@ -905,16 +909,16 @@ sub GetNextSeq { $newlastvalue3 = $pattern->{setto3} if ( $newlastvalue3 > $pattern->{whenmorethan3} ); } } - if ( $calc{'X'} ) { - my $newlastvalue1string = _numeration( $newlastvalue1, $pattern->{numbering1}, $locale ); + if($calc{'X'}) { + my $newlastvalue1string = _numeration( $newlastvalue1, $pattern->{numbering1}, $locale, $translations ); $calculated =~ s/\{X\}/$newlastvalue1string/g; } - if ( $calc{'Y'} ) { - my $newlastvalue2string = _numeration( $newlastvalue2, $pattern->{numbering2}, $locale ); + if($calc{'Y'}) { + my $newlastvalue2string = _numeration( $newlastvalue2, $pattern->{numbering2}, $locale, $translations ); $calculated =~ s/\{Y\}/$newlastvalue2string/g; } - if ( $calc{'Z'} ) { - my $newlastvalue3string = _numeration( $newlastvalue3, $pattern->{numbering3}, $locale ); + if($calc{'Z'}) { + my $newlastvalue3string = _numeration( $newlastvalue3, $pattern->{numbering3}, $locale, $translations ); $calculated =~ s/\{Z\}/$newlastvalue3string/g; } @@ -952,21 +956,20 @@ sub GetSeq { my $locale = $subscription->{locale}; + my $translations = $subscription->{translations}; + my $calculated = $pattern->{numberingmethod}; my $newlastvalue1 = $subscription->{'lastvalue1'} || 0; - $newlastvalue1 = _numeration( $newlastvalue1, $pattern->{numbering1}, $locale ) - if ( $pattern->{numbering1} ); # reset counter if needed. + $newlastvalue1 = _numeration($newlastvalue1, $pattern->{numbering1}, $locale, $translations) if ($pattern->{numbering1}); # reset counter if needed. $calculated =~ s/\{X\}/$newlastvalue1/g; my $newlastvalue2 = $subscription->{'lastvalue2'} || 0; - $newlastvalue2 = _numeration( $newlastvalue2, $pattern->{numbering2}, $locale ) - if ( $pattern->{numbering2} ); # reset counter if needed. + $newlastvalue2 = _numeration($newlastvalue2, $pattern->{numbering2}, $locale, $translations) if ($pattern->{numbering2}); # reset counter if needed. $calculated =~ s/\{Y\}/$newlastvalue2/g; my $newlastvalue3 = $subscription->{'lastvalue3'} || 0; - $newlastvalue3 = _numeration( $newlastvalue3, $pattern->{numbering3}, $locale ) - if ( $pattern->{numbering3} ); # reset counter if needed. + $newlastvalue3 = _numeration($newlastvalue3, $pattern->{numbering3}, $locale, $translations) if ($pattern->{numbering3}); # reset counter if needed. $calculated =~ s/\{Z\}/$newlastvalue3/g; my $dt = dt_from_string( $subscription->{firstacquidate} ); @@ -1072,7 +1075,7 @@ sub ModSubscriptionHistory { return unless ($subscriptionid); my $dbh = C4::Context->dbh; - my $query = "UPDATE subscriptionhistory + my $query = "UPDATE subscriptionhistory SET histstartdate=?,histenddate=?,recievedlist=?,missinglist=?,opacnote=?,librariannote=? WHERE subscriptionid=? "; @@ -1279,7 +1282,7 @@ sub GetNextExpected { ModNextExpected($subscriptionid,$date) Update the planneddate for the current expected issue of the subscription. -This will modify all future prediction results. +This will modify all future prediction results. C<$date> is an ISO date. @@ -1582,7 +1585,7 @@ sub ReNewSubscription { my $subscription = GetSubscription($subscriptionid); my $query = qq| SELECT * - FROM biblio + FROM biblio LEFT JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber WHERE biblio.biblionumber=? |; @@ -1719,7 +1722,7 @@ sub NewIssue { 1 or 0 = HasSubscriptionStrictlyExpired($subscriptionid) -the subscription has stricly expired when today > the end subscription date +the subscription has stricly expired when today > the end subscription date return : 1 if true, 0 if false, -1 if the expiration date is not set. @@ -1907,7 +1910,7 @@ sub DelIssue { this function selects missing issues on database - where serial.status = MISSING* or serial.status = LATE or planneddatedbh; my $sth = $dbh->prepare( - "SELECT count(routingid) routingids FROM subscription LEFT JOIN subscriptionroutinglist + "SELECT count(routingid) routingids FROM subscription LEFT JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid WHERE subscription.subscriptionid = ? GROUP BY routingid ORDER BY ranking ASC " @@ -2172,7 +2175,7 @@ sub getroutinglist { my $dbh = C4::Context->dbh; my $sth = $dbh->prepare( 'SELECT routingid, borrowernumber, ranking, biblionumber - FROM subscription + FROM subscription JOIN subscriptionroutinglist ON subscription.subscriptionid = subscriptionroutinglist.subscriptionid WHERE subscription.subscriptionid = ? ORDER BY ranking ASC' ); @@ -2240,7 +2243,7 @@ sub HasItems { my $dbh = C4::Context->dbh; my $query = q| SELECT COUNT(serialitems.itemnumber) - FROM serial + FROM serial LEFT JOIN serialitems USING(serialid) WHERE subscriptionid=? AND serialitems.serialid IS NOT NULL |; @@ -2606,7 +2609,7 @@ num_type can take : =cut sub _numeration { - my ( $value, $num_type, $locale ) = @_; + my ($value, $num_type, $locale, $translations) = @_; $value ||= 0; $num_type //= ''; $locale ||= 'en'; @@ -2637,13 +2640,11 @@ sub _numeration { ? $dt->format_cldr("LLLL") : $dt->strftime("%b"); } elsif ( $num_type =~ /^season$/ ) { - my @seasons = qw( Spring Summer Fall Winter ); - $value = $value % 4; - $string = $seasons[$value]; + $value = $value % 4; + $string = $translations->{seasons}->[$value]; } elsif ( $num_type =~ /^seasonabrv$/ ) { - my @seasonsabrv = qw( Spr Sum Fal Win ); - $value = $value % 4; - $string = $seasonsabrv[$value]; + $value = $value % 4; + $string = $translations->{seasonsabrv}->[$value]; } else { $string = $value; } @@ -2713,6 +2714,58 @@ sub ReopenSubscription { $sth->execute( EXPECTED, $subscriptionid, STOPPED ); } +=head2 GetSeasonsTranslations + +Provide translations for the seasons and their abbreviations for the specified locale + +=cut + +sub GetSeasonsTranslations { + my ( $need_seasons, $need_seasonsabrv, $subtag ) = @_; + my @seasons = qw( Spring Summer Fall Winter ); + my @seasonsabrv = qw( Spr Sum Fal Win ); + my @error_messages = qw(); + + my @dirs = ( + C4::Context->config('intranetdir') . '/misc/translator/po', + C4::Context->config('intranetdir') . '/../../misc/translator/po', + ); + + my $dir = first { -d } @dirs; + + if ($dir) { + my @po_paths = glob("$dir/$subtag*staff-prog.po"); + if (@po_paths) { + my $po_path = $po_paths[0]; + my $po_filename = ( $po_path =~ s/$dir\///gr ); + my $po_data = Locale::PO->load_file_ashash( $po_path, 'utf8' ); + + if ($need_seasons) { + my $response = _translate( $po_data, @seasons ); + @seasons = @{ $response->{translations} }; + @error_messages = + ( @error_messages, map { sprintf( $_, $po_filename ) } @{ $response->{error_messages} } ); + } + + if ($need_seasonsabrv) { + my $response = _translate( $po_data, @seasonsabrv ); + @seasonsabrv = @{ $response->{translations} }; + @error_messages = + ( @error_messages, map { sprintf( $_, $po_filename ) } @{ $response->{error_messages} } ); + } + } else { + push( + @error_messages, + "No translation file found for $subtag. Ensure that the translation files for $subtag are installed." + ); + } + } else { + push( @error_messages, "The PO directory has not been found. There is a problem in your Koha installation." ); + } + + return { seasons => \@seasons, seasonsabrv => \@seasonsabrv, error_messages => \@error_messages }; +} + =head2 subscriptionCurrentlyOnOrder $bool = subscriptionCurrentlyOnOrder( $subscriptionid ); @@ -2803,6 +2856,47 @@ sub _can_do_on_subscription { return 0; } +=head2 _translate + +Provide translations for the given words based on the provided data + +=cut + +sub _translate { + my ( $data, @words ) = @_; + + my @translations = qw(); + my @error_messages = qw(); + + for my $word (@words) { + my $regex = qr/^"$word"$|\%s$word\%s/; + my @msgids = grep /$regex/, keys %{$data}; + my $translation = ""; + + while ( ( $translation eq "" ) && ( my $msgid = shift(@msgids) ) ) { + next unless ( $data->{$msgid}->reference =~ /serials/ ); + my $msgstr = $data->{$msgid}->msgstr; + my @ids = split( /\%s/, $msgid ); + my @strs = split( /\%s/, $msgstr ); + + next unless ( scalar @ids == scalar @strs ); + + my $idx = first { $ids[$_] eq $word || $ids[$_] eq $msgid } 0 .. $#ids; + $translation = Locale::PO->dequote( $strs[$idx] ) if ( defined $idx ); + } + + if ( $translation eq "" ) { + push( @translations, $word ); + push( @error_messages, "The translation for $word doesn't exist in the file \%s" ); + next; + } + + push( @translations, $translation ); + } + + return { translations => \@translations, error_messages => \@error_messages }; +} + =head2 findSerialsByStatus @serials = findSerialsByStatus($status, $subscriptionid); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/showpredictionpattern.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/showpredictionpattern.tt index 7522a9d7fc..76ff4ccaf2 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/showpredictionpattern.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/showpredictionpattern.tt @@ -68,20 +68,30 @@ [% IF (ask_for_irregularities) %] [% UNLESS (loop.first) %] - [% IF (prediction.not_published) %] - - [% ELSE %] - - [% END %] + [% IF (prediction.not_published) %] + + [% ELSE %] + [% END %] + [% END %] [% END %] - - [% END %] - - + + [% END %] + + + +[% IF (error_messages) %] +
+

Warning

+ [% FOREACH error_message IN error_messages %] +

[% error_message | html %]

+ [% END %] +
+[% END %] + +
+ +
-
- -
[% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt index 1e62a1108e..6f539d6164 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-add.tt @@ -305,7 +305,8 @@ > [% END %] - +
Serials planning @@ -316,14 +317,14 @@ [% ELSE %] [% firstacquidate | $KohaDates %] - + [% END %] Required [% IF (more_than_one_serial) %]
  • - - + +
  • [% END %]
  • @@ -331,7 +332,13 @@
  • @@ -340,31 +347,34 @@ (enter amount in numerals) - +
  • @@ -379,7 +389,13 @@ Required @@ -394,12 +410,13 @@ [% FOREACH l IN locales %] [% IF l.language == locale %] - + [% ELSE %] - + [% END %] [% END %] +
    If empty, English is used
  • @@ -417,9 +434,9 @@ [% IF (more_than_one_serial) %] - Last value: + Last value: [% ELSE %] - Begins with: + Begins with: [% END %] @@ -442,107 +459,108 @@
  • -
    [% IF additional_fields %] -
    [% INCLUDE 'additional-fields-entry.inc' available=additional_fields values=additional_field_values %]
    +
    + [% INCLUDE 'additional-fields-entry.inc' available=additional_fields values=additional_field_values %] +
    [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-numberpatterns.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-numberpatterns.tt index 64217e7a9a..8a3808616b 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-numberpatterns.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-numberpatterns.tt @@ -192,65 +192,66 @@
    - Test prediction pattern -
      -
    1. - - -
    2. -
    3. - - -
    4. -
    5. - - - -
    6. -
    7. - - - If empty, English is used -
    8. -
    - - - - - - - - - - - - - - - - - - - - - - - -
     XYZ
    Begins with
    Inner counter
    -
    + Test prediction pattern +
      +
    1. + + +
    2. +
    3. + + +
    4. +
    5. + + + +
    6. +
    7. + + + If empty, English is used + +
    8. +
    + + + + + + + + + + + + + + + + + + + + + + + +
     XYZ
    Begins with
    Inner counter
    +
    @@ -331,6 +332,10 @@ [% INCLUDE 'calendar.inc' %] [% INCLUDE 'datatables.inc' %] [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js b/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js index 76aded3f56..89835f5ae0 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js +++ b/koha-tmpl/intranet-tmpl/prog/js/subscription-add.js @@ -384,37 +384,12 @@ function testPredictionPattern() { } var ajaxParams = [ - "to", - "subtype", - "sublength", - "frequency", - "numberingmethod", - "lastvalue1", - "lastvalue2", - "lastvalue3", - "add1", - "add2", - "add3", - "every1", - "every2", - "every3", - "innerloop1", - "innerloop2", - "innerloop3", - "setto1", - "setto2", - "setto3", - "numbering1", - "numbering2", - "numbering3", - "whenmorethan1", - "whenmorethan2", - "whenmorethan3", - "locale", - "sfdescription", - "unitsperissue", - "issuesperunit", - "unit", + 'to', 'subtype', 'sublength', 'frequency', 'numberingmethod', + 'lastvalue1', 'lastvalue2', 'lastvalue3', 'add1', 'add2', 'add3', + 'every1', 'every2', 'every3', 'innerloop1', 'innerloop2', 'innerloop3', + 'setto1', 'setto2', 'setto3', 'numbering1', 'numbering2', 'numbering3', + 'whenmorethan1', 'whenmorethan2', 'whenmorethan3', 'locale', 'subtag', + 'sfdescription', 'unitsperissue', 'issuesperunit', 'unit' ]; for (i in ajaxParams) { var param = ajaxParams[i]; @@ -688,7 +663,11 @@ function mana_comment_close() { $("#new_mana_comment").hide(); } -function showPredictionPatternTest(data) { +function setSubtag () { + $("#subtag").val($("#locale").find(':selected').data('subtag')); +} + +function showPredictionPatternTest( data ){ $("#displayexample").html(data).show(); $("#page_2 > div").attr("class", "col-sm-6"); } @@ -891,4 +870,10 @@ $(document).ready(function () { const display = $(this).val() == "1" ? "flex" : "none"; $(".use_items").css("display", display).find("select").val(""); }); + + $("#locale").on("change", function() { + setSubtag(); + }); + + setSubtag(); }); diff --git a/serials/showpredictionpattern.pl b/serials/showpredictionpattern.pl index 19d414ccba..fd82f22dcd 100755 --- a/serials/showpredictionpattern.pl +++ b/serials/showpredictionpattern.pl @@ -30,11 +30,12 @@ publication date, based on frequency and first publication date. use Modern::Perl; -use CGI qw ( -utf8 ); -use Date::Calc qw( Add_Delta_Days Add_Delta_YM Day_of_Week Delta_Days ); -use C4::Auth qw( get_template_and_user ); -use C4::Output qw( output_html_with_http_headers ); -use C4::Serials qw( GetSubscription GetFictiveIssueNumber GetSeq GetSubscriptionIrregularities GetNextDate GetNextSeq ); +use CGI qw ( -utf8 ); +use Date::Calc qw( Add_Delta_Days Add_Delta_YM Day_of_Week Delta_Days ); +use C4::Auth qw( get_template_and_user ); +use C4::Output qw( output_html_with_http_headers ); +use C4::Serials + qw( GetSubscription GetFictiveIssueNumber GetSeq GetSubscriptionIrregularities GetNextDate GetNextSeq GetSeasonsTranslations ); use C4::Serials::Frequency; use Koha::DateUtils qw( dt_from_string ); @@ -97,6 +98,14 @@ $enddate = dt_from_string($enddate)->ymd if $enddate; $nextacquidate = $nextacquidate ? dt_from_string($nextacquidate)->ymd : $firstacquidate; my $date = $nextacquidate; +my $need_seasons = grep { $_ =~ /^season$/ } %pattern; +my $need_seasonsabrv = grep { $_ =~ /^seasonabrv$/ } %pattern; +my $subtag = scalar $input->param('subtag'); + +my $translations; +$translations = GetSeasonsTranslations( $need_seasons, $need_seasonsabrv, $subtag ) + if ( $need_seasons || $need_seasonsabrv ); + my %subscription = ( locale => scalar $input->param('locale') // '', lastvalue1 => scalar $input->param('lastvalue1') // '', @@ -108,6 +117,7 @@ my %subscription = ( irregularity => '', countissuesperunit => 1, firstacquidate => $firstacquidate, + translations => $translations, ); my $issuenumber; @@ -189,6 +199,7 @@ while ( $i < 1000 ) { $template->param( predictions_loop => \@predictions_loop, + error_messages => \@{ $translations->{error_messages} }, ); if ( $frequency->{unit} and not $custompattern ) { diff --git a/serials/subscription-add.pl b/serials/subscription-add.pl index 8dff969978..61340f2d41 100755 --- a/serials/subscription-add.pl +++ b/serials/subscription-add.pl @@ -231,6 +231,7 @@ if ( $op eq 'cud-addsubscription' ) { map { { language => $_->{iso639_2_code}, + subtag => $_->{subtag}, description => $_->{language_description} || $_->{language} } } @{ C4::Languages::getAllLanguages() } diff --git a/serials/subscription-numberpatterns.pl b/serials/subscription-numberpatterns.pl index 9211240650..f7c9cae93e 100755 --- a/serials/subscription-numberpatterns.pl +++ b/serials/subscription-numberpatterns.pl @@ -119,6 +119,7 @@ if ( $op && ( $op eq 'new' || $op eq 'modify' ) ) { map { { language => $_->{iso639_2_code}, + subtag => $_->{subtag}, description => $_->{language_description} || $_->{language} } } @{ C4::Languages::getAllLanguages() } diff --git a/t/db_dependent/Serials.t b/t/db_dependent/Serials.t index 9ddc4719dd..3b0de3d444 100755 --- a/t/db_dependent/Serials.t +++ b/t/db_dependent/Serials.t @@ -6,25 +6,27 @@ use Modern::Perl; use C4::Serials - qw( getroutinglist updateClaim NewSubscription GetSubscription GetSubscriptionHistoryFromSubscriptionId SearchSubscriptions ModSubscription GetExpirationDate GetSerials GetSerialInformation NewIssue AddItem2Serial DelSubscription GetFullSubscription PrepareSerialsData GetSubscriptionsFromBiblionumber ModSubscriptionHistory GetSerials2 GetLatestSerials GetNextSeq GetSeq CountSubscriptionFromBiblionumber ModSerialStatus findSerialsByStatus HasSubscriptionStrictlyExpired HasSubscriptionExpired GetLateOrMissingIssues check_routing addroutingmember GetNextDate ); + qw( getroutinglist updateClaim NewSubscription GetSubscription GetSubscriptionHistoryFromSubscriptionId SearchSubscriptions ModSubscription GetExpirationDate GetSerials GetSerialInformation NewIssue AddItem2Serial DelSubscription GetFullSubscription PrepareSerialsData GetSubscriptionsFromBiblionumber ModSubscriptionHistory GetSerials2 GetLatestSerials GetNextSeq GetSeq CountSubscriptionFromBiblionumber ModSerialStatus findSerialsByStatus HasSubscriptionStrictlyExpired HasSubscriptionExpired GetLateOrMissingIssues check_routing addroutingmember GetNextDate GetSeasonsTranslations ); use C4::Serials::Frequency; use C4::Serials::Numberpattern; use C4::Biblio qw( AddBiblio GetMarcFromKohaField ); use C4::Budgets qw( AddBudgetPeriod AddBudget ); -use C4::Items qw( AddItemFromMarc ); +use C4::Context; +use C4::Items qw( AddItemFromMarc ); +use Encode qw( encode ); +use File::Temp; use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Acquisition::Booksellers; use t::lib::Mocks; use t::lib::TestBuilder; use Test::MockModule; -use Test::NoWarnings; use Test::More tests => 64; BEGIN { use_ok( 'C4::Serials', - qw( updateClaim NewSubscription GetSubscription GetSubscriptionHistoryFromSubscriptionId SearchSubscriptions ModSubscription GetExpirationDate GetSerials GetSerialInformation NewIssue AddItem2Serial DelSubscription GetFullSubscription PrepareSerialsData GetSubscriptionsFromBiblionumber ModSubscriptionHistory GetSerials2 GetLatestSerials GetNextSeq GetSeq CountSubscriptionFromBiblionumber ModSerialStatus findSerialsByStatus HasSubscriptionStrictlyExpired HasSubscriptionExpired GetLateOrMissingIssues check_routing addroutingmember GetNextDate ) + qw( updateClaim NewSubscription GetSubscription GetSubscriptionHistoryFromSubscriptionId SearchSubscriptions ModSubscription GetExpirationDate GetSerials GetSerialInformation NewIssue AddItem2Serial DelSubscription GetFullSubscription PrepareSerialsData GetSubscriptionsFromBiblionumber ModSubscriptionHistory GetSerials2 GetLatestSerials GetNextSeq GetSeq CountSubscriptionFromBiblionumber ModSerialStatus findSerialsByStatus HasSubscriptionStrictlyExpired HasSubscriptionExpired GetLateOrMissingIssues check_routing addroutingmember GetNextDate GetSeasonsTranslations ) ); } @@ -740,7 +742,7 @@ subtest "test numbering pattern with dates in GetSeq GetNextSeq" => sub { subtest "_numeration" => sub { - plan tests => 6; + plan tests => 8; my $s = C4::Serials::_numeration( 0, 'monthname', 'cat' ); is( $s, "gener" ); @@ -755,4 +757,74 @@ subtest "_numeration" => sub { like( $s, qr{^ene\.?} ); $s = C4::Serials::_numeration( 0, 'monthabrv', 'fr' ); is( $s, "janv." ); + + my $translations = { + seasons => [ 'Spring', 'Summer', 'Fall', 'Winter' ], + seasonsabrv => [ 'Spr', 'Sum', 'Fal', 'Win' ], + }; + + $s = C4::Serials::_numeration( 0, 'season', 'en', $translations ); + is( $s, "Spring" ); + $s = C4::Serials::_numeration( 0, 'seasonabrv', 'en', $translations ); + is( $s, "Spr" ); +}; + +subtest "GetSeasonsTranslations" => sub { + + plan tests => 8; + + my @seasons = qw( Printemps Été Automne Hiver ); + my @seasonsabrv = qw( Pri Été Aut Hiv ); + + my $dir = C4::Context->config('intranetdir') . '/misc/translator/po/'; + my $fh = File::Temp->new( DIR => $dir, SUFFIX => '-staff-prog.po' ); + my $filename = $fh->filename; + + my $txt = ' +#: koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt:530 +#, c-format +msgid "Spring" +msgstr "Printemps" + +#: koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt:530 +#, c-format +msgid "Summer" +msgstr "Été" + +#: koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt:530 +#, c-format +msgid "Fall" +msgstr "Automne" + +#: koha-tmpl/intranet-tmpl/prog/en/modules/serials/subscription-detail.tt:530 +#, c-format +msgid "Winter" +msgstr "Hiver" + +#: koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt:208 +#: koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt:210 +#, c-format +msgid "Sum" +msgstr "Total" + +#: koha-tmpl/intranet-tmpl/prog/en/modules/serials/serials-collection.tt:248 +#: koha-tmpl/intranet-tmpl/prog/en/modules/serials/showpredictionpattern.tt:43 +#, c-format +msgid "" +"%s %sSpring%s %s %sSummer%s %s %sFall%s %s %sWinter%s %s %sSpr%s %s %sSum%s " +"%s %sFal%s %s %sWin%s %s %s %s " +msgstr "" +"%s %sPrintemps%s %s %sÉté%s %s %sAutomne%s %s %sHiver%s %s %sPri%s %s %sÉté" +"%s %s %sAut%s %s %sHiv%s %s %s %s " + '; + + print $fh encode( 'utf-8', $txt ); + close($fh); + + my $translations = GetSeasonsTranslations( 1, 1, $filename =~ s/($dir|-staff-prog.po)//gr ); + + for my $idx ( 0 .. $#{ $translations->{seasons} } ) { + is( $translations->{seasons}->[$idx], $seasons[$idx] ); + is( $translations->{seasonsabrv}->[$idx], $seasonsabrv[$idx] ); + } }; diff --git a/t/db_dependent/Serials/GetNextSeq.t b/t/db_dependent/Serials/GetNextSeq.t index fb43902217..54217c87f7 100755 --- a/t/db_dependent/Serials/GetNextSeq.t +++ b/t/db_dependent/Serials/GetNextSeq.t @@ -144,6 +144,7 @@ $subscription = { irregularity => '3;4;6', countissuesperunit => 1, locale => 'C', + translations => { seasons => [ 'Spring', 'Summer', 'Fall', 'Winter' ] }, }; $pattern = { add1 => 1, add2 => 0, add3 => 0, diff --git a/t/db_dependent/Serials/GetNextSeq.t.tdy b/t/db_dependent/Serials/GetNextSeq.t.tdy new file mode 100755 index 0000000000..cbca088e8d --- /dev/null +++ b/t/db_dependent/Serials/GetNextSeq.t.tdy @@ -0,0 +1,279 @@ +#!/usr/bin/perl + +use C4::Context; +use Test::More tests => 32; +use Modern::Perl; +use Koha::Database; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +use C4::Serials::Frequency; +use C4::Serials qw( GetNextDate GetNextSeq ); + +my $frequency = { + description => "One issue per day", + unit => 'day', + issuesperunit => 1, + unitsperissue => 1, +}; +my $id = AddSubscriptionFrequency($frequency); + +# TEST CASE 1 - 1 variable, from 1 to 4 +my $pattern = { + add1 => 1, add2 => 0, add3 => 0, + every1 => 1, every2 => 0, every3 => 0, + whenmorethan1 => 4, whenmorethan2 => 0, whenmorethan3 => 0, + setto1 => 1, setto2 => 0, setto3 => 0, + numberingmethod => 'X: {X}', + numbering1 => '', + numbering2 => '', + numbering3 => '', +}; + +my $subscription = { + periodicity => $id, + firstacquidate => '1970-01-01', + lastvalue1 => 1, lastvalue2 => 1, lastvalue3 => 1, + innerloop1 => 0, innerloop2 => 0, innerloop3 => 0, + skip_serialseq => 1, + irregularity => '3;5', + countissuesperunit => 1, + locale => 'en', +}; +my $publisheddate = $subscription->{firstacquidate}; + +my $seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: 2' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: 4' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: 2' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: 3' ); + +# TEST CASE 2 - 1 variable, use 'dayname' numbering, from 1 to 7 +$subscription = { + periodicity => $id, + firstacquidate => '1970-01-01', + lastvalue1 => 1, lastvalue2 => 1, lastvalue3 => 1, + innerloop1 => 0, innerloop2 => 0, innerloop3 => 0, + skip_serialseq => 1, + irregularity => '3;4;6', + countissuesperunit => 1, + locale => 'C', +}; +$pattern = { + add1 => 1, add2 => 0, add3 => 0, + every1 => 1, every2 => 0, every3 => 0, + whenmorethan1 => 7, whenmorethan2 => 0, whenmorethan3 => 0, + setto1 => 1, setto2 => 0, setto3 => 0, + numberingmethod => 'X: {X}', + numbering1 => 'dayname', + numbering2 => '', + numbering3 => '', +}; + +$publisheddate = $subscription->{firstacquidate}; + +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: Tuesday' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: Friday' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: Sunday' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: Monday' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: Tuesday' ); + +# TEST CASE 3 - 1 variable, use 'monthname' numbering, from 0 to 11 by step of 2 +$subscription = { + periodicity => $id, + firstacquidate => '1970-01-01', + lastvalue1 => 0, lastvalue2 => 1, lastvalue3 => 1, + innerloop1 => 0, innerloop2 => 0, innerloop3 => 0, + skip_serialseq => 1, + irregularity => '3;4;6', + countissuesperunit => 1, + locale => 'C', +}; +$pattern = { + add1 => 2, add2 => 0, add3 => 0, + every1 => 1, every2 => 0, every3 => 0, + whenmorethan1 => 11, whenmorethan2 => 0, whenmorethan3 => 0, + setto1 => 0, setto2 => 0, setto3 => 0, + numberingmethod => 'X: {X}', + numbering1 => 'monthname', + numbering2 => '', + numbering3 => '', +}; + +$publisheddate = $subscription->{firstacquidate}; + +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: March' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: September' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: January' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: March' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: May' ); + +# TEST CASE 4 - 1 variable, use 'season' numbering, from 0 to 3 +$subscription = { + periodicity => $id, + firstacquidate => '1970-01-01', + lastvalue1 => 0, lastvalue2 => 1, lastvalue3 => 1, + innerloop1 => 0, innerloop2 => 0, innerloop3 => 0, + skip_serialseq => 1, + irregularity => '3;4;6', + countissuesperunit => 1, + locale => 'C', + translations => { seasons => [ 'Spring', 'Summer', 'Fall', 'Winter' ] }, +}; +$pattern = { + add1 => 1, add2 => 0, add3 => 0, + every1 => 1, every2 => 0, every3 => 0, + whenmorethan1 => 3, whenmorethan2 => 0, whenmorethan3 => 0, + setto1 => 0, setto2 => 0, setto3 => 0, + numberingmethod => 'X: {X}', + numbering1 => 'season', + numbering2 => '', + numbering3 => '', +}; + +$publisheddate = $subscription->{firstacquidate}; + +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: Summer' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: Spring' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: Fall' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: Winter' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'X: Spring' ); + +# TEST CASE 5 - 2 variables, from 1 to 12, and from 1 to 4 +$subscription = { + periodicity => $id, + firstacquidate => '1970-01-01', + lastvalue1 => 1, lastvalue2 => 1, lastvalue3 => 1, + innerloop1 => 0, innerloop2 => 0, innerloop3 => 0, + skip_serialseq => 1, + irregularity => '3;4;6', + countissuesperunit => 1, + locale => 'C', +}; +$pattern = { + add1 => 1, add2 => 1, add3 => 0, + every1 => 1, every2 => 4, every3 => 0, + whenmorethan1 => 4, whenmorethan2 => 12, whenmorethan3 => 0, + setto1 => 1, setto2 => 1, setto3 => 0, + numberingmethod => 'Y: {Y}, X: {X}', + numbering1 => '', + numbering2 => '', + numbering3 => '', +}; + +$publisheddate = $subscription->{firstacquidate}; + +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Y: 1, X: 2' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Y: 2, X: 1' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Y: 2, X: 3' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Y: 2, X: 4' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Y: 3, X: 1' ); + +# TEST CASE 6 - 3 variables, from 1 to 12, from 1 to 8, and from 1 to 4 +$subscription = { + periodicity => $id, + firstacquidate => '1970-01-01', + lastvalue1 => 1, lastvalue2 => 1, lastvalue3 => 1, + innerloop1 => 0, innerloop2 => 0, innerloop3 => 0, + skip_serialseq => 1, + irregularity => '3;4;6;110', + countissuesperunit => 1, + locale => 'C', +}; +$pattern = { + add1 => 1, add2 => 1, add3 => 1, + every1 => 1, every2 => 4, every3 => 32, + whenmorethan1 => 4, whenmorethan2 => 8, whenmorethan3 => 12, + setto1 => 1, setto2 => 1, setto3 => 1, + numberingmethod => 'Z: {Z}, Y: {Y}, X: {X}', + numbering1 => '', + numbering2 => '', + numbering3 => '', +}; + +$publisheddate = $subscription->{firstacquidate}; + +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Z: 1, Y: 1, X: 2' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Z: 1, Y: 2, X: 1' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Z: 1, Y: 2, X: 3' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Z: 1, Y: 2, X: 4' ); + +for ( 1 .. 100 ) { + $publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); + $seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +} +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Z: 4, Y: 4, X: 1' ); + +# 110th is here +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Z: 4, Y: 4, X: 3' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Z: 4, Y: 4, X: 4' ); +$publisheddate = GetNextDate( $subscription, $publisheddate, $frequency ); +$seq = _next_seq( $subscription, $pattern, $frequency, $publisheddate ); +is( $seq, 'Z: 4, Y: 5, X: 1' ); + +sub _next_seq { + my ( $subscription, $pattern, $frequency, $publisheddate ) = @_; + my $seq; + ( + $seq, $subscription->{lastvalue1}, $subscription->{lastvalue2}, + $subscription->{lastvalue3}, $subscription->{innerloop1}, + $subscription->{innerloop2}, $subscription->{innerloop3} + ) = GetNextSeq( $subscription, $pattern, $frequency, $publisheddate ); + return $seq; +} -- 2.43.0