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

Last change on this file since 953 was 953, checked in by nanardon, 12 years ago
  • split code to increase readability
File size: 19.2 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    my $id = $self->{object}
252        ? $self->{object}->id . '_'
253        : '';
254    $id =~ s/\./_/g;
255    $id . $attr;
256}
257
258sub attr_item {
259    my ($self, $attribute, $value, $htmlid) = @_;
260
261    my $htmlname = $self->escape($self->attr_field_name($attribute->name));
262    $htmlid ||= $htmlname;
263
264    my $html_field = '';
265    my $html_ref = '';
266
267    warn $attribute->form_type;
268    for ($attribute->form_type) {
269            /^textarea$/i and do {
270                return sprintf(
271                    '<textarea id="%s" name="%s" cols="40">%s</textarea>',
272                    $self->escape($htmlid),
273                    $self->escape($htmlname),
274                    $self->escape($value || ''),
275                );
276                last;
277            };
278
279            /^label$/i and do {
280                $value or last;
281                my $html_field = $self->escape($value);
282                $html_field =~ s/\n/<br>/g;
283                $html_field .= sprintf('<input type="hidden" name="%s" value="%s">',
284                    $self->escape($htmlname), ($value || ''));
285                return $html_field;
286                last;
287            };
288
289            /^date$/i and do {
290                my ($date, $time) = split(/ /, $value || '');
291                if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) {
292                    $date = "$3/$2/$1";
293                }
294                my $html = "\n" . q{<SCRIPT LANGUAGE="JavaScript" ID="js13">
295                var cal13 = new CalendarPopup();
296                </SCRIPT>} . "\n";
297                $html .= sprintf(
298                    '<input type="text" id="%s" name="%s" value="%s" size="12">',
299                    $self->escape($htmlid),
300                    $self->escape($htmlname),
301                    $self->escape($date)
302                );
303                $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>};
304                $html .= qq|
305                <A HREF="#"
306                    onClick="cal13.select(document.getElementById('$htmlid'),'${htmlid}_anc','dd/MM/yyyy');return false;"
307                    TITLE="Date"
308                    NAME="${htmlid}_anc" ID="${htmlid}_anc">
309                    <img class="attr" src="| . $self->{c}->uri_for(qw(/static icons view-calendar-day.png))
310                    . qq{" style="ref"></A>
311                    } . "\n";
312                return $html;
313                last;
314            };
315
316            /^checkbox/i and do {
317                my $options = $1 || '';
318                my $html = sprintf('<input type="checkbox" id="%s" name="%s"%s>',
319                    $htmlid,
320                    $self->escape($htmlname),
321                    $value ? '  checked="yes"' : ''
322                ); 
323                $html .= sprintf('<input type="hidden" name="%s">',
324                    $self->escape($htmlname));
325                if ($attribute->form_option('rawvalue')) {
326                    $html .= $value
327                        ? ' ' . $value
328                        : ''; 
329                }
330                return $html;
331                last;
332            };
333
334            /^LIST/i and do {
335                my $options = $1 || '';
336                my $select = sprintf('<select id="%s" name="%s">',
337                    $self->escape($htmlid),
338                    $self->escape($htmlname)) . "\n";
339                $select .= '<option value="">--</option>' . "\n"
340                    unless($attribute->mandatory);
341                $value ||= '';
342                my $initial_observed = '';
343                my @valslist;
344                foreach my $val (sort $attribute->can_values) {
345                    push(@valslist, {
346                        val => $val,
347                        disp => $attribute->display($val || ''),
348                    });
349                }
350                foreach (sort { $a->{disp} cmp $b->{disp} } @valslist) {
351                    $select .= sprintf(
352                        '    <option value="%s"%s>%s</option>',
353                        $self->escape($_->{val} || ''),
354                        $value eq $_->{val} ? ' selected="selected"' : '',
355                        $self->escape($_->{disp} || ''),
356                    );
357                    $select .= "\n";
358                }
359                $select .= "</select>\n";
360
361                return $select;
362                last;
363            };
364
365            /^text/i and do {
366                return sprintf(
367                    '<input type="text" id="%s" name="%s" value="%s" size="%d">',
368                    $self->escape($htmlid),
369                    $self->escape($htmlname),
370                    $self->escape($value),
371                    $attribute->form_option('length') || 30,
372                );
373#                if ($flag =~ /A/) {
374#                    $html_field .= qq|<span id="${html_id}_auto_complete"></span>|;
375#                    $html_field .= "\n";
376#                    $html_field .= $self->{c}->prototype->auto_complete_field(
377#                        $html_id,
378#                        {
379#                            update => "${html_id}_auto_complete",
380#                            url => $self->{c}->uri_for('/ajax', 'attrvalues', $self->otype, $attr),
381#                            indicator => "${html_id}_stat", min_chars => 1,
382#                            with => "'val='+document.getElementById('$html_id').value",
383#                            frequency => 2,
384#                        }
385#                    );
386#                }
387#                if ($attribute->uniq) {
388#                    $html_ref .= qq|<span class="inputvalidate" id="${html_id}_observer_uniq"></span>|;
389#                    $html_ref .= "\n";
390#                    $html_ref .= $self->{c}->prototype->observe_field(
391#                        $html_id,
392#                        {
393#                            update => "${html_id}_observer_uniq",
394#                            url => $self->{c}->uri_for('/ajax', 'objattrexist',
395#                                $self->otype, $attr),
396#                            frequency => 2,
397#                            indicator => "${html_id}_stat", min_chars => 1,
398#                            with => "'val='+document.getElementById('$html_id').value" .
399#                            ($self->{object} ? "+'&exclude=" . $self->{object}->id . "'" :
400#                                ''),
401#                        }
402#                    );
403#                    $html_ref .= qq|<span style="display:none" id="${html_id}_stat">Searching...</span>|;
404#                }
405                last;
406            };
407#        }
408#        if (my $ref = $attribute->reference) {
409#            my $uri_part= {
410#                user => 'users',
411#                group => 'groups',
412#                nethost => 'nethosts',
413#                netzone => 'netzones',
414#                site => 'sites'
415#            }->{$ref};
416#            my $text;
417#            if ($self->base->attribute($ref, 'displayName')) {
418#                if ($attr_raw_value &&
419#                    (my $obj = $self->base->get_object($ref, $attr_raw_value)))
420#                {
421#                    $text = $obj->get_attributes('displayName');
422#                }
423#               
424#                $html_ref .= $self->{c}->prototype->observe_field( $html_id, {
425#                        update => "${html_id}_span",
426#                        url => $self->{c}->uri_for('/ajax', 'rawattr', $ref),
427#                        frequency => 1,
428#                        with   => "'attr=displayName" .
429#                        "&id=' + element.options[element.selectedIndex].text",
430#                    }) . "\n" if ($attribute->form_type =~ /list/i);
431#            } elsif($attr_raw_value && $uri_part) {
432#                $text = sprintf(
433#                    '<img class="attr" src="%s" title="%s">',
434#                    $self->{c}->uri_for('/static', 'icons', 'arrow-right.png'),
435#                    $attr_raw_value,
436#                )
437#            }
438#            $html_ref .= sprintf(qq{<span id="%s" style="margin-left: 1em">},
439#                "${html_id}_span");
440#
441#            if (defined($text)) {
442#                $html_ref .= $uri_part
443#                    ? sprintf('<a href="%s">%s</a>',
444#                        $self->{c}->uri_for("/$uri_part", $attribute->display($attr_raw_value)),
445#                        $text,)
446#                    : $text;
447#            }
448#
449#            $html_ref .= "</span>\n";
450        }
451#        if ($attribute->{multiple} && (!$attr_raw_value)) {
452#            my $esc_html_field = $html_field;
453#            my $esc_html_ref = $html_ref;
454#            $esc_html_ref =~ s/'/\\'/g;
455#            #$esc_html_field =~ s/"/\\"/g;
456#            $addempty = sprintf(
457#                '<div id="%s"></div>',
458#                "${html_id}_mul_emp0",
459#            );
460#            $addempty .= sprintf(qq#
461#            <script type="text/javascript">
462#                var cn_${html_id} = 0;
463#                function add_${html_id} () {
464#                    cn_${html_id}++;
465#                    var cn = cn_${html_id};
466#                    var htmlid = '${html_id}'.replace(/NB/g, cn);
467#                    document.getElementById('${html_id}_mul_emp' + (cn - 1)).innerHTML +=
468#                        '%s<span class="inputvalidate" id="${html_id}_observer_uniq"></span><br>'.replace(/NB/g, cn);
469#                    document.getElementById('${html_id}_mul_emp' + (cn - 1)).innerHTML +=
470#                        '<div id="${html_id}_mul_emp' + cn + '"></div>';
471#
472#                    new Form.Element.Observer( '${html_id}'.replace('NB', cn),
473#                           2,
474#                           function( element, value ) {
475#                                new Ajax.Updater( '${html_id}_observer_uniq'.replace(/NB/g, cn),
476#                                    'http://localhost:8080/ajax/objattrexist/nethost/cname',
477#                                    { parameters: 'val='+document.getElementById(htmlid).value+'&exclude=$attr_raw_value',asynchronous: 1 } )
478#                            } );
479#                }
480#            </script>
481#            <button type="button" onclick="add_${html_id}()">+</button>
482#            #,
483#            $esc_html_field
484#            );
485#        }
486    return $html_field . '<br>'; 
487}
488
489sub attr_check {
490    my ($self, $attribute, $value, $htmlid) = @_;
491    if (my $ref = $attribute->reference) {
492        my $uri_part = {
493            user => 'users',
494            group => 'groups',
495            nethost => 'nethosts',
496            netzone => 'netzones',
497            site => 'sites'
498        }->{$ref};
499        my $text;
500        if ($self->base->attribute($ref, 'displayName')) {
501            if ($value &&
502                (my $obj = $self->base->get_object($ref, $attr_raw_value)))
503            {
504                $text = $obj->get_attributes('displayName');
505            }
506
507            $html_ref .= $self->{c}->prototype->observe_field( $html_id, {
508                    update => "${html_id}_span",
509                    url => $self->{c}->uri_for('/ajax', 'rawattr', $ref),
510                    frequency => 1,
511                    with   => "'attr=displayName" .
512                    "&id=' + element.options[element.selectedIndex].text",
513                }) . "\n" if ($attribute->form_type =~ /list/i);
514        } elsif($value && $uri_part) {
515            $text = sprintf(
516                '<img class="attr" src="%s" title="%s">',
517                $self->{c}->uri_for('/static', 'icons', 'arrow-right.png'),
518                $value,
519            )
520        }
521        $html_ref .= sprintf(qq{<span id="%s" style="margin-left: 1em">},
522            "${html_id}_span");
523    }
524}
525
526sub attr_field {
527    my ($self, $attr, $type) = @_;
528
529    my $attr_info = $type
530        ? { formtype => $type }
531        : undef;
532
533    my $attribute = ($self->{object}
534        ? $self->{object}->attribute($attr)
535        : $self->base->attribute($self->otype, $attr));
536
537    $attribute ||= $self->base->attribute(
538        $self->otype,
539        { 
540            name => $attr,
541            formtype => $type
542        },
543    );
544
545    my $htmlname = $self->escape($self->attr_field_name($attr));
546
547    my @html_fields;
548    foreach my $attr_raw_value (
549        $attribute->{multiple}
550            ? ((grep { $_ } $self->attr_raw_value($attr)),
551                $attribute->readonly
552                    ? ()
553                    : ('')
554            )
555            : ($self->attr_raw_value($attr))) {
556
557        my $html_id = $htmlname .
558            (scalar(@html_fields) ? scalar(@html_fields) : '');
559        $html_id .= 'NB' if ($attribute->multiple);
560        push(@html_fields,
561            $self->attr_item(
562                $attribute, $attr_raw_value, $html_id
563            )
564        );
565        push(@html_fields,
566            $self->attr_check(
567                $attribute, $attr_raw_value, $html_id
568            )
569        );
570
571    }
572    return join("<br>\n", @html_fields);
573}
574
575sub submit {
576    my ($self) = @_;
577    return sprintf(
578        '<input type="submit" name="%s" value="Enregistrer">',
579        $self->escape($self->label),
580    );
581}
582
583sub write_attributes {
584    my ($self) = @_;
585    my @attrs;
586    foreach ($self->attributes) {
587        my $attr = ($self->{object}
588            ? $self->{object}->attribute($_)
589            : $self->base->attribute($self->otype, $_)) or next;
590        $attr->readonly and next;
591        push(@attrs, $_);
592    }
593    @attrs;
594}
595
596sub set_attrs {
597    my ($self, $attrs) = @_;
598    $self->{c}->req->param($self->label) || $attrs or return;
599    my $prefix = $self->attr_field_name('');
600    my %fields;
601    foreach ($attrs ? @{ $attrs } : $self->write_attributes) {
602        my $attr = ($self->{object}
603            ? $self->{object}->attribute($_)
604            : $self->base->attribute($self->otype, $_)) or next;
605        if ($attr->{multiple}) {
606            $fields{$_} = [ grep { $_ } $self->{c}->req->param("$prefix$_") ];
607        } else {
608            $fields{$_} = $self->{c}->req->param("$prefix$_");
609        }
610    }
611    $self->{object}->set_c_fields(%fields) or do {
612        $self->{c}->stash->{page}{error} =
613            LATMOS::Accounts::Log::lastmessage(LA_ERR);
614        $self->{object}->base->rollback;
615        return;
616    };
617    $self->{object}->base->commit;
618}
619
620=head1 AUTHOR
621
622Thauvin Olivier
623
624=head1 LICENSE
625
626This library is free software, you can redistribute it and/or modify
627it under the same terms as Perl itself.
628
629=cut
630
6311;
Note: See TracBrowser for help on using the repository browser.