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

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

Add/reorder attributes on forms

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