source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Model/AttrForms.pm @ 1331

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

typo

File size: 6.8 KB
Line 
1package LATMOS::Accounts::Web::Model::AttrForms;
2
3use strict;
4use warnings;
5use base 'Catalyst::Model';
6use LATMOS::Accounts::Log;
7use HTML::Prototype;
8
9=head1 NAME
10
11LATMOS::Accounts::Web::Model::AttrForms - Catalyst Model
12
13=head1 DESCRIPTION
14
15Catalyst Model.
16
17=cut
18
19my $forms = {
20    usersys => {
21        name => 'SystÚme',
22        attrs => [ qw(
23            sn givenName description
24            comment
25            uid uidNumber gidNumber gecos homeDirectory loginShell
26            mail initials nickname
27            expire
28            locked
29            create
30            date
31        ) ],
32    },
33    user => {
34        name => 'Statut RH',
35        attrs => [ qw(
36            sn givenName description
37            comment
38            company
39            department
40            manager
41            managerContact
42            contratType
43            expire
44            snNative
45            givenNameNative
46            wWWHomePage
47            halReference
48            create
49            date
50        ) ],
51    },
52    useraddress => {
53        name => 'Adresse',
54        attrs => [ qw(
55            initials
56            mail
57        ) ],
58    },
59    usermy => {
60        name => 'My',
61        attrs => [ qw(
62            snNative
63            givenNameNative
64            wWWHomePage
65            halReference
66        ) ],
67    },
68    site => {
69        name => 'Site',
70        attrs => [ qw(
71            description
72            siteNick
73            streetAddress
74            postOfficeBox
75            postalCode
76            l
77            st
78            co
79            facsimileTelephoneNumber
80            create
81            date
82        ) ],
83    },
84    address => {
85        name => 'Adresse',
86        attrs => [ qw(
87            isMainAddress
88            telephoneNumber
89            streetAddress
90            postOfficeBox
91            postalCode
92            l
93            st
94            physicalDeliveryOfficeName
95            site
96            co
97            unexported
98            description
99            facsimileTelephoneNumber
100        ) ],
101    },
102    group => {
103        name => 'SystÚme',
104        attrs => [ qw(
105            gidNumber
106            description label
107            comment
108            managedBy
109            managedAlsoBy
110            autoMemberFilter
111            sutype
112            create
113            date
114        ) ],
115    },
116    nethost => {
117        name => 'Ordinateur',
118        attrs => [ qw(
119            name
120            description
121            comment
122            related
123            serialNumber
124            encryptKey
125            owner
126            user
127            ip
128            macaddr
129            puppetClass
130            noInheritPuppet
131            noDynamic
132            cname
133            otherName
134            reverse
135            create
136            date
137            unexported
138            sshfp
139        ) ],
140    },
141    netzone => {
142        name => 'Zone réseau',
143        attrs => [ qw(
144            name
145            description
146            type
147            net
148            netExclude
149            puppetClass
150            outputD
151            templateD
152            site
153            allow_dyn
154            dynFrom
155            domain
156            ipCount
157            freeIPCount
158            create
159            date
160            dnsRevision
161            lastUpdate
162            unexported
163        ) ],
164    },
165    aliases => {
166        name => 'Alias mail',
167        attrs => [ qw(
168            forward
169            expire
170            description
171            comment
172            finalpoint
173            parents
174            create
175            date
176            unexported
177        )],
178    },
179    services => {
180        name => 'Services',
181        attrs => [ qw(
182            description
183            comment
184            manager
185            dependOn
186            create
187            date
188            unexported
189        )],
190    },
191};
192
193sub escape {
194    my ($self, $text) = @_;
195    $text ||= '';
196    for ($text) {
197        s/&/&/g;
198        s/</&lt;/g;
199        s/>/&gt;/g;
200        s/"/&quot;/g;
201    }
202    $text;
203}
204
205sub new {
206    my ($class) = @_;
207    bless({}, $class);
208}
209
210# call either OBJ or type + base
211
212sub ACCEPT_CONTEXT {
213    my ($self, $c, $form, $object, $base) = @_;
214    my $new = {};
215    $new->{c} = $c;
216    $new->{form} = $form;
217    $new->{object} = $object if (ref $object);
218    $new->{base} = $base || (ref $object ? $object->base : undef) or return $self;
219    $new->{otype} = ref $object ? $object->type : $object;
220
221
222    foreach (@{ $forms->{$form}->{attrs} }) {
223        $new->{_attr}{$_} = $c->model('AttrFormsA', $_, ($object
224                ? ($object)
225                : ($base, $new->{otype})))
226            or die "Cannot get attribute $_";
227    }
228
229    bless($new, __PACKAGE__);
230}
231
232sub base {
233    my ( $self ) = @_;
234    $self->{base}
235}
236
237sub otype {
238    my ($self) = @_;
239    $self->{otype};
240}
241
242sub label {
243    my ($self) = @_;
244    $forms->{$self->{form}}->{name} || ''
245}
246
247sub attributes {
248    my ($self, $for) = @_;
249    grep { $_ }
250    grep { $self->base->attribute($self->otype, $_) }
251    @{ $forms->{$self->{form}}->{attrs} };
252}
253
254sub _uri_part {
255    my ($self, $ref) = @_;
256    my $uri_part = {
257        user => 'users',
258        group => 'groups',
259        nethost => 'nethosts',
260        netzone => 'netzones',
261        site => 'sites',
262        aliasess => 'aliases',
263    }->{$ref};
264}
265
266sub attr_label {
267    my ($self, $attr, $label, $hint) = @_;
268
269    if ($label) {
270        return $label;
271    } elsif (my $hash = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
272        return $hash->{label};
273    } else {
274       return $attr;
275    } 
276}
277
278sub attr_hint {
279    my ($self, $attr) = @_;
280
281    $self->{_attr}{$attr}->attr_hint;
282}
283
284sub attr_field {
285    my ($self, $attr, $type) = @_;
286
287    $self->{_attr}{$attr}->attr_field($type);
288}
289
290
291sub write_attributes {
292    my ($self) = @_;
293    my @attrs;
294    foreach (values %{ $self->{_attr} }) {
295        $_->readonly and next;
296        push(@attrs, $_);
297    }
298    @attrs;
299}
300
301sub set_attrs {
302    my ($self, $attrs) = @_;
303    $self->{c}->req->param($self->label) || $attrs or return;
304    my %fields;
305    foreach ($attrs ? @{ $attrs } : $self->write_attributes) {
306        my $attr = ref $_ ? $_ : $self->{_attr}{$_};
307        if ($attr->{multiple}) {
308            $fields{$attr->name} = [ grep { $_ } $self->{c}->req->param($attr->htmlname) ];
309        } else {
310            $fields{$attr->name} = $self->{c}->req->param($attr->htmlname);
311        }
312    }
313    $self->{object}->set_c_fields(%fields) or do {
314        $self->{c}->stash->{page}{error} =
315            LATMOS::Accounts::Log::lastmessage(LA_ERR);
316        $self->{object}->base->rollback;
317        return;
318    };
319    $self->{object}->base->commit;
320}
321
322sub submit {
323    my ($self) = @_;
324    return sprintf(
325        '<input type="submit" name="%s" value="Enregistrer">',
326        $self->escape($self->label),
327    );
328}
329
330
331=head1 AUTHOR
332
333Thauvin Olivier
334
335=head1 LICENSE
336
337This library is free software, you can redistribute it and/or modify
338it under the same terms as Perl itself.
339
340=cut
341
3421;
Note: See TracBrowser for help on using the repository browser.