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

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