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

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