Bug 14707 - Remove atomic update
[koha.git] / opac / opac-suggestions.pl
index 6d2b88b..388e04a 100755 (executable)
@@ -22,7 +22,6 @@ use CGI qw ( -utf8 );
 use Encode qw( encode );
 use C4::Auth;    # get_template_and_user
 use C4::Members;
-use C4::Branch;
 use C4::Koha;
 use C4::Output;
 use C4::Suggestions;
@@ -112,12 +111,14 @@ if ( $op eq 'else' ) {
     }
 }
 
-my $patrons_pending_suggestions_count = scalar @{ SearchSuggestion( { suggestedby => $borrowernumber, STATUS => 'ASKED' } ) };
+my $patrons_pending_suggestions_count = 0;
+if ( $borrowernumber && C4::Context->preference("MaxOpenSuggestions") ne '' ) {
+    $patrons_pending_suggestions_count = scalar @{ SearchSuggestion( { suggestedby => $borrowernumber, STATUS => 'ASKED' } ) } ;
+}
 
 my $suggestions_loop = &SearchSuggestion($suggestion);
 if ( $op eq "add_confirm" ) {
-    my $count_own_suggestions = $borrowernumber ? &SearchSuggestion( { suggestedby => $borrowernumber } ) : 0;
-    if ( $patrons_pending_suggestions_count >= C4::Context->preference("MaxOpenSuggestions") )
+    if ( C4::Context->preference("MaxOpenSuggestions") ne '' && $patrons_pending_suggestions_count >= C4::Context->preference("MaxOpenSuggestions") ) #only check limit for signed in borrowers
     {
         push @messages, { type => 'error', code => 'too_many' };
     }
@@ -146,13 +147,16 @@ if ( $op eq "add_confirm" ) {
         $suggestion->{branchcode} = $input->param('branchcode') || C4::Context->userenv->{"branch"};
 
         &NewSuggestion($suggestion);
+        $patrons_pending_suggestions_count++;
 
         # delete empty fields, to avoid filter in "SearchSuggestion"
         foreach my $field ( qw( title author publishercode copyrightdate place collectiontitle isbn STATUS ) ) {
-            delete $suggestion->{$field} unless $suggestion->{$field};
+            delete $suggestion->{$field}; #clear search filters (except borrower related) to show all suggestions after placing a new one
         }
         $suggestions_loop = &SearchSuggestion($suggestion);
+
         push @messages, { type => 'info', code => 'success_on_inserted' };
+
     }
     $op = 'else';
 }
@@ -197,9 +201,14 @@ if ( C4::Context->preference("AllowPurchaseSuggestionBranchChoice") ) {
     }
     my $branchcode = $input->param('branchcode') || $borr->{'branchcode'} || $userbranch || '' ;
 
-# make branch selection options...
-    my $branchloop = GetBranchesLoop($branchcode);
-    $template->param( branchloop => $branchloop );
+    $template->param( branchcode => $branchcode );
+}
+
+my $mandatoryfields = '';
+{
+    last unless ($op eq 'add');
+    my $fldsreq_sp = C4::Context->preference("OPACSuggestionMandatoryFields") || 'title';
+    $mandatoryfields = join(', ', (map { '"'.$_.'"'; } sort split(/\s*\,\s*/, $fldsreq_sp)));
 }
 
 $template->param(
@@ -211,6 +220,7 @@ $template->param(
     messages              => \@messages,
     suggestionsview       => 1,
     suggested_by_anyone   => $suggested_by_anyone,
+    mandatoryfields       => $mandatoryfields,
     patrons_pending_suggestions_count => $patrons_pending_suggestions_count,
 );