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

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

Exchange manager and managerContact attribute to propagate manager to other bases

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