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

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