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

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