source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Accreq.pm @ 1315

Last change on this file since 1315 was 1315, checked in by nanardon, 9 years ago

merge changes

  • Property svn:keywords set to Id Rev
File size: 4.5 KB
Line 
1package LATMOS::Accounts::Bases::Sql::Accreq;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use LATMOS::Accounts::Utils;
8use LATMOS::Accounts::Log;
9use base qw(LATMOS::Accounts::Bases::Sql::objects);
10use YAML;
11
12our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
13
14=head1 NAME
15
16LATMOS::Accounts::Bases::Sql::Accreq - Accreq object in LATMOS::Accounts system
17
18=head1 DESCRIPTION
19
20The C<Accreq> object is a form handle information about form allowing user to
21request admin for change
22
23=head1 FUNCTIONS
24
25=cut
26
27sub _yaml_attr { qw(description notifyMail) }
28
29sub _object_table { 'accreq' }
30
31sub _key_field { 'name' }
32
33sub _has_extended_attributes { 1 }
34
35sub _get_attr_schema {
36    my ($class, $base) = @_;
37
38    $class->SUPER::_get_attr_schema($base,
39        {
40            unexported  => { inline => 1, formtype => 'CHECKBOX',},
41            name        => { inline => 1, ro => 1, },
42            create      => { inline => 1, ro => 1, },
43            date        => { inline => 1, ro => 1, },
44            attributes  => {
45                ro => 1,
46                multiple => 1,
47                get => sub {
48                    my ($self) = @_;
49                    my $ref = _parse_form($self,
50                        $self->object->get_attributes('form'));
51                    my @attributes = ();
52                    my @attrs = @{ $ref->{attrs} };
53                    while (my $attr = shift(@attrs)) {
54                        my $attrname = ref $attr
55                        ? $attr->{name}
56                        : $attr;
57                        push(@attributes, $attrname);
58                    }
59                    \@attributes
60                },
61            },
62            oType       => {
63                can_values => sub {
64                    return $base->list_supported_objects
65                },
66                mandatory => 1,
67            },
68            requireObject => { formtype => 'CHECKBOX', },
69            hidden => { formtype => 'CHECKBOX', },
70            form => {
71                get => sub {
72                    my ($self) = @_;
73                    if (my $obj = $self->object) {
74                        my $res = $obj->parse_form;
75                        foreach (_yaml_attr()) {
76                            $res->{$_} = $obj->get_field($_);
77                        }
78                        return YAML::freeze($res);
79                    } else {
80                        return;
81                    }
82                },
83                set => sub {
84                    my ($self, $yaml) = @_;
85                    my $res = _parse_form($self, $yaml)
86                        or return 0;
87                    my %attrs;
88                    foreach (_yaml_attr()) {
89                        $attrs{$_} = $res->{$_} if (exists($res->{$_}));
90                    }
91                    $self->object->set_fields(%attrs, form => $yaml);
92                },
93            },
94            comment => { },
95            description => { },
96            notifyMail => { },
97        }
98    )
99}
100
101=head2 parse_form
102
103Load C<form> attribute and return reference to data read from YAML data
104
105=cut 
106
107sub parse_form {
108    my ($self) = @_;
109    if (my $text = $self->get_field('form')) {
110        $self->_parse_form($text);
111    } else {
112        return { attrs => [] };
113    }
114}
115
116sub _parse_form {
117    my ($self, $yaml) = @_;
118
119    my $ref = YAML::thaw($yaml) or return 0;
120
121    my @attributes = ();
122    my @attrs = @{ $ref->{attrs} };
123    while (my $attr = shift(@attrs)) {
124        my $attrname = ref $attr
125            ? $attr->{name}
126            : $attr;
127
128        if (!$attrname) {
129            $self->base->log('LA_ERR', 'No attribute name supplied');
130            return;
131        }
132    }
133
134    $ref
135}
136
137=head2 attr_info($attr)
138
139Return information for attribute C<$attr> from form.
140
141=cut
142
143sub attr_info {
144    my ($self, $wanted_attr) = @_;
145
146    my $ref = $self->parse_form;
147
148    my @attrs = @{ $ref->{attrs} };
149    while (my $attr = shift(@attrs)) {
150        my $attrname = ref $attr
151            ? $attr->{name}
152            : $attr;
153        if ($wanted_attr eq $attrname) {
154            if (ref $attr) {
155                return $attr;
156            } else {
157                return;
158            }
159        }
160    }
161    return;
162}
163
164
1651;
166
167__END__
168
169=head1 SEE ALSO
170
171L<LATMOS::Accounts::Bases::Sql>
172
173=head1 AUTHOR
174
175Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
176
177=head1 COPYRIGHT AND LICENSE
178
179Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
180
181This library is free software; you can redistribute it and/or modify
182it under the same terms as Perl itself, either Perl version 5.10.0 or,
183at your option, any later version of Perl 5 you may have available.
184
185=cut
Note: See TracBrowser for help on using the repository browser.