From 0a99e4f7b455d1818d1b98c914bb3e99aa7abf5b Mon Sep 17 00:00:00 2001
From: jeremy breuillard <jeremy.breuillard@biblibre.com>
Date: Mon, 21 Mar 2022 15:29:57 +0100
Subject: [PATCH] Bug 29311: Do not allow editing of bibliographic information
 when entering suggestion from existing bibs

This patch changes the behavior of the suggestion form in OPAC. The suggestion form for an existing record has its total of inputs limited, otherwise the inputs are all available.

Test plan:
1)OPAC: Login to your OPAC account
2)Choose a record and get onto its details page
3)Click 'suggest for purchase'
4)Notice: all the fields can be modified
5)Apply patch
6)Repeat 2) and 3) or refresh
7)You see that the fields about bibliographic informations are disabled now
---
 .../bootstrap/en/modules/opac-suggestions.tt  | 47 ++++++++++++++++---
 opac/opac-suggestions.pl                      | 14 +++++-
 2 files changed, 53 insertions(+), 8 deletions(-)

diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-suggestions.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-suggestions.tt
index 7bee65b938..1477d3776e 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-suggestions.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-suggestions.tt
@@ -113,7 +113,10 @@
                                         [% END %]
                                         [% UNLESS ( isbn_hidden )%]
                                             <li>
-                                                [% IF ( isbn_required ) %]
+                                                [% IF ( biblionumber ) %]
+                                                    <label for="isbn" class="required">Standard number (ISBN, ISSN or other):</label>
+                                                    [% isbn | html %]
+                                                [% ELSIF ( isbn_required ) %]
                                                     <label for="isbn" class="required">Standard number (ISBN, ISSN or other):</label>
                                                     <input type="text" id="isbn" name="isbn"  maxlength="80" value="[% isbn | html %]" required="required" />
                                                     <div class="required_label required">Required</div>
@@ -125,7 +128,10 @@
                                         [% END %]
                                         [% UNLESS ( publishercode_hidden ) %]
                                             <li>
-                                                [% IF ( publishercode_required ) %]
+                                                [% IF ( biblionumber ) %]
+                                                    <label for="publishercode" class="required">Publisher:</label>
+                                                    [% publishercode | html %]
+                                                [% ELSIF ( publishercode_required ) %]
                                                     <label for="publishercode" class="required">Publisher:</label>
                                                     <input type="text" id="publishercode" name="publishercode" class="span6" maxlength="80" value="[% publishercode | html %]" required="required" />
                                                     <div class="required_label required">Required</div>
@@ -137,7 +143,10 @@
                                         [% END %]
                                         [% UNLESS ( collectiontitle_hidden ) %]
                                             <li>
-                                                [% IF ( collectiontitle_required ) %]
+                                                [% IF ( biblionumber ) %]
+                                                    <label for="collectiontitle" class="required">Collection title:</label>
+                                                    [% collectiontitle | html %]
+                                                [% ELSIF ( collectiontitle_required ) %]
                                                     <label for="collectiontitle" class="required">Collection title:</label>
                                                     <input type="text" id="collectiontitle" name="collectiontitle" class="span6" maxlength="80" value="[% collectiontitle | html %]" required="required" />
                                                     <div class="required_label required">Required</div>
@@ -149,7 +158,10 @@
                                         [% END %]
                                         [% UNLESS ( place_hidden ) %]
                                             <li>
-                                                [% IF ( place_required ) %]
+                                                [% IF ( biblionumber ) %]
+                                                    <label for="place" class="required">Publication place:</label>
+                                                    [% place | html %]
+                                                [% ELSIF ( place_required ) %]
                                                     <label for="place" class="required">Publication place:</label>
                                                     <input type="text" id="place" name="place" required="required" maxlength="80" value="[% place | html %]" />
                                                     <div class="required_label required">Required</div>
@@ -479,7 +491,6 @@
                                     [% END %]
                                 [% END %]
                             [% END # / IF suggestions_loop %]
-
                         [% END # IF op_else %]
                     </div> <!-- / #usersuggestions -->
                 </div> <!-- / .col-lg-10 -->
@@ -490,7 +501,31 @@
 [% INCLUDE 'opac-bottom.inc' %]
 [% BLOCK jsinclude %]
 [% INCLUDE 'datatables.inc' %]
-<script>
+<script> 
+    // Disable fields if there is a biblionumber
+    [% IF ( biblionumber ) %]
+        window.addEventListener("DOMContentLoaded", (event) => {
+            function disabled() {
+                const title = document.getElementById('title');
+                const author = document.getElementById('author');
+                const copyrightdate = document.getElementById('copyrightdate');
+                const isbn = document.getElementById('isbn');
+                const publishercode = document.getElementById('publishercode');
+                const collectiontitle = document.getElementById('collectiontitle');
+                const place = document.getElementById('place');
+
+                title.disabled = true;          
+                author.disabled = true;
+                copyrightdate.disabled = true;
+                isbn.disabled = true;
+                publishercode.disabled = true;
+                collectiontitle.disabled = true;
+                place.disabled = true;
+            }
+            disabled();
+        });
+    [% END %]
+
     [% IF ( loggedinusername ) %]
         function enableCheckboxActions(){
             // Enable/disable controls if checkboxes are checked
diff --git a/opac/opac-suggestions.pl b/opac/opac-suggestions.pl
index fcc95801aa..381f3f2dc5 100755
--- a/opac/opac-suggestions.pl
+++ b/opac/opac-suggestions.pl
@@ -192,6 +192,17 @@ if ( $op eq "add_confirm" ) {
         $suggestion->{suggesteddate} = dt_from_string;
         $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
         $suggestion->{STATUS} = 'ASKED';
+        if ( $biblionumber ) {
+            my $biblio = Koha::Biblios->find($biblionumber);
+            $suggestion->{biblionumber} = $biblio->biblionumber;
+            $suggestion->{title} = $biblio->title;
+            $suggestion->{author} = $biblio->author;
+            $suggestion->{copyrightdate} = $biblio->copyrightdate;
+            $suggestion->{isbn} = $biblio->biblioitem->isbn;
+            $suggestion->{publishercode} = $biblio->biblioitem->publishercode;
+            $suggestion->{collectiontitle} = $biblio->biblioitem->collectiontitle;
+            $suggestion->{place} = $biblio->biblioitem->place;
+        }
 
         &NewSuggestion($suggestion);
         $patrons_pending_suggestions_count++;
@@ -292,5 +303,4 @@ $template->param(
     patrons_total_suggestions_count => $patrons_total_suggestions_count,
 );
 
-output_html_with_http_headers $input, $cookie, $template->output, undef, { force_no_caching => 1 };
-
+output_html_with_http_headers $input, $cookie, $template->output, undef, { force_no_caching => 1 };
\ No newline at end of file
-- 
2.17.1