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

Last change on this file since 2148 was 2148, checked in by nanardon, 6 years ago

Show new attribute into web interface

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