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

Last change on this file since 176 was 176, checked in by nanardon, 15 years ago
  • end user creation
File size: 7.3 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' ],
19    uidNumber => [ 'UID' ],
20    gidNumber => [ 'GID', 'select:group:gidNumber' ],
21    manager => [ 'Responsable', 'select-N:user:uid' ],
22    sn => [ 'Nom' ],
23    givenName => [ 'Prénom' ],
24    homeDirectory => [ 'Home' ],
25    loginShell => [ 'Shell' ],
26    physicalDeliveryOfficeName => [ 'Bureau' ],
27    telephoneNumber => [ 'Téléphone' ],
28    otherTelephone => [ 'Téléphone (autre)' ],
29    company => [ 'Société' ],
30    l => [ 'Ville' ],
31    postalCode => [ 'Code postal' ],
32    postOfficeBox => [ 'BP' ],
33    department => [ 'Département', 'select-N:department' ],
34    streetAddress => [ 'Rue', 'textarea' ],
35    st => [ 'Etat' ],
36    title => [ 'Fonction' ],
37    expire => [ 'Expire le', 'date' ],
38    st => [ 'État (US)' ],
39    sutype => [ 'Structure admin.', 'select-N:sutype' ],
40    exported => [ undef, 'checkbox' ],
41};
42
43my $forms = {
44    user => {
45        name => 'SystÚme',
46        acl => 'admin',
47        attrs => [ qw(
48            sn givenName description
49            uid uidNumber gidNumber gecos homeDirectory loginShell
50            mail
51        ) ],
52    },
53    useraddress => {
54        name => 'Adresse',
55        attrs => [ qw(
56            initials
57            telephoneNumber
58            otherTelephone
59            mail
60            streetAddress
61            postOfficeBox
62            postalCode
63            l
64            st
65            physicalDeliveryOfficeName
66        ) ],
67    },
68    userstatus => {
69        name => 'Status',
70        attrs => [ qw(
71            company
72            manager
73            department
74            title
75            expire
76            exported
77        ) ],
78    },
79    site => {
80        name => 'Site',
81        attrs => [ qw(
82            description
83            streetAddress
84            postOfficeBox
85            postalCode
86            st
87            l
88        ) ],
89    },
90    department => {
91        name => 'Département',
92        attrs => [qw(description manager) ],
93    },
94    group => {
95        name => 'SystÚme',
96        acl => 'admin',
97        attrs => [ qw(
98            gidNumber description
99            manager
100            sutype exported
101        ) ],
102    },
103};
104
105sub escape {
106    my ($self, $text) = @_;
107    $text ||= '';
108    for ($text) {
109        s/&/&/g;
110        s/</&lt;/g;
111        s/>/&gt;/g;
112        s/"/&quot;/g;
113    }
114    $text;
115}
116
117sub new {
118    my ($class) = @_;
119    bless({}, $class);
120}
121
122# call either OBJ or type + base
123
124sub ACCEPT_CONTEXT {
125    my ($self, $c, $form, $object, $base) = @_;
126    my $new = {};
127    $new->{c} = $c;
128    $new->{form} = $form;
129    $new->{object} = $object if (ref $object);
130    $new->{base} = $base || ($object ? $object->base : undef) or return $self;
131    $new->{otype} = ref $object ? $object->type : $object;
132    bless($new, 'LATMOS::Accounts::Web::Model::AttrForms');
133}
134
135sub base {
136    my ( $self ) = @_;
137    $self->{base}
138}
139
140sub otype {
141    my ($self) = @_;
142    $self->{otype};
143}
144
145sub label {
146    my ($self) = @_;
147    $forms->{$self->{form}}->{name} || ''
148}
149
150sub attributes {
151    my ($self) = @_;
152    grep { $self->base->get_field_name($self->otype, $_, 'a') }
153    @{ $forms->{$self->{form}}->{attrs} };
154}
155
156sub attr_label {
157    my ($self, $attr) = @_;
158    return $self->escape($attrs->{$attr}[0] || $attr);
159}
160
161sub attr_raw_value {
162    my ($self, $attr) = @_;
163    return $self->{c}->req->param($attr) ||
164        ($self->{object} ? $self->{object}->get_c_field($attr) : '')
165}
166
167sub attr_field {
168    my ($self, $attr, $type) = @_;
169    $type ||= $self->base->get_field_name($self->otype, $attr, 'w')
170        ? $attrs->{$attr}[1] || ''
171        : 'label';
172    # exception: gidNumber is used also in group, but we don't want
173    # group list here, really the number !
174    $type = '' if (($self->{form} || '') =~ /^group/ && $attr eq 'gidNumber');
175    for ($type) {
176        /^textarea$/ and return sprintf(
177            '<textarea name="%s">%s</textarea>',
178            $self->escape($attr),
179            $self->escape($self->attr_raw_value($attr)),
180        );
181        /^label$/ and return $self->escape(
182            $self->attr_raw_value($attr)
183        );
184        /^date$/ and do {
185            my ($date, $time) = split(/ /, $self->attr_raw_value($attr) || '');
186            if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) {
187                $date = "$3/$2/$1";
188            }
189            my $html = "\n" . q{<SCRIPT LANGUAGE="JavaScript" ID="js13">
190            var cal13 = new CalendarPopup();
191            </SCRIPT>} . "\n";
192            $html .= sprintf(
193                '<input type="text" name="%s" value="%s">',
194                $attr,
195                $self->escape($date)
196            );
197            $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>};
198            $html .= qq{
199            <A HREF="#"
200                onClick="cal13.select(document.forms[0].$attr,'anchor13','dd/MM/yyyy');return false;"
201                TITLE="cal13.select(document.forms[0].$attr,'anchor13','dd/MM/yyyy'); return false;"
202                NAME="anchor13" ID="anchor13">
203                <img src="} . $self->{c}->uri_for(qw(/static icons view-calendar-day.png))
204                . qq{" style="ref"></A>} . "\n";
205            return $html;
206        };
207        /^checkbox$/ and do {
208            return sprintf('<input type="checkbox" name="%s"%s>',
209                $attr,
210                $self->attr_raw_value($attr) ? '  checked="yes"' : ''
211            );
212        };
213        /^select(-\w+)?:([^:]+)(?::(.*))?$/ and do {
214            my $options = $1 || '';
215            my $otype = $2;
216            my $keyfield = $3;
217            my $select = sprintf('<select name="%s">',
218                $self->escape($attr)) . "\n";
219            $select .= '<option value="">--</option>' . "\n" if ($options =~ /N/);
220            my $value = $self->attr_raw_value($attr) || '';
221            foreach my $id ($self->base->list_objects($otype)) {
222                my $obj = $self->base->get_object($otype, $id) or next;
223                my $val = $keyfield ? $obj->get_c_field($keyfield) : $id;
224                $select .= sprintf(
225                    '    <option value="%s"%s>%s</options>',
226                    $self->escape($val),
227                    $value eq $val ? ' "selected"' : '',
228                    $self->escape($id),
229                );
230                $select .= "\n";
231            }
232            $select .= "</select>\n";
233            return $select;
234        };
235    }
236    return sprintf(
237        '<input type="text" name="%s" value="%s">',
238        $attr,
239        $self->escape($self->attr_raw_value($attr))
240    );
241}
242
243sub submit {
244    my ($self) = @_;
245    return sprintf(
246        '<input type="submit" name="%s">',
247        $self->escape($self->label),
248    );
249}
250
251sub set_attrs {
252    my ($self) = @_;
253    $self->{c}->req->param($self->label) or return;
254    $self->{object}->set_c_fields(
255        map {
256            $_ =>
257                ($attrs->{$_}[1] || '') eq 'checkbox'
258                ? ($self->{c}->req->param($_) ? 1 : 0)
259                : $self->{c}->req->param($_)
260        } $self->attributes
261    );
262    $self->{object}->base->commit;
263}
264
265=head1 AUTHOR
266
267Thauvin Olivier
268
269=head1 LICENSE
270
271This library is free software, you can redistribute it and/or modify
272it under the same terms as Perl itself.
273
274=cut
275
2761;
Note: See TracBrowser for help on using the repository browser.