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

Last change on this file since 1162 was 1162, checked in by nanardon, 12 years ago

fix conflict

File size: 17.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            uid uidNumber gidNumber gecos homeDirectory loginShell
25            mail initials nickname
26            expire
27            locked
28            create
29            date
30        ) ],
31    },
32    user => {
33        name => 'Statut',
34        attrs => [ qw(
35            sn givenName description
36            company
37            department
38            manager
39            managerContact
40            contratType
41            expire
42            snNative
43            givenNameNative
44            wWWHomePage
45            halReference
46            create
47            date
48        ) ],
49    },
50    useraddress => {
51        name => 'Adresse',
52        attrs => [ qw(
53            initials
54            mail
55        ) ],
56    },
57    usermy => {
58        name => 'My',
59        attrs => [ qw(
60            snNative
61            givenNameNative
62            wWWHomePage
63            halReference
64        ) ],
65    },
66    site => {
67        name => 'Site',
68        attrs => [ qw(
69            description
70            siteNick
71            streetAddress
72            postOfficeBox
73            postalCode
74            l
75            st
76            co
77            facsimileTelephoneNumber
78            create
79            date
80        ) ],
81    },
82    address => {
83        name => 'Adresse',
84        attrs => [ qw(
85            isMainAddress
86            telephoneNumber
87            streetAddress
88            postOfficeBox
89            postalCode
90            l
91            st
92            physicalDeliveryOfficeName
93            site
94            co
95            unexported
96            description
97        ) ],
98    },
99    group => {
100        name => 'SystÚme',
101        attrs => [ qw(
102            gidNumber description
103            managedBy
104            autoMemberFilter
105            sutype
106            create
107            date
108        ) ],
109    },
110    nethost => {
111        name => 'Ordinateur',
112        attrs => [ qw(
113            name
114            description
115            serialNumber
116            owner
117            user
118            ip
119            macaddr
120            puppetClass
121            noInheritPuppet
122            noDynamic
123            cname
124            otherName
125            reverse
126            create
127            date
128            unexported
129        ) ],
130    },
131    netzone => {
132        name => 'Zone réseau',
133        attrs => [ qw(
134            name
135            description
136            type
137            net
138            netExclude
139            puppetClass
140            outputD
141            templateD
142            site
143            allow_dyn
144            dynFrom
145            domain
146            ipCount
147            freeIPCount
148            create
149            date
150            dnsRevision
151            lastUpdate
152            unexported
153        ) ],
154    },
155    aliases => {
156        name => 'Alias mail',
157        attrs => [ qw(
158            forward
159            expire
160            finalpoint
161            parents
162            created
163            date
164            unexported
165        )],
166    },
167};
168
169sub escape {
170    my ($self, $text) = @_;
171    $text ||= '';
172    for ($text) {
173        s/&/&/g;
174        s/</&lt;/g;
175        s/>/&gt;/g;
176        s/"/&quot;/g;
177    }
178    $text;
179}
180
181sub new {
182    my ($class) = @_;
183    bless({}, $class);
184}
185
186# call either OBJ or type + base
187
188sub ACCEPT_CONTEXT {
189    my ($self, $c, $form, $object, $base) = @_;
190    my $new = {};
191    $new->{c} = $c;
192    $new->{form} = $form;
193    $new->{object} = $object if (ref $object);
194    $new->{base} = $base || ($object ? $object->base : undef) or return $self;
195    $new->{otype} = ref $object ? $object->type : $object;
196    bless($new, __PACKAGE__);
197}
198
199sub base {
200    my ( $self ) = @_;
201    $self->{base}
202}
203
204sub otype {
205    my ($self) = @_;
206    $self->{otype};
207}
208
209sub label {
210    my ($self) = @_;
211    $forms->{$self->{form}}->{name} || ''
212}
213
214sub attributes {
215    my ($self, $for) = @_;
216    grep { $_ }
217    grep { $self->base->attribute($self->otype, $_) }
218    @{ $forms->{$self->{form}}->{attrs} };
219}
220
221sub attr_label {
222    my ($self, $attr, $label, $hint) = @_;
223    $label ||= '';
224    $hint ||= '';
225    LATMOS::Accounts::Web->config->{attrs} ||= {};
226    my $attribute = $self->base->attribute($self->otype, $attr);
227    my $htmlname = $self->escape($self->attr_field_name($attr));
228    if (my $attr_config = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
229        $label ||= $attr_config->{label} || '';
230        #utf8::is_utf8($label) && utf8::encode($label);
231        $hint ||= $attr_config->{hint} || '';
232        #utf8::is_utf8($hint) && utf8::encode($hint);
233    }
234    return sprintf('<label %sfor="%s">%s</label>',
235        ($hint ? sprintf('title="%s"', $self->escape($hint)) : ''),
236        $self->escape($htmlname),
237        $self->escape($label || $attr),
238    );
239}
240
241sub attr_hint {
242    my ($self, $attr) = @_;
243    LATMOS::Accounts::Web->config->{attrs} ||= {};
244    if (my $attr_config = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
245        return $attr_config->{hint} || '';
246    }
247    return;
248}
249
250sub attr_raw_value {
251    my ($self, $attribute) = @_;
252    my @val = grep { $_ } 
253        ($self->{c}->req->param($attribute->name))
254        || ($self->{object}
255            ? ($self->{object}->get_attributes($attribute->name))
256            : ());
257
258    if (@val) {
259        return @val;
260    } else {
261        return (grep { $_ } ($attribute->default_value))
262    }
263}
264
265sub attr_field_name {
266    my ($self, $attr) = @_;
267    my $id = $self->{object}
268        ? ($self->{object}->id || '')
269        : '';
270    $id .= '_' if ($id);
271    $id =~ s/\./_/g;
272    $id =~ s/-/_/g;
273    $id . $attr;
274}
275
276sub attr_item {
277    my ($self, $attribute, $value, $htmlid) = @_;
278
279    my $html = $self->_attr_item($attribute, $value, $htmlid);
280   
281    if ($attribute->uniq) {
282        $html .= qq|<span class="inputvalidate" id="${htmlid}_uniq"></span>|;
283    }
284    if (my $ref = $attribute->reference) {
285        my $uri_part = $self->_uri_part($ref);
286        if ($self->base->attribute($ref, 'displayName')) {
287            my $text = '';
288            if ($value &&
289                (my $obj = $self->base->get_object($ref, $value))) {
290                $text = $obj->get_attributes('displayName');
291            }
292            $html .= sprintf(
293                qq|<span id="${htmlid}_ref" style="margin-left: 1em">$text</span>|,
294                $self->escape($text),
295            );
296        } elsif($value && $uri_part) {
297            $html .= sprintf(
298                '<a href="%s"><img class="attr" src="%s" title="%s"></a>',
299                $self->{c}->uri_for('/', $uri_part, $value),
300                $self->{c}->uri_for('/static', 'icons', 'arrow-right.png'),
301                $value,
302            );
303        }
304    }
305
306    $html
307}
308
309sub _attr_item {
310    my ($self, $attribute, $value, $htmlid) = @_;
311
312    my $htmlname = $self->escape($self->attr_field_name($attribute->name));
313    $htmlid ||= $htmlname;
314
315    for ($attribute->form_type) {
316            /^textarea$/i and do {
317                return sprintf(
318                    '<textarea id="%s" name="%s" cols="40">%s</textarea>',
319                    $self->escape($htmlid),
320                    $self->escape($htmlname),
321                    $self->escape($value || ''),
322                );
323                last;
324            };
325
326            /^label$/i and do {
327                $value or last;
328                my $html_field = $self->escape($value);
329                $html_field =~ s/\n/<br>/g;
330                $html_field .= sprintf('<input type="hidden" name="%s" value="%s">',
331                    $self->escape($htmlname), ($value || ''));
332                return $html_field;
333                last;
334            };
335
336            /^date$/i and do {
337                my ($date, $time) = split(/ /, $value || '');
338                if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) {
339                    $date = "$3/$2/$1";
340                }
341                my $html = "\n" . q{<SCRIPT LANGUAGE="JavaScript" ID="js13">
342                var cal13 = new CalendarPopup();
343                </SCRIPT>} . "\n";
344                $html .= sprintf(
345                    '<input type="text" id="%s" name="%s" value="%s" size="12">',
346                    $self->escape($htmlid),
347                    $self->escape($htmlname),
348                    $self->escape($date)
349                );
350                $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>};
351                $html .= qq|
352                <A HREF="#"
353                    onClick="cal13.select(document.getElementById('$htmlid'),'${htmlid}_anc','dd/MM/yyyy');return false;"
354                    TITLE="Date"
355                    NAME="${htmlid}_anc" ID="${htmlid}_anc">
356                    <img class="attr" src="| . $self->{c}->uri_for(qw(/static icons view-calendar-day.png))
357                    . qq{" style="ref"></A>
358                    } . "\n";
359                return $html;
360                last;
361            };
362
363            /^checkbox/i and do {
364                my $options = $1 || '';
365                my $html = sprintf('<input type="checkbox" id="%s" name="%s"%s>',
366                    $htmlid,
367                    $self->escape($htmlname),
368                    $value ? '  checked="yes"' : ''
369                ); 
370                $html .= sprintf('<input type="hidden" name="%s">',
371                    $self->escape($htmlname));
372                if ($attribute->form_option('rawvalue')) {
373                    $html .= $value
374                        ? ' ' . $value
375                        : ''; 
376                }
377                return $html;
378                last;
379            };
380
381            /^LIST/i and do {
382                my $select = sprintf('<select id="%s" name="%s">',
383                    $self->escape($htmlid),
384                    $self->escape($htmlname)) . "\n";
385                $select .= '<option value="">--</option>' . "\n"
386                    unless($attribute->mandatory);
387                $value ||= '';
388                my $initial_observed = '';
389                my @valslist;
390                foreach my $val (sort $attribute->can_values) {
391                    push(@valslist, {
392                        val => $val,
393                        disp => $attribute->display($val || ''),
394                    });
395                }
396                foreach (sort { $a->{disp} cmp $b->{disp} } @valslist) {
397                    $select .= sprintf(
398                        '    <option value="%s"%s>%s</option>',
399                        $self->escape($_->{val} || ''),
400                        $value eq $_->{val} ? ' selected="selected"' : '',
401                        $self->escape($_->{disp} || ''),
402                    );
403                    $select .= "\n";
404                }
405                $select .= "</select>\n";
406
407                return $select;
408                last;
409            };
410
411            /^text/i and do {
412                return sprintf(
413                    '<input type="text" id="%s" name="%s" value="%s" size="%d">',
414                    $self->escape($htmlid),
415                    $self->escape($htmlname),
416                    $self->escape($value),
417                    $attribute->form_option('length') || 30,
418                );
419                last;
420            };
421        }
422    return '';
423}
424
425sub _uri_part {
426    my ($self, $ref) = @_;
427    my $uri_part = {
428        user => 'users',
429        group => 'groups',
430        nethost => 'nethosts',
431        netzone => 'netzones',
432        site => 'sites',
433        aliasess => 'aliases',
434    }->{$ref};
435}
436
437sub attr_js {
438    my ($self, $attribute, $value, $htmlid) = @_;
439
440    my (@js);
441
442    if (my $ref = $attribute->reference) {
443        my $uri_part = $self->_uri_part($ref);
444        if ($self->base->attribute($ref, 'displayName')) {
445            push(@js, 
446                $self->{c}->prototype->observe_field( $htmlid,
447                    {
448                        update => "${htmlid}_ref",
449                        url => $self->{c}->uri_for('/ajax', 'rawattr', $ref),
450                        frequency => 1,
451                        with   => "'attr=displayName&id=' + element.options[element.selectedIndex].text",
452                    }
453                )
454            ) if ($attribute->form_type =~ /list/i);
455        }
456    }
457
458    if ($attribute->uniq) {
459        push(@js,
460            $self->{c}->prototype->observe_field( $htmlid,
461                {
462                    update => "${htmlid}_uniq",
463                    url => $self->{c}->uri_for('/ajax', 'objattrexist',
464                        $self->otype, $attribute->name),
465                    frequency => 1,
466                    with => "'val='+element.value" .
467                    ($self->{object} ?
468                        "+'&exclude=" . ($self->{object}->id || '') . "'" 
469                        : ''),
470                }
471            )
472        );
473    }
474
475    return(join("\n", @js));
476}
477
478sub attr_field {
479    my ($self, $attr, $type) = @_;
480
481    my $attribute;
482    if ($type) {
483        if (ref $type) {
484            $attribute = $self->base->attribute(
485                $self->otype,
486                { %$type, name => $attr },
487            );
488        } else {
489            $attribute =  $self->base->attribute(
490                $self->otype,
491                { formtype => $type , name => $attr },
492            );
493        }
494    } else {
495        if ($self->{object}) {
496            $attribute = $self->{object}->attribute($attr);
497        } else {
498            $attribute = $self->base->attribute(
499                $self->otype, $attr,
500                { formtype => $type }
501            );
502        }
503    }
504    $attribute ||= $self->base->attribute($self->otype, { name => $attr });
505
506
507    my $htmlname = $self->escape($self->attr_field_name($attr));
508
509    my @html_fields;
510
511    # Force Array context:
512    my @attr_raw_value = ();
513
514    if ($attribute->multiple) {
515        @attr_raw_value = grep { $_ } $self->attr_raw_value($attribute);
516    } else {
517        my ($val) = $self->attr_raw_value($attribute);
518        @attr_raw_value = ($val || '');
519    }
520
521    foreach my $attr_raw_value (@attr_raw_value) {
522
523        my $html_id = $htmlname .
524            (scalar(@html_fields) ? scalar(@html_fields) : '');
525        push(@html_fields,
526            join("\n",
527                $self->attr_item(
528                    $attribute, $attr_raw_value, $html_id
529                ),
530                $self->attr_js(
531                    $attribute, $attr_raw_value, $html_id
532                )
533            )
534        );
535    }
536
537    if ($attribute->{multiple} && !$attribute->readonly) {
538        my $html_id = $htmlname . '_m_SUBID';
539
540        my $attr = $self->attr_item($attribute, '', $html_id);
541        # JS: string are on the line only
542        $attr =~ s/\n//mg;
543        $attr =~ s/'/\'/g;
544
545        my $js = $self->attr_js($attribute, '', 'NANAR');
546        $js =~ s!'NANAR([_\w]*)'!('$html_id$1'.replace(/SUBID/g, cn))!gm;
547        $js =~ s!<script[^>]+>!!gm;
548        $js =~ s!</script>!!gm;
549        $js =~ s!-->!!gm;
550        $js =~ s/<!--//gm;
551
552        my $addempty = sprintf('<div id="%s"></div>', "${htmlname}_div_0");
553        $addempty .= "\n";
554        $addempty .= sprintf(qq#
555            <script type="text/javascript">
556            var cn_${htmlname} = 0;
557            function add_${htmlname} () {
558            cn_${htmlname}++;
559            // It work only using temporary variable
560            var cn = cn_${htmlname};
561            document.getElementById('${htmlname}_div_' + (cn - 1)).innerHTML +=
562                '$attr'.replace(/SUBID/g, cn );
563            document.getElementById('${htmlname}_div_' + (cn - 1)).innerHTML +=
564                '<div id="${htmlname}_div_'+ cn +'" style="display:block"></div>';
565            %s
566            }
567            </script>
568            #, $js
569        );
570
571        $addempty .= "\n";
572        $addempty .=
573            qq|<button type="button" style="display:block" onclick="add_${htmlname}()">+</button>|,
574
575        push(@html_fields, $addempty);
576    }
577
578    return join("<br>\n", @html_fields);
579}
580
581sub submit {
582    my ($self) = @_;
583    return sprintf(
584        '<input type="submit" name="%s" value="Enregistrer">',
585        $self->escape($self->label),
586    );
587}
588
589sub write_attributes {
590    my ($self) = @_;
591    my @attrs;
592    foreach ($self->attributes) {
593        my $attr = ($self->{object}
594            ? $self->{object}->attribute($_)
595            : $self->base->attribute($self->otype, $_)) or next;
596        $attr->readonly and next;
597        push(@attrs, $_);
598    }
599    @attrs;
600}
601
602sub set_attrs {
603    my ($self, $attrs) = @_;
604    $self->{c}->req->param($self->label) || $attrs or return;
605    my $prefix = $self->attr_field_name('');
606    my %fields;
607    foreach ($attrs ? @{ $attrs } : $self->write_attributes) {
608        my $attr = ($self->{object}
609            ? $self->{object}->attribute($_)
610            : $self->base->attribute($self->otype, $_)) or next;
611        if ($attr->{multiple}) {
612            $fields{$_} = [ grep { $_ } $self->{c}->req->param("$prefix$_") ];
613        } else {
614            $fields{$_} = $self->{c}->req->param("$prefix$_");
615        }
616    }
617    $self->{object}->set_c_fields(%fields) or do {
618        $self->{c}->stash->{page}{error} =
619            LATMOS::Accounts::Log::lastmessage(LA_ERR);
620        $self->{object}->base->rollback;
621        return;
622    };
623    $self->{object}->base->commit;
624}
625
626=head1 AUTHOR
627
628Thauvin Olivier
629
630=head1 LICENSE
631
632This library is free software, you can redistribute it and/or modify
633it under the same terms as Perl itself.
634
635=cut
636
6371;
Note: See TracBrowser for help on using the repository browser.