Bug 14973: Check existing biblio when submitting a purchase suggestion (opac side)
authorAlex Arnaud <alex.arnaud>
Wed, 7 Oct 2015 15:34:33 +0000 (17:34 +0200)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Thu, 2 Jan 2020 12:30:56 +0000 (12:30 +0000)
Test plan:
- Go to opac suggestions page (opac-suggestions.pl) and click on "New purchase suggestion",
- type the title (and author?) of a document that stand in you database
- you should get a warning message about an exiting biblio.
- clicking on confirm your suggestion add it, cancel does nothing

Signed-off-by: Nicole Engard <nengard@bywatersolutions.com>
Signed-off-by: Lisette Scheer <lisetteslatah@gmail.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

C4/Suggestions.pm
koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-suggestions.tt
opac/opac-suggestions.pl
t/db_dependent/Suggestions.t

index 836b13f..2689151 100644 (file)
@@ -27,6 +27,7 @@ use C4::Context;
 use C4::Output;
 use C4::Debug;
 use C4::Letters;
+use C4::Biblio qw( GetMarcFromKohaField );
 use Koha::DateUtils;
 use Koha::Suggestions;
 
@@ -48,6 +49,7 @@ our @EXPORT  = qw(
   SearchSuggestion
   DelSuggestionsOlderThan
   GetUnprocessedSuggestions
+  MarcRecordFromNewSuggestion
 );
 
 =head1 NAME
@@ -655,6 +657,46 @@ sub GetUnprocessedSuggestions {
     return $s;
 }
 
+=head2 MarcRecordFromNewSuggestion
+
+    $record = MarcRecordFromNewSuggestion ( $suggestion )
+
+This function build a marc record object from a suggestion
+
+=cut
+
+sub MarcRecordFromNewSuggestion {
+    my ($suggestion) = @_;
+    my $record = MARC::Record->new();
+
+    my ($title_tag, $title_subfield) = GetMarcFromKohaField('biblio.title', '');
+    $record->append_fields(
+        MARC::Field->new($title_tag, ' ', ' ', $title_subfield => $suggestion->{title})
+    );
+
+    my ($author_tag, $author_subfield) = GetMarcFromKohaField('biblio.author', '');
+    if ($record->field( $author_tag )) {
+        $record->field( $author_tag )->add_subfields( $author_subfield => $suggestion->{author} );
+    }
+    else {
+        $record->append_fields(
+            MARC::Field->new($author_tag, ' ', ' ', $author_subfield => $suggestion->{author})
+        );
+    }
+
+    my ($it_tag, $it_subfield) = GetMarcFromKohaField('biblioitems.itemtype', '');
+    if ($record->field( $it_tag )) {
+        $record->field( $it_tag )->add_subfields( $it_subfield => $suggestion->{itemtype} );
+    }
+    else {
+        $record->append_fields(
+            MARC::Field->new($it_tag, ' ', ' ', $it_subfield => $suggestion->{author})
+        );
+    }
+
+    return $record;
+}
+
 1;
 __END__
 
index 2da7660..6208287 100644 (file)
                             <p>Please fill out this form to make a purchase suggestion. You will receive an email when the library processes your suggestion.</p>
                             <p>Only certain fields (marked in red) are required, but the more information you enter the easier it will be for the librarians to find the title you're requesting. The "Notes" field can be used to provide any additional information.</p>
 
+                            [% FOR m IN messages %]
+                                <div class="alert alert-[% m.type %]">
+                                    [% SWITCH m.code %]
+                                    [% CASE 'biblio_exists' %]
+                                        A similar document already exists: <a href="/cgi-bin/koha/opac-detail.pl?biblionumber=[% m.id %]">[% m.title %]</a>. Click on "Confirm your suggestion" to ignore this message.
+                                    [% CASE %]
+                                        [% m.code %]
+                                    [% END %]
+                                </div>
+                            [% END %]
+
                             <form action="/cgi-bin/koha/opac-suggestions.pl" method="post" id="add_suggestion_form">
                                 <fieldset class="rows">
                                     <ol>
                                         <li>
                                             [% IF ( title_required ) %]
                                                 <label for="title" class="required">Title:</label>
-                                                <input type="text" id="title" name="title" class="span6" maxlength="255" required="required" />
+                                                <input type="text" id="title" name="title" class="span6" maxlength="255" value="[% title %]" required="required" />
                                                 <span class="required">Required</span>
                                             [% ELSE %]
                                                 <label for="title">Title:</label>
-                                                <input type="text" id="title" name="title" class="span6" maxlength="255" />
+                                                <input type="text" id="title" name="title" class="span6" maxlength="255" value="[% title %]" />
                                             [% END %]
                                         </li>
                                         <li>
                                             [% IF ( author_required ) %]
                                                 <label for="author" class="required">Author:</label>
