1 package SL::Helper::Csv;
8 use Params::Validate qw(:all);
10 use Rose::Object::MakeMethods::Generic scalar => [ qw(
11 file encoding sep_char quote_char escape_char header profile class
12 numberformat dateformat ignore_unknown_columns _io _csv _objects _parsed
16 use SL::Helper::Csv::Dispatcher;
22 my %params = validate(@_, {
23 sep_char => { default => ';' },
24 quote_char => { default => '"' },
25 escape_char => { default => '"' },
26 header => { type => ARRAYREF, optional => 1 },
27 profile => { type => HASHREF, optional => 1 },
33 ignore_unknown_columns => 0,
35 my $self = bless {}, $class;
37 $self->$_($params{$_}) for keys %params;
39 $self->_io(IO::File->new);
40 $self->_csv(Text::CSV->new({
42 sep_char => $self->sep_char,
43 quote_char => $self->quote_char,
44 escape_char => $self->escape_char,
53 my ($self, %params) = @_;
56 return if ! $self->_check_header;
57 return if ! $self->dispatcher->parse_profile;
58 return if ! $self->_parse_data;
69 my ($self, %params) = @_;
70 croak 'no class given' unless $self->class;
71 croak 'must parse first' unless $self->_parsed;
73 $self->_make_objects unless $self->_objects;
74 return wantarray ? @{ $self->_objects } : $self->_objects;
88 my ($self, %params) = @_;
90 $self->encoding($self->_guess_encoding) if !$self->encoding;
92 $self->_io->open($self->file, '<' . $self->_encode_layer)
93 or die "could not open file " . $self->file;
99 my ($self, %params) = @_;
100 return $self->header if $self->header;
102 my $header = $self->_csv->getline($self->_io);
105 $self->_csv->error_input,
106 $self->_csv->error_diag,
110 $self->header($header);
114 my ($self, %params) = @_;
117 $self->_csv->column_names(@{ $self->header });
120 my $row = $self->_csv->getline($self->_io);
121 last if $self->_csv->eof;
124 @hr{@{ $self->header }} = @$row;
128 $self->_csv->error_input,
129 $self->_csv->error_diag,
130 $self->_io->input_line_number,
135 $self->_data(\@data);
136 $self->_push_error(@errors);
142 ':encoding(' . $_[0]->encoding . ')';
146 my ($self, %params) = @_;
149 eval "require " . $self->class;
150 local $::myconfig{numberformat} = $self->numberformat if $self->numberformat;
151 local $::myconfig{dateformat} = $self->dateformat if $self->dateformat;
153 for my $line (@{ $self->_data }) {
154 my $tmp_obj = $self->class->new;
155 $self->dispatcher->dispatch($tmp_obj, $line);
156 push @objs, $tmp_obj;
159 $self->_objects(\@objs);
163 my ($self, %params) = @_;
165 $self->{_dispatcher} ||= $self->_make_dispatcher;
168 sub _make_dispatcher {
169 my ($self, %params) = @_;
171 die 'need a header to make a dispatcher' unless $self->header;
173 return SL::Helper::Csv::Dispatcher->new($self);
176 sub _guess_encoding {
182 my ($self, @errors) = @_;
183 my @new_errors = ($self->errors, @errors);
184 $self->_errors(\@new_errors);
196 SL::Helper::Csv - take care of csv file uploads
202 my $csv = SL::Helper::Csv->new(
203 file => \$::form->{upload_file},
204 encoding => 'utf-8', # undef means utf8
205 sep_char => ',', # default ';'
206 quote_char => '\'', # default '"'
207 escape_char => '"', # default '"'
208 header => [qw(id text sellprice word)], # see later
209 profile => { sellprice => 'sellprice_as_number' },
210 class => 'SL::DB::CsvLine', # if present, map lines to this
213 my $status = $csv->parse;
214 my $hrefs = $csv->get_data;
215 my @objects = $scv->get_objects;
221 Text::CSV offeres already good functions to get lines out of a csv file, but in
222 most cases you will want those line to be parsed into hashes or even objects,
223 so this model just skips ahead and gives you objects.
225 Encoding autodetection is not easy, and should not be trusted. Try to avoid it
234 Standard constructor. You can use this to set most of the data.
238 Do the actual work. Will return true ($self actually) if success, undef if not.
242 Parse the data into objects and return those.
244 This method will return list or arrayref depending on context.
248 Returns an arrayref of the raw lines as hashrefs.
252 Return all errors that came up druing parsing. See error handling for detailed
263 The file which contents are to be read. Can be a name of a physical file or a
264 scalar ref for memory data.
268 Encoding of the CSV file. Note that this module does not do any encoding
269 guessing. Know what your data is. Defaults to utf-8.
277 Same as in L<Text::CSV>
279 =item C<header> \@FIELDS
281 Can be an array of columns, in this case the first line is not used as a
282 header. Empty header fields will be ignored in objects.
284 =item C<profile> \%ACCESSORS
286 May be used to map header fields to custom accessors. Example:
288 { listprice => listprice_as_number }
290 In this case C<listprice_as_number> will be used to read in values from the
293 In case of a One-To-One relationsship these can also be set over
294 relationsships by sparating the steps with a dot (C<.>). This will work:
296 { customer => 'customer.name' }
298 And will result in something like this:
300 $obj->customer($obj->meta->relationship('customer')->class->new);
301 $obj->customer->name($csv_line->{customer})
303 But beware, this will not try to look up anything in the database. You will
304 simply receive objects that represent what the profile defined. If some of
305 these information are unique, and should be connected to preexisting data, you
306 will have to do that for yourself. Since you provided the profile, it is
307 assumed you know what to do in this case.
311 If present, the line will be handed to the new sub of this class,
312 and the return value used instead of the line itself.
314 =item C<ignore_unknown_columns>
316 If set, the import will ignore unkown header columns. Useful for lazy imports,
317 but deactivated by default.
321 =head1 ERROR HANDLING
323 After parsing a file all errors will be accumulated into C<errors>.
325 Each entry is an arrayref with the following structure:
328 0 offending raw input,
329 1 Text::CSV error code if Text:CSV signalled an error, 0 else,
332 4 estimated line in file,
335 Note that the last entry can be off, but will give an estimate.
343 sep_char, quote_char, and escape_char are passed to Text::CSV on creation.
344 Changing them later has no effect currently.
348 Encoding errors are not dealt with properly.
354 Dispatch to child objects, like this:
356 $csv = SL::Helper::Csv->new(
358 class => SL::DB::Part,
373 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>