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

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