Bug 24368: Remove Koha::Libraries->pickup_locations
[koha-ffzg.git] / suggestion / suggestion.pl
index 6b655f8..4f5c915 100755 (executable)
@@ -83,7 +83,7 @@ my $input           = CGI->new;
 my $redirect  = $input->param('redirect');
 my $suggestedbyme   = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
 my $op              = $input->param('op')||'else';
-my @editsuggestions = $input->multi_param('edit_field');
+my @editsuggestions = $input->multi_param('suggestionid');
 my $suggestedby     = $input->param('suggestedby');
 my $returnsuggestedby = $input->param('returnsuggestedby');
 my $returnsuggested = $input->param('returnsuggested');
@@ -91,11 +91,13 @@ my $managedby       = $input->param('managedby');
 my $displayby       = $input->param('displayby') || '';
 my $tabcode         = $input->param('tabcode');
 my $save_confirmed  = $input->param('save_confirmed') || 0;
+my $notify          = $input->param('notify');
+my $filter_archived = $input->param('filter_archived');
 
 my $reasonsloop     = GetAuthorisedValues("SUGGEST");
 
 # filter informations which are not suggestion related.
-my $suggestion_ref  = $input->Vars;
+my $suggestion_ref  = { %{$input->Vars} }; # Copying, otherwise $input will be modified
 
 # get only the columns of Suggestion
 my $schema = Koha::Database->new()->schema;
@@ -103,16 +105,16 @@ 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 );
+delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode notify filter_archived );
 foreach (keys %$suggestion_ref){
-    delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
+    delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' ));
 }
 my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user(
         {
             template_name   => "suggestion/suggestion.tt",
             query           => $input,
             type            => "intranet",
-            flagsrequired   => { acquisition => 'suggestions_manage' },
+            flagsrequired   => { suggestions => 'suggestions_manage' },
         }
     );
 
@@ -154,7 +156,7 @@ if ( $op =~ /save/i ) {
                 $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "by" }   = C4::Context->userenv->{number};
             }
             $suggestion_only->{manageddate} = dt_from_string;
-            $suggestion_only->{"managedby"}   = C4::Context->userenv->{number};
+            $suggestion_only->{"managedby"} ||= C4::Context->userenv->{number};
         }
 
         my $otherreason = $input->param('other_reason');
