MT 1587 : CSV export for cart and shelves, with the ability to define different expor...
[koha.git] / tools / csv-profiles.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 =head1 NAME
21
22 csv-profile.pl : Defines a CSV export profile
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 This script allow the user to define a new profile for CSV export
30
31 =head1 FUNCTIONS
32
33 =over 2
34
35 =cut
36
37 use strict;
38 use Data::Dumper;
39
40 use C4::Auth;
41 use C4::Context;
42 use C4::Output;
43 use CGI;
44 use C4::Koha;
45 use C4::Csv;
46
47 my $input        = new CGI;
48 my $dbh          = C4::Context->dbh;
49
50 # open template
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52     {
53         template_name   => "tools/csv-profiles.tmpl",
54         query           => $input,
55         type            => "intranet",
56         authnotrequired => 0,
57         flagsrequired   => { tools => 'manage_csv_profiles' },
58         debug           => 1,
59     }
60 );
61
62
63 my $profile_name        = $input->param("profile_name");
64 my $profile_description = $input->param("profile_description");
65 my $profile_content     = $input->param("profile_content");
66 my $action              = $input->param("action");
67 my $delete              = $input->param("delete");
68 my $id                  = $input->param("id");
69 if ($delete) { $action = "delete"; }
70
71 if ($profile_name && $profile_content && $profile_description && $action) {
72     my $rows;
73
74     if ($action eq "create") {
75         my $query = "INSERT INTO export_format(export_format_id, profile, description, marcfields) VALUES (NULL, ?, ?, ?)";
76         my $sth   = $dbh->prepare($query);
77         $rows  = $sth->execute($profile_name, $profile_description, $profile_content);
78     
79     }
80
81     if ($action eq "edit") {
82         my $query = "UPDATE export_format SET description=?, marcfields=? WHERE export_format_id=? LIMIT 1";
83         my $sth   = $dbh->prepare($query);
84         $rows  = $sth->execute($profile_description, $profile_content, $profile_name);
85
86     }
87
88     if ($action eq "delete") {
89         my $query = "DELETE FROM export_format WHERE export_format_id=? LIMIT 1";
90         my $sth   = $dbh->prepare($query);
91         $rows  = $sth->execute($profile_name);
92
93     }
94
95     $rows ? $template->param(success => 1) : $template->param(error => 1);
96     $template->param(profile_name => $profile_name);
97     $template->param(action => $action);
98
99 }
100
101     # If a profile has been selected for modification
102     if ($id) {
103         my $query = "SELECT export_format_id, profile, description, marcfields FROM export_format WHERE export_format_id = ?";
104         my $sth;
105         $sth = $dbh->prepare($query);
106
107         $sth->execute($id);
108         my $selected_profile = $sth->fetchrow_arrayref();
109         $template->param(
110             selected_profile_id          => $selected_profile->[0],
111             selected_profile_name        => $selected_profile->[1],
112             selected_profile_description => $selected_profile->[2],
113             selected_profile_marcfields  => $selected_profile->[3]
114         );
115
116     }
117
118     # List of existing profiles
119     $template->param(existing_profiles => GetCsvProfilesLoop());
120
121 output_html_with_http_headers $input, $cookie, $template->output;