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

Last change on this file since 984 was 971, checked in by nanardon, 12 years ago
  • set properties
  • Property svn:keywords set to Id Rev
File size: 4.2 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            form => {
70                get => sub {
71                    my ($self) = @_;
72                    if (my $obj = $self->object) {
73                        my $res = $obj->parse_form;
74                        foreach (_yaml_attr()) {
75                            $res->{$_} = $obj->get_field($_);
76                        }
77                        return YAML::freeze($res);
78                    } else {
79                        return;
80                    }
81                },
82                set => sub {
83                    my ($self, $yaml) = @_;
84                    my $res = _parse_form($self, $yaml)
85                        or return 0;
86                    my %attrs;
87                    foreach (_yaml_attr()) {
88                        $attrs{$_} = $res->{$_} if (exists($res->{$_}));
89                    }
90                    $self->object->set_fields(%attrs, form => $yaml);
91                },
92            },
93        }
94    )
95}
96
97sub parse_form {
98    my ($self) = @_;
99    if (my $text = $self->get_field('form')) {
100        $self->_parse_form($text);
101    } else {
102        return { attrs => [] };
103    }
104}
105
106sub attr_info {
107    my ($self, $wanted_attr) = @_;
108
109    my $ref = $self->parse_form;
110
111    my @attrs = @{ $ref->{attrs} };
112    while (my $attr = shift(@attrs)) {
113        my $attrname = ref $attr
114            ? $attr->{name}
115            : $attr;
116        if ($wanted_attr eq $attrname) {
117            if (ref $attr) {
118                return $attr;
119            } else {
120                return;
121            }
122        }
123    }
124    return;
125}
126
127sub _parse_form {
128    my ($self, $yaml) = @_;
129
130    my $ref = YAML::thaw($yaml) or return 0;
131
132    my @attributes = ();
133    my @attrs = @{ $ref->{attrs} };
134    while (my $attr = shift(@attrs)) {
135        my $attrname = ref $attr
136            ? $attr->{name}
137            : $attr;
138
139        if (!$attrname) {
140            $self->base->log('LA_ERR', 'No attribute name supplied');
141            return;
142        }
143    }
144
145    $ref
146}
147
1481;
149
150__END__
151
152=head1 SEE ALSO
153
154=head1 AUTHOR
155
156Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
157
158=head1 COPYRIGHT AND LICENSE
159
160Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
161
162This library is free software; you can redistribute it and/or modify
163it under the same terms as Perl itself, either Perl version 5.10.0 or,
164at your option, any later version of Perl 5 you may have available.
165
166=cut
Note: See TracBrowser for help on using the repository browser.