Bug 13167 Stage MARC for Import hangs for biblio containing invalid ISBN-13
[koha.git] / C4 / Suggestions.pm
index ddd1d6a..1d9edbb 100644 (file)
@@ -26,9 +26,10 @@ use CGI;
 use C4::Context;
 use C4::Output;
 use C4::Dates qw(format_date format_date_in_iso);
-use C4::SQLHelper qw(:all);
 use C4::Debug;
 use C4::Letters;
+use Koha::DateUtils qw( dt_from_string );
+
 use List::MoreUtils qw(any);
 use C4::Dates qw(format_date_in_iso);
 use base qw(Exporter);
@@ -132,10 +133,10 @@ sub SearchSuggestion {
     }
 
     # filter on user branch
-    if ( C4::Context->preference('IndependantBranches') ) {
+    if ( C4::Context->preference('IndependentBranches') ) {
         my $userenv = C4::Context->userenv;
         if ($userenv) {
-            if ( ( $userenv->{flags} % 2 ) != 1 && !$suggestion->{branchcode} )
+            if ( !C4::Context->IsSuperLibrarian() && !$suggestion->{branchcode} )
             {
                 push @sql_params, $$userenv{branch};
                 push @query,      q{
@@ -143,11 +144,18 @@ sub SearchSuggestion {
                 };
             }
         }
+    } else {
+        if ( defined $suggestion->{branchcode} && $suggestion->{branchcode} ) {
+            unless ( $suggestion->{branchcode} eq '__ANY__' ) {
+                push @sql_params, $suggestion->{branchcode};
+                push @query,      qq{ AND suggestions.branchcode=? };
+            }
+        }
     }
 
     # filter on nillable fields
     foreach my $field (
-        qw( STATUS branchcode itemtype suggestedby managedby acceptedby budgetid biblionumber )
+        qw( STATUS itemtype suggestedby managedby acceptedby budgetid biblionumber )
       )
     {
         if ( exists $suggestion->{$field} ) {
@@ -332,10 +340,10 @@ sub GetSuggestionByStatus {
     };
 
     # filter on branch
-    if ( C4::Context->preference("IndependantBranches") || $branchcode ) {
+    if ( C4::Context->preference("IndependentBranches") || $branchcode ) {
         my $userenv = C4::Context->userenv;
         if ($userenv) {
-            unless ( $userenv->{flags} % 2 == 1 ) {
+            unless ( C4::Context->IsSuperLibrarian() ) {
                 push @sql_params, $userenv->{branch};
                 $query .= q{ AND (U1.branchcode = ? OR U1.branchcode ='') };
             }
@@ -345,7 +353,7 @@ sub GetSuggestionByStatus {
             $query .= q{ AND (U1.branchcode = ? OR U1.branchcode ='') };
         }
     }
-    
+
     my $sth = $dbh->prepare($query);
     $sth->execute(@sql_params);
     my $results;
@@ -382,8 +390,8 @@ sub CountSuggestion {
     my $dbh = C4::Context->dbh;
     my $sth;
     my $userenv = C4::Context->userenv;
-    if ( C4::Context->preference("IndependantBranches")
-        && $userenv->{flags} % 2 != 1 )
+    if ( C4::Context->preference("IndependentBranches")
+        && !C4::Context->IsSuperLibrarian() )
     {
         my $query = q{
             SELECT count(*)
@@ -419,8 +427,26 @@ Insert a new suggestion on database with value given on input arg.
 
 sub NewSuggestion {
     my ($suggestion) = @_;
+
+    for my $field ( qw(
+        suggestedby
+        managedby
+        manageddate
+        acceptedby
+        accepteddate
+        rejectedby
+        rejecteddate
+    ) ) {
+        # Set the fields to NULL if not given.
+        $suggestion->{$field} ||= undef;
+    }
+
     $suggestion->{STATUS} = "ASKED" unless $suggestion->{STATUS};
-    return InsertInTable( "suggestions", $suggestion );
+
+    $suggestion->{suggesteddate} = dt_from_string unless $suggestion->{suggesteddate};
+
+    my $rs = Koha::Database->new->schema->resultset('Suggestion');
+    return $rs->create($suggestion)->id;
 }
 
 =head2 ModSuggestion
@@ -429,16 +455,39 @@ sub NewSuggestion {
 
 Modify the suggestion according to the hash passed by ref.
 The hash HAS to contain suggestionid
-Data not defined is not updated unless it is a note or sort1 
+Data not defined is not updated unless it is a note or sort1
 Send a mail to notify the user that did the suggestion.
 
-Note that there is no function to modify a suggestion. 
+Note that there is no function to modify a suggestion.
 
 =cut
 
 sub ModSuggestion {
     my ($suggestion) = @_;
-    my $status_update_table = UpdateInTable( "suggestions", $suggestion );
+    return unless( $suggestion and defined($suggestion->{suggestionid}) );
+
+    for my $field ( qw(
+        suggestedby
+        managedby
+        manageddate
+        acceptedby
+        accepteddate
+        rejectedby
+        rejecteddate
+    ) ) {
+        # Set the fields to NULL if not given.
+        $suggestion->{$field} = undef
+          if exists $suggestion->{$field}
+          and ($suggestion->{$field} eq '0'
+            or $suggestion->{$field} eq '' );
+    }
+
+    my $rs = Koha::Database->new->schema->resultset('Suggestion')->find($suggestion->{suggestionid});
+    my $status_update_table = 1;
+    eval {
+        $rs->update($suggestion);
+    };
+    $status_update_table = 0 if( $@ );
 
     if ( $suggestion->{STATUS} ) {
 
@@ -526,9 +575,9 @@ sub DelSuggestion {
 
 =head2 DelSuggestionsOlderThan
     &DelSuggestionsOlderThan($days)
-    
+
     Delete all suggestions older than TODAY-$days , that have be accepted or rejected.
-    
+
 =cut
 
 sub DelSuggestionsOlderThan {