Bug 14383: Fix some typos in comments and documentation
[koha.git] / tools / letter.pl
index a462aba..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
 
 
 use strict;
 use warnings;
-use CGI;
+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)
-# - return FROM WHERE clause and bind args for a letter
-sub _letter_from_where {
-    my ($branchcode, $module, $code) = @_;
-    my $sql = q{FROM letter WHERE branchcode = ? AND module = ? AND code = ?};
-    my @args = ($branchcode || '', $module, $code);
-# 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);
-}
-
-# letter_exists($branchcode,$module, $code)
-# - return true if a letter with the given $branchcode, $module and $code exists
-sub letter_exists {
-    my ($sql, $args) = _letter_from_where(@_);
-    my $dbh = C4::Context->dbh;
-    my $letter = $dbh->selectrow_hashref("SELECT * $sql", undef, @$args);
-    return $letter;
-}
-
 # $protected_letters = protected_letters()
 # - return a hashref of letter_codes representing letters that should never be deleted
 sub protected_letters {
@@ -90,14 +63,14 @@ my $searchfield = $input->param('searchfield');
 my $script_name = '/cgi-bin/koha/tools/letter.pl';
 our $branchcode  = $input->param('branchcode');
 my $code        = $input->param('code');
-my $module      = $input->param('module');
+my $module      = $input->param('module') || '';
 my $content     = $input->param('content');
 my $op          = $input->param('op') || '';
 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,
@@ -106,7 +79,7 @@ our ( $template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user
     }
 );
 
-our $my_branch = C4::Context->preference("IndependantBranches") && !$staffflags->{'superlibrarian'}
+our $my_branch = C4::Context->preference("IndependentBranches") && !$staffflags->{'superlibrarian'}
   ?  C4::Context->userenv()->{'branch'}
   : undef;
 # we show only the TMPL_VAR names $op
