source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Controller/Tools/CSV.pm @ 1991

Last change on this file since 1991 was 1991, checked in by nanardon, 7 years ago

Use hash for loadCSV options to allow more parameters

File size: 2.2 KB
Line 
1package LATMOS::Accounts::Web::Controller::Tools::CSV;
2use Moose;
3use namespace::autoclean;
4use LATMOS::Accounts::Utils;
5use LATMOS::Accounts::Log;
6
7BEGIN { extends 'Catalyst::Controller'; }
8
9=head1 NAME
10
11LATMOS::Accounts::Web::Controller::Tools::MassCreation - Catalyst Controller
12
13=head1 DESCRIPTION
14
15Catalyst Controller.
16
17=head1 METHODS
18
19=cut
20
21
22=head2 index
23
24=cut
25
26sub index :Path :Args(0) {
27    my ( $self, $c ) = @_;
28
29    if ($c->req->upload('csv')) {
30        $c->go('parseCSV');
31    }
32}
33
34sub parseCSV : Private {
35    my ( $self, $c ) = @_;
36
37    my $upload = $c->req->upload('csv');
38    my $otype = $c->req->param('otype');
39    $c->stash->{otype} = $otype;
40
41    my $error = 0;
42    loadCSV(
43        $upload->fh,
44        cb => sub {
45            my ($res, $count) = @_;
46
47            my $ochelper = $c->model('Accounts')->db->ochelper($otype);
48
49            my $info = {
50                contents => $res
51            };
52
53            if ($res->{name}) {
54                $info->{name}{content} = $res->{name};
55            }
56
57            if ($ochelper->Automate($info)) {
58                push(@{ $c->stash->{results} },
59                    {
60                        objectName => $info->{name}{content},
61                        info => $res,
62                    }
63                );
64                push(@{ $c->session->{selectedObjects} }, $info->{name}{content});
65                $c->session->{selectedOtype} = $otype;
66            } else {
67                $error = 1;
68                push(@{ $c->stash->{failures} },
69                    {
70                        line => $count,
71                        error => LATMOS::Accounts::Log::lastmessage,
72                    }
73                );
74            }
75            return 1;
76        },
77        initcb => sub {
78            my ($csv) = @_;
79            @{ $c->stash->{attrs} } = $csv->column_names;
80        },
81    );
82
83    if ($error) {
84        $c->stash->{failure} = 1;
85        $c->model('Accounts')->db->rollback;
86    } else {
87        $c->model('Accounts')->db->commit;
88    }
89   
90}
91
92
93
94=encoding utf8
95
96=head1 AUTHOR
97
98Olivier Thauvin,Guyancourt - B1428,+33 1 80285052,
99
100=head1 LICENSE
101
102This library is free software. You can redistribute it and/or modify
103it under the same terms as Perl itself.
104
105=cut
106
107__PACKAGE__->meta->make_immutable;
108
1091;
Note: See TracBrowser for help on using the repository browser.