From 77289cded8e18b48303352022d624e2c34d1f932 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Thu, 30 Jan 2025 16:48:40 +0100
Subject: [PATCH] Bug 36081: Use multivalue_preference

C4::Context->multivalue_preference is not used so far and split on |
However the values of "multiple" sysprefs are separated by... comma!

Let support both here.

This patch also removes silly JS code in the template.
---
 C4/Context.pm                                 |  6 +++--
 Koha/ArticleRequest.pm                        |  7 +++---
 Koha/Template/Plugin/Koha.pm                  |  5 ++++
 .../admin/preferences/circulation.pref        |  5 +++-
 .../en/modules/opac-request-article.tt        | 24 ++++++++-----------
 t/Context.t                                   |  5 +++-
 6 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/C4/Context.pm b/C4/Context.pm
index 8d49feda7c4..783ce4d5326 100644
--- a/C4/Context.pm
+++ b/C4/Context.pm
@@ -310,9 +310,11 @@ sub multivalue_preference {
     my ( $self, $preference ) = @_;
 
     my $syspref = $self->preference($preference) // q{};
-    my $values  = [ split qr{\|}, $syspref ];
 
-    return $values;
+    return [ split qr{\|}, $syspref ]
+        if $syspref =~ qr{\|};
+
+    return [ split qr{,}, $syspref ]
 }
 
 =head2 enable_syspref_cache
diff --git a/Koha/ArticleRequest.pm b/Koha/ArticleRequest.pm
index cacd5347dfd..1f45dacb576 100644
--- a/Koha/ArticleRequest.pm
+++ b/Koha/ArticleRequest.pm
@@ -266,10 +266,9 @@ sub store {
     if ( !$self->in_storage ) {
         $self->created_on( dt_from_string() );
     }
-    my $format = $self->format;
-    if ( C4::Context->preference('ArticleRequestsSupportedFormats') !~ /\b$format\b/ ) {
-        Koha::Exceptions::ArticleRequest::WrongFormat->throw;
-    }
+
+    Koha::Exceptions::ArticleRequest::WrongFormat->throw
+        unless grep { $_ eq $self->format } @{ C4::Context->multivalue_preference('ArticleRequestsSupportedFormats') };
 
     return $self->SUPER::store;
 }
diff --git a/Koha/Template/Plugin/Koha.pm b/Koha/Template/Plugin/Koha.pm
index 0b69249779d..88ece6fc141 100644
--- a/Koha/Template/Plugin/Koha.pm
+++ b/Koha/Template/Plugin/Koha.pm
@@ -71,6 +71,11 @@ sub Preference {
     return C4::Context->preference( $pref );
 }
 
+sub MultivaluePreference {
+    my ( $self, $pref ) = @_;
+    return C4::Context->multivalue_preference( $pref );
+}
+
 =head3 CSVDelimiter
 
 The delimiter option 'tabs' is stored in the DB as 'tabulation' to avoid issues
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref
index 65cf0b5030f..1bc4f269e7a 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref
@@ -1403,7 +1403,10 @@ Circulation:
         -
             - "The following article request formats are supported:"
             - pref: ArticleRequestsSupportedFormats
-            - ". Valid choices are currently: PHOTOCOPY and SCAN. Separate the supported formats by a vertical bar. The first listed format is selected by default when you request via the OPAC."
+              multiple_sortable:
+                PHOTOCOPY: Photocopy
+                SCAN: Scan
+            - "The first listed format is selected by default when you request via the OPAC."
 
 
     Item bundles:
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt
index a7a8cbdf84f..d145acf503c 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt
@@ -155,13 +155,21 @@
                                         <input type="text" name="patron_notes" id="patron_notes" size="50"/>
                                     </li>
 
+                                    [% SET ArticleRequestsSupportedFormats = Koha.MultivaluePreference('ArticleRequestsSupportedFormats') %]
+                                    [% IF ArticleRequestsSupportedFormats.size %]
                                     <li>
                                         <label for="format">Format:</label>
                                         <select name="format" id="format">
-                                            <option value="PHOTOCOPY">Photocopy</option>
-                                            <option value="SCAN">Digital scan</option>
+                                            [% FOR format IN ArticleRequestsSupportedFormats %]
+                                                [% IF format == 'PHOTOCOPY' %]
+                                                    <option value="PHOTOCOPY">Photocopy</option>
+                                                [% ELSIF format == 'SCAN' %]
+                                                    <option value="SCAN">Digital scan</option>
+                                                [% END %]
+                                            [% END %]
                                         </select>
                                     </li>
+                                    [% END %]
 
                                     [% IF AllPublicBranches.size > 1 %]
                                         <li>
@@ -299,18 +307,6 @@ $(document).ready( function() {
         }
     });
 
-    // Initialize format(s)
-    var supported_formats = "[% Koha.Preference('ArticleRequestsSupportedFormats') | html %]";
-    if( !supported_formats.match(/PHOTOCOPY/) )
-        $('#format option[value="PHOTOCOPY"]').remove();
-    if( !supported_formats.match(/SCAN/) )
-        $('#format option[value="SCAN"]').remove();
-
-    if( $('#format option').length > 1 ) {
-        // Select first listed format
-        var first_format = supported_formats.split('|')[0].replace(/^\s*|\s*$/g, '');
-        $('#format option[value="'+first_format+'"]').attr('selected', true);
-    }
 });
 </script>
 [% END %]
diff --git a/t/Context.t b/t/Context.t
index 9903719e8da..497fd47cd27 100755
--- a/t/Context.t
+++ b/t/Context.t
@@ -60,7 +60,7 @@ subtest 'yaml_preference() tests' => sub {
 
 subtest 'multivalue_preference() tests' => sub {
 
-    plan tests => 3;
+    plan tests => 4;
 
     t::lib::Mocks::mock_preference( 'MultiValuedSyspref', '' );
     is_deeply( C4::Context->multivalue_preference('MultiValuedSyspref'), [] );
@@ -70,6 +70,9 @@ subtest 'multivalue_preference() tests' => sub {
 
     t::lib::Mocks::mock_preference( 'MultiValuedSyspref', 'some|more|values' );
     is_deeply( C4::Context->multivalue_preference('MultiValuedSyspref'), [ 'some', 'more', 'values' ] );
+
+    t::lib::Mocks::mock_preference( 'MultiValuedSyspref', 'some,more,values' );
+    is_deeply( C4::Context->multivalue_preference('MultiValuedSyspref'), [ 'some', 'more', 'values' ] );
 };
 
 subtest 'needs_install() tests' => sub {
-- 
2.34.1