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

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