@@ -119,17 +92,24 @@ $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();
+    $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') || q||;
+    add_form($oldbranchcode, $module, $code);
+    $template->param(
+        oldbranchcode => $oldbranchcode,
+        branchcode => $branchcode,
+        branchloop => _branchloop($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);
@@ -152,30 +132,73 @@ if ($op) {
 output_html_with_http_headers $input, $cookie, $template->output;
 
 sub add_form {
-    my ($branchcode,$module, $code ) = @_;
+    my ( $branchcode,$module, $code ) = @_;
 
-    my $letter;
+    my $letters;
     # if code has been passed we can identify letter and its an update action
     if ($code) {
-        $letter = letter_exists($branchcode,$module, $code);
+        $letters = C4::Letters::GetLetterTemplates(
+            {
+                branchcode => $branchcode,
+                module     => $module,
+                code       => $code,
+            }
+        );
     }
-    if ($letter) {
-        $template->param( modify => 1 );
-        $template->param( code   => $letter->{code} );
+
+    my $message_transport_types = GetMessageTransportTypes();
+    my @letter_loop;
+    if ($letters) {
+        $template->param(
+            modify     => 1,
+            code       => $code,
+            branchcode => $branchcode,
+        );
+        my $first_flag = 1;
+        # The letter name is contained into each mtt row.
+        # So we can only sent the first one to the template.
+        for my $mtt ( @$message_transport_types ) {
+            # The letter_name
+            if ( $first_flag and $letters->{$mtt}{name} ) {
+                $template->param(
+                    letter_name=> $letters->{$mtt}{name},
+                );
+                $first_flag = 0;
+            }
+
+            push @letter_loop, {
+                message_transport_type => $mtt,
+                is_html    => $letters->{$mtt}{is_html},
+                title      => $letters->{$mtt}{title},
+                content    => $letters->{$mtt}{content}//'',
+            };
+        }
     }
     else { # initialize the new fields
-        $letter = {
+        for my $mtt ( @$message_transport_types ) {
+            push @letter_loop, {
+                message_transport_type => $mtt,
+            }
+        }
+        $template->param(
             branchcode => $branchcode,
             module     => $module,
-        };
+        );
         $template->param( adding => 1 );
     }
 
+    $template->param(
+        letters => \@letter_loop,
+    );
+
     my $field_selection;
     push @{$field_selection}, add_fields('branches');
     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');
     }
@@ -195,8 +218,9 @@ sub add_form {
     }
     else {
         push @{$field_selection}, add_fields('biblio','biblioitems'),
-            {value => q{},             text => '---ITEMS---'  },
+            add_fields('items'),
             {value => 'items.content', text => 'items.content'},
+            {value => 'items.fine',    text => 'items.fine'},
             add_fields('borrowers');
         if ($module eq 'circulation') {
             push @{$field_selection}, add_fields('opac_news');
@@ -211,13 +235,7 @@ sub add_form {
     }
 
     $template->param(
-        branchcode => $letter->{branchcode},
-        name       => $letter->{name},
-        is_html    => $letter->{is_html},
-        title      => $letter->{title},
-        content    => $letter->{content},
         module     => $module,
-        $module    => 1,
         branchloop => _branchloop($branchcode),
         SQLfieldname => $field_selection,
     );
@@ -226,67 +244,75 @@ sub add_form {
 
 sub add_validate {
     my $dbh        = C4::Context->dbh;
-    my $oldbranchcode = $input->param('oldbranchcode');
     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 $is_html       = $input->param('is_html');
-    my $title         = $input->param('title');
-    my $content       = $input->param('content');
-    if (letter_exists($oldbranchcode,$oldmodule, $code)) {
-        $dbh->do(
-            q{UPDATE letter SET branchcode = ?, module = ?, name = ?, is_html = ?, title = ?, content = ? WHERE branchcode = ? AND module = ? AND code = ?},
-            undef,
-            $branchcode, $module, $name, $is_html || 0, $title, $content,
-            $oldbranchcode, $oldmodule, $code
-        );
-    } else {
-        $dbh->do(
-            q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content) VALUES (?,?,?,?,?,?,?)},
-            undef,
-            $branchcode, $module, $code, $name, $is_html || 0, $title, $content
-        );
+    my @mtt           = $input->param('message_transport_type');
+    my @title         = $input->param('title');
+    my @content       = $input->param('content');
+    for my $mtt ( @mtt ) {
+        my $is_html = $input->param("is_html_$mtt");
+        my $title   = shift @title;
+        my $content = shift @content;
+        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 ( $letter and $letter->{message_transport_type} eq $mtt ) {
+            $dbh->do(
+                q{
+                    UPDATE letter
+                    SET branchcode = ?, module = ?, name = ?, is_html = ?, title = ?, content = ?
+                    WHERE branchcode = ? AND module = ? AND code = ? AND message_transport_type = ?
+                },
+                undef,
+                $branchcode, $module, $name, $is_html || 0, $title, $content,
+                $branchcode, $oldmodule, $code, $mtt
+            );
+        } else {
+            $dbh->do(
+                q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content,message_transport_type) VALUES (?,?,?,?,?,?,?,?)},
+                undef,
+                $branchcode, $module, $code, $name, $is_html || 0, $title, $content, $mtt
+            );
+        }
     }
     # 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 letter_exists($branchcode,$module, $code);
-
-    my $old_letter = letter_exists($oldbranchcode,$module, $code);
-
-    $dbh->do(
-        q{INSERT INTO letter (branchcode,module,code,name,is_html,title,content) VALUES (?,?,?,?,?,?,?)},
-        undef,
-        $branchcode, $module, $code, $old_letter->{name}, $old_letter->{is_html}, $old_letter->{title}, $old_letter->{content}
-    );
+    return 1;
 }
 
 sub delete_confirm {
     my ($branchcode, $module, $code) = @_;
     my $dbh = C4::Context->dbh;
-    my $letter = letter_exists($branchcode, $module, $code);
-    $template->param( branchcode => $branchcode, branchname => GetBranchName($branchcode) );
-    $template->param( code => $code );
-    $template->param( module => $module);
-    $template->param( name => $letter->{name});
+    my $letter = C4::Letters::getletter($module, $code, $branchcode);
+    my @values = values %$letter;
+    $template->param(
+        letter => $letter,
+    );
     return;
 }
 
 sub delete_confirmed {
-    my ($branchcode, $module, $code) = @_;
-    my ($sql, $args) = _letter_from_where($branchcode, $module, $code);
-    my $dbh    = C4::Context->dbh;
-    $dbh->do("DELETE $sql", undef, @$args);
+    my ($branchcode, $module, $code, $mtt) = @_;
+    C4::Letters::DelLetter(
+        {
+            branchcode => $branchcode,
+            module     => $module,
+            code       => $code,
+            mtt        => $mtt
+        }
+    );
     # setup default display for screen
     default_display($branchcode);
     return;
@@ -301,7 +327,8 @@ sub retrieve_letters {
     my ($sql, @where, @args);
     $sql = "SELECT branchcode, module, code, name, branchname
             FROM letter
-            LEFT OUTER JOIN branches USING (branchcode)";
+            LEFT OUTER JOIN branches USING (branchcode)
+    ";
     if ($searchstring && $searchstring=~m/(\S+)/) {
         $searchstring = $1 . q{%};
         push @where, 'code LIKE ?';
@@ -317,8 +344,9 @@ sub retrieve_letters {
     }
 
     $sql .= " WHERE ".join(" AND ", @where) if @where;
+    $sql .= " GROUP BY branchcode,module,code";
     $sql .= " ORDER BY module, code, branchcode";
-#   use Data::Dumper; die Dumper($sql, \@args);
+
     return $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
 }
 
@@ -373,7 +401,7 @@ sub add_fields {
 
 sub get_columns_for {
     my $table = shift;
-# FIXME untranslateable
+# FIXME untranslatable
     my %column_map = (
         aqbooksellers => '---BOOKSELLERS---',
         aqorders      => '---ORDERS---',