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