Hochladen und Anzeigen von hochgeladenen Dateien implementiert
[kivitendo-erp.git] / SL / Controller / CsvImport.pm
1 package SL::Controller::CsvImport;
2
3 use strict;
4
5 use SL::DB::Buchungsgruppe;
6 use SL::DB::CsvImportProfile;
7 use SL::Helper::Flash;
8 use SL::SessionFile;
9
10 use List::MoreUtils qw(none);
11
12 use parent qw(SL::Controller::Base);
13
14 use Rose::Object::MakeMethods::Generic
15 (
16  scalar => [ qw(type profile file all_profiles all_charsets sep_char all_sep_chars quote_char all_quote_chars escape_char all_escape_chars all_buchungsgruppen) ],
17 );
18
19 __PACKAGE__->run_before('check_auth');
20 __PACKAGE__->run_before('ensure_form_structure');
21 __PACKAGE__->run_before('check_type');
22 __PACKAGE__->run_before('load_all_profiles');
23
24 #
25 # actions
26 #
27
28 sub action_new {
29   my ($self) = @_;
30
31   $self->load_default_profile unless $self->profile;
32   $self->render_inputs;
33 }
34
35 sub action_test {
36   my ($self) = @_;
37   $self->test_and_import(test => 1);
38 }
39
40 sub action_import {
41   my $self = shift;
42   $self->test_and_import(test => 0);
43 }
44
45 sub action_save {
46   my ($self) = @_;
47
48   $self->profile_from_form(SL::DB::Manager::CsvImportProfile->find_by(name => $::form->{profile}->{name}));
49   $self->profile->save;
50
51   flash_later('info', $::locale->text("The profile has been saved under the name '#1'.", $self->profile->name));
52   $self->redirect_to(action => 'new', 'profile.type' => $self->type, 'profile.id' => $self->profile->id);
53 }
54
55 sub action_destroy {
56   my $self = shift;
57
58   my $profile = SL::DB::CsvImportProfile->new(id => $::form->{profile}->{id});
59   $profile->delete(cascade => 1);
60
61   flash_later('info', $::locale->text('The profile \'#1\' has been deleted.', $profile->name));
62   $self->redirect_to(action => 'new', 'profile.type' => $self->type);
63 }
64
65 #
66 # filters
67 #
68
69 sub check_auth {
70   $::auth->assert('config');
71 }
72
73 sub check_type {
74   my ($self) = @_;
75
76   die "Invalid CSV import type" if none { $_ eq $::form->{profile}->{type} } qw(parts customers_vendors addresses contacts);
77   $self->type($::form->{profile}->{type});
78 }
79
80 sub ensure_form_structure {
81   my ($self, %params) = @_;
82
83   $::form->{profile}  = {} unless ref $::form->{profile}  eq 'HASH';
84   $::form->{settings} = {} unless ref $::form->{settings} eq 'HASH';
85 }
86
87 #
88 # helpers
89 #
90
91 sub render_inputs {
92   my ($self, %params) = @_;
93
94   $self->all_charsets([ [ 'UTF-8',       'UTF-8'                 ],
95                         [ 'ISO-8859-1',  'ISO-8859-1 (Latin 1)'  ],
96                         [ 'ISO-8859-15', 'ISO-8859-15 (Latin 9)' ],
97                         [ 'CP850',       'CP850 (DOS/ANSI)'      ],
98                         [ 'CP1252',      'CP1252 (Windows)'      ],
99                       ]);
100
101   my %char_map = $self->char_map;
102
103   foreach my $type (qw(sep quote escape)) {
104     my $sub = "all_${type}_chars";
105     $self->$sub([ sort { $a->[0] cmp $b->[0] } values %{ $char_map{$type} } ]);
106
107     my $char = $self->profile->get($type . '_char');
108     $sub     = "${type}_char";
109     $self->$sub(($char_map{$type}->{$char} || [])->[0] || $char);
110   }
111
112   $self->file(SL::SessionFile->new($self->csv_file_name));
113
114   my $title = $self->type eq 'customers_vendors' ? $::locale->text('CSV import: customers and vendors')
115             : $self->type eq 'addresses'         ? $::locale->text('CSV import: shipping addresses')
116             : $self->type eq 'contacts'          ? $::locale->text('CSV import: contacts')
117             : $self->type eq 'parts'             ? $::locale->text('CSV import: parts and services')
118             : die;
119
120   $self->all_buchungsgruppen(SL::DB::Manager::Buchungsgruppe->get_all_sorted);
121
122   $self->render('csv_import/form', title => $title);
123 }
124
125 sub test_and_import {
126   my ($self, %params) = @_;
127
128   $self->profile_from_form;
129
130   if ($::form->{file}) {
131     my $file = SL::SessionFile->new($self->csv_file_name, "w");
132     $file->fh->print($::form->{file});
133     $file->fh->close;
134   }
135
136   my $file = SL::SessionFile->new($self->csv_file_name, "w");
137   if (!$file->fh) {
138     flash('error', $::locale->text('No file has been uploaded yet.'));
139     return $self->action_new;
140   }
141
142   # do the import thingy...
143   $self->action_new;
144 }
145
146 sub load_default_profile {
147   my ($self) = @_;
148
149   if ($::form->{profile}->{id}) {
150     $self->profile(SL::DB::CsvImportProfile->new(id => $::form->{profile}->{id})->load);
151
152   } else {
153     $self->profile(SL::DB::Manager::CsvImportProfile->find_by(type => $self->{type}, is_default => 1));
154     $self->profile(SL::DB::CsvImportProfile->new(type => $self->{type})) unless $self->profile;
155   }
156
157   $self->profile->set_defaults;
158 }
159
160 sub load_all_profiles {
161   my ($self, %params) = @_;
162
163   $self->all_profiles(SL::DB::Manager::CsvImportProfile->get_all(where => [ type => $self->type ], sort_by => 'name'));
164 }
165
166 sub profile_from_form {
167   my ($self, $existing_profile) = @_;
168
169   delete $::form->{profile}->{id};
170
171   my %char_map = $self->char_map;
172   my @settings;
173
174   foreach my $type (qw(sep quote escape)) {
175     my %rev_chars = map { $char_map{$type}->{$_}->[0] => $_ } keys %{ $char_map{$type} };
176     my $char      = $::form->{"${type}_char"} eq 'custom' ? $::form->{"custom_${type}_char"} : $rev_chars{ $::form->{"${type}_char"} };
177
178     push @settings, { key => "${type}_char", value => $char };
179   }
180
181   delete $::form->{profile}->{id};
182   $self->profile($existing_profile || SL::DB::CsvImportProfile->new);
183   $self->profile->assign_attributes(%{ $::form->{profile} });
184   $self->profile->settings(map({ { key => $_, value => $::form->{settings}->{$_} } } keys %{ $::form->{settings} }),
185                            @settings);
186   $self->profile->set_defaults;
187 }
188
189 sub char_map {
190   return ( sep    => { ','  => [ 'comma',     $::locale->text('Comma')     ],
191                        ';'  => [ 'semicolon', $::locale->text('Semicolon') ],
192                        "\t" => [ 'tab',       $::locale->text('Tab')       ],
193                        ' '  => [ 'space',     $::locale->text('Space')     ],
194                      },
195            quote  => { '"' => [ 'quote', $::locale->text('Quotes') ],
196                        "'" => [ 'singlequote', $::locale->text('Single quotes') ],
197                      },
198            escape => { '"' => [ 'quote', $::locale->text('Quotes') ],
199                        "'" => [ 'singlequote', $::locale->text('Single quotes') ],
200                      },
201          );
202 }
203
204 sub csv_file_name {
205   my ($self) = @_;
206   return "csv-import-" . $self->type . ".csv";
207 }
208
209 1;