1 | package LATMOS::Accounts::Bases::Sql::Accreq; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use LATMOS::Accounts::Utils; |
---|
8 | use LATMOS::Accounts::Log; |
---|
9 | use base qw(LATMOS::Accounts::Bases::Sql::objects); |
---|
10 | use YAML; |
---|
11 | |
---|
12 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
13 | |
---|
14 | =head1 NAME |
---|
15 | |
---|
16 | LATMOS::Accounts::Bases::Sql::Accreq - Accreq object in LATMOS::Accounts system |
---|
17 | |
---|
18 | =head1 DESCRIPTION |
---|
19 | |
---|
20 | The C<Accreq> object is a form handle information about form allowing user to |
---|
21 | request admin for change |
---|
22 | |
---|
23 | =head1 FUNCTIONS |
---|
24 | |
---|
25 | =cut |
---|
26 | |
---|
27 | sub _yaml_attr { qw(description notifyMail) } |
---|
28 | |
---|
29 | sub _object_table { 'accreq' } |
---|
30 | |
---|
31 | sub _key_field { 'name' } |
---|
32 | |
---|
33 | sub _has_extended_attributes { 1 } |
---|
34 | |
---|
35 | sub _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 | |
---|
100 | Load C<form> attribute and return reference to data read from YAML data |
---|
101 | |
---|
102 | =cut |
---|
103 | |
---|
104 | sub 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 | |
---|
113 | sub _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 | |
---|
136 | Return information for attribute C<$attr> from form. |
---|
137 | |
---|
138 | =cut |
---|
139 | |
---|
140 | sub 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 | |
---|
162 | 1; |
---|
163 | |
---|
164 | __END__ |
---|
165 | |
---|
166 | =head1 SEE ALSO |
---|
167 | |
---|
168 | L<LATMOS::Accounts::Bases::Sql> |
---|
169 | |
---|
170 | =head1 AUTHOR |
---|
171 | |
---|
172 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
173 | |
---|
174 | =head1 COPYRIGHT AND LICENSE |
---|
175 | |
---|
176 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
177 | |
---|
178 | This library is free software; you can redistribute it and/or modify |
---|
179 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
180 | at your option, any later version of Perl 5 you may have available. |
---|
181 | |
---|
182 | =cut |
---|