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

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