ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / tools / letter.pl
index 2371218..0cc0029 100755 (executable)
@@ -4,18 +4,18 @@
 #
 # This file is part of Koha.
 #
-# 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>.
 
 =head1 tools/letter.pl
 
 # 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 CGI;
+use Modern::Perl;
+use CGI qw ( -utf8 );
 use C4::Auth;
 use C4::Context;
 use C4::Output;
-use C4::Branch; # GetBranches
 use C4::Letters;
-use C4::Members::Attributes;
-
-# _letter_from_where($branchcode,$module, $code, $mtt)
-# - return FROM WHERE clause and bind args for a letter
-sub _letter_from_where {
-    my ($branchcode, $module, $code, $mtt) = @_;
-    my $sql = q{FROM letter WHERE branchcode = ? AND module = ? AND code = ?};
-    $sql .= q{ AND message_transport_type = ?} if $mtt ne '*';
-    my @args = ( $branchcode || '', $module, $code, ($mtt ne '*' ? $mtt : ()) );
-# Mysql is retarded. cause branchcode is part of the primary key it cannot be null. How does that
-# work with foreign key constraint I wonder...
-
-#   if ($branchcode) {
-#       $sql .= " AND branchcode = ?";
-#       push @args, $branchcode;
-#   } else {
-#       $sql .= " AND branchcode IS NULL";
-#   }
-
-    return ($sql, \@args);
-}
 
-# get_letters($branchcode,$module, $code, $mtt)
-# - return letters with the given $branchcode, $module, $code and $mtt exists
-sub get_letters {
-    my ($sql, $args) = _letter_from_where(@_);
-    my $dbh = C4::Context->dbh;
-    my $letter = $dbh->selectall_hashref("SELECT * $sql", 'message_transport_type', undef, @$args);
-    return $letter;
-}
+use Koha::Patron::Attribute::Types;
 
 # $protected_letters = protected_letters()
 # - return a hashref of letter_codes representing letters that should never be deleted
@@ -91,15 +61,17 @@ our $input       = new CGI;
 my $searchfield = $input->param('searchfield');
 my $script_name = '/cgi-bin/koha/tools/letter.pl';
 our $branchcode  = $input->param('branchcode');
