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

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

Display createdby and modifiedby attributes

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