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

Last change on this file since 431 was 431, checked in by nanardon, 15 years ago
  • add grade and contratType in web forms
File size: 11.1 KB
Line 
1package LATMOS::Accounts::Web::Model::AttrForms;
2
3use strict;
4use warnings;
5use base 'Catalyst::Model';
6
7=head1 NAME
8
9LATMOS::Accounts::Web::Model::AttrForms - Catalyst Model
10
11=head1 DESCRIPTION
12
13Catalyst Model.
14
15=cut
16
17my $attrs = {
18    uid => [ 'Login', 'text:12' ],
19    uidNumber => [ 'UID', 'text-U:6' ],
20    gidNumber => [ 'GID', 'select:group:gidNumber' ],
21    manager => [ 'Responsable', 'select-N:user' ],
22    managedBy => [ 'Responsable', 'select-N:user' ],
23    sn => [ 'Nom' ],
24    givenName => [ 'Prénom' ],
25    homeDirectory => [ 'Home', 'text-U:15' ],
26    loginShell => [ 'Shell', 'text-A' ],
27    physicalDeliveryOfficeName => [ 'Bureau' ],
28    telephoneNumber => [ 'Téléphone' ],
29    otherTelephone => [ 'Téléphone (autre)' ],
30    company => [ 'Société', 'text-A' ],
31    l => [ 'Ville' ],
32    postalCode => [ 'Code postal' ],
33    postOfficeBox => [ 'BP' ],
34    department => [ 'Département' ],
35    streetAddress => [ 'Rue', 'textarea' ],
36    title => [ 'Fonction' ],
37    expire => [ 'Expire le', 'date' ],
38    st => [ 'État (US)' ],
39    sutype => [ 'Structure admin.', 'select-N:sutype' ],
40    exported => [ 'Exporté', 'checkbox' ],
41    locked => [ 'Vérouillé', 'checkbox' ],
42    isMainAddress => [ 'Adresse principale', 'checkbox' ],
43    site => [ 'Site', 'select-N:site' ],
44    co => [ 'Pays' ],
45    mail => [ 'Mail', 'text-U:30' ],
46    contratType => [ 'Type de contrat', 'text-A' ],
47    grade => [ 'Grade', 'text-A' ],
48};
49
50my $forms = {
51    user => {
52        name => 'SystÚme',
53        acl => 'admin',
54        attrs => [ qw(
55            sn givenName description
56            uid uidNumber gidNumber gecos homeDirectory loginShell
57            mail
58        ) ],
59    },
60    useraddress => {
61        name => 'Adresse',
62        attrs => [ qw(
63            initials
64            mail
65        ) ],
66    },
67    userstatus => {
68        name => 'Status',
69        attrs => [ qw(
70            company
71            site
72            manager
73            department
74            grade
75            contratType
76            expire
77            locked
78            exported
79        ) ],
80    },
81    site => {
82        name => 'Site',
83        attrs => [ qw(
84            description
85            streetAddress
86            postOfficeBox
87            postalCode
88            l
89            st
90            co
91        ) ],
92    },
93    address => {
94        name => 'Adresse',
95        attrs => [ qw(
96            isMainAddress
97            telephoneNumber
98            streetAddress
99            postOfficeBox
100            postalCode
101            l
102            st
103            physicalDeliveryOfficeName
104            site
105            co
106        ) ],
107    },
108    group => {
109        name => 'SystÚme',
110        acl => 'admin',
111        attrs => [ qw(
112            gidNumber description
113            managedBy
114            sutype exported
115        ) ],
116    },
117};
118
119sub escape {
120    my ($self, $text) = @_;
121    $text ||= '';
122    for ($text) {
123        s/&/&/g;
124        s/</&lt;/g;
125        s/>/&gt;/g;
126        s/"/&quot;/g;
127    }
128    $text;
129}
130
131sub new {
132    my ($class) = @_;
133    bless({}, $class);
134}
135
136# call either OBJ or type + base
137
138sub ACCEPT_CONTEXT {
139    my ($self, $c, $form, $object, $base) = @_;
140    my $new = {};
141    $new->{c} = $c;
142    $new->{form} = $form;
143    $new->{object} = $object if (ref $object);
144    $new->{base} = $base || ($object ? $object->base : undef) or return $self;
145    $new->{otype} = ref $object ? $object->type : $object;
146    bless($new, 'LATMOS::Accounts::Web::Model::AttrForms');
147}
148
149sub base {
150    my ( $self ) = @_;
151    $self->{base}
152}
153
154sub otype {
155    my ($self) = @_;
156    $self->{otype};
157}
158
159sub label {
160    my ($self) = @_;
161    $forms->{$self->{form}}->{name} || ''
162}
163
164sub attributes {
165    my ($self, $for) = @_;
166    grep { $self->base->get_field_name($self->otype, $_, $for || 'a') }
167    @{ $forms->{$self->{form}}->{attrs} };
168}
169
170sub attr_label {
171    my ($self, $attr) = @_;
172    my $htmlname = ($self->{object} ? $self->{object}->id . '_' : '') . $attr;
173    return sprintf('<label for="%s">%s</label>',
174        $self->escape($htmlname),
175        $self->escape($attrs->{$attr}[0] || $attr)
176    );
177}
178
179sub attr_raw_value {
180    my ($self, $attr) = @_;
181    return $self->{c}->req->param($attr) ||
182        ($self->{object} ? $self->{object}->get_c_field($attr) : '')
183}
184
185sub attr_field {
186    my ($self, $attr, $type) = @_;
187    $type ||= $self->base->get_field_name($self->otype, $attr, 'w')
188        ? $attrs->{$attr}[1] || ''
189        : 'label';
190    # exception: gidNumber is used also in group, but we don't want
191    # group list here, really the number !
192    $type ||= 'text';
193    $type = 'text-U:6' if (($self->{form} || '') =~ /^group/ && $attr eq 'gidNumber');
194    my $htmlname = $self->escape(($self->{object} ? $self->{object}->id . '_' :
195            '') . $attr);
196    for ($type) {
197        /^textarea$/ and return sprintf(
198            '<textarea id="%s" name="%s" cols="40">%s</textarea>',
199            $self->escape($htmlname),
200            $self->escape($htmlname),
201            $self->escape($self->attr_raw_value($attr)),
202        );
203        /^label$/ and do {
204            my $field = $self->escape(
205                $self->attr_raw_value($attr)
206            );
207            $field =~ s/\n/<br>/g;
208            return $field;
209        };
210        /^date$/ and do {
211            my ($date, $time) = split(/ /, $self->attr_raw_value($attr) || '');
212            if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) {
213                $date = "$3/$2/$1";
214            }
215            my $html = "\n" . q{<SCRIPT LANGUAGE="JavaScript" ID="js13">
216            var cal13 = new CalendarPopup();
217            </SCRIPT>} . "\n";
218            $html .= sprintf(
219                '<input type="text" id="%s" name="%s" value="%s" size="12">',
220                $self->escape($htmlname),
221                $self->escape($htmlname),
222                $self->escape($date)
223            );
224            $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>};
225            $html .= qq|
226            <A HREF="#"
227                onClick="cal13.select(document.forms[0].$htmlname,'${htmlname}_anc','dd/MM/yyyy');return false;"
228                TITLE="cal13.select(document.forms[0].$htmlname,'${htmlname}_anc','dd/MM/yyyy');return false;"
229                NAME="${htmlname}_anc" ID="${htmlname}_anc">
230                <img src="| . $self->{c}->uri_for(qw(/static icons view-calendar-day.png))
231                . qq{" style="ref"></A>
232                } . "\n";
233            return $html;
234        };
235        /^checkbox$/ and do {
236            return sprintf('<input type="checkbox" name="%s"%s>',
237                $self->escape($htmlname),
238                $self->attr_raw_value($attr) ? '  checked="yes"' : ''
239            ) . sprintf('<input type="hidden" name="%s">',
240                $self->escape($htmlname));
241        };
242        /^select(-\w+)?:([^:]+)(?::(.*))?$/ and do {
243            my $options = $1 || '';
244            my $otype = $2;
245            my $keyfield = $3;
246            my $observe_keyfield = $keyfield || 'displayName';
247            my $select = sprintf('<select id="%s" name="%s">',
248                $self->escape($htmlname),
249                $self->escape($htmlname)) . "\n";
250            $select .= '<option value="">--</option>' . "\n" if ($options =~ /N/);
251            my $value = $self->attr_raw_value($attr) || '';
252            my $initial_observed = '';
253            foreach my $id ($self->base->list_objects($otype)) {
254                my $obj = $self->base->get_object($otype, $id) or next;
255                my $val = $keyfield ? $obj->get_c_field($keyfield) : $id;
256                $select .= sprintf(
257                    '    <option value="%s"%s>%s</options>',
258                    $self->escape($val),
259                    $value eq $val ? ' "selected"' : '',
260                    $self->escape($id),
261                );
262                $select .= "\n";
263                $initial_observed = $obj->get_c_field($observe_keyfield) || ''
264                    if($value eq $val);
265            }
266            $select .= "</select>\n";
267            $select .= $self->{c}->prototype->observe_field( $htmlname, {
268                update => "${htmlname}_span",
269                url => $self->{c}->uri_for('/ajax', 'rawattr', $otype),
270                frequency => 1,
271                with   => "'attr=" . $observe_keyfield .
272                "&id='+element.options[element.selectedIndex].text",
273            }) .
274            qq|<span id="${htmlname}_span">$initial_observed</span>|;
275            return $select;
276        };
277        /^text(-\w+)?(?::(\d+))?/ and do {
278            my $flag = $1 || '';
279            my $textf = sprintf(
280                '<input type="text" id="%s" name="%s" value="%s" size="%d">',
281                $self->escape($htmlname),
282                $self->escape($htmlname),
283                $self->escape($self->attr_raw_value($attr)),
284                $2 || 30,
285            );
286            if ($flag =~ /A/) {
287            $textf .= qq|<span id="${htmlname}_auto_complete"></span>|;
288            $textf .= "\n";
289            $textf .= $self->{c}->prototype->auto_complete_field(
290                $htmlname,
291                {
292                update => "${htmlname}_auto_complete",
293                url => $self->{c}->uri_for('/ajax', 'attrvalues', $self->otype, $attr),
294                indicator => "${htmlname}_stat", min_chars => 1,
295                with => "'val='+document.getElementById('$htmlname').value",
296                frequency => 2,
297                }
298            );
299            }
300            if ($flag =~ /U/) {
301            $textf .= qq|<span id="${htmlname}_observer_uniq"></span>|;
302            $textf .= "\n";
303            $textf .= $self->{c}->prototype->observe_field(
304                $htmlname,
305                {
306                update => "${htmlname}_observer_uniq",
307                url => $self->{c}->uri_for('/ajax', 'objattrexist',
308                    $self->otype, $attr),
309                frequency => 2,
310                indicator => "${htmlname}_stat", min_chars => 1,
311                with => "'val='+document.getElementById('$htmlname').value" .
312                    ($self->{object} ? "+'&exclude=" . $self->{object}->id . "'" :
313                        ''),
314                }
315            );
316            }
317            $textf .= qq|<span style="display:none" id="${htmlname}_stat">Searching...</span>|;
318
319            return $textf;
320        };
321    }
322}
323
324sub submit {
325    my ($self) = @_;
326    return sprintf(
327        '<input type="submit" name="%s">',
328        $self->escape($self->label),
329    );
330}
331
332sub set_attrs {
333    my ($self) = @_;
334    $self->{c}->req->param($self->label) or return;
335    my $prefix = $self->{object}->id . '_';
336    $self->{object}->set_c_fields(
337        map {
338            $_ =>
339                ($attrs->{$_}[1] || '') eq 'checkbox'
340                ? ($self->{c}->req->param("$prefix$_") ? 1 : 0)
341                : ($self->{c}->req->param("$prefix$_") || undef)
342        } grep { $self->base->get_field_name($self->otype, $_, 'w') } $self->attributes
343    ) or return;
344    $self->{object}->base->commit;
345    $self->{c}->model('Accounts')->sync->sync_object($self->{object}->type, $self->{object}->id);
346}
347
348=head1 AUTHOR
349
350Thauvin Olivier
351
352=head1 LICENSE
353
354This library is free software, you can redistribute it and/or modify
355it under the same terms as Perl itself.
356
357=cut
358
3591;
Note: See TracBrowser for help on using the repository browser.