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

Last change on this file since 1589 was 1589, checked in by nanardon, 8 years ago

Add hosted attribute

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