-                                                <input type="text" id="author" name="author" class="span6" maxlength="80" required="required" />
+                                                <input type="text" id="author" name="author" class="span6" maxlength="80" value="[% author %]" required="required" />
                                                 <span class="required">Required</span>
                                             [% ELSE %]
                                                 <label for="author">Author:</label>
-                                                <input type="text" id="author" name="author" class="span6" maxlength="80" />
+                                                <input type="text" id="author" name="author" class="span6" maxlength="80" value="[% author %]" />
                                             [% END %]
                                         </li>
                                         <li>
                                             <div title="Copyright or publication year, for example: 2016">
                                                 [% IF ( copyrightdate_required ) %]
                                                     <label for="copyrightdate" class="required">Copyright date:</label>
-                                                    <input type="text" id="copyrightdate" name="copyrightdate" pattern="[12]\d{3}" size="4" maxlength="4" required="required" />
+                                                    <input type="text" id="copyrightdate" name="copyrightdate" pattern="[12]\d{3}" size="4" maxlength="4" value="[% copyrightdate %]" required="required" />
                                                 <span class="required">Required</span>
                                                 [% ELSE %]
                                                     <label for="copyrightdate">Copyright date:</label>
-                                                    <input type="text" id="copyrightdate" name="copyrightdate" pattern="[12]\d{3}" size="4" maxlength="4" />
+                                                    <input type="text" id="copyrightdate" name="copyrightdate" pattern="[12]\d{3}" size="4" maxlength="4" value="[% copyrightdate %]" />
                                                 [% END %]
                                             </div>
                                         </li>
                                         <li>
                                             [% IF ( isbn_required ) %]
                                                 <label for="isbn" class="required">Standard number (ISBN, ISSN or other):</label>
-                                                <input type="text" id="isbn" name="isbn"  maxlength="80" required="required" />
+                                                <input type="text" id="isbn" name="isbn"  maxlength="80" value="[% isbn %]" required="required" />
                                                 <span class="required">Required</span>
                                             [% ELSE %]
                                                 <label for="isbn">Standard number (ISBN, ISSN or other):</label>
-                                                <input type="text" id="isbn" name="isbn"  maxlength="80" />
+                                                <input type="text" id="isbn" name="isbn"  maxlength="80" value="[% isbn %]" />
                                             [% END %]
                                         </li>
                                         <li>
                                             [% IF ( publishercode_required ) %]
                                                 <label for="publishercode" class="required">Publisher:</label>
-                                                <input type="text" id="publishercode" name="publishercode" class="span6" maxlength="80" required="required" />
+                                                <input type="text" id="publishercode" name="publishercode" class="span6" maxlength="80" value="[% publishercode %]" required="required" />
                                                 <span class="required">Required</span>
                                             [% ELSE %]
                                                 <label for="publishercode">Publisher:</label>
-                                                <input type="text" id="publishercode" name="publishercode" class="span6" maxlength="80" />
+                                                <input type="text" id="publishercode" name="publishercode" class="span6" maxlength="80" value="[% publishercode %]" />
                                             [% END %]
                                         </li>
                                         <li>
                                             [% IF ( collectiontitle_required ) %]
                                                 <label for="collectiontitle" class="required">Collection title:</label>
-                                                <input type="text" id="collectiontitle" name="collectiontitle" class="span6" maxlength="80" required="required" />
+                                                <input type="text" id="collectiontitle" name="collectiontitle" class="span6" maxlength="80" value="[% collectiontitle %]" required="required" />
                                                 <span class="required">Required</span>
                                             [% ELSE %]
                                                 <label for="collectiontitle">Collection title:</label>
-                                                <input type="text" id="collectiontitle" name="collectiontitle" class="span6" maxlength="80" />
+                                                <input type="text" id="collectiontitle" name="collectiontitle" class="span6" maxlength="80" value="[% collectiontitle %]" />
                                             [% END %]
                                         </li>
                                         <li>
                                             [% IF ( place_required ) %]
                                                 <label for="place" class="required">Publication place:</label>
-                                                <input type="text" id="place" name="place" required="required" maxlength="80" />
+                                                <input type="text" id="place" name="place" required="required" maxlength="80" value="[% place %]" />
                                                 <span class="required">Required</span>
                                             [% ELSE %]
                                                 <label for="place">Publication place:</label>
-                                                <input type="text" id="place" name="place"  maxlength="80" />
+                                                <input type="text" id="place" name="place"  maxlength="80" value="[% place %]" />
                                             [% END %]
                                         </li>
                                         <li id="opac-suggestion-quantity">
                                             [% ELSE %]
                                                 <label for="note">Notes:</label>
                                                 <textarea name="note" id="note" rows="5" cols="40"></textarea>
+                                                <textarea name="note" id="note" rows="5" cols="40">[% note %]</textarea>
                                             [% END %]
                                         </li>
 
                                 <fieldset class="action">
                                     <input type="hidden" name="suggested_by_anyone" value="[% suggested_by_anyone | html %]" />
                                     <input type="hidden" name="op" value="add_confirm" />
