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 | 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', 'select-N:department' ], |
---|
35 | streetAddress => [ 'Rue', 'textarea' ], |
---|
36 | st => [ 'Etat' ], |
---|
37 | title => [ 'Fonction' ], |
---|
38 | expire => [ 'Expire le', 'date' ], |
---|
39 | st => [ 'Ãtat (US)' ], |
---|
40 | sutype => [ 'Structure admin.', 'select-N:sutype' ], |
---|
41 | exported => [ 'Exporté', 'checkbox' ], |
---|
42 | locked => [ 'Vérouillé', 'checkbox' ], |
---|
43 | }; |
---|
44 | |
---|
45 | my $forms = { |
---|
46 | user => { |
---|
47 | name => 'SystÚme', |
---|
48 | acl => 'admin', |
---|
49 | attrs => [ qw( |
---|
50 | sn givenName description |
---|
51 | uid uidNumber gidNumber gecos homeDirectory loginShell |
---|
52 | mail |
---|
53 | ) ], |
---|
54 | }, |
---|
55 | useraddress => { |
---|
56 | name => 'Adresse', |
---|
57 | attrs => [ qw( |
---|
58 | initials |
---|
59 | telephoneNumber |
---|
60 | otherTelephone |
---|
61 | mail |
---|
62 | streetAddress |
---|
63 | postOfficeBox |
---|
64 | postalCode |
---|
65 | l |
---|
66 | st |
---|
67 | physicalDeliveryOfficeName |
---|
68 | ) ], |
---|
69 | }, |
---|
70 | userstatus => { |
---|
71 | name => 'Status', |
---|
72 | attrs => [ qw( |
---|
73 | company |
---|
74 | manager |
---|
75 | department |
---|
76 | title |
---|
77 | expire |
---|
78 | locked |
---|
79 | exported |
---|
80 | ) ], |
---|
81 | }, |
---|
82 | site => { |
---|
83 | name => 'Site', |
---|
84 | attrs => [ qw( |
---|
85 | description |
---|
86 | streetAddress |
---|
87 | postOfficeBox |
---|
88 | postalCode |
---|
89 | st |
---|
90 | l |
---|
91 | ) ], |
---|
92 | }, |
---|
93 | department => { |
---|
94 | name => 'Département', |
---|
95 | attrs => [qw(description manager) ], |
---|
96 | }, |
---|
97 | group => { |
---|
98 | name => 'SystÚme', |
---|
99 | acl => 'admin', |
---|
100 | attrs => [ qw( |
---|
101 | gidNumber description |
---|
102 | managedBy |
---|
103 | sutype exported |
---|
104 | ) ], |
---|
105 | }, |
---|
106 | }; |
---|
107 | |
---|
108 | sub escape { |
---|
109 | my ($self, $text) = @_; |
---|
110 | $text ||= ''; |
---|
111 | for ($text) { |
---|
112 | s/&/&/g; |
---|
113 | s/</</g; |
---|
114 | s/>/>/g; |
---|
115 | s/"/"/g; |
---|
116 | } |
---|
117 | $text; |
---|
118 | } |
---|
119 | |
---|
120 | sub new { |
---|
121 | my ($class) = @_; |
---|
122 | bless({}, $class); |
---|
123 | } |
---|
124 | |
---|
125 | # call either OBJ or type + base |
---|
126 | |
---|
127 | sub 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 | |
---|
138 | sub base { |
---|
139 | my ( $self ) = @_; |
---|
140 | $self->{base} |
---|
141 | } |
---|
142 | |
---|
143 | sub otype { |
---|
144 | my ($self) = @_; |
---|
145 | $self->{otype}; |
---|
146 | } |
---|
147 | |
---|
148 | sub label { |
---|
149 | my ($self) = @_; |
---|
150 | $forms->{$self->{form}}->{name} || '' |
---|
151 | } |
---|
152 | |
---|
153 | sub attributes { |
---|
154 | my ($self) = @_; |
---|
155 | grep { $self->base->get_field_name($self->otype, $_, 'a') } |
---|
156 | @{ $forms->{$self->{form}}->{attrs} }; |
---|
157 | } |
---|
158 | |
---|
159 | sub attr_label { |
---|
160 | my ($self, $attr) = @_; |
---|
161 | return $self->escape($attrs->{$attr}[0] || $attr); |
---|
162 | } |
---|
163 | |
---|
164 | sub 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 | |
---|
170 | sub 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/ && $attr eq 'gidNumber'); |
---|
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 && $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 | /^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 | } |
---|
239 | return sprintf( |
---|
240 | '<input type="text" name="%s" value="%s">', |
---|
241 | $attr, |
---|
242 | $self->escape($self->attr_raw_value($attr)) |
---|
243 | ); |
---|
244 | } |
---|
245 | |
---|
246 | sub submit { |
---|
247 | my ($self) = @_; |
---|
248 | return sprintf( |
---|
249 | '<input type="submit" name="%s">', |
---|
250 | $self->escape($self->label), |
---|
251 | ); |
---|
252 | } |
---|
253 | |
---|
254 | sub set_attrs { |
---|
255 | my ($self) = @_; |
---|
256 | $self->{c}->req->param($self->label) or return; |
---|
257 | $self->{object}->set_c_fields( |
---|
258 | map { |
---|
259 | $_ => |
---|
260 | ($attrs->{$_}[1] || '') eq 'checkbox' |
---|
261 | ? ($self->{c}->req->param($_) ? 1 : 0) |
---|
262 | : $self->{c}->req->param($_) |
---|
263 | } $self->attributes |
---|
264 | ) or return; |
---|
265 | $self->{object}->base->commit; |
---|
266 | $self->{c}->model('Accounts')->sync->sync_object($self->{object}->type, $self->{object}->id); |
---|
267 | } |
---|
268 | |
---|
269 | =head1 AUTHOR |
---|
270 | |
---|
271 | Thauvin Olivier |
---|
272 | |
---|
273 | =head1 LICENSE |
---|
274 | |
---|
275 | This library is free software, you can redistribute it and/or modify |
---|
276 | it under the same terms as Perl itself. |
---|
277 | |
---|
278 | =cut |
---|
279 | |
---|
280 | 1; |
---|