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

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