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

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

Add autoFromSutype attribute: merge group member

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