1 | package LATMOS::Accounts::Web::Controller::Tools::CSV; |
---|
2 | use Moose; |
---|
3 | use namespace::autoclean; |
---|
4 | use LATMOS::Accounts::Utils; |
---|
5 | use LATMOS::Accounts::Log; |
---|
6 | |
---|
7 | BEGIN { extends 'Catalyst::Controller'; } |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | LATMOS::Accounts::Web::Controller::Tools::MassCreation - Catalyst Controller |
---|
12 | |
---|
13 | =head1 DESCRIPTION |
---|
14 | |
---|
15 | Catalyst Controller. |
---|
16 | |
---|
17 | =head1 METHODS |
---|
18 | |
---|
19 | =cut |
---|
20 | |
---|
21 | |
---|
22 | =head2 index |
---|
23 | |
---|
24 | =cut |
---|
25 | |
---|
26 | sub index :Path :Args(0) { |
---|
27 | my ( $self, $c ) = @_; |
---|
28 | |
---|
29 | if ($c->req->upload('csv')) { |
---|
30 | $c->go('parseCSV'); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | sub 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 | |
---|
98 | Olivier Thauvin,Guyancourt - B1428,+33 1 80285052, |
---|
99 | |
---|
100 | =head1 LICENSE |
---|
101 | |
---|
102 | This library is free software. You can redistribute it and/or modify |
---|
103 | it under the same terms as Perl itself. |
---|
104 | |
---|
105 | =cut |
---|
106 | |
---|
107 | __PACKAGE__->meta->make_immutable; |
---|
108 | |
---|
109 | 1; |
---|