Bug 14383: Fix some typos in comments and documentation
[koha.git] / tools / letter.pl
index c19d995..ca9f0ce 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
 
@@ -42,7 +42,7 @@
 
 use strict;
 use warnings;
-use CGI;
+use CGI qw ( -utf8 );
 use C4::Auth;
 use C4::Context;
 use C4::Output;
@@ -50,34 +50,6 @@ 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;
-}
 # $protected_letters = protected_letters()
 # - return a hashref of letter_codes representing letters that should never be deleted
 sub protected_letters {
@@ -98,7 +70,7 @@ 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,
@@ -127,13 +99,11 @@ if ( $op eq 'add_validate' or $op eq 'copy_validate' ) {
 if ($op eq 'copy_form') {
     my $oldbranchcode = $input->param('oldbranchcode') || q||;
     my $branchcode = $input->param('branchcode') || q||;
-    my $oldcode = $input->param('oldcode') || $input->param('code');
     add_form($oldbranchcode, $module, $code);
     $template->param(
         oldbranchcode => $oldbranchcode,
         branchcode => $branchcode,
         branchloop => _branchloop($branchcode),
-        oldcode => $oldcode,
         copying => 1,
         modify => 0,
     );
@@ -145,8 +115,7 @@ 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 {
@@ -168,7 +137,13 @@ 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();
@@ -221,6 +196,9 @@ sub add_form {
     if ($module eq 'reserves') {
         push @{$field_selection}, add_fields('borrowers', 'reserves', 'biblio', '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');
     }
@@ -278,12 +256,19 @@ sub add_validate {
         my $is_html = $input->param("is_html_$mtt");
         my $title   = shift @title;
         my $content = shift @content;
-        my $letter = get_letters($branchcode,$oldmodule, $code, $mtt);
+        my $letter = C4::Letters::getletter( $oldmodule, $code, $branchcode, $mtt);
+
+        # 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 ne $letter->{branchcode} ) {
+            $letter = undef;
+        }
         unless ( $title and $content ) {
+            # Delete this mtt if no title or content given
             delete_confirmed( $branchcode, $oldmodule, $code, $mtt );
             next;
         }
-        elsif ( exists $letter->{$mtt} ) {
+        elsif ( $letter and $letter->{message_transport_type} eq $mtt ) {
             $dbh->do(
                 q{
                     UPDATE letter
@@ -310,23 +295,24 @@ sub add_validate {
 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);
+    C4::Letters::DelLetter(
+        {
+            branchcode => $branchcode,
+            module     => $module,
+            code       => $code,
+            mtt        => $mtt
+        }
+    );
     # setup default display for screen
     default_display($branchcode);
     return;
@@ -415,7 +401,7 @@ sub add_fields {
 
 sub get_columns_for {
     my $table = shift;
-# FIXME untranslateable
+# FIXME untranslatable
     my %column_map = (
         aqbooksellers => '---BOOKSELLERS---',
         aqorders      => '---ORDERS---',