1 | package LATMOS::Accounts::Web::Model::AttrForms; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base 'Catalyst::Model'; |
---|
6 | |
---|
7 | =head1 NAME |
---|
8 | |
---|
9 | LATMOS::Accounts::Web::Model::AttrForms - Catalyst Model |
---|
10 | |
---|
11 | =head1 DESCRIPTION |
---|
12 | |
---|
13 | Catalyst Model. |
---|
14 | |
---|
15 | =cut |
---|
16 | |
---|
17 | my $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 | |
---|
43 | my $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 | sutype exported |
---|
100 | ) ], |
---|
101 | }, |
---|
102 | }; |
---|
103 | |
---|
104 | sub escape { |
---|
105 | my ($self, $text) = @_; |
---|
106 | $text ||= ''; |
---|
107 | for ($text) { |
---|
108 | s/&/&/g; |
---|
109 | s/</</g; |
---|
110 | s/>/>/g; |
---|
111 | s/"/"/g; |
---|
112 | } |
---|
113 | $text; |
---|
114 | } |
---|
115 | |
---|
116 | sub new { |
---|
117 | my ($class) = @_; |
---|
118 | bless({}, $class); |
---|
119 | } |
---|
120 | |
---|
121 | # call either OBJ or type + base |
---|
122 | |
---|
123 | sub ACCEPT_CONTEXT { |
---|
124 | my ($self, $c, $form, $object, $base) = @_; |
---|
125 | my $new = {}; |
---|
126 | $new->{c} = $c; |
---|
127 | $new->{form} = $form; |
---|
128 | $new->{object} = $object if (ref $object); |
---|
129 | $new->{base} = $base || ($object ? $object->base : undef) or return $self; |
---|
130 | $new->{otype} = ref $object ? $object->type : $object; |
---|
131 | bless($new, 'LATMOS::Accounts::Web::Model::AttrForms'); |
---|
132 | } |
---|
133 | |
---|
134 | sub base { |
---|
135 | my ( $self ) = @_; |
---|
136 | $self->{base} |
---|
137 | } |
---|
138 | |
---|
139 | sub otype { |
---|
140 | my ($self) = @_; |
---|
141 | $self->{otype}; |
---|
142 | } |
---|
143 | |
---|
144 | sub label { |
---|
145 | my ($self) = @_; |
---|
146 | $forms->{$self->{form}}->{name} || '' |
---|
147 | } |
---|
148 | |
---|
149 | sub attributes { |
---|
150 | my ($self) = @_; |
---|
151 | grep { $self->base->get_field_name($self->otype, $_, 'a') } |
---|
152 | @{ $forms->{$self->{form}}->{attrs} }; |
---|
153 | } |
---|
154 | |
---|
155 | sub attr_label { |
---|
156 | my ($self, $attr) = @_; |
---|
157 | return $self->escape($attrs->{$attr}[0] || $attr); |
---|
158 | } |
---|
159 | |
---|
160 | sub attr_raw_value { |
---|
161 | my ($self, $attr) = @_; |
---|
162 | return $self->{c}->req->param($attr) || |
---|
163 | ($self->{object} ? $self->{object}->get_c_field($attr) : '') |
---|
164 | } |
---|
165 | |
---|
166 | sub attr_field { |
---|
167 | my ($self, $attr, $type) = @_; |
---|
168 | $type ||= $self->base->get_field_name($self->otype, $attr, 'w') |
---|
169 | ? $attrs->{$attr}[1] || '' |
---|
170 | : 'label'; |
---|
171 | # exception: gidNumber is used also in group, but we don't want |
---|
172 | # group list here, really the number ! |
---|
173 | $type = '' if (($self->{form} || '') =~ /^group/ && $attr eq 'gidNumber'); |
---|
174 | for ($type) { |
---|
175 | /^textarea$/ and return sprintf( |
---|
176 | '<textarea name="%s">%s</textarea>', |
---|
177 | $self->escape($attr), |
---|
178 | $self->escape($self->attr_raw_value($attr)), |
---|
179 | ); |
---|
180 | /^label$/ and return $self->escape( |
---|
181 | $self->attr_raw_value($attr) |
---|
182 | ); |
---|
183 | /^date$/ and do { |
---|
184 | my ($date, $time) = split(/ /, $self->attr_raw_value($attr)); |
---|
185 | if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) { |
---|
186 | $date = "$3/$2/$1"; |
---|
187 | } |
---|
188 | my $html = "\n" . q{<SCRIPT LANGUAGE="JavaScript" ID="js13"> |
---|
189 | var cal13 = new CalendarPopup(); |
---|
190 | </SCRIPT>} . "\n"; |
---|
191 | $html .= sprintf( |
---|
192 | '<input type="text" name="%s" value="%s">', |
---|
193 | $attr, |
---|
194 | $self->escape($date) |
---|
195 | ); |
---|
196 | $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>}; |
---|
197 | $html .= qq{ |
---|
198 | <A HREF="#" |
---|
199 | onClick="cal13.select(document.forms[0].$attr,'anchor13','dd/MM/yyyy');return false;" |
---|
200 | TITLE="cal13.select(document.forms[0].$attr,'anchor13','dd/MM/yyyy'); return false;" |
---|
201 | NAME="anchor13" ID="anchor13"> |
---|
202 | <img src="} . $self->{c}->uri_for(qw(/static icons view-calendar-day.png)) |
---|
203 | . qq{" style="ref"></A>} . "\n"; |
---|
204 | return $html; |
---|
205 | }; |
---|
206 | /^checkbox$/ and do { |
---|
207 | return sprintf('<input type="checkbox" name="%s"%s>', |
---|
208 | $attr, |
---|
209 | $self->attr_raw_value($attr) ? ' checked="yes"' : '' |
---|
210 | ); |
---|
211 | }; |
---|
212 | /^select(-\w+)?:([^:]+)(?::(.*))?$/ and do { |
---|
213 | my $options = $1 || ''; |
---|
214 | my $otype = $2; |
---|
215 | my $keyfield = $3; |
---|
216 | my $select = sprintf('<select name="%s">', |
---|
217 | $self->escape($attr)) . "\n"; |
---|
218 | $select .= '<option value="">--</option>' . "\n" if ($options =~ /N/); |
---|
219 | my $value = $self->attr_raw_value($attr) || ''; |
---|
220 | foreach my $id ($self->base->list_objects($otype)) { |
---|
221 | my $obj = $self->base->get_object($otype, $id) or next; |
---|
222 | my $val = $keyfield ? $obj->get_c_field($keyfield) : $id; |
---|
223 | $select .= sprintf( |
---|
224 | ' <option value="%s"%s>%s</options>', |
---|
225 | $self->escape($val), |
---|
226 | $value eq $val ? ' "selected"' : '', |
---|
227 | $self->escape($id), |
---|
228 | ); |
---|
229 | $select .= "\n"; |
---|
230 | } |
---|
231 | $select .= "</select>\n"; |
---|
232 | return $select; |
---|
233 | }; |
---|
234 | } |
---|
235 | return sprintf( |
---|
236 | '<input type="text" name="%s" value="%s">', |
---|
237 | $attr, |
---|
238 | $self->escape($self->attr_raw_value($attr)) |
---|
239 | ); |
---|
240 | } |
---|
241 | |
---|
242 | sub set_attrs { |
---|
243 | my ($self) = @_; |
---|
244 | $self->{object}->set_c_fields( |
---|
245 | map { |
---|
246 | $_ => $self->{c}->req->param($_) |
---|
247 | } grep { |
---|
248 | exists $self->{c}->req->params->{$_} |
---|
249 | } $self->attributes |
---|
250 | ); |
---|
251 | $self->{object}->base->commit; |
---|
252 | } |
---|
253 | |
---|
254 | =head1 AUTHOR |
---|
255 | |
---|
256 | Thauvin Olivier |
---|
257 | |
---|
258 | =head1 LICENSE |
---|
259 | |
---|
260 | This library is free software, you can redistribute it and/or modify |
---|
261 | it under the same terms as Perl itself. |
---|
262 | |
---|
263 | =cut |
---|
264 | |
---|
265 | 1; |
---|