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
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/&/&/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->{form} || '') =~ /^group/ && $attr eq 'gidNumber');
206    $type ||= 'text';
207    my $htmlname = $self->escape(($self->{object} ? $self->{object}->id . '_' :
208            '') . $attr);
209    for ($type) {
210        /^textarea$/ and return sprintf(
211            '<textarea id="%s" name="%s" cols="40">%s</textarea>',
212            $self->escape($htmlname),
213            $self->escape($htmlname),
214            $self->escape($self->attr_raw_value($attr)),
215        );
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        };
223        /^date$/ and do {
224            my ($date, $time) = split(/ /, $self->attr_raw_value($attr) || '');
225            if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) {
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(
232                '<input type="text" id="%s" name="%s" value="%s" size="12">',
233                $self->escape($htmlname),
234                $self->escape($htmlname),
235                $self->escape($date)
236            );
237            $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>};
238            $html .= qq|
239            <A HREF="#"
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">
243                <img src="| . $self->{c}->uri_for(qw(/static icons view-calendar-day.png))
244                . qq{" style="ref"></A>
245                } . "\n";
246            return $html;
247        };
248        /^checkbox$/ and do {
249            return sprintf('<input type="checkbox" name="%s"%s>',
250                $self->escape($htmlname),
251                $self->attr_raw_value($attr) ? '  checked="yes"' : ''
252            ) . sprintf('<input type="hidden" name="%s">',
253                $self->escape($htmlname));
254        };
255        /^select(-\w+)?:([^:]+)(?::(.*))?$/ and do {
256            my $options = $1 || '';
257            my $otype = $2;
258            my $keyfield = $3;
259            my $observe_keyfield = $keyfield || 'displayName';
260            my $select = sprintf('<select id="%s" name="%s">',
261                $self->escape($htmlname),
262                $self->escape($htmlname)) . "\n";
263            $select .= '<option value="">--</option>' . "\n" if ($options =~ /N/);
264            my $value = $self->attr_raw_value($attr) || '';
265            my $initial_observed = '';
266            foreach my $id ($self->base->list_objects($otype)) {
267                my $obj = $self->base->get_object($otype, $id) or next;
268                my $val = $keyfield ? $obj->get_c_field($keyfield) : $id;
269                $select .= sprintf(
270                    '    <option value="%s"%s>%s</option>',
271                    $self->escape($val || ''),
272                    $value eq $val ? ' selected="selected"' : '',
273                    $self->escape($id || ''),
274                );
275                $select .= "\n";
276                $initial_observed = $obj->get_c_field($observe_keyfield) || ''
277                    if($value eq $val);
278            }
279            $select .= "</select>\n";
280            $select .= $self->{c}->prototype->observe_field( $htmlname, {
281                update => "${htmlname}_span",
282                url => $self->{c}->uri_for('/ajax', 'rawattr', $otype),
283                frequency => 1,
284                with   => "'attr=" . $observe_keyfield .
285                "&id='+element.options[element.selectedIndex].text",
286            }) .
287            qq|<span id="${htmlname}_span">$initial_observed</span>|;
288            return $select;
289        };
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">',
294                $self->escape($htmlname),
295                $self->escape($htmlname),
296                $self->escape($self->attr_raw_value($attr)),
297                $2 || 30,
298            );
299            if ($flag =~ /A/) {
300            $textf .= qq|<span id="${htmlname}_auto_complete"></span>|;
301            $textf .= "\n";
302            $textf .= $self->{c}->prototype->auto_complete_field(
303                $htmlname,
304                {
305                update => "${htmlname}_auto_complete",
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",
309                frequency => 2,
310                }
311            );
312            }
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
332            return $textf;
333        };
334    }
335}
336
337sub submit {
338    my ($self) = @_;
339    return sprintf(
340        '<input type="submit" name="%s" value="Enregistrer">',
341        $self->escape($self->label),
342    );
343}
344
345sub set_attrs {
346    my ($self) = @_;
347    $self->{c}->req->param($self->label) or return;
348    my $prefix = $self->{object}->id . '_';
349    $self->{object}->set_c_fields(
350        map {
351            $_ =>
352                ($attrs->{$_}[1] || '') eq 'checkbox'
353                ? ($self->{c}->req->param("$prefix$_") ? 1 : 0)
354                : ($self->{c}->req->param("$prefix$_") || undef)
355        } grep { $self->base->get_field_name($self->otype, $_, 'w') } $self->attributes
356    ) or return;
357    $self->{object}->base->commit;
358}
359
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.