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

Last change on this file since 2097 was 2097, checked in by nanardon, 7 years ago

Add hostType and endOfWarranty attribute

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