Bug 14766: Complete cataloguing plugin unimarc_field_4XX
[koha.git] / suggestion / suggestion.pl
index 2af64ab..43834f6 100755 (executable)
@@ -4,23 +4,23 @@
 # Copyright 2006-2010 BibLibre
 
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 #use warnings; FIXME - Bug 2505
 require Exporter;
-use CGI;
+use CGI qw ( -utf8 );
 use C4::Auth;    # get_template_and_user
 use C4::Output;
 use C4::Suggestions;
@@ -28,9 +28,11 @@ use C4::Koha; #GetItemTypes
 use C4::Branch;
 use C4::Budgets;
 use C4::Search;
-use C4::Dates qw(format_date);
 use C4::Members;
 use C4::Debug;
+
+use Koha::DateUtils qw( dt_from_string );
+
 use URI::Escape;
 
 sub Init{
@@ -38,21 +40,13 @@ sub Init{
     # "Managed by" is used only when a suggestion is being edited (not when created)
     if ($suggestion->{'suggesteddate'} eq "0000-00-00" ||$suggestion->{'suggesteddate'} eq "") {
         # new suggestion
-        $suggestion->{'suggesteddate'} = C4::Dates->today;
+        $suggestion->{suggesteddate} = dt_from_string;
         $suggestion->{'suggestedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'suggestedby'});
     }
     else {
         # editing of an existing suggestion
-        $suggestion->{'manageddate'} = C4::Dates->today;
+        $suggestion->{manageddate} = dt_from_string;
         $suggestion->{'managedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'managedby'});
-        # suggesteddate, when coming from the DB, needs to be formated
-        $suggestion->{'suggesteddate'} = format_date($suggestion->{'suggesteddate'});
-    }
-    foreach my $date ( qw(rejecteddate accepteddate) ){
-    $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
-                                "":
-                                format_date($suggestion->{$date}) 
-                              );
     }
     $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
 }
@@ -95,13 +89,19 @@ my $tabcode         = $input->param('tabcode');
 # filter informations which are not suggestion related.
 my $suggestion_ref  = $input->Vars;
 
+# get only the columns of Suggestion
+my $schema = Koha::Database->new()->schema;
+my $columns = ' '.join(' ', $schema->source('Suggestion')->columns).' ';
+my $suggestion_only = { map { $columns =~ / $_ / ? ($_ => $suggestion_ref->{$_}) : () } keys %$suggestion_ref };
+$suggestion_only->{STATUS} = $suggestion_ref->{STATUS};
+
 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
 foreach (keys %$suggestion_ref){
     delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
 }
 my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user(
         {
-            template_name   => "suggestion/suggestion.tmpl",
+            template_name   => "suggestion/suggestion.tt",
             query           => $input,
             type            => "intranet",
             flagsrequired   => { catalogue => 1 },
@@ -115,26 +115,34 @@ $template->param('borrowernumber' => $borrowernumber);
 ##  Operations
 ##
 if ( $op =~ /save/i ) {
-        if ( $$suggestion_ref{"STATUS"} ) {
-        if ( my $tmpstatus = lc( $$suggestion_ref{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
-            $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "date" } = C4::Dates->today;
-            $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "by" }   = C4::Context->userenv->{number};
+    $suggestion_only->{suggesteddate} = dt_from_string( $suggestion_only->{suggesteddate} )
+        if $suggestion_only->{suggesteddate};
+
+    if ( $suggestion_only->{"STATUS"} ) {
+        if ( my $tmpstatus = lc( $suggestion_only->{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
+            $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "date" } = dt_from_string;
+            $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "by" }   = C4::Context->userenv->{number};
         }
-        $$suggestion_ref{"manageddate"} = C4::Dates->today;
-        $$suggestion_ref{"managedby"}   = C4::Context->userenv->{number};
+        $suggestion_only->{manageddate} = dt_from_string;
+        $suggestion_only->{"managedby"}   = C4::Context->userenv->{number};
     }
-    if ( $$suggestion_ref{'suggestionid'} > 0 ) {
-        &ModSuggestion($suggestion_ref);
+    if ( $suggestion_only->{'suggestionid'} > 0 ) {
+        &ModSuggestion($suggestion_only);
     } else {
         ###FIXME:Search here if suggestion already exists.
         my $suggestions_loop =
-            SearchSuggestion( $suggestion_ref );
+            SearchSuggestion( $suggestion_only );
         if (@$suggestions_loop>=1){
             #some suggestion are answering the request Donot Add
+            my @messages;
+            for my $suggestion ( @$suggestions_loop ) {
+                push @messages, { type => 'error', code => 'already_exists', id => $suggestion->{suggestionid} };
+            }
+            $template->param( messages => \@messages );
         } 
         else {    
             ## Adding some informations related to suggestion
-            &NewSuggestion($suggestion_ref);
+            &NewSuggestion($suggestion_only);
         }
         # empty fields, to avoid filter in "SearchSuggestion"
     }  
@@ -160,31 +168,28 @@ elsif ($op=~/edit/) {
 elsif ($op eq "change" ) {
     # set accepted/rejected/managed informations if applicable
     # ie= if the librarian has choosen some action on the suggestions
-    if ($$suggestion_ref{"STATUS"} eq "ACCEPTED"){
-        $$suggestion_ref{"accepteddate"}=C4::Dates->today;
-        $$suggestion_ref{"acceptedby"}=C4::Context->userenv->{number};
-    } elsif ($$suggestion_ref{"STATUS"} eq "REJECTED"){
-        $$suggestion_ref{"rejecteddate"}=C4::Dates->today;
-        $$suggestion_ref{"rejectedby"}=C4::Context->userenv->{number};
+    if ($suggestion_only->{"STATUS"} eq "ACCEPTED"){
+        $suggestion_only->{accepteddate} = dt_from_string;
+        $suggestion_only->{"acceptedby"}=C4::Context->userenv->{number};
+    } elsif ($suggestion_only->{"STATUS"} eq "REJECTED"){
+        $suggestion_only->{rejecteddate} = dt_from_string;
+        $suggestion_only->{"rejectedby"}=C4::Context->userenv->{number};
     }
-    if ($$suggestion_ref{"STATUS"}){
-        $$suggestion_ref{"manageddate"}=C4::Dates->today;
-        $$suggestion_ref{"managedby"}=C4::Context->userenv->{number};
+    if ($suggestion_only->{"STATUS"}){
+        $suggestion_only->{manageddate} = dt_from_string;
+        $suggestion_only->{"managedby"}=C4::Context->userenv->{number};
     }
     if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
         if ( $reason eq "other" ) {
-                $reason = $$suggestion_ref{"other_reason$tabcode"};
+            $reason = $$suggestion_ref{"other_reason$tabcode"};
         }
-        $$suggestion_ref{'reason'}=$reason;
-    }
-    delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
-     foreach (keys %$suggestion_ref){
-        delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
+        $suggestion_only->{reason}=$reason;
     }
+
     foreach my $suggestionid (@editsuggestions) {
         next unless $suggestionid;
-        $$suggestion_ref{'suggestionid'}=$suggestionid;
-        &ModSuggestion($suggestion_ref);
+        $suggestion_only->{'suggestionid'}=$suggestionid;
+        &ModSuggestion($suggestion_only);
     }
     my $params = '';
     foreach my $key (
@@ -249,13 +254,6 @@ if ($op=~/else/) {
                 my $bud = GetBudget( $suggestion->{budgetid} );
                 $suggestion->{budget_name} = $bud->{budget_name} if $bud;
             }
-            foreach my $date (qw(suggesteddate manageddate accepteddate)) {
-                if ($suggestion->{$date} and $suggestion->{$date} ne "0000-00-00") {
-                    $suggestion->{$date} = format_date( $suggestion->{$date} );
-                } else {
-                    $suggestion->{$date} = "";
-                }
-            }
         }
         push @allsuggestions,{
                             "suggestiontype"=>$criteriumvalue||"suggest",
@@ -413,7 +411,7 @@ foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
 
 $template->param(
     %hashlists,
-    borrowernumber           => $input->param('borrowernumber'),
+    borrowernumber           => ($input->param('borrowernumber') // undef),
     SuggestionStatuses       => GetAuthorisedValues('SUGGEST_STATUS'),
 );
 output_html_with_http_headers $input, $cookie, $template->output;