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

Last change on this file since 697 was 697, checked in by nanardon, 14 years ago
  • readd exported attribute on forms but no modification can be done
File size: 11.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/&/&/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 !
[491]205    $type = $modallow ? 'text-U:6' : 'label' if (($self->{form} || '') =~ /^group/ && $attr eq 'gidNumber');
[206]206    $type ||= 'text';
[407]207    my $htmlname = $self->escape(($self->{object} ? $self->{object}->id . '_' :
208            '') . $attr);
[143]209    for ($type) {
210        /^textarea$/ and return sprintf(
[272]211            '<textarea id="%s" name="%s" cols="40">%s</textarea>',
[236]212            $self->escape($htmlname),
[272]213            $self->escape($htmlname),
[144]214            $self->escape($self->attr_raw_value($attr)),
[143]215        );
[364]216        /^label$/ and do {
217            my $field = $self->escape(
218                $self->attr_raw_value($attr)
219            );
220            $field =~ s/\n/<br>/g;
221            return $field;
222        };
[160]223        /^date$/ and do {
[170]224            my ($date, $time) = split(/ /, $self->attr_raw_value($attr) || '');
[168]225            if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) {
[160]226                $date = "$3/$2/$1";
227            }
228            my $html = "\n" . q{<SCRIPT LANGUAGE="JavaScript" ID="js13">
229            var cal13 = new CalendarPopup();
230            </SCRIPT>} . "\n";
231            $html .= sprintf(
[272]232                '<input type="text" id="%s" name="%s" value="%s" size="12">',
233                $self->escape($htmlname),
234                $self->escape($htmlname),
[160]235                $self->escape($date)
236            );
237            $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>};
[362]238            $html .= qq|
[160]239            <A HREF="#"
[272]240                onClick="cal13.select(document.forms[0].$htmlname,'${htmlname}_anc','dd/MM/yyyy');return false;"
241                TITLE="cal13.select(document.forms[0].$htmlname,'${htmlname}_anc','dd/MM/yyyy');return false;"
242                NAME="${htmlname}_anc" ID="${htmlname}_anc">
[362]243                <img src="| . $self->{c}->uri_for(qw(/static icons view-calendar-day.png))
244                . qq{" style="ref"></A>
245                } . "\n";
[160]246            return $html;
247        };
[167]248        /^checkbox$/ and do {
249            return sprintf('<input type="checkbox" name="%s"%s>',
[236]250                $self->escape($htmlname),
[167]251                $self->attr_raw_value($attr) ? '  checked="yes"' : ''
[362]252            ) . sprintf('<input type="hidden" name="%s">',
253                $self->escape($htmlname));
[167]254        };
[151]255        /^select(-\w+)?:([^:]+)(?::(.*))?$/ and do {
256            my $options = $1 || '';
257            my $otype = $2;
258            my $keyfield = $3;
[362]259            my $observe_keyfield = $keyfield || 'displayName';
[272]260            my $select = sprintf('<select id="%s" name="%s">',
261                $self->escape($htmlname),
[236]262                $self->escape($htmlname)) . "\n";
[151]263            $select .= '<option value="">--</option>' . "\n" if ($options =~ /N/);
264            my $value = $self->attr_raw_value($attr) || '';
[362]265            my $initial_observed = '';
[145]266            foreach my $id ($self->base->list_objects($otype)) {
267                my $obj = $self->base->get_object($otype, $id) or next;
[143]268                my $val = $keyfield ? $obj->get_c_field($keyfield) : $id;
269                $select .= sprintf(
[652]270                    '    <option value="%s"%s>%s</option>',
[475]271                    $self->escape($val || ''),
[630]272                    $value eq $val ? ' selected="selected"' : '',
[475]273                    $self->escape($id || ''),
[143]274                );
275                $select .= "\n";
[364]276                $initial_observed = $obj->get_c_field($observe_keyfield) || ''
[362]277                    if($value eq $val);
[143]278            }
279            $select .= "</select>\n";
[272]280            $select .= $self->{c}->prototype->observe_field( $htmlname, {
281                update => "${htmlname}_span",
282                url => $self->{c}->uri_for('/ajax', 'rawattr', $otype),
283                frequency => 1,
[362]284                with   => "'attr=" . $observe_keyfield .
285                "&id='+element.options[element.selectedIndex].text",
[272]286            }) .
[362]287            qq|<span id="${htmlname}_span">$initial_observed</span>|;
[143]288            return $select;
289        };
[272]290        /^text(-\w+)?(?::(\d+))?/ and do {
291            my $flag = $1 || '';
292            my $textf = sprintf(
293                '<input type="text" id="%s" name="%s" value="%s" size="%d">',
[236]294                $self->escape($htmlname),
[272]295                $self->escape($htmlname),
[206]296                $self->escape($self->attr_raw_value($attr)),
[272]297                $2 || 30,
[206]298            );
[272]299            if ($flag =~ /A/) {
[407]300            $textf .= qq|<span id="${htmlname}_auto_complete"></span>|;
[272]301            $textf .= "\n";
302            $textf .= $self->{c}->prototype->auto_complete_field(
303                $htmlname,
[407]304                {
305                update => "${htmlname}_auto_complete",
[272]306                url => $self->{c}->uri_for('/ajax', 'attrvalues', $self->otype, $attr),
307                indicator => "${htmlname}_stat", min_chars => 1,
308                with => "'val='+document.getElementById('$htmlname').value",
[407]309                frequency => 2,
[272]310                }
[407]311            );
[272]312            }
[407]313            if ($flag =~ /U/) {
314            $textf .= qq|<span id="${htmlname}_observer_uniq"></span>|;
315            $textf .= "\n";
316            $textf .= $self->{c}->prototype->observe_field(
317                $htmlname,
318                {
319                update => "${htmlname}_observer_uniq",
320                url => $self->{c}->uri_for('/ajax', 'objattrexist',
321                    $self->otype, $attr),
322                frequency => 2,
323                indicator => "${htmlname}_stat", min_chars => 1,
324                with => "'val='+document.getElementById('$htmlname').value" .
325                    ($self->{object} ? "+'&exclude=" . $self->{object}->id . "'" :
326                        ''),
327                }
328            );
329            }
330            $textf .= qq|<span style="display:none" id="${htmlname}_stat">Searching...</span>|;
331
[272]332            return $textf;
[206]333        };
[143]334    }
[111]335}
336
[174]337sub submit {
338    my ($self) = @_;
339    return sprintf(
[686]340        '<input type="submit" name="%s" value="Enregistrer">',
[174]341        $self->escape($self->label),
342    );
343}
344
[112]345sub set_attrs {
346    my ($self) = @_;
[174]347    $self->{c}->req->param($self->label) or return;
[236]348    my $prefix = $self->{object}->id . '_';
[112]349    $self->{object}->set_c_fields(
350        map {
[169]351            $_ =>
[170]352                ($attrs->{$_}[1] || '') eq 'checkbox'
[236]353                ? ($self->{c}->req->param("$prefix$_") ? 1 : 0)
354                : ($self->{c}->req->param("$prefix$_") || undef)
[362]355        } grep { $self->base->get_field_name($self->otype, $_, 'w') } $self->attributes
[180]356    ) or return;
[112]357    $self->{object}->base->commit;
358}
359
[111]360=head1 AUTHOR
361
362Thauvin Olivier
363
364=head1 LICENSE
365
366This library is free software, you can redistribute it and/or modify
367it under the same terms as Perl itself.
368
369=cut
370
3711;
Note: See TracBrowser for help on using the repository browser.