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

Last change on this file since 926 was 926, checked in by nanardon, 12 years ago
  • don't hardcode package name
File size: 15.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};
151
152sub escape {
153    my ($self, $text) = @_;
154    $text ||= '';
155    for ($text) {
156        s/&/&/g;
157        s/</&lt;/g;
158        s/>/&gt;/g;
159        s/"/&quot;/g;
160    }
161    $text;
162}
163
164sub new {
165    my ($class) = @_;
166    bless({}, $class);
167}
168
169# call either OBJ or type + base
170
171sub ACCEPT_CONTEXT {
172    my ($self, $c, $form, $object, $base) = @_;
173    my $new = {};
174    $new->{c} = $c;
175    $new->{form} = $form;
176    $new->{object} = $object if (ref $object);
177    $new->{base} = $base || ($object ? $object->base : undef) or return $self;
178    $new->{otype} = ref $object ? $object->type : $object;
179    bless($new, '__PACKAGE__');
180}
181
182sub base {
183    my ( $self ) = @_;
184    $self->{base}
185}
186
187sub otype {
188    my ($self) = @_;
189    $self->{otype};
190}
191
192sub label {
193    my ($self) = @_;
194    $forms->{$self->{form}}->{name} || ''
195}
196
197sub attributes {
198    my ($self, $for) = @_;
199    grep { $_ }
200    grep { $self->base->attribute($self->otype, $_) }
201    @{ $forms->{$self->{form}}->{attrs} };
202}
203
204sub attr_label {
205    my ($self, $attr) = @_;
206    my ($hint, $label) = ('', '');
207    LATMOS::Accounts::Web->config->{attrs} ||= {};
208    my $htmlname = ($self->{object} ? $self->{object}->id . '_' : '') . $attr;
209    if (my $attr_config = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
210        $label = $attr_config->{label} || '';
211        #utf8::is_utf8($label) && utf8::encode($label);
212        $hint = $attr_config->{hint} || '';
213        #utf8::is_utf8($hint) && utf8::encode($hint);
214    }
215    return sprintf('<label %sfor="%s">%s</label>',
216        ($hint ? sprintf('title="%s"', $self->escape($hint)) : ''),
217        $self->escape($htmlname),
218        $self->escape($label || $attr),
219    );
220}
221
222sub attr_hint {
223    my ($self, $attr) = @_;
224    LATMOS::Accounts::Web->config->{attrs} ||= {};
225    if (my $attr_config = LATMOS::Accounts::Web->config->{attrs}{$attr}) {
226        return $attr_config->{hint} || '';
227    }
228    return;
229}
230
231sub attr_raw_value {
232    my ($self, $attr) = @_;
233    return $self->{c}->req->param($attr) ||
234        ($self->{object} ? $self->{object}->get_attributes($attr) : '')
235}
236
237sub attr_field_name {
238    my ($self, $attr) = @_;
239    return ($self->{object}
240            ? $self->{object}->id . '_'
241            : ''
242        ) . $attr
243}
244
245sub attr_field {
246    my ($self, $attr, $type) = @_;
247
248    my $attribute = ($self->{object}
249        ? $self->{object}->attribute($attr)
250        : $self->base->attribute($self->otype, $attr)) or return;
251
252    my $htmlname = $self->escape($self->attr_field_name($attr));
253
254    my @html_fields;
255    foreach my $attr_raw_value (
256        $attribute->{multiple}
257            ? ((grep { $_ } $self->attr_raw_value($attr)),
258                $attribute->readonly
259                    ? ()
260                    : ('')
261            )
262            : ($self->attr_raw_value($attr))) {
263
264        my $html_id = $htmlname .
265            (scalar(@html_fields) ? scalar(@html_fields) : '');
266
267        my $html_field = '';
268        for ($attribute->form_type) {
269            /^textarea$/i and do {
270                $html_field = sprintf(
271                    '<textarea id="%s" name="%s" cols="40">%s</textarea>',
272                    $self->escape($html_id),
273                    $self->escape($htmlname),
274                    $self->escape($attr_raw_value || ''),
275                );
276                last;
277            };
278            /^label$/i and do {
279                $attr_raw_value or last;
280                $html_field = $self->escape($attr_raw_value);
281                $html_field =~ s/\n/<br>/g;
282                $html_field .= sprintf('<input type="hidden" name="%s" value="%s">',
283                    $self->escape($htmlname), ($attr_raw_value || ''));
284                last;
285            };
286            /^date$/i and do {
287                my ($date, $time) = split(/ /, $self->attr_raw_value($attr) || '');
288                if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) {
289                    $date = "$3/$2/$1";
290                }
291                my $html = "\n" . q{<SCRIPT LANGUAGE="JavaScript" ID="js13">
292                var cal13 = new CalendarPopup();
293                </SCRIPT>} . "\n";
294                $html .= sprintf(
295                    '<input type="text" id="%s" name="%s" value="%s" size="12">',
296                    $self->escape($html_id),
297                    $self->escape($htmlname),
298                    $self->escape($date)
299                );
300                $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>};
301                $html .= qq|
302                <A HREF="#"
303                    onClick="cal13.select(document.getElementById('$html_id'),'${html_id}_anc','dd/MM/yyyy');return false;"
304                    TITLE="Date"
305                    NAME="${html_id}_anc" ID="${html_id}_anc">
306                    <img class="attr" src="| . $self->{c}->uri_for(qw(/static icons view-calendar-day.png))
307                    . qq{" style="ref"></A>
308                    } . "\n";
309                $html_field = $html;
310                last;
311            };
312            /^checkbox(?::(\w+))?$/i and do {
313                my $options = $1 || '';
314                $html_field = sprintf('<input type="checkbox" name="%s"%s>',
315                    $self->escape($htmlname),
316                    $attr_raw_value ? '  checked="yes"' : ''
317                ); 
318                $html_field .= sprintf('<input type="hidden" name="%s">',
319                    $self->escape($htmlname));
320                if ($attribute->form_option('rawvalue')) {
321                    $html_field .= $attr_raw_value
322                        ? ' ' . $attr_raw_value
323                        : ''; 
324                }
325                last;
326            };
327            /^LIST/ and do {
328                my $options = $1 || '';
329                my $select = sprintf('<select id="%s" name="%s">',
330                    $self->escape($html_id),
331                    $self->escape($htmlname)) . "\n";
332                $select .= '<option value="">--</option>' . "\n"
333                    unless($attribute->mandatory);
334                my $value = $attr_raw_value || '';
335                my $initial_observed = '';
336                my @valslist;
337                foreach my $val (sort $attribute->can_values) {
338                    push(@valslist, {
339                        val => $val,
340                        disp => $attribute->display($val || ''),
341                    });
342                }
343                foreach (sort { $a->{disp} cmp $b->{disp} } @valslist) {
344                    $select .= sprintf(
345                        '    <option value="%s"%s>%s</option>',
346                        $self->escape($_->{val} || ''),
347                        $value eq $_->{val} ? ' selected="selected"' : '',
348                        $self->escape($_->{disp} || ''),
349                    );
350                    $select .= "\n";
351                }
352                $select .= "</select>\n";
353
354                $html_field = $select;
355                last;
356            };
357            /^text(-\w+)?(?::(\d+))?/i and do {
358                my $flag = $1 || '';
359                $html_field = sprintf(
360                    '<input type="text" id="%s" name="%s" value="%s" size="%d">',
361                    $self->escape($html_id),
362                    $self->escape($htmlname),
363                    $self->escape($attr_raw_value),
364                    $attribute->form_option('length') || 30,
365                );
366                if ($flag =~ /A/) {
367                    $html_field .= qq|<span id="${html_id}_auto_complete"></span>|;
368                    $html_field .= "\n";
369                    $html_field .= $self->{c}->prototype->auto_complete_field(
370                        $html_id,
371                        {
372                            update => "${html_id}_auto_complete",
373                            url => $self->{c}->uri_for('/ajax', 'attrvalues', $self->otype, $attr),
374                            indicator => "${html_id}_stat", min_chars => 1,
375                            with => "'val='+document.getElementById('$html_id').value",
376                            frequency => 2,
377                        }
378                    );
379                }
380                if ($attribute->uniq) {
381                    $html_field .= qq|<span class="inputvalidate" id="${html_id}_observer_uniq"></span>|;
382                    $html_field .= "\n";
383                    $html_field .= $self->{c}->prototype->observe_field(
384                        $html_id,
385                        {
386                            update => "${html_id}_observer_uniq",
387                            url => $self->{c}->uri_for('/ajax', 'objattrexist',
388                                $self->otype, $attr),
389                            frequency => 2,
390                            indicator => "${html_id}_stat", min_chars => 1,
391                            with => "'val='+document.getElementById('$html_id').value" .
392                            ($self->{object} ? "+'&exclude=" . $self->{object}->id . "'" :
393                                ''),
394                        }
395                    );
396                    $html_field .= qq|<span style="display:none" id="${html_id}_stat">Searching...</span>|;
397                }
398                last;
399            };
400        }
401        if (my $ref = $attribute->reference) {
402            my $uri_part= {
403                user => 'users',
404                group => 'groups',
405                nethost => 'nethosts',
406                netzone => 'netzones',
407                site => 'sites'
408            }->{$ref};
409            my $text;
410            if ($self->base->attribute($ref, 'displayName')) {
411                if ($attr_raw_value &&
412                    (my $obj = $self->base->get_object($ref, $attr_raw_value)))
413                {
414                    $text = $obj->get_attributes('displayName');
415                }
416               
417                $html_field .= $self->{c}->prototype->observe_field( $html_id, {
418                        update => "${html_id}_span",
419                        url => $self->{c}->uri_for('/ajax', 'rawattr', $ref),
420                        frequency => 1,
421                        with   => "'attr=displayName" .
422                        "&id=' + element.options[element.selectedIndex].text",
423                    }) . "\n" if ($attribute->form_type =~ /list/i);
424            } elsif($attr_raw_value && $uri_part) {
425                $text = sprintf(
426                    '<img class="attr" src="%s" title="%s">',
427                    $self->{c}->uri_for('/static', 'icons', 'arrow-right.png'),
428                    $attr_raw_value,
429                )
430            }
431            $html_field .= sprintf(qq{<span id="%s" style="margin-left: 1em">},
432                "${html_id}_span");
433
434            if (defined($text)) {
435                $html_field .= $uri_part
436                    ? sprintf('<a href="%s">%s</a>',
437                        $self->{c}->uri_for("/$uri_part", $attribute->display($attr_raw_value)),
438                        $text,)
439                    : $text;
440            }
441
442            $html_field .= "</span>\n";
443        }
444        push(@html_fields, $html_field);
445    }
446    return join("<br>\n", @html_fields);
447}
448
449sub submit {
450    my ($self) = @_;
451    return sprintf(
452        '<input type="submit" name="%s" value="Enregistrer">',
453        $self->escape($self->label),
454    );
455}
456
457sub write_attributes {
458    my ($self) = @_;
459    my @attrs;
460    foreach ($self->attributes) {
461        my $attr = ($self->{object}
462            ? $self->{object}->attribute($_)
463            : $self->base->attribute($self->otype, $_)) or next;
464        $attr->readonly and next;
465        push(@attrs, $_);
466    }
467    @attrs;
468}
469
470sub set_attrs {
471    my ($self) = @_;
472    $self->{c}->req->param($self->label) or return;
473    my $prefix = $self->{object}->id . '_';
474    my %fields;
475    foreach ($self->write_attributes) {
476        my $attr = ($self->{object}
477            ? $self->{object}->attribute($_)
478            : $self->base->attribute($self->otype, $_)) or next;
479        if ($attr->{multiple}) {
480            $fields{$_} = [ grep { $_ } $self->{c}->req->param("$prefix$_") ];
481        } else {
482            $fields{$_} = $self->{c}->req->param("$prefix$_");
483        }
484    }
485    $self->{object}->set_c_fields(%fields) or do {
486        $self->{c}->stash->{page}{error} =
487            LATMOS::Accounts::Log::lastmessage(LA_ERR);
488        $self->{object}->base->rollback;
489        return;
490    };
491    $self->{object}->base->commit;
492}
493
494=head1 AUTHOR
495
496Thauvin Olivier
497
498=head1 LICENSE
499
500This library is free software, you can redistribute it and/or modify
501it under the same terms as Perl itself.
502
503=cut
504
5051;
Note: See TracBrowser for help on using the repository browser.