@@ -163,7 +165,41 @@ if ( $op =~ /save/i ) {
         }
 
         if ( $suggestion_only->{'suggestionid'} > 0 ) {
+
+            $suggestion_only->{lastmodificationdate} = dt_from_string;
+            $suggestion_only->{lastmodificationby}   = C4::Context->userenv->{number};
+
             &ModSuggestion($suggestion_only);
+
+            if ( $notify ) {
+                my $patron = Koha::Patrons->find( $suggestion_only->{managedby} );
+                my $email_address = $patron->notice_email_address;
+                if ($patron->notice_email_address) {
+                    my $library = $patron->library;
+                    my $admin_email_address = $library->branchemail
+                      || C4::Context->preference('KohaAdminEmailAddress');
+
+                    my $letter = C4::Letters::GetPreparedLetter(
+                        module      => 'suggestions',
+                        letter_code => 'NOTIFY_MANAGER',
+                        branchcode  => $patron->branchcode,
+                        lang        => $patron->lang,
+                        tables      => {
+                            suggestions => $suggestion_only->{suggestionid},
+                            branches    => $patron->branchcode,
+                            borrowers   => $patron->borrowernumber,
+                        },
+                    );
+                    C4::Letters::EnqueueLetter(
+                        {
+                            letter                 => $letter,
+                            borrowernumber         => $patron->borrowernumber,
+                            message_transport_type => 'email',
+                            from_address           => $admin_email_address,
+                        }
+                    );
+                }
+            }
         } else {
             ###FIXME:Search here if suggestion already exists.
             my $suggestions_loop =
@@ -210,7 +246,7 @@ elsif ($op=~/edit/) {
     Init($suggestion_ref);
     $op ='save';
 }  
-elsif ($op eq "change" ) {
+elsif ($op eq "update_status" ) {
 
     my $suggestion;
     # set accepted/rejected/managed informations if applicable
@@ -234,9 +270,9 @@ elsif ($op eq "change" ) {
         $suggestion->{managedby}   = C4::Context->userenv->{number};
         $suggestion->{STATUS}      = $STATUS;
     }
-    if ( my $reason = $input->param("reason$tabcode") ) {
+    if ( my $reason = $input->param("reason") ) {
         if ( $reason eq "other" ) {
-            $reason = $input->param("other_reason$tabcode");
+            $reason = $input->param("other_reason");
         }
         $suggestion->{reason} = $reason;
     }
@@ -246,25 +282,38 @@ elsif ($op eq "change" ) {
         $suggestion->{suggestionid} = $suggestionid;
         &ModSuggestion($suggestion);
     }
-    my $params = '';
-    foreach my $key (
-        qw(
-        displayby branchcode title author isbn publishercode copyrightdate
-        collectiontitle suggestedby suggesteddate_from suggesteddate_to
-        manageddate_from manageddate_to accepteddate_from
-        accepteddate_to budgetid
-        )
-      )
-    {
-        $params .= $key . '=' . uri_escape($input->param($key)) . '&'
-          if defined($input->param($key));
-    }
-    print $input->redirect("/cgi-bin/koha/suggestion/suggestion.pl?$params");
+    redirect_with_params($input);
 }elsif ($op eq "delete" ) {
     foreach my $delete_field (@editsuggestions) {
         &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
     }
-    $op = 'else';
+    redirect_with_params($input);
+}
+elsif ($op eq "archive" ) {
+    Koha::Suggestions->find($_)->update({ archived => 1 }) for @editsuggestions;
+
+    redirect_with_params($input);
+}
+elsif ($op eq "unarchive" ) {
+    Koha::Suggestions->find($_)->update({ archived => 0 }) for @editsuggestions;
+
+    redirect_with_params($input);
+}
+elsif ( $op eq 'update_itemtype' ) {
+    my $new_itemtype = $input->param('suggestion_itemtype');
+    foreach my $suggestionid (@editsuggestions) {
+        next unless $suggestionid;
+        &ModSuggestion({ suggestionid => $suggestionid, itemtype => $new_itemtype });
+    }
+    redirect_with_params($input);
+}
+elsif ( $op eq 'update_manager' ) {
+    my $managedby = $input->param('suggestion_managedby');
+    foreach my $suggestionid (@editsuggestions) {
+        next unless $suggestionid;
+        &ModSuggestion({ suggestionid => $suggestionid, managedby => $managedby });
+    }
+    redirect_with_params($input);
 }
 elsif ( $op eq 'show' ) {
     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
@@ -308,7 +357,7 @@ if ($op=~/else/) {
         next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue ) and ($displayby ne 'branchcode' or $branchfilter ne '__ANY__' );
         $$suggestion_ref{$displayby} = $criteriumvalue;
 
-        my $suggestions = &SearchSuggestion($suggestion_ref);
+        my $suggestions = &SearchSuggestion({ %$suggestion_ref, archived => $filter_archived });
         foreach my $suggestion (@$suggestions) {
             if ($suggestion->{budgetid}){
                 my $bud = GetBudget( $suggestion->{budgetid} );
@@ -321,7 +370,7 @@ if ($op=~/else/) {
                             "suggestionscount"=>scalar(@$suggestions),             
                             'suggestions_loop'=>$suggestions,
                             'reasonsloop'     => $reasonsloop,
-                            };
+                            } if @$suggestions;
 
         delete $$suggestion_ref{$displayby} unless $definedvalue;
     }
@@ -333,25 +382,13 @@ if ($op=~/else/) {
     );
 }
 
-foreach my $element ( qw(managedby suggestedby acceptedby) ) {
-#    $debug || warn $$suggestion_ref{$element};
-    if ($$suggestion_ref{$element}){
-        my $patron = Koha::Patrons->find( $$suggestion_ref{$element} );
-        my $category = $patron->category;
-        $template->param(
-            $element."_borrowernumber"=>$patron->borrowernumber,
-            $element."_firstname"=>$patron->firstname,
-            $element."_surname"=>$patron->surname,
-            $element."_cardnumber"=>$patron->cardnumber,
-            $element."_branchcode"=>$patron->branchcode,
-            $element."_description"=>$category->description,
-            $element."_category_type"=>$category->category_type,
-        );
-    }
-}
 $template->param(
-    %$suggestion_ref,  
-    "op_$op"                => 1,
+    "${_}_patron" => scalar Koha::Patrons->find( $suggestion_ref->{$_} ) )
+  for qw(managedby suggestedby acceptedby lastmodificationby);
+
+$template->param(
+    %$suggestion_ref,
+    filter_archived => $filter_archived,
     "op"             =>$op,
 );
 
@@ -421,3 +458,21 @@ $template->param(
     SuggestionStatuses       => GetAuthorisedValues('SUGGEST_STATUS'),
 );
 output_html_with_http_headers $input, $cookie, $template->output;
+
+sub redirect_with_params {
+    my ( $input ) = @_;
+    my $params = '';
+    foreach my $key (
+        qw(
+        displayby branchcode title author isbn publishercode copyrightdate
+        collectiontitle suggestedby suggesteddate_from suggesteddate_to
+        manageddate_from manageddate_to accepteddate_from
+        accepteddate_to budgetid filter_archived
+        )
+      )
+    {
+        $params .= $key . '=' . uri_escape(scalar $input->param($key)) . '&'
+          if defined($input->param($key));
+    }
+    print $input->redirect("/cgi-bin/koha/suggestion/suggestion.pl?$params");
+}