+$branchcode = '' if defined $branchcode and $branchcode eq '*';
 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 $dbh = C4::Context->dbh;
 
 our ( $template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
     {
-        template_name   => 'tools/letter.tmpl',
+        template_name   => 'tools/letter.tt',
         query           => $input,
         type            => 'intranet',
         authnotrequired => 0,
@@ -121,24 +93,34 @@ $template->param(
        action => $script_name
 );
 
-if ($op eq 'copy') {
-    add_copy();
-    $op = 'add_form';
+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");
+        exit;
+    } else {
+        $op = q{}; # we return to the default screen for the next operation
+    }
 }
-
-if ($op eq 'add_form') {
-    add_form($branchcode, $module, $code);
+if ($op eq 'copy_form') {
+    my $oldbranchcode = $input->param('oldbranchcode') || q||;
+    my $branchcode = $input->param('branchcode');
+    add_form($oldbranchcode, $module, $code);
+    $template->param(
+        oldbranchcode => $oldbranchcode,
+        branchcode => $branchcode,
+        copying => 1,
+        modify => 0,
+    );
 }
-elsif ( $op eq 'add_validate' ) {
-    add_validate();
-    $op = q{}; # next operation is to return to default screen
+elsif ( $op eq 'add_form' ) {
+    add_form($branchcode, $module, $code);
 }
 elsif ( $op eq 'delete_confirm' ) {
     delete_confirm($branchcode, $module, $code);
 }
 elsif ( $op eq 'delete_confirmed' ) {
-    my $mtt = $input->param('message_transport_type');
-    delete_confirmed($branchcode, $module, $code, $mtt);
+    delete_confirmed($branchcode, $module, $code);
     $op = q{}; # next operation is to return to default screen
 }
 else {
@@ -160,67 +142,98 @@ sub add_form {
     my $letters;
     # if code has been passed we can identify letter and its an update action
     if ($code) {
-        $letters = get_letters($branchcode,$module, $code, '*');
+        $letters = C4::Letters::GetLetterTemplates(
+            {
+                branchcode => $branchcode,
+                module     => $module,
+                code       => $code,
+            }
+        );
     }
 
     my $message_transport_types = GetMessageTransportTypes();
-    my @letter_loop;
+    my $templates = { map { $_ => { message_transport_type => $_ } } sort @$message_transport_types };
+    my %letters = ( default => { templates => $templates } );
+
+    if ( C4::Context->preference('TranslateNotices') ) {
+        my $translated_languages =
+          C4::Languages::getTranslatedLanguages( 'opac',
+            C4::Context->preference('template') );
+        for my $language (@$translated_languages) {
+            for my $sublanguage( @{ $language->{sublanguages_loop} } ) {
+                if ( $language->{plural} ) {
+                    $letters{ $sublanguage->{rfc4646_subtag} } = {
+                        description => $sublanguage->{native_description}
+                          . ' '
+                          . $sublanguage->{region_description} . ' ('
+                          . $sublanguage->{rfc4646_subtag} . ')',
+                        templates => { %$templates },
+                    };
+                }
+                else {
+                    $letters{ $sublanguage->{rfc4646_subtag} } = {
+                        description => $sublanguage->{native_description}
+                          . ' ('
+                          . $sublanguage->{rfc4646_subtag} . ')',
+                        templates => { %$templates },
+                    };
+                }
+            }
+        }
+        $template->param( languages => $translated_languages );
+    }
     if ($letters) {
-        my $first_flag = 1;
-        for my $mtt ( @$message_transport_types ) {
-            if ( $first_flag ) {
+        $template->param(
+            modify     => 1,
+            code       => $code,
+        );
+        my $first_flag_name = 1;
+        my ( $lang, @templates );
+        # The letter name is contained into each mtt row.
+        # So we can only sent the first one to the template.
+        for my $letter ( @$letters ) {
+            # The letter_name
+            if ( $first_flag_name and $letter->{name} ) {
                 $template->param(
-                    modify     => 1,
-                    code       => $code,
-                    branchcode => $branchcode,
-                    name       => $letters->{$mtt}{name},
+                    letter_name=> $letter->{name},
                 );
-                $first_flag = 0;
+                $first_flag_name = 0;
             }
 
-            push @letter_loop, {
-                message_transport_type => $mtt,
-                is_html    => $letters->{$mtt}{is_html},
-                title      => $letters->{$mtt}{title},
-                content    => $letters->{$mtt}{content},
+            my $lang = $letter->{lang};
+            my $mtt = $letter->{message_transport_type};
+            $letters{ $lang }{templates}{$mtt} = {
+                message_transport_type => $letter->{message_transport_type},
+                is_html    => $letter->{is_html},
+                title      => $letter->{title},
+                content    => $letter->{content} // '',
             };
         }
     }
-    else { # initialize the new fields
-        for my $mtt ( @$message_transport_types ) {
-            push @letter_loop, {
-                message_transport_type => $mtt,
-            }
-        }
-        $template->param(
-            branchcode => $branchcode,
-            module     => $module,
-        );
+    else {
         $template->param( adding => 1 );
     }
 
     $template->param(
-        letters => \@letter_loop,
+        letters => \%letters,
     );
 
     my $field_selection;
     push @{$field_selection}, add_fields('branches');
     if ($module eq 'reserves') {
-        push @{$field_selection}, add_fields('borrowers', 'reserves', 'biblio', 'items');
+        push @{$field_selection}, add_fields('borrowers', 'reserves', 'biblio', 'biblioitems', 'items');
+    }
+    elsif ( $module eq 'acquisition' ) {
+        push @{$field_selection}, add_fields('aqbooksellers', 'aqorders', 'biblio', 'items');
     }
-    elsif ($module eq 'claimacquisition') {
-        push @{$field_selection}, add_fields('aqbooksellers', 'aqorders', 'biblio', 'biblioitems');
+    elsif ($module eq 'claimacquisition' || $module eq 'orderacquisition') {
+        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');
     }
     elsif ($module eq 'suggestions') {
         push @{$field_selection}, add_fields('suggestions', 'borrowers', 'biblio');
@@ -236,108 +249,103 @@ sub add_form {
 
         }
 
-        if ( $module eq 'circulation' && $code eq "CHECKIN" ) {
+        if ( $module eq 'circulation' and $code and $code eq "CHECKIN" ) {
             push @{$field_selection}, add_fields('old_issues');
         } else {
             push @{$field_selection}, add_fields('issues');
         }
+
+        if ( $module eq 'circulation' and $code =~ /^AR_/  ) {
+            push @{$field_selection}, add_fields('article_requests');
+        }
     }
 
+    my $preview_is_available = grep {/^$code$/} qw(
+        CHECKIN CHECKOUT HOLD_SLIP
+    );
     $template->param(
         module     => $module,
-        branchloop => _branchloop($branchcode),
-        SQLfieldname => $field_selection,
+        SQLfieldnames => $field_selection,
+        branchcode => $branchcode,
+        preview_is_available => $preview_is_available,
     );
     return;
 }
 
 sub add_validate {
     my $dbh        = C4::Context->dbh;
-    my $oldbranchcode = $input->param('oldbranchcode');
-    my $branchcode    = $input->param('branchcode') || '';
+    my $branchcode    = $input->param('branchcode');
     my $module        = $input->param('module');
     my $oldmodule     = $input->param('oldmodule');
     my $code          = $input->param('code');
     my $name          = $input->param('name');
-    my @mtt           = $input->param('message_transport_type');
-    my @title         = $input->param('title');
-    my @content       = $input->param('content');
+    my @mtt           = $input->multi_param('message_transport_type');
+    my @title         = $input->multi_param('title');
+    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 $title   = shift @title;
         my $content = shift @content;
-        my $letter = get_letters($oldbranchcode,$oldmodule, $code, $mtt);
+        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
+        # If we got the default one and we needed the specific one, we didn't get the one we needed!
+        if ( $letter and $branchcode and $branchcode ne $letter->{branchcode} ) {
+            $letter = undef;
+        }
         unless ( $title and $content ) {
-            delete_confirmed( $oldbranchcode, $oldmodule, $code, $mtt );
+            # Delete this mtt if no title or content given
+            delete_confirmed( $branchcode, $oldmodule, $code, $mtt, $lang );
             next;
         }
-        if ( exists $letter->{$mtt} ) {
+        elsif ( $letter and $letter->{message_transport_type} eq $mtt and $letter->{lang} eq $lang ) {
             $dbh->do(
                 q{
                     UPDATE letter
-                    SET branchcode = ?, module = ?, name = ?, is_html = ?, title = ?, content = ?
-                    WHERE branchcode = ? AND module = ? AND code = ? AND message_transport_type = ?
+                    SET branchcode = ?, module = ?, name = ?, is_html = ?, title = ?, content = ?, lang = ?
+                    WHERE branchcode = ? AND module = ? AND code = ? AND message_transport_type = ? AND lang = ?
                 },
                 undef,
-                $branchcode, $module, $name, $is_html || 0, $title, $content,
-                $oldbranchcode, $oldmodule, $code, $mtt
+                $branchcode || '', $module, $name, $is_html || 0, $title, $content, $lang,
+                $branchcode, $oldmodule, $code, $mtt, $lang
             );
         } else {
             $dbh->do(
-                q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES (?,?,?,?,?,?,?,?)},
+                q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content,message_transport_type, lang) VALUES (?,?,?,?,?,?,?,?,?)},
                 undef,
-                $branchcode, $module, $code, $name, $is_html || 0, $title, $content, $mtt
+                $branchcode || '', $module, $code, $name, $is_html || 0, $title, $content, $mtt, $lang
             );
         }
     }
     # set up default display
     default_display($branchcode);
-}
-
-sub add_copy {
-    my $dbh        = C4::Context->dbh;
-    my $oldbranchcode = $input->param('oldbranchcode');
-    my $branchcode    = $input->param('branchcode');
-    my $module        = $input->param('module');
-    my $code          = $input->param('code');
-
-    return if keys %{ get_letters($branchcode,$module, $code, '*') };
-
-    my $old_letters = get_letters($oldbranchcode,$module, $code, '*');
-
-    my $message_transport_types = GetMessageTransportTypes();
-    for my $mtt ( @$message_transport_types ) {
-        next unless exists $old_letters->{$mtt};
-        my $old_letter = $old_letters->{$mtt};
-
-        $dbh->do(
-            q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES (?,?,?,?,?,?,?,?)},
-            undef,
-            $branchcode, $module, $code, $old_letter->{name}, $old_letter->{is_html}, $old_letter->{title}, $old_letter->{content}, $mtt
-        );
-    }
+    return 1;
 }
 
 sub delete_confirm {
     my ($branchcode, $module, $code) = @_;
     my $dbh = C4::Context->dbh;
-    my $letter = get_letters($branchcode, $module, $code, '*');
+    my $letter = C4::Letters::getletter($module, $code, $branchcode);
     my @values = values %$letter;
     $template->param(
-        branchcode => $branchcode,
-        branchname => GetBranchName($branchcode),
-        code => $code,
-        module => $module,
-        name => $values[0]->{name},
+        letter => $letter,
     );
     return;
 }
 
 sub delete_confirmed {
-    my ($branchcode, $module, $code, $mtt) = @_;
-    my ($sql, $args) = _letter_from_where($branchcode, $module, $code, $mtt);
-    my $dbh    = C4::Context->dbh;
-    $dbh->do("DELETE $sql", undef, @$args);
+    my ($branchcode, $module, $code, $mtt, $lang) = @_;
+    C4::Letters::DelLetter(
+        {
+            branchcode => $branchcode || '',
+            module     => $module,
+            code       => $code,
+            mtt        => $mtt,
+            lang       => $lang,
+        }
+    );
     # setup default display for screen
     default_display($branchcode);
     return;
@@ -369,7 +377,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);
@@ -378,6 +387,12 @@ sub retrieve_letters {
 sub default_display {
     my ($branchcode, $searchfield) = @_;
 
+    unless ( defined $branchcode ) {
+        if ( C4::Context->preference('DefaultToLoggedInLibraryNoticesSlips') ) {
+            $branchcode = C4::Context::mybranch();
+        }
+    }
+
     if ( $searchfield  ) {
         $template->param( search      => 1 );
     }
@@ -393,26 +408,10 @@ sub default_display {
 
     $template->param(
         letter => $loop_data,
-        branchloop => _branchloop($branchcode),
+        branchcode => $branchcode,
     );
 }
 
-sub _branchloop {
-    my ($branchcode) = @_;
-
-    my $branches = GetBranches();
-    my @branchloop;
-    for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
-        push @branchloop, {
-            value      => $thisbranch,
-            selected   => $branchcode && $thisbranch eq $branchcode,
-            branchname => $branches->{$thisbranch}->{'branchname'},
-        };
-    }
-
-    return \@branchloop;
-}
-
 sub add_fields {
     my @tables = @_;
     my @fields = ();
@@ -426,7 +425,7 @@ sub add_fields {
 
 sub get_columns_for {
     my $table = shift;
-# FIXME untranslateable
+# FIXME untranslatable
     my %column_map = (
         aqbooksellers => '---BOOKSELLERS---',
         aqorders      => '---ORDERS---',
@@ -461,12 +460,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,
             }
         }
     }