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

Last change on this file since 1329 was 1329, checked in by nanardon, 9 years ago

Merge branch 'jquery'

File size: 6.8 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
[125]25            uid uidNumber gidNumber gecos homeDirectory loginShell
[670]26            mail initials nickname
[894]27            expire
[677]28            locked
29            create
30            date
[112]31        ) ],
[117]32    },
[894]33    user => {
[1264]34        name => 'Statut RH',
[129]35        attrs => [ qw(
[894]36            sn givenName description
[1291]37            comment
[129]38            company
[733]39            department
[151]40            manager
[733]41            managerContact
[431]42            contratType
[160]43            expire
[894]44            snNative
45            givenNameNative
46            wWWHomePage
47            halReference
48            create
49            date
[129]50        ) ],
51    },
[894]52    useraddress => {
53        name => 'Adresse',
54        attrs => [ qw(
55            initials
56            mail
57        ) ],
58    },
[861]59    usermy => {
60        name => 'My',
61        attrs => [ qw(
62            snNative
63            givenNameNative
64            wWWHomePage
65            halReference
66        ) ],
67    },
[134]68    site => {
69        name => 'Site',
70        attrs => [ qw(
[148]71            description
[861]72            siteNick
[134]73            streetAddress
74            postOfficeBox
75            postalCode
[364]76            l
[160]77            st
[364]78            co
[448]79            facsimileTelephoneNumber
[740]80            create
81            date
[364]82        ) ],
83    },
84    address => {
85        name => 'Adresse',
86        attrs => [ qw(
87            isMainAddress
88            telephoneNumber
89            streetAddress
90            postOfficeBox
91            postalCode
[134]92            l
[364]93            st
94            physicalDeliveryOfficeName
95            site
96            co
[861]97            unexported
[714]98            description
[1329]99            facsimileTelephoneNumber
[134]100        ) ],
101    },
[125]102    group => {
103        name => 'SystÚme',
104        attrs => [ qw(
[1255]105            gidNumber
106            description label
[1282]107            comment
[183]108            managedBy
[1186]109            managedAlsoBy
[1137]110            autoMemberFilter
[861]111            sutype
[740]112            create
113            date
[125]114        ) ],
115    },
[861]116    nethost => {
117        name => 'Ordinateur',
118        attrs => [ qw(
119            name
120            description
[1282]121            comment
[1302]122            related
[861]123            serialNumber
[1237]124            encryptKey
[861]125            owner
[924]126            user
[861]127            ip
128            macaddr
[1064]129            puppetClass
[1087]130            noInheritPuppet
[861]131            noDynamic
132            cname
133            otherName
134            reverse
135            create
136            date
137            unexported
[1225]138            sshfp
[861]139        ) ],
140    },
141    netzone => {
142        name => 'Zone réseau',
143        attrs => [ qw(
144            name
145            description
146            type
147            net
148            netExclude
[1064]149            puppetClass
[861]150            outputD
151            templateD
152            site
153            allow_dyn
154            dynFrom
155            domain
156            ipCount
157            freeIPCount
158            create
159            date
160            dnsRevision
161            lastUpdate
162            unexported
163        ) ],
164    },
[929]165    aliases => {
166        name => 'Alias mail',
167        attrs => [ qw(
168            forward
[939]169            expire
[1281]170            description
[1282]171            comment
[929]172            finalpoint
173            parents
[1305]174            create
[929]175            date
176            unexported
177        )],
178    },
[1305]179    services => {
180        name => 'Services',
181        attrs => [ qw(
182            description
183            comment
184            manager
185            dependOn
186            create
187            date
188            unexported
189        )],
190    },
[111]191};
192
[116]193sub escape {
194    my ($self, $text) = @_;
[117]195    $text ||= '';
[116]196    for ($text) {
197        s/&/&/g;
198        s/</&lt;/g;
199        s/>/&gt;/g;
200        s/"/&quot;/g;
201    }
202    $text;
203}
[111]204
205sub new {
206    my ($class) = @_;
207    bless({}, $class);
208}
209
[144]210# call either OBJ or type + base
211
[111]212sub ACCEPT_CONTEXT {
[144]213    my ($self, $c, $form, $object, $base) = @_;
214    my $new = {};
215    $new->{c} = $c;
216    $new->{form} = $form;
217    $new->{object} = $object if (ref $object);
[1190]218    $new->{base} = $base || (ref $object ? $object->base : undef) or return $self;
[144]219    $new->{otype} = ref $object ? $object->type : $object;
[1329]220
221
222    foreach (@{ $forms->{$form}->{attrs} }) {
223        $new->{_attr}{$_} = $c->model('AttrFormsA', $_, ($object
224                ? ($object)
225                : ($base, $new->{otype})))
226            or die "Cannot get attribute $_";
227    }
228
[927]229    bless($new, __PACKAGE__);
[111]230}
231
[144]232sub base {
233    my ( $self ) = @_;
234    $self->{base}
235}
236
237sub otype {
238    my ($self) = @_;
239    $self->{otype};
240}
241
[117]242sub label {
243    my ($self) = @_;
244    $forms->{$self->{form}}->{name} || ''
245}
246
[111]247sub attributes {
[373]248    my ($self, $for) = @_;
[507]249    grep { $_ }
[861]250    grep { $self->base->attribute($self->otype, $_) }
[143]251    @{ $forms->{$self->{form}}->{attrs} };
[111]252}
253
[955]254sub _uri_part {
255    my ($self, $ref) = @_;
256    my $uri_part = {
257        user => 'users',
258        group => 'groups',
259        nethost => 'nethosts',
260        netzone => 'netzones',
[1080]261        site => 'sites',
262        aliasess => 'aliases',
[955]263    }->{$ref};
264}
265
[1329]266sub attr_label {
267    my ($self, $attr, $label, $hint) = @_;
[955]268
[1329]269    if ($label) {
270        return $label;
271    } elsif (my $hash = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
272        return $hash->{label};
273    } else {
274       retrun $attr;
275    } 
276}
[955]277
[1329]278sub attr_hint {
279    my ($self, $attr) = @_;
[861]280
[1329]281    $self->{_attr}{$attr}->attr_hint;
[953]282}
[951]283
[953]284sub attr_field {
285    my ($self, $attr, $type) = @_;
286
[1329]287    $self->{_attr}{$attr}->attr_field($type);
[111]288}
289
[174]290
[861]291sub write_attributes {
292    my ($self) = @_;
293    my @attrs;
[1329]294    foreach (values %{ $self->{_attr} }) {
295        $_->readonly and next;
[861]296        push(@attrs, $_);
297    }
298    @attrs;
299}
300
[112]301sub set_attrs {
[927]302    my ($self, $attrs) = @_;
303    $self->{c}->req->param($self->label) || $attrs or return;
[738]304    my %fields;
[927]305    foreach ($attrs ? @{ $attrs } : $self->write_attributes) {
[1329]306        my $attr = ref $_ ? $_ : $self->{_attr}{$_};
[899]307        if ($attr->{multiple}) {
[1329]308            $fields{$attr->name} = [ grep { $_ } $self->{c}->req->param($attr->htmlname) ];
[738]309        } else {
[1329]310            $fields{$attr->name} = $self->{c}->req->param($attr->htmlname);
[738]311        }
312    }
[861]313    $self->{object}->set_c_fields(%fields) or do {
314        $self->{c}->stash->{page}{error} =
315            LATMOS::Accounts::Log::lastmessage(LA_ERR);
316        $self->{object}->base->rollback;
317        return;
318    };
[112]319    $self->{object}->base->commit;
320}
321
[1329]322sub submit {
323    my ($self) = @_;
324    return sprintf(
325        '<input type="submit" name="%s" value="Enregistrer">',
326        $self->escape($self->label),
327    );
328}
329
330
[111]331=head1 AUTHOR
332
333Thauvin Olivier
334
335=head1 LICENSE
336
337This library is free software, you can redistribute it and/or modify
338it under the same terms as Perl itself.
339
340=cut
341
3421;
Note: See TracBrowser for help on using the repository browser.