-                                    <input type="submit" class="btn" value="Submit your suggestion" /> <a class="action" href="/cgi-bin/koha/opac-suggestions.pl">Cancel</a>
+                                    [% IF ( need_confirm ) %]
+                                        <input type="submit" onclick="this.form.op.value = 'add_confirm'; this.form.submit();" class="btn" value="Confirm your suggestion" />
+                                    [% ELSE %]
+                                        <input type="submit" class="btn" value="Submit your suggestion" />
+                                    [% END %]
+                                    <a class="action" href="/cgi-bin/koha/opac-suggestions.pl">Cancel</a>
                                 </fieldset>
                             </form>
                             [% END %]
index 8f792cd..b5765f3 100755 (executable)
@@ -27,6 +27,7 @@ use C4::Output;
 use C4::Suggestions;
 use C4::Koha;
 use C4::Scrubber;
+use C4::Search qw( FindDuplicate );
 
 use Koha::AuthorisedValues;
 use Koha::Libraries;
@@ -39,6 +40,7 @@ my $op              = $input->param('op');
 my $suggestion      = $input->Vars;
 my $negcaptcha      = $input->param('negcap');
 my $suggested_by_anyone = $input->param('suggested_by_anyone') || 0;
+my $need_confirm    = 0;
 
 # If a spambot accidentally populates the 'negcap' field in the sugesstions form, then silently skip and return.
 if ($negcaptcha ) {
@@ -56,7 +58,7 @@ if ( ! C4::Context->preference('suggestion') ) {
     exit;
 }
 
-delete $suggestion->{$_} foreach qw<op suggested_by_anyone>;
+delete $suggestion->{$_} foreach qw<op suggested_by_anyone confirm>;
 $op = 'else' unless $op;
 
 my ( $template, $borrowernumber, $cookie, @messages );
@@ -114,6 +116,16 @@ if ( $op eq 'else' ) {
     }
 }
 
+if ( $op eq "add_validate" ) {
+    $op = 'add_confirm';
+    my $biblio = MarcRecordFromNewSuggestion($suggestion);
+    if ( my ($duplicatebiblionumber, $duplicatetitle) = FindDuplicate($biblio) ) {
+        push @messages, { type => 'error', code => 'biblio_exists', id => $duplicatebiblionumber, title => $duplicatetitle };
+        $need_confirm = 1;
+        $op = 'add';
+    }
+}
+
 my $patrons_pending_suggestions_count = 0;
 if ( $borrowernumber && C4::Context->preference("MaxOpenSuggestions") ne '' ) {
     $patrons_pending_suggestions_count = scalar @{ SearchSuggestion( { suggestedby => $borrowernumber, STATUS => 'ASKED' } ) } ;
@@ -164,6 +176,7 @@ if ( $op eq "add_confirm" ) {
     $op = 'else';
 }
 
+my $suggestions_loop = &SearchSuggestion({suggestedby => $suggestion->{suggestedby}});
 if ( $op eq "delete_confirm" ) {
     my @delete_field = $input->multi_param("delete_field");
     foreach my $delete_field (@delete_field) {
@@ -228,6 +241,7 @@ $template->param(
     suggestionsview       => 1,
     suggested_by_anyone   => $suggested_by_anyone,
     patrons_pending_suggestions_count => $patrons_pending_suggestions_count,
+    need_confirm => $need_confirm,
 );
 
 output_html_with_http_headers $input, $cookie, $template->output, undef, { force_no_caching => 1 };
index 7a75264..0349353 100644 (file)
@@ -18,7 +18,7 @@
 use Modern::Perl;
 
 use DateTime::Duration;
-use Test::More tests => 110;
+use Test::More tests => 113;
 use Test::Warn;
 
 use t::lib::Mocks;
@@ -423,6 +423,24 @@ ModSuggestion( $my_suggestion );
 $suggestion = GetSuggestion($my_suggestionid_test_budgetid);
 is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
 
+my $suggestion2 = {
+    title => "Cuisine d'automne",
+    author => "Catherine",
+    itemtype => "LIV"
+};
+
+my $record = MarcRecordFromNewSuggestion($suggestion2);
+
+is("MARC::Record", ref($record), "MarcRecordFromNewSuggestion should return a MARC::Record object");
+
+my ($title_tag, $title_subfield) = C4::Biblio::GetMarcFromKohaField('biblio.title', '');
+
+is($record->field( $title_tag )->subfield( $title_subfield ), "Cuisine d'automne", "Record from suggestion title should be 'Cuisine d'automne'");
+
+my ($author_tag, $author_subfield) = C4::Biblio::GetMarcFromKohaField('biblio.author', '');
+
+is($record->field( $author_tag )->subfield( $author_subfield ), "Catherine", "Record from suggestion author should be 'Catherine'");
+
 subtest 'GetUnprocessedSuggestions' => sub {
     plan tests => 11;
     $dbh->do(q|DELETE FROM suggestions|);