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

Last change on this file since 1133 was 1099, checked in by nanardon, 12 years ago

Form tagged as hidden are not displayed

  • Property svn:keywords set to Id Rev
File size: 4.4 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        }
95    )
96}
97
98=head2 parse_form
99
100Load C<form> attribute and return reference to data read from YAML data
101
102=cut 
103
104sub parse_form {
105    my ($self) = @_;
106    if (my $text = $self->get_field('form')) {
107        $self->_parse_form($text);
108    } else {
109        return { attrs => [] };
110    }
111}
112
113sub _parse_form {
114    my ($self, $yaml) = @_;
115
116    my $ref = YAML::thaw($yaml) or return 0;
117
118    my @attributes = ();
119    my @attrs = @{ $ref->{attrs} };
120    while (my $attr = shift(@attrs)) {
121        my $attrname = ref $attr
122            ? $attr->{name}
123            : $attr;
124
125        if (!$attrname) {
126            $self->base->log('LA_ERR', 'No attribute name supplied');
127            return;
128        }
129    }
130
131    $ref
132}
133
134=head2 attr_info($attr)
135
136Return information for attribute C<$attr> from form.
137
138=cut
139
140sub attr_info {
141    my ($self, $wanted_attr) = @_;
142
143    my $ref = $self->parse_form;
144
145    my @attrs = @{ $ref->{attrs} };
146    while (my $attr = shift(@attrs)) {
147        my $attrname = ref $attr
148            ? $attr->{name}
149            : $attr;
150        if ($wanted_attr eq $attrname) {
151            if (ref $attr) {
152                return $attr;
153            } else {
154                return;
155            }
156        }
157    }
158    return;
159}
160
161
1621;
163
164__END__
165
166=head1 SEE ALSO
167
168L<LATMOS::Accounts::Bases::Sql>
169
170=head1 AUTHOR
171
172Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
173
174=head1 COPYRIGHT AND LICENSE
175
176Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
177
178This library is free software; you can redistribute it and/or modify
179it under the same terms as Perl itself, either Perl version 5.10.0 or,
180at your option, any later version of Perl 5 you may have available.
181
182=cut
Note: See TracBrowser for help on using the repository browser.