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
RevLine 
[111]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 = {
[206]18    uid => [ 'Login', 'text:12' ],
[407]19    uidNumber => [ 'UID', 'text-U:6' ],
[143]20    gidNumber => [ 'GID', 'select:group:gidNumber' ],
[236]21    manager => [ 'Responsable', 'select-N:user' ],
22    managedBy => [ 'Responsable', 'select-N:user' ],
[143]23    sn => [ 'Nom' ],
24    givenName => [ 'Prénom' ],
[472]25    homeDirectory => [ 'Home', 'text-U:25' ],
[272]26    loginShell => [ 'Shell', 'text-A' ],
[143]27    physicalDeliveryOfficeName => [ 'Bureau' ],
28    telephoneNumber => [ 'Téléphone' ],
29    otherTelephone => [ 'Téléphone (autre)' ],
[272]30    company => [ 'Société', 'text-A' ],
[143]31    l => [ 'Ville' ],
32    postalCode => [ 'Code postal' ],
33    postOfficeBox => [ 'BP' ],
[466]34    department => [ 'Département', 'select-N:group' ],
[143]35    streetAddress => [ 'Rue', 'textarea' ],
[145]36    title => [ 'Fonction' ],
[160]37    expire => [ 'Expire le', 'date' ],
[165]38    st => [ 'État (US)' ],
[236]39    sutype => [ 'Structure admin.', 'select-N:sutype' ],
[697]40    exported => [ 'Propagé', 'label' ],
[186]41    locked => [ 'Vérouillé', 'checkbox' ],
[364]42    isMainAddress => [ 'Adresse principale', 'checkbox' ],
43    site => [ 'Site', 'select-N:site' ],
44    co => [ 'Pays' ],
[407]45    mail => [ 'Mail', 'text-U:30' ],
[431]46    contratType => [ 'Type de contrat', 'text-A' ],
47    grade => [ 'Grade', 'text-A' ],
[448]48    facsimileTelephoneNumber => [ 'Fax', 'text' ],
[526]49    nickname => [ 'Nick', 'text-U:3' ],
[633]50    cn => [ 'Nom' ],
[670]51    initials => [ 'Initiales', 'text-U:5' ],
[677]52    create => [ 'Créer le' ],
53    date => [ 'Modifié le' ],
[111]54};
55
56my $forms = {
[117]57    user => {
58        name => 'SystÚme',
[111]59        acl => 'admin',
[112]60        attrs => [ qw(
[125]61            sn givenName description
62            uid uidNumber gidNumber gecos homeDirectory loginShell
[670]63            mail initials nickname
[677]64            locked
[697]65            exported
[677]66            create
67            date
[112]68        ) ],
[117]69    },
70    useraddress => {
71        name => 'Adresse',
72        attrs => [ qw(
[129]73            initials
74            mail
[117]75        ) ],
76    },
[129]77    userstatus => {
78        name => 'Status',
79        attrs => [ qw(
80            company
[151]81            manager
[141]82            department
[431]83            grade
84            contratType
[160]85            expire
[129]86        ) ],
87    },
[134]88    site => {
89        name => 'Site',
90        attrs => [ qw(
[148]91            description
[134]92            streetAddress
93            postOfficeBox
94            postalCode
[364]95            l
[160]96            st
[364]97            co
[448]98            facsimileTelephoneNumber
[364]99        ) ],
100    },
101    address => {
102        name => 'Adresse',
103        attrs => [ qw(
104            isMainAddress
105            telephoneNumber
106            streetAddress
107            postOfficeBox
108            postalCode
[134]109            l
[364]110            st
111            physicalDeliveryOfficeName
112            site
113            co
[134]114        ) ],
115    },
[125]116    group => {
117        name => 'SystÚme',
118        acl => 'admin',
119        attrs => [ qw(
[126]120            gidNumber description
[183]121            managedBy
[697]122            sutype exported
[125]123        ) ],
124    },
[111]125};
126
[116]127sub escape {
128    my ($self, $text) = @_;
[117]129    $text ||= '';
[116]130    for ($text) {
131        s/&/&amp;/g;
132        s/</&lt;/g;
133        s/>/&gt;/g;
134        s/"/&quot;/g;
135    }
136    $text;
137}
[111]138
139sub new {
140    my ($class) = @_;
141    bless({}, $class);
142}
143
[144]144# call either OBJ or type + base
145
[111]146sub ACCEPT_CONTEXT {
[144]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');
[111]155}
156
[144]157sub base {
158    my ( $self ) = @_;
159    $self->{base}
160}
161
162sub otype {
163    my ($self) = @_;
164    $self->{otype};
165}
166
[117]167sub label {
168    my ($self) = @_;
169    $forms->{$self->{form}}->{name} || ''
170}
171
[111]172sub attributes {
[373]173    my ($self, $for) = @_;
[507]174    grep { $_ }
[475]175    grep { $self->base->check_acl($self->{object} || $self->otype, $_, 'r') }
[373]176    grep { $self->base->get_field_name($self->otype, $_, $for || 'a') }
[143]177    @{ $forms->{$self->{form}}->{attrs} };
[111]178}
179
180sub attr_label {
181    my ($self, $attr) = @_;
[272]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    );
[111]187}
188
[144]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
[111]195sub attr_field {
[143]196    my ($self, $attr, $type) = @_;
[491]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
[143]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 !
[698]205    $type = $modallow ? 'text-U:6' : 'label' if ($self->{otype} eq 'group' && $attr eq 'gidNumber');
[206]206    $type ||= 'text';
[698]207   
208    my $htmlname = $self->escape(($self->{object} 
209            ? $self->{object}->id . '_'
210            : ''
211        ) . $attr
212    );
[143]213    for ($type) {
214        /^textarea$/ and return sprintf(
[272]215            '<textarea id="%s" name="%s" cols="40">%s</textarea>',
[236]216            $self->escape($htmlname),
[272]217            $self->escape($htmlname),
[144]218            $self->escape($self->attr_raw_value($attr)),
[143]219        );
[364]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        };
[160]227        /^date$/ and do {
[170]228            my ($date, $time) = split(/ /, $self->attr_raw_value($attr) || '');
[168]229            if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) {
[160]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(
[272]236                '<input type="text" id="%s" name="%s" value="%s" size="12">',
237                $self->escape($htmlname),
238                $self->escape($htmlname),
[160]239                $self->escape($date)
240            );
241            $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>};
[362]242            $html .= qq|
[160]243            <A HREF="#"
[272]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">
[362]247                <img src="| . $self->{c}->uri_for(qw(/static icons view-calendar-day.png))
248                . qq{" style="ref"></A>
249                } . "\n";
[160]250            return $html;
251        };
[167]252        /^checkbox$/ and do {
253            return sprintf('<input type="checkbox" name="%s"%s>',
[236]254                $self->escape($htmlname),
[167]255                $self->attr_raw_value($attr) ? '  checked="yes"' : ''
[362]256            ) . sprintf('<input type="hidden" name="%s">',
257                $self->escape($htmlname));
[167]258        };
[151]259        /^select(-\w+)?:([^:]+)(?::(.*))?$/ and do {
260            my $options = $1 || '';
261            my $otype = $2;
262            my $keyfield = $3;
[362]263            my $observe_keyfield = $keyfield || 'displayName';
[272]264            my $select = sprintf('<select id="%s" name="%s">',
265                $self->escape($htmlname),
[236]266                $self->escape($htmlname)) . "\n";
[151]267            $select .= '<option value="">--</option>' . "\n" if ($options =~ /N/);
268            my $value = $self->attr_raw_value($attr) || '';
[362]269            my $initial_observed = '';
[145]270            foreach my $id ($self->base->list_objects($otype)) {
271                my $obj = $self->base->get_object($otype, $id) or next;
[143]272                my $val = $keyfield ? $obj->get_c_field($keyfield) : $id;
273                $select .= sprintf(
[652]274                    '    <option value="%s"%s>%s</option>',
[475]275                    $self->escape($val || ''),
[630]276                    $value eq $val ? ' selected="selected"' : '',
[475]277                    $self->escape($id || ''),
[143]278                );
279                $select .= "\n";
[364]280                $initial_observed = $obj->get_c_field($observe_keyfield) || ''
[362]281                    if($value eq $val);
[143]282            }
283            $select .= "</select>\n";
[272]284            $select .= $self->{c}->prototype->observe_field( $htmlname, {
285                update => "${htmlname}_span",
286                url => $self->{c}->uri_for('/ajax', 'rawattr', $otype),
287                frequency => 1,
[362]288                with   => "'attr=" . $observe_keyfield .
289                "&id='+element.options[element.selectedIndex].text",
[272]290            }) .
[362]291            qq|<span id="${htmlname}_span">$initial_observed</span>|;
[143]292            return $select;
293        };
[272]294        /^text(-\w+)?(?::(\d+))?/ and do {
295            my $flag = $1 || '';
[699]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                    );
[272]309                }
[699]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                );
[407]333                }
[699]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;
[407]354            }
[206]355        };
[143]356    }
[111]357}
358
[174]359sub submit {
360    my ($self) = @_;
361    return sprintf(
[686]362        '<input type="submit" name="%s" value="Enregistrer">',
[174]363        $self->escape($self->label),
364    );
365}
366
[112]367sub set_attrs {
368    my ($self) = @_;
[174]369    $self->{c}->req->param($self->label) or return;
[236]370    my $prefix = $self->{object}->id . '_';
[112]371    $self->{object}->set_c_fields(
372        map {
[169]373            $_ =>
[170]374                ($attrs->{$_}[1] || '') eq 'checkbox'
[236]375                ? ($self->{c}->req->param("$prefix$_") ? 1 : 0)
376                : ($self->{c}->req->param("$prefix$_") || undef)
[362]377        } grep { $self->base->get_field_name($self->otype, $_, 'w') } $self->attributes
[180]378    ) or return;
[112]379    $self->{object}->base->commit;
380}
381
[111]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.