Bug 25771: Allow the user to sort checkouts by the renew column in the OPAC
[koha-ffzg.git] / tools / letter.pl
index 9222079..7af71dd 100755 (executable)
 # TODO This script drives the CRUD operations on the letter table
 # The DB interaction should be handled by calls to C4/Letters.pm
 
-use strict;
-use warnings;
+use Modern::Perl;
 use CGI qw ( -utf8 );
 use C4::Auth;
 use C4::Context;
 use C4::Output;
 use C4::Letters;
-use C4::Members::Attributes;
+
+use Koha::Patron::Attribute::Types;
 
 # $protected_letters = protected_letters()
 # - return a hashref of letter_codes representing letters that should never be deleted
@@ -66,7 +66,9 @@ my $code        = $input->param('code');
 my $module      = $input->param('module') || '';
 my $content     = $input->param('content');
 my $op          = $input->param('op') || '';
-my $redirect      = $input->param('redirect');
+my $redirect    = $input->param('redirect');
+my $section     = $input->param('section');
+
 my $dbh = C4::Context->dbh;
 
 our ( $template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
@@ -74,7 +76,6 @@ our ( $template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user
         template_name   => 'tools/letter.tt',
         query           => $input,
         type            => 'intranet',
-        authnotrequired => 0,
         flagsrequired   => { tools => 'edit_notices' },
         debug           => 1,
     }
@@ -90,13 +91,14 @@ $template->param(
        script_name => $script_name,
   searchfield => $searchfield,
     branchcode => $branchcode,
+    section => $section,
        action => $script_name
 );
 
 if ( $op eq 'add_validate' or $op eq 'copy_validate' ) {
     add_validate();
     if( $redirect eq "just_save" ){
-        print $input->redirect("/cgi-bin/koha/tools/letter.pl?op=add_form&branchcode=$branchcode&module=$module&code=$code&redirect=done");
+        print $input->redirect("/cgi-bin/koha/tools/letter.pl?op=add_form&branchcode=$branchcode&module=$module&code=$code&redirect=done&section=$section");
         exit;
     } else {
         $op = q{}; # we return to the default screen for the next operation
@@ -188,7 +190,7 @@ sub add_form {
             code       => $code,
         );
         my $first_flag_name = 1;
-        my ( $lang, @templates );
+        my $lang;
         # The letter name is contained into each mtt row.
         # So we can only sent the first one to the template.
         for my $letter ( @$letters ) {
@@ -205,6 +207,7 @@ sub add_form {
             $letters{ $lang }{templates}{$mtt} = {
                 message_transport_type => $letter->{message_transport_type},
                 is_html    => $letter->{is_html},
+                updated_on => $letter->{updated_on},
                 title      => $letter->{title},
                 content    => $letter->{content} // '',
             };
@@ -230,15 +233,7 @@ sub add_form {
         push @{$field_selection}, add_fields('aqbooksellers', 'aqbasket', 'aqorders', 'biblio', 'biblioitems');
     }
     elsif ($module eq 'claimissues') {
-        push @{$field_selection}, add_fields('aqbooksellers', 'serial', 'subscription');
-        push @{$field_selection},
-        {
-            value => q{},
-            text => '---BIBLIO---'
-        };
-        foreach(qw(title author serial)) {
-            push @{$field_selection}, {value => "biblio.$_", text => ucfirst $_ };
-        }
+        push @{$field_selection}, add_fields('aqbooksellers', 'serial', 'subscription', 'biblio', 'biblioitems');
     }
     elsif ($module eq 'serial') {
         push @{$field_selection}, add_fields('branches', 'biblio', 'biblioitems', 'borrowers', 'subscription', 'serial');
@@ -263,15 +258,26 @@ sub add_form {
             push @{$field_selection}, add_fields('issues');
         }
 
-        if ( $module eq 'circulation' and $code =~ /^AR_/  ) {
+        if ( $module eq 'circulation' and $code and $code =~ /^AR_/  ) {
             push @{$field_selection}, add_fields('article_requests');
         }
+
+        if ( $module eq 'members' and $code and $code eq 'PROBLEM_REPORT' ) {
+            push @{$field_selection}, add_fields('problem_reports');
+        }
+    }
+
+    my $preview_is_available = 0;
+
+    if ($code) {
+        $preview_is_available = grep {$_ eq $code } qw( CHECKIN CHECKOUT HOLD_SLIP );
     }
 
     $template->param(
         module     => $module,
         SQLfieldnames => $field_selection,
         branchcode => $branchcode,
+        preview_is_available => $preview_is_available,
     );
     return;
 }
@@ -288,10 +294,10 @@ sub add_validate {
     my @content       = $input->multi_param('content');
     my @lang          = $input->multi_param('lang');
     for my $mtt ( @mtt ) {
-        my $is_html = $input->param("is_html_$mtt");
+        my $lang = shift @lang;
+        my $is_html = $input->param("is_html_$mtt\_$lang");
         my $title   = shift @title;
         my $content = shift @content;
-        my $lang = shift @lang;
         my $letter = C4::Letters::getletter( $oldmodule, $code, $branchcode, $mtt, $lang );
 
         # getletter can return the default letter even if we pass a branchcode
@@ -362,7 +368,7 @@ sub retrieve_letters {
 
     my $dbh = C4::Context->dbh;
     my ($sql, @where, @args);
-    $sql = "SELECT branchcode, module, code, name, branchname
+    $sql = "SELECT branchcode, module, code, name, branchname, MAX(updated_on) as updated_on
             FROM letter
             LEFT OUTER JOIN branches USING (branchcode)
     ";
@@ -381,7 +387,8 @@ sub retrieve_letters {
     }
 
     $sql .= " WHERE ".join(" AND ", @where) if @where;
-    $sql .= " GROUP BY branchcode,module,code";
+    $sql .= " GROUP BY branchcode,module,code,name,branchname";
+
     $sql .= " ORDER BY module, code, branchcode";
 
     return $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
@@ -463,12 +470,14 @@ sub get_columns_for {
         }
     }
     if ($table eq 'borrowers') {
-        if ( my $attributes = C4::Members::Attributes::GetAttributes() ) {
-            foreach (@$attributes) {
-                push @fields, {
-                    value => "borrower-attribute:$_",
-                    text  => "attribute:$_",
-                }
+        my $attribute_types = Koha::Patron::Attribute::Types->search(
+            {},
+            { order_by => 'code' },
+        );
+        while ( my $at = $attribute_types->next ) {
+            push @fields, {
+                value => "borrower-attribute:" . $at->code,
+                text  => "attribute:" . $at->code,
             }
         }
     }