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

Last change on this file since 939 was 939, checked in by nanardon, 12 years ago
  • add aliases.expires attributes support
File size: 15.4 KB
Line 
1package LATMOS::Accounts::Web::Model::AttrForms;
2
3use strict;
4use warnings;
5use base 'Catalyst::Model';
6use LATMOS::Accounts::Log;
7
8=head1 NAME
9
10LATMOS::Accounts::Web::Model::AttrForms - Catalyst Model
11
12=head1 DESCRIPTION
13
14Catalyst Model.
15
16=cut
17
18my $forms = {
19    usersys => {
20        name => 'SystÚme',
21        attrs => [ qw(
22            sn givenName description
23            uid uidNumber gidNumber gecos homeDirectory loginShell
24            mail initials nickname
25            expire
26            locked
27            create
28            date
29        ) ],
30    },
31    user => {
32        name => 'Statut',
33        attrs => [ qw(
34            sn givenName description
35            company
36            department
37            manager
38            managerContact
39            contratType
40            expire
41            snNative
42            givenNameNative
43            wWWHomePage
44            halReference
45            create
46            date
47        ) ],
48    },
49    useraddress => {
50        name => 'Adresse',
51        attrs => [ qw(
52            initials
53            mail
54        ) ],
55    },
56    usermy => {
57        name => 'My',
58        attrs => [ qw(
59            snNative
60            givenNameNative
61            wWWHomePage
62            halReference
63        ) ],
64    },
65    site => {
66        name => 'Site',
67        attrs => [ qw(
68            description
69            siteNick
70            streetAddress
71            postOfficeBox
72            postalCode
73            l
74            st
75            co
76            facsimileTelephoneNumber
77            create
78            date
79        ) ],
80    },
81    address => {
82        name => 'Adresse',
83        attrs => [ qw(
84            isMainAddress
85            telephoneNumber
86            streetAddress
87            postOfficeBox
88            postalCode
89            l
90            st
91            physicalDeliveryOfficeName
92            site
93            co
94            unexported
95            description
96        ) ],
97    },
98    group => {
99        name => 'SystÚme',
100        attrs => [ qw(
101            gidNumber description
102            managedBy
103            sutype
104            create
105            date
106        ) ],
107    },
108    nethost => {
109        name => 'Ordinateur',
110        attrs => [ qw(
111            name
112            description
113            serialNumber
114            owner
115            user
116            ip
117            macaddr
118            noDynamic
119            cname
120            otherName
121            reverse
122            create
123            date
124            unexported
125        ) ],
126    },
127    netzone => {
128        name => 'Zone réseau',
129        attrs => [ qw(
130            name
131            description
132            type
133            net
134            netExclude
135            outputD
136            templateD
137            site
138            allow_dyn
139            dynFrom
140            domain
141            ipCount
142            freeIPCount
143            create
144            date
145            dnsRevision
146            lastUpdate
147            unexported
148        ) ],
149    },
150    aliases => {
151        name => 'Alias mail',
152        attrs => [ qw(
153            forward
154            expire
155            finalpoint
156            parents
157            created
158            date
159            unexported
160        )],
161    },
162};
163
164sub escape {
165    my ($self, $text) = @_;
166    $text ||= '';
167    for ($text) {
168        s/&/&/g;
169        s/</&lt;/g;
170        s/>/&gt;/g;
171        s/"/&quot;/g;
172    }
173    $text;
174}
175
176sub new {
177    my ($class) = @_;
178    bless({}, $class);
179}
180
181# call either OBJ or type + base
182
183sub ACCEPT_CONTEXT {
184    my ($self, $c, $form, $object, $base) = @_;
185    my $new = {};
186    $new->{c} = $c;
187    $new->{form} = $form;
188    $new->{object} = $object if (ref $object);
189    $new->{base} = $base || ($object ? $object->base : undef) or return $self;
190    $new->{otype} = ref $object ? $object->type : $object;
191    bless($new, __PACKAGE__);
192}
193
194sub base {
195    my ( $self ) = @_;
196    $self->{base}
197}
198
199sub otype {
200    my ($self) = @_;
201    $self->{otype};
202}
203
204sub label {
205    my ($self) = @_;
206    $forms->{$self->{form}}->{name} || ''
207}
208
209sub attributes {
210    my ($self, $for) = @_;
211    grep { $_ }
212    grep { $self->base->attribute($self->otype, $_) }
213    @{ $forms->{$self->{form}}->{attrs} };
214}
215
216sub attr_label {
217    my ($self, $attr) = @_;
218    my ($hint, $label) = ('', '');
219    LATMOS::Accounts::Web->config->{attrs} ||= {};
220    my $htmlname = ($self->{object} ? $self->{object}->id . '_' : '') . $attr;
221    if (my $attr_config = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
222        $label = $attr_config->{label} || '';
223        #utf8::is_utf8($label) && utf8::encode($label);
224        $hint = $attr_config->{hint} || '';
225        #utf8::is_utf8($hint) && utf8::encode($hint);
226    }
227    return sprintf('<label %sfor="%s">%s</label>',
228        ($hint ? sprintf('title="%s"', $self->escape($hint)) : ''),
229        $self->escape($htmlname),
230        $self->escape($label || $attr),
231    );
232}
233
234sub attr_hint {
235    my ($self, $attr) = @_;
236    LATMOS::Accounts::Web->config->{attrs} ||= {};
237    if (my $attr_config = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
238        return $attr_config->{hint} || '';
239    }
240    return;
241}
242
243sub attr_raw_value {
244    my ($self, $attr) = @_;
245    return $self->{c}->req->param($attr) ||
246        ($self->{object} ? $self->{object}->get_attributes($attr) : '')
247}
248
249sub attr_field_name {
250    my ($self, $attr) = @_;
251    return ($self->{object}
252            ? $self->{object}->id . '_'
253            : ''
254        ) . $attr
255}
256
257sub attr_field {
258    my ($self, $attr, $type) = @_;
259
260    my $attribute = ($self->{object}
261        ? $self->{object}->attribute($attr)
262        : $self->base->attribute($self->otype, $attr)) or return;
263
264    my $htmlname = $self->escape($self->attr_field_name($attr));
265
266    my @html_fields;
267    foreach my $attr_raw_value (
268        $attribute->{multiple}
269            ? ((grep { $_ } $self->attr_raw_value($attr)),
270                $attribute->readonly
271                    ? ()
272                    : ('')
273            )
274            : ($self->attr_raw_value($attr))) {
275
276        my $html_id = $htmlname .
277            (scalar(@html_fields) ? scalar(@html_fields) : '');
278
279        my $html_field = '';
280        for ($attribute->form_type) {
281            /^textarea$/i and do {
282                $html_field = sprintf(
283                    '<textarea id="%s" name="%s" cols="40">%s</textarea>',
284                    $self->escape($html_id),
285                    $self->escape($htmlname),
286                    $self->escape($attr_raw_value || ''),
287                );
288                last;
289            };
290            /^label$/i and do {
291                $attr_raw_value or last;
292                $html_field = $self->escape($attr_raw_value);
293                $html_field =~ s/\n/<br>/g;
294                $html_field .= sprintf('<input type="hidden" name="%s" value="%s">',
295                    $self->escape($htmlname), ($attr_raw_value || ''));
296                last;
297            };
298            /^date$/i and do {
299                my ($date, $time) = split(/ /, $self->attr_raw_value($attr) || '');
300                if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) {
301                    $date = "$3/$2/$1";
302                }
303                my $html = "\n" . q{<SCRIPT LANGUAGE="JavaScript" ID="js13">
304                var cal13 = new CalendarPopup();
305                </SCRIPT>} . "\n";
306                $html .= sprintf(
307                    '<input type="text" id="%s" name="%s" value="%s" size="12">',
308                    $self->escape($html_id),
309                    $self->escape($htmlname),
310                    $self->escape($date)
311                );
312                $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>};
313                $html .= qq|
314                <A HREF="#"
315                    onClick="cal13.select(document.getElementById('$html_id'),'${html_id}_anc','dd/MM/yyyy');return false;"
316                    TITLE="Date"
317                    NAME="${html_id}_anc" ID="${html_id}_anc">
318                    <img class="attr" src="| . $self->{c}->uri_for(qw(/static icons view-calendar-day.png))
319                    . qq{" style="ref"></A>
320                    } . "\n";
321                $html_field = $html;
322                last;
323            };
324            /^checkbox(?::(\w+))?$/i and do {
325                my $options = $1 || '';
326                $html_field = sprintf('<input type="checkbox" name="%s"%s>',
327                    $self->escape($htmlname),
328                    $attr_raw_value ? '  checked="yes"' : ''
329                ); 
330                $html_field .= sprintf('<input type="hidden" name="%s">',
331                    $self->escape($htmlname));
332                if ($attribute->form_option('rawvalue')) {
333                    $html_field .= $attr_raw_value
334                        ? ' ' . $attr_raw_value
335                        : ''; 
336                }
337                last;
338            };
339            /^LIST/ and do {
340                my $options = $1 || '';
341                my $select = sprintf('<select id="%s" name="%s">',
342                    $self->escape($html_id),
343                    $self->escape($htmlname)) . "\n";
344                $select .= '<option value="">--</option>' . "\n"
345                    unless($attribute->mandatory);
346                my $value = $attr_raw_value || '';
347                my $initial_observed = '';
348                my @valslist;
349                foreach my $val (sort $attribute->can_values) {
350                    push(@valslist, {
351                        val => $val,
352                        disp => $attribute->display($val || ''),
353                    });
354                }
355                foreach (sort { $a->{disp} cmp $b->{disp} } @valslist) {
356                    $select .= sprintf(
357                        '    <option value="%s"%s>%s</option>',
358                        $self->escape($_->{val} || ''),
359                        $value eq $_->{val} ? ' selected="selected"' : '',
360                        $self->escape($_->{disp} || ''),
361                    );
362                    $select .= "\n";
363                }
364                $select .= "</select>\n";
365
366                $html_field = $select;
367                last;
368            };
369            /^text(-\w+)?(?::(\d+))?/i and do {
370                my $flag = $1 || '';
371                $html_field = sprintf(
372                    '<input type="text" id="%s" name="%s" value="%s" size="%d">',
373                    $self->escape($html_id),
374                    $self->escape($htmlname),
375                    $self->escape($attr_raw_value),
376                    $attribute->form_option('length') || 30,
377                );
378                if ($flag =~ /A/) {
379                    $html_field .= qq|<span id="${html_id}_auto_complete"></span>|;
380                    $html_field .= "\n";
381                    $html_field .= $self->{c}->prototype->auto_complete_field(
382                        $html_id,
383                        {
384                            update => "${html_id}_auto_complete",
385                            url => $self->{c}->uri_for('/ajax', 'attrvalues', $self->otype, $attr),
386                            indicator => "${html_id}_stat", min_chars => 1,
387                            with => "'val='+document.getElementById('$html_id').value",
388                            frequency => 2,
389                        }
390                    );
391                }
392                if ($attribute->uniq) {
393                    $html_field .= qq|<span class="inputvalidate" id="${html_id}_observer_uniq"></span>|;
394                    $html_field .= "\n";
395                    $html_field .= $self->{c}->prototype->observe_field(
396                        $html_id,
397                        {
398                            update => "${html_id}_observer_uniq",
399                            url => $self->{c}->uri_for('/ajax', 'objattrexist',
400                                $self->otype, $attr),
401                            frequency => 2,
402                            indicator => "${html_id}_stat", min_chars => 1,
403                            with => "'val='+document.getElementById('$html_id').value" .
404                            ($self->{object} ? "+'&exclude=" . $self->{object}->id . "'" :
405                                ''),
406                        }
407                    );
408                    $html_field .= qq|<span style="display:none" id="${html_id}_stat">Searching...</span>|;
409                }
410                last;
411            };
412        }
413        if (my $ref = $attribute->reference) {
414            my $uri_part= {
415                user => 'users',
416                group => 'groups',
417                nethost => 'nethosts',
418                netzone => 'netzones',
419                site => 'sites'
420            }->{$ref};
421            my $text;
422            if ($self->base->attribute($ref, 'displayName')) {
423                if ($attr_raw_value &&
424                    (my $obj = $self->base->get_object($ref, $attr_raw_value)))
425                {
426                    $text = $obj->get_attributes('displayName');
427                }
428               
429                $html_field .= $self->{c}->prototype->observe_field( $html_id, {
430                        update => "${html_id}_span",
431                        url => $self->{c}->uri_for('/ajax', 'rawattr', $ref),
432                        frequency => 1,
433                        with   => "'attr=displayName" .
434                        "&id=' + element.options[element.selectedIndex].text",
435                    }) . "\n" if ($attribute->form_type =~ /list/i);
436            } elsif($attr_raw_value && $uri_part) {
437                $text = sprintf(
438                    '<img class="attr" src="%s" title="%s">',
439                    $self->{c}->uri_for('/static', 'icons', 'arrow-right.png'),
440                    $attr_raw_value,
441                )
442            }
443            $html_field .= sprintf(qq{<span id="%s" style="margin-left: 1em">},
444                "${html_id}_span");
445
446            if (defined($text)) {
447                $html_field .= $uri_part
448                    ? sprintf('<a href="%s">%s</a>',
449                        $self->{c}->uri_for("/$uri_part", $attribute->display($attr_raw_value)),
450                        $text,)
451                    : $text;
452            }
453
454            $html_field .= "</span>\n";
455        }
456        push(@html_fields, $html_field);
457    }
458    return join("<br>\n", @html_fields);
459}
460
461sub submit {
462    my ($self) = @_;
463    return sprintf(
464        '<input type="submit" name="%s" value="Enregistrer">',
465        $self->escape($self->label),
466    );
467}
468
469sub write_attributes {
470    my ($self) = @_;
471    my @attrs;
472    foreach ($self->attributes) {
473        my $attr = ($self->{object}
474            ? $self->{object}->attribute($_)
475            : $self->base->attribute($self->otype, $_)) or next;
476        $attr->readonly and next;
477        push(@attrs, $_);
478    }
479    @attrs;
480}
481
482sub set_attrs {
483    my ($self, $attrs) = @_;
484    $self->{c}->req->param($self->label) || $attrs or return;
485    my $prefix = $self->{object}->id . '_';
486    my %fields;
487    foreach ($attrs ? @{ $attrs } : $self->write_attributes) {
488        my $attr = ($self->{object}
489            ? $self->{object}->attribute($_)
490            : $self->base->attribute($self->otype, $_)) or next;
491        if ($attr->{multiple}) {
492            $fields{$_} = [ grep { $_ } $self->{c}->req->param("$prefix$_") ];
493        } else {
494            $fields{$_} = $self->{c}->req->param("$prefix$_");
495        }
496    }
497    $self->{object}->set_c_fields(%fields) or do {
498        $self->{c}->stash->{page}{error} =
499            LATMOS::Accounts::Log::lastmessage(LA_ERR);
500        $self->{object}->base->rollback;
501        return;
502    };
503    $self->{object}->base->commit;
504}
505
506=head1 AUTHOR
507
508Thauvin Olivier
509
510=head1 LICENSE
511
512This library is free software, you can redistribute it and/or modify
513it under the same terms as Perl itself.
514
515=cut
516
5171;
Note: See TracBrowser for help on using the repository browser.