[111] | 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 = { |
---|
[206] | 18 | uid => [ 'Login', 'text:12' ], |
---|
[407] | 19 | uidNumber => [ 'UID', 'text-U:6' ], |
---|
[143] | 20 | gidNumber => [ 'GID', 'select:group:gidNumber' ], |
---|
[236] | 21 | manager => [ 'Responsable', 'select-N:user' ], |
---|
| 22 | managedBy => [ 'Responsable', 'select-N:user' ], |
---|
[143] | 23 | sn => [ 'Nom' ], |
---|
| 24 | givenName => [ 'Prénom' ], |
---|
[472] | 25 | homeDirectory => [ 'Home', 'text-U:25' ], |
---|
[272] | 26 | loginShell => [ 'Shell', 'text-A' ], |
---|
[143] | 27 | physicalDeliveryOfficeName => [ 'Bureau' ], |
---|
| 28 | telephoneNumber => [ 'Téléphone' ], |
---|
| 29 | otherTelephone => [ 'Téléphone (autre)' ], |
---|
[272] | 30 | company => [ 'Société', 'text-A' ], |
---|
[143] | 31 | l => [ 'Ville' ], |
---|
| 32 | postalCode => [ 'Code postal' ], |
---|
| 33 | postOfficeBox => [ 'BP' ], |
---|
[466] | 34 | department => [ 'Département', 'select-N:group' ], |
---|
[143] | 35 | streetAddress => [ 'Rue', 'textarea' ], |
---|
[145] | 36 | title => [ 'Fonction' ], |
---|
[160] | 37 | expire => [ 'Expire le', 'date' ], |
---|
[165] | 38 | st => [ 'Ãtat (US)' ], |
---|
[236] | 39 | sutype => [ 'Structure admin.', 'select-N:sutype' ], |
---|
[645] | 40 | exported => [ 'Propagé', 'checkbox' ], |
---|
[186] | 41 | locked => [ 'Vérouillé', 'checkbox' ], |
---|
[364] | 42 | isMainAddress => [ 'Adresse principale', 'checkbox' ], |
---|
| 43 | site => [ 'Site', 'select-N:site' ], |
---|
| 44 | co => [ 'Pays' ], |
---|
[407] | 45 | mail => [ 'Mail', 'text-U:30' ], |
---|
[431] | 46 | contratType => [ 'Type de contrat', 'text-A' ], |
---|
| 47 | grade => [ 'Grade', 'text-A' ], |
---|
[448] | 48 | facsimileTelephoneNumber => [ 'Fax', 'text' ], |
---|
[526] | 49 | nickname => [ 'Nick', 'text-U:3' ], |
---|
[633] | 50 | cn => [ 'Nom' ], |
---|
[670] | 51 | initials => [ 'Initiales', 'text-U:5' ], |
---|
[677] | 52 | create => [ 'Créer le' ], |
---|
| 53 | date => [ 'Modifié le' ], |
---|
[111] | 54 | }; |
---|
| 55 | |
---|
| 56 | my $forms = { |
---|
[117] | 57 | user => { |
---|
| 58 | name => 'SystÚme', |
---|
[111] | 59 | acl => 'admin', |
---|
[112] | 60 | attrs => [ qw( |
---|
[125] | 61 | sn givenName description |
---|
| 62 | uid uidNumber gidNumber gecos homeDirectory loginShell |
---|
[670] | 63 | mail initials nickname |
---|
[677] | 64 | locked |
---|
| 65 | create |
---|
| 66 | date |
---|
[112] | 67 | ) ], |
---|
[117] | 68 | }, |
---|
| 69 | useraddress => { |
---|
| 70 | name => 'Adresse', |
---|
| 71 | attrs => [ qw( |
---|
[129] | 72 | initials |
---|
| 73 | mail |
---|
[117] | 74 | ) ], |
---|
| 75 | }, |
---|
[129] | 76 | userstatus => { |
---|
| 77 | name => 'Status', |
---|
| 78 | attrs => [ qw( |
---|
| 79 | company |
---|
[151] | 80 | manager |
---|
[141] | 81 | department |
---|
[431] | 82 | grade |
---|
| 83 | contratType |
---|
[160] | 84 | expire |
---|
[129] | 85 | ) ], |
---|
| 86 | }, |
---|
[134] | 87 | site => { |
---|
| 88 | name => 'Site', |
---|
| 89 | attrs => [ qw( |
---|
[148] | 90 | description |
---|
[134] | 91 | streetAddress |
---|
| 92 | postOfficeBox |
---|
| 93 | postalCode |
---|
[364] | 94 | l |
---|
[160] | 95 | st |
---|
[364] | 96 | co |
---|
[448] | 97 | facsimileTelephoneNumber |
---|
[364] | 98 | ) ], |
---|
| 99 | }, |
---|
| 100 | address => { |
---|
| 101 | name => 'Adresse', |
---|
| 102 | attrs => [ qw( |
---|
| 103 | isMainAddress |
---|
| 104 | telephoneNumber |
---|
| 105 | streetAddress |
---|
| 106 | postOfficeBox |
---|
| 107 | postalCode |
---|
[134] | 108 | l |
---|
[364] | 109 | st |
---|
| 110 | physicalDeliveryOfficeName |
---|
| 111 | site |
---|
| 112 | co |
---|
[134] | 113 | ) ], |
---|
| 114 | }, |
---|
[125] | 115 | group => { |
---|
| 116 | name => 'SystÚme', |
---|
| 117 | acl => 'admin', |
---|
| 118 | attrs => [ qw( |
---|
[126] | 119 | gidNumber description |
---|
[183] | 120 | managedBy |
---|
[690] | 121 | sutype |
---|
[125] | 122 | ) ], |
---|
| 123 | }, |
---|
[111] | 124 | }; |
---|
| 125 | |
---|
[116] | 126 | sub escape { |
---|
| 127 | my ($self, $text) = @_; |
---|
[117] | 128 | $text ||= ''; |
---|
[116] | 129 | for ($text) { |
---|
| 130 | s/&/&/g; |
---|
| 131 | s/</</g; |
---|
| 132 | s/>/>/g; |
---|
| 133 | s/"/"/g; |
---|
| 134 | } |
---|
| 135 | $text; |
---|
| 136 | } |
---|
[111] | 137 | |
---|
| 138 | sub new { |
---|
| 139 | my ($class) = @_; |
---|
| 140 | bless({}, $class); |
---|
| 141 | } |
---|
| 142 | |
---|
[144] | 143 | # call either OBJ or type + base |
---|
| 144 | |
---|
[111] | 145 | sub ACCEPT_CONTEXT { |
---|
[144] | 146 | my ($self, $c, $form, $object, $base) = @_; |
---|
| 147 | my $new = {}; |
---|
| 148 | $new->{c} = $c; |
---|
| 149 | $new->{form} = $form; |
---|
| 150 | $new->{object} = $object if (ref $object); |
---|
| 151 | $new->{base} = $base || ($object ? $object->base : undef) or return $self; |
---|
| 152 | $new->{otype} = ref $object ? $object->type : $object; |
---|
| 153 | bless($new, 'LATMOS::Accounts::Web::Model::AttrForms'); |
---|
[111] | 154 | } |
---|
| 155 | |
---|
[144] | 156 | sub base { |
---|
| 157 | my ( $self ) = @_; |
---|
| 158 | $self->{base} |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | sub otype { |
---|
| 162 | my ($self) = @_; |
---|
| 163 | $self->{otype}; |
---|
| 164 | } |
---|
| 165 | |
---|
[117] | 166 | sub label { |
---|
| 167 | my ($self) = @_; |
---|
| 168 | $forms->{$self->{form}}->{name} || '' |
---|
| 169 | } |
---|
| 170 | |
---|
[111] | 171 | sub attributes { |
---|
[373] | 172 | my ($self, $for) = @_; |
---|
[507] | 173 | grep { $_ } |
---|
[475] | 174 | grep { $self->base->check_acl($self->{object} || $self->otype, $_, 'r') } |
---|
[373] | 175 | grep { $self->base->get_field_name($self->otype, $_, $for || 'a') } |
---|
[143] | 176 | @{ $forms->{$self->{form}}->{attrs} }; |
---|
[111] | 177 | } |
---|
| 178 | |
---|
| 179 | sub attr_label { |
---|
| 180 | my ($self, $attr) = @_; |
---|
[272] | 181 | my $htmlname = ($self->{object} ? $self->{object}->id . '_' : '') . $attr; |
---|
| 182 | return sprintf('<label for="%s">%s</label>', |
---|
| 183 | $self->escape($htmlname), |
---|
| 184 | $self->escape($attrs->{$attr}[0] || $attr) |
---|
| 185 | ); |
---|
[111] | 186 | } |
---|
| 187 | |
---|
[144] | 188 | sub attr_raw_value { |
---|
| 189 | my ($self, $attr) = @_; |
---|
| 190 | return $self->{c}->req->param($attr) || |
---|
| 191 | ($self->{object} ? $self->{object}->get_c_field($attr) : '') |
---|
| 192 | } |
---|
| 193 | |
---|
[111] | 194 | sub attr_field { |
---|
[143] | 195 | my ($self, $attr, $type) = @_; |
---|
[491] | 196 | my $modallow = $self->base->check_acl($self->{object} |
---|
| 197 | ? ($self->{object}, $attr, 'w') |
---|
| 198 | : ($self->otype, '@CREATE', 'w')); |
---|
| 199 | $type ||= $self->base->get_field_name($self->otype, $attr, 'w') && $modallow |
---|
[143] | 200 | ? $attrs->{$attr}[1] || '' |
---|
| 201 | : 'label'; |
---|
| 202 | # exception: gidNumber is used also in group, but we don't want |
---|
| 203 | # group list here, really the number ! |
---|
[491] | 204 | $type = $modallow ? 'text-U:6' : 'label' if (($self->{form} || '') =~ /^group/ && $attr eq 'gidNumber'); |
---|
[206] | 205 | $type ||= 'text'; |
---|
[407] | 206 | my $htmlname = $self->escape(($self->{object} ? $self->{object}->id . '_' : |
---|
| 207 | '') . $attr); |
---|
[143] | 208 | for ($type) { |
---|
| 209 | /^textarea$/ and return sprintf( |
---|
[272] | 210 | '<textarea id="%s" name="%s" cols="40">%s</textarea>', |
---|
[236] | 211 | $self->escape($htmlname), |
---|
[272] | 212 | $self->escape($htmlname), |
---|
[144] | 213 | $self->escape($self->attr_raw_value($attr)), |
---|
[143] | 214 | ); |
---|
[364] | 215 | /^label$/ and do { |
---|
| 216 | my $field = $self->escape( |
---|
| 217 | $self->attr_raw_value($attr) |
---|
| 218 | ); |
---|
| 219 | $field =~ s/\n/<br>/g; |
---|
| 220 | return $field; |
---|
| 221 | }; |
---|
[160] | 222 | /^date$/ and do { |
---|
[170] | 223 | my ($date, $time) = split(/ /, $self->attr_raw_value($attr) || ''); |
---|
[168] | 224 | if ($date && $date =~ /^(\d+)-(\d+)-(\d+)$/) { |
---|
[160] | 225 | $date = "$3/$2/$1"; |
---|
| 226 | } |
---|
| 227 | my $html = "\n" . q{<SCRIPT LANGUAGE="JavaScript" ID="js13"> |
---|
| 228 | var cal13 = new CalendarPopup(); |
---|
| 229 | </SCRIPT>} . "\n"; |
---|
| 230 | $html .= sprintf( |
---|
[272] | 231 | '<input type="text" id="%s" name="%s" value="%s" size="12">', |
---|
| 232 | $self->escape($htmlname), |
---|
| 233 | $self->escape($htmlname), |
---|
[160] | 234 | $self->escape($date) |
---|
| 235 | ); |
---|
| 236 | $html .= q{<DIV ID="testdiv1" STYLE="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></DIV>}; |
---|
[362] | 237 | $html .= qq| |
---|
[160] | 238 | <A HREF="#" |
---|
[272] | 239 | onClick="cal13.select(document.forms[0].$htmlname,'${htmlname}_anc','dd/MM/yyyy');return false;" |
---|
| 240 | TITLE="cal13.select(document.forms[0].$htmlname,'${htmlname}_anc','dd/MM/yyyy');return false;" |
---|
| 241 | NAME="${htmlname}_anc" ID="${htmlname}_anc"> |
---|
[362] | 242 | <img src="| . $self->{c}->uri_for(qw(/static icons view-calendar-day.png)) |
---|
| 243 | . qq{" style="ref"></A> |
---|
| 244 | } . "\n"; |
---|
[160] | 245 | return $html; |
---|
| 246 | }; |
---|
[167] | 247 | /^checkbox$/ and do { |
---|
| 248 | return sprintf('<input type="checkbox" name="%s"%s>', |
---|
[236] | 249 | $self->escape($htmlname), |
---|
[167] | 250 | $self->attr_raw_value($attr) ? ' checked="yes"' : '' |
---|
[362] | 251 | ) . sprintf('<input type="hidden" name="%s">', |
---|
| 252 | $self->escape($htmlname)); |
---|
[167] | 253 | }; |
---|
[151] | 254 | /^select(-\w+)?:([^:]+)(?::(.*))?$/ and do { |
---|
| 255 | my $options = $1 || ''; |
---|
| 256 | my $otype = $2; |
---|
| 257 | my $keyfield = $3; |
---|
[362] | 258 | my $observe_keyfield = $keyfield || 'displayName'; |
---|
[272] | 259 | my $select = sprintf('<select id="%s" name="%s">', |
---|
| 260 | $self->escape($htmlname), |
---|
[236] | 261 | $self->escape($htmlname)) . "\n"; |
---|
[151] | 262 | $select .= '<option value="">--</option>' . "\n" if ($options =~ /N/); |
---|
| 263 | my $value = $self->attr_raw_value($attr) || ''; |
---|
[362] | 264 | my $initial_observed = ''; |
---|
[145] | 265 | foreach my $id ($self->base->list_objects($otype)) { |
---|
| 266 | my $obj = $self->base->get_object($otype, $id) or next; |
---|
[143] | 267 | my $val = $keyfield ? $obj->get_c_field($keyfield) : $id; |
---|
| 268 | $select .= sprintf( |
---|
[652] | 269 | ' <option value="%s"%s>%s</option>', |
---|
[475] | 270 | $self->escape($val || ''), |
---|
[630] | 271 | $value eq $val ? ' selected="selected"' : '', |
---|
[475] | 272 | $self->escape($id || ''), |
---|
[143] | 273 | ); |
---|
| 274 | $select .= "\n"; |
---|
[364] | 275 | $initial_observed = $obj->get_c_field($observe_keyfield) || '' |
---|
[362] | 276 | if($value eq $val); |
---|
[143] | 277 | } |
---|
| 278 | $select .= "</select>\n"; |
---|
[272] | 279 | $select .= $self->{c}->prototype->observe_field( $htmlname, { |
---|
| 280 | update => "${htmlname}_span", |
---|
| 281 | url => $self->{c}->uri_for('/ajax', 'rawattr', $otype), |
---|
| 282 | frequency => 1, |
---|
[362] | 283 | with => "'attr=" . $observe_keyfield . |
---|
| 284 | "&id='+element.options[element.selectedIndex].text", |
---|
[272] | 285 | }) . |
---|
[362] | 286 | qq|<span id="${htmlname}_span">$initial_observed</span>|; |
---|
[143] | 287 | return $select; |
---|
| 288 | }; |
---|
[272] | 289 | /^text(-\w+)?(?::(\d+))?/ and do { |
---|
| 290 | my $flag = $1 || ''; |
---|
| 291 | my $textf = sprintf( |
---|
| 292 | '<input type="text" id="%s" name="%s" value="%s" size="%d">', |
---|
[236] | 293 | $self->escape($htmlname), |
---|
[272] | 294 | $self->escape($htmlname), |
---|
[206] | 295 | $self->escape($self->attr_raw_value($attr)), |
---|
[272] | 296 | $2 || 30, |
---|
[206] | 297 | ); |
---|
[272] | 298 | if ($flag =~ /A/) { |
---|
[407] | 299 | $textf .= qq|<span id="${htmlname}_auto_complete"></span>|; |
---|
[272] | 300 | $textf .= "\n"; |
---|
| 301 | $textf .= $self->{c}->prototype->auto_complete_field( |
---|
| 302 | $htmlname, |
---|
[407] | 303 | { |
---|
| 304 | update => "${htmlname}_auto_complete", |
---|
[272] | 305 | url => $self->{c}->uri_for('/ajax', 'attrvalues', $self->otype, $attr), |
---|
| 306 | indicator => "${htmlname}_stat", min_chars => 1, |
---|
| 307 | with => "'val='+document.getElementById('$htmlname').value", |
---|
[407] | 308 | frequency => 2, |
---|
[272] | 309 | } |
---|
[407] | 310 | ); |
---|
[272] | 311 | } |
---|
[407] | 312 | if ($flag =~ /U/) { |
---|
| 313 | $textf .= qq|<span id="${htmlname}_observer_uniq"></span>|; |
---|
| 314 | $textf .= "\n"; |
---|
| 315 | $textf .= $self->{c}->prototype->observe_field( |
---|
| 316 | $htmlname, |
---|
| 317 | { |
---|
| 318 | update => "${htmlname}_observer_uniq", |
---|
| 319 | url => $self->{c}->uri_for('/ajax', 'objattrexist', |
---|
| 320 | $self->otype, $attr), |
---|
| 321 | frequency => 2, |
---|
| 322 | indicator => "${htmlname}_stat", min_chars => 1, |
---|
| 323 | with => "'val='+document.getElementById('$htmlname').value" . |
---|
| 324 | ($self->{object} ? "+'&exclude=" . $self->{object}->id . "'" : |
---|
| 325 | ''), |
---|
| 326 | } |
---|
| 327 | ); |
---|
| 328 | } |
---|
| 329 | $textf .= qq|<span style="display:none" id="${htmlname}_stat">Searching...</span>|; |
---|
| 330 | |
---|
[272] | 331 | return $textf; |
---|
[206] | 332 | }; |
---|
[143] | 333 | } |
---|
[111] | 334 | } |
---|
| 335 | |
---|
[174] | 336 | sub submit { |
---|
| 337 | my ($self) = @_; |
---|
| 338 | return sprintf( |
---|
[686] | 339 | '<input type="submit" name="%s" value="Enregistrer">', |
---|
[174] | 340 | $self->escape($self->label), |
---|
| 341 | ); |
---|
| 342 | } |
---|
| 343 | |
---|
[112] | 344 | sub set_attrs { |
---|
| 345 | my ($self) = @_; |
---|
[174] | 346 | $self->{c}->req->param($self->label) or return; |
---|
[236] | 347 | my $prefix = $self->{object}->id . '_'; |
---|
[112] | 348 | $self->{object}->set_c_fields( |
---|
| 349 | map { |
---|
[169] | 350 | $_ => |
---|
[170] | 351 | ($attrs->{$_}[1] || '') eq 'checkbox' |
---|
[236] | 352 | ? ($self->{c}->req->param("$prefix$_") ? 1 : 0) |
---|
| 353 | : ($self->{c}->req->param("$prefix$_") || undef) |
---|
[362] | 354 | } grep { $self->base->get_field_name($self->otype, $_, 'w') } $self->attributes |
---|
[180] | 355 | ) or return; |
---|
[112] | 356 | $self->{object}->base->commit; |
---|
| 357 | } |
---|
| 358 | |
---|
[111] | 359 | =head1 AUTHOR |
---|
| 360 | |
---|
| 361 | Thauvin Olivier |
---|
| 362 | |
---|
| 363 | =head1 LICENSE |
---|
| 364 | |
---|
| 365 | This library is free software, you can redistribute it and/or modify |
---|
| 366 | it under the same terms as Perl itself. |
---|
| 367 | |
---|
| 368 | =cut |
---|
| 369 | |
---|
| 370 | 1; |
---|