1 | package LATMOS::Accounts::Web::Controller::Create; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base 'Catalyst::Controller'; |
---|
6 | use LATMOS::Accounts::Utils; |
---|
7 | use LATMOS::Accounts::Log; |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | LATMOS::Accounts::Web::Controller::Create - 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 : Private { |
---|
27 | my ( $self, $c ) = @_; |
---|
28 | |
---|
29 | $c->response->body('Matched LATMOS::Accounts::Web::Controller::Create in Create.'); |
---|
30 | } |
---|
31 | |
---|
32 | sub default : LocalPath { |
---|
33 | my ($self, $c, undef, $otype ) = @_; |
---|
34 | my $base = $c->model('Accounts')->db; |
---|
35 | my ($step) = $c->req->param('step'); |
---|
36 | my $form = $c->model('AttrForms', undef, $otype, $base); |
---|
37 | my ($status, $info); |
---|
38 | $c->stash->{page}{title} = "Ajouter un objet / $otype"; |
---|
39 | if ($step) { |
---|
40 | $info = $c->session->{ochelper}; |
---|
41 | } |
---|
42 | if ($info->{name}{ask}) { |
---|
43 | $info->{name}{content} = $c->req->param('_name'); |
---|
44 | } |
---|
45 | foreach my $attr (@{$info->{ask} || []}) { |
---|
46 | my $field = $form->field($attr); |
---|
47 | my @vals = grep { $_ } $c->req->param($field->attr_field_name()); |
---|
48 | $info->{contents}{$attr} = @vals > 1 ? \@vals : $vals[0] |
---|
49 | if (@vals); |
---|
50 | } |
---|
51 | my $ochelper = $base->ochelper($otype); |
---|
52 | ($status, $info) = $ochelper->step($info); |
---|
53 | |
---|
54 | if ($status eq 'CREATED') { |
---|
55 | $base->commit; |
---|
56 | $c->res->redirect($c->uri_for('/', |
---|
57 | $c->forward('/obj_to_uri', [ $otype ]), |
---|
58 | $info->{name}{content})); |
---|
59 | } elsif ($status eq 'ERROR') { |
---|
60 | # On error, restart the process |
---|
61 | $c->stash->{page}{error} = |
---|
62 | 'Erreur lors de la création de l\'objet: ' . |
---|
63 | LATMOS::Accounts::Log::lastmessage(LA_ERR); |
---|
64 | $base->rollback; |
---|
65 | foreach my $attr (@{$info->{ask} || []}) { |
---|
66 | my $field = $form->field($attr); |
---|
67 | $c->req->params->{$field->attr_field_name()} = $info->{contents}{$attr}; |
---|
68 | } |
---|
69 | $info->{step} = 0; # reset |
---|
70 | ($status, $info) = $ochelper->step($info); |
---|
71 | } else { |
---|
72 | foreach my $attr (@{$info->{ask} || []}) { |
---|
73 | my $field = $form->field($attr); |
---|
74 | $c->req->params->{$field->attr_field_name()} = $info->{contents}{$attr}; |
---|
75 | } |
---|
76 | $base->rollback; |
---|
77 | } |
---|
78 | $c->stash->{form} = $form; |
---|
79 | $c->stash->{ochelper} = $info; |
---|
80 | $c->session->{ochelper} = $info; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | =head1 AUTHOR |
---|
85 | |
---|
86 | Thauvin Olivier |
---|
87 | |
---|
88 | =head1 LICENSE |
---|
89 | |
---|
90 | This library is free software, you can redistribute it and/or modify |
---|
91 | it under the same terms as Perl itself. |
---|
92 | |
---|
93 | =cut |
---|
94 | |
---|
95 | 1; |
---|