Bug 19073: (bug 15758 follow-up) Dereference correct value from Koha::Object.
[koha.git] / tools / csv-profiles.pl
index 32f2b98..dd09677 100755 (executable)
@@ -1,21 +1,22 @@
 #!/usr/bin/perl
 
 # Copyright 2009 BibLibre
+# Copyright 2015 Koha Development Team
 #
 # 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 NAME
 
@@ -33,25 +34,23 @@ This script allow the user to define a new profile for CSV export
 
 =cut
 
-use strict;
-#use warnings; FIXME - Bug 2505
-use Data::Dumper;
+use Modern::Perl;
 use Encode;
 
 use C4::Auth;
 use C4::Context;
 use C4::Output;
-use CGI;
+use CGI qw ( -utf8 );
 use C4::Koha;
-use C4::Csv;
+use Koha::CsvProfiles;
 
-my $input        = new CGI;
-my $dbh          = C4::Context->dbh;
+my $input            = new CGI;
+my $export_format_id = $input->param('export_format_id');
+my $op               = $input->param('op') || 'list';
+my @messages;
 
-# open template
 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
-    {
-        template_name   => "tools/csv-profiles.tmpl",
+    {   template_name   => "tools/csv-profiles.tt",
         query           => $input,
         type            => "intranet",
         authnotrequired => 0,
@@ -61,73 +60,94 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 );
 
 # Getting available encodings list
-my @encodings = Encode->encodings();
-my @encodings_loop = map{{encoding => $_}} @encodings;
-$template->param(encodings => \@encodings_loop);
-
-my $profile_name        = $input->param("profile_name");
-my $profile_description = $input->param("profile_description");
-my $profile_content     = $input->param("profile_content");
-my $csv_separator       = $input->param("csv_separator");
-my $field_separator     = $input->param("field_separator");
-my $subfield_separator  = $input->param("subfield_separator");
-my $encoding            = $input->param("encoding");
-my $action              = $input->param("action");
-my $delete              = $input->param("delete");
-my $id                  = $input->param("id");
-if ($delete) { $action = "delete"; }
-
-if ($profile_name && $profile_content && $action) {
-    my $rows;
-
-    if ($action eq "create") {
-       my $query = "INSERT INTO export_format(export_format_id, profile, description, marcfields, csv_separator, field_separator, subfield_separator, encoding) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?)";
-       my $sth   = $dbh->prepare($query);
-       $rows  = $sth->execute($profile_name, $profile_description, $profile_content, $csv_separator, $field_separator, $subfield_separator, $encoding);
-    
-    }
+$template->param( encodings => [ Encode->encodings ] );
 
-    if ($action eq "edit") {
-       my $query = "UPDATE export_format SET description=?, marcfields=?, csv_separator=?, field_separator=?, subfield_separator=?, encoding=? WHERE export_format_id=? LIMIT 1";
-       my $sth   = $dbh->prepare($query);
-       $rows  = $sth->execute($profile_description, $profile_content, $csv_separator, $field_separator, $subfield_separator, $encoding, $profile_name);
+if ( $op eq 'add_form' ) {
+    my $csv_profile;
+    if ($export_format_id) {
+        $csv_profile = Koha::CsvProfiles->find($export_format_id);
     }
-
-    if ($action eq "delete") {
-       my $query = "DELETE FROM export_format WHERE export_format_id=? LIMIT 1";
-       my $sth   = $dbh->prepare($query);
-       $rows  = $sth->execute($profile_name);
-
+    $template->param( csv_profile => $csv_profile, );
+} elsif ( $op eq 'add_validate' ) {
+    my $profile     = $input->param("profile");
+    my $description = $input->param("description");
+    my $type        = $input->param("type");
+    my $used_for    =
+        $type eq "marc"
+      ? $input->param("used_for_marc")
+      : $input->param("used_for_sql");
+    my $content =
+        $type eq "marc"
+      ? $input->param("marc_content")
+      : $input->param("sql_content");
+    my $csv_separator      = $input->param("csv_separator");
+    my $field_separator    = $input->param("field_separator");
+    my $subfield_separator = $input->param("subfield_separator");
+    my $encoding           = $input->param("encoding");
+
+    if ($export_format_id) {
+        my $csv_profile = Koha::CsvProfiles->find($export_format_id)
+            or die "Something went wrong! This export_format_id does not match any existing CSV profile.";
+        $csv_profile->profile($profile);
+        $csv_profile->description($description);
+        $csv_profile->content($content);
+        $csv_profile->csv_separator($csv_separator);
+        $csv_profile->field_separator($field_separator);
+        $csv_profile->subfield_separator($subfield_separator);
+        $csv_profile->encoding($encoding);
+        $csv_profile->type($type);
+        $csv_profile->used_for($used_for);
+        eval { $csv_profile->store; };
+
+        if ($@) {
+            push @messages, { type => 'error', code => 'error_on_update' };
+        } else {
+            push @messages, { type => 'message', code => 'success_on_update' };
+        }
+    } else {
+        my $csv_profile = Koha::CsvProfile->new(
+            {   profile            => $profile,
+                description        => $description,
+                content            => $content,
+                csv_separator      => $csv_separator,
+                field_separator    => $field_separator,
+                subfield_separator => $subfield_separator,
+                encoding           => $encoding,
+                type               => $type,
+                used_for           => $used_for,
+            }
+        );
+        eval { $csv_profile->store; };
+        if ($@) {
+            push @messages, { type => 'error', code => 'error_on_insert' };
+        } else {
+            push @messages, { type => 'message', code => 'success_on_insert' };
+        }
     }
-
-    $rows ? $template->param(success => 1) : $template->param(error => 1);
-    $template->param(profile_name => $profile_name);
-    $template->param(action => $action);
-
+    $op = 'list';
+} elsif ( $op eq 'delete_confirm' ) {
+    my $csv_profile = Koha::CsvProfiles->find($export_format_id);
+    $template->param( csv_profile => $csv_profile, );
+} elsif ( $op eq 'delete_confirmed' ) {
+    my $csv_profile = Koha::CsvProfiles->find($export_format_id);
+    my $deleted = eval { $csv_profile->delete; };
+
+    if ( $@ or not $deleted ) {
+        push @messages, { type => 'error', code => 'error_on_delete' };
+    } else {
+        push @messages, { type => 'message', code => 'success_on_delete' };
+    }
+    $op = 'list';
 }
 
-    # If a profile has been selected for modification
-    if ($id) {
-       my $query = "SELECT export_format_id, profile, description, marcfields, csv_separator, field_separator, subfield_separator, encoding FROM export_format WHERE export_format_id = ?";
-       my $sth;
-       $sth = $dbh->prepare($query);
-
-       $sth->execute($id);
-       my $selected_profile = $sth->fetchrow_arrayref();
-       $template->param(
-           selected_profile_id          => $selected_profile->[0],
-           selected_profile_name        => $selected_profile->[1],
-           selected_profile_description => $selected_profile->[2],
-           selected_profile_marcfields  => $selected_profile->[3],
-           selected_csv_separator       => $selected_profile->[4],
-           selected_field_separator     => $selected_profile->[5],
-           selected_subfield_separator  => $selected_profile->[6],
-           selected_encoding            => $selected_profile->[7]
-       );
-
-    }
+if ( $op eq 'list' ) {
+    my $csv_profiles = Koha::CsvProfiles->search;
+    $template->param( csv_profiles => $csv_profiles, );
+}
 
-    # List of existing profiles
-    $template->param(existing_profiles => GetCsvProfilesLoop());
+$template->param(
+    messages => \@messages,
+    op       => $op,
+);
 
 output_html_with_http_headers $input, $cookie, $template->output;