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

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

Add some attribute to service, fixe services definition for other objects

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