[1329] | 1 | package LATMOS::Accounts::Web::Model::AttrFormsA; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use base 'LATMOS::Accounts::Bases::Attributes'; |
---|
| 6 | use base 'Catalyst::Model'; |
---|
| 7 | use LATMOS::Accounts::Log; |
---|
| 8 | |
---|
| 9 | =head1 NAME |
---|
| 10 | |
---|
| 11 | LATMOS::Accounts::Web::Model::AttrForms - Catalyst Model |
---|
| 12 | |
---|
| 13 | =head1 DESCRIPTION |
---|
| 14 | |
---|
| 15 | Catalyst Model. |
---|
| 16 | |
---|
| 17 | =cut |
---|
| 18 | |
---|
| 19 | sub escape { |
---|
| 20 | my ($self, $text) = @_; |
---|
| 21 | $text ||= ''; |
---|
| 22 | for ($text) { |
---|
| 23 | s/&/&/g; |
---|
| 24 | s/</</g; |
---|
| 25 | s/>/>/g; |
---|
| 26 | s/"/"/g; |
---|
| 27 | } |
---|
| 28 | $text; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | sub new { |
---|
| 32 | my ($class) = @_; |
---|
| 33 | bless({}, $class); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | # call either OBJ or type + base |
---|
| 37 | |
---|
| 38 | sub ACCEPT_CONTEXT { |
---|
| 39 | my ($self, $c, $attributes, $base_or_object, $maybe_otype) = @_; |
---|
| 40 | |
---|
| 41 | my $new = LATMOS::Accounts::Bases::Attributes->new($attributes, $base_or_object, $maybe_otype); |
---|
| 42 | $new->{c} = $c; |
---|
| 43 | $new->{htmlname} = $attributes; |
---|
| 44 | $new->{htmlname} =~ s/[\.-]/_/g; |
---|
| 45 | $new->{htmlid} = $attributes; |
---|
| 46 | $new->{htmlid} =~ s/[\.-]/_/g; |
---|
| 47 | $new->{valcount} = 0; |
---|
| 48 | $new->{hconfig} = LATMOS::Accounts::Web->config->{attrs}{$attributes} || {}; |
---|
| 49 | bless($new, __PACKAGE__); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | sub htmlname { |
---|
| 53 | $_[0]->{htmlname}; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | sub htmlid { |
---|
| 57 | my ($self) = @_; |
---|
| 58 | |
---|
| 59 | if ($self->{valcount}) { |
---|
| 60 | return sprintf('%s_%03d', $self->{htmlid}, $self->{valcount}); |
---|
| 61 | } else { |
---|
| 62 | return $_[0]->{htmlid}; |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | sub htmlnewid { |
---|
| 67 | my ($self) = @_; |
---|
| 68 | |
---|
| 69 | $self->{valcount}++; |
---|
| 70 | |
---|
| 71 | return $self->htmlid; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | sub hconfig { |
---|
| 75 | my ($self, $val) = @_; |
---|
| 76 | $self->{hconfig}{$val} |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | sub attr_label { |
---|
| 80 | my ($self, $label, $hint) = @_; |
---|
| 81 | $label ||= ''; |
---|
| 82 | |
---|
| 83 | $hint = $self->hconfig('hint'); |
---|
| 84 | |
---|
| 85 | return sprintf('<label %sfor="%s">%s</label>', |
---|
| 86 | ($hint ? sprintf('title="%s"', $self->escape($hint)) : ''), |
---|
| 87 | $self->escape($self->htmlname), |
---|
| 88 | $self->escape($self->hconfig('label') || $self->htmlname), |
---|
| 89 | ); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | sub attr_raw_value { |
---|
| 93 | my ($self) = @_; |
---|
| 94 | |
---|
[1397] | 95 | my @val = grep { $_ } (exists($self->{c}->req->params->{$self->htmlname})) |
---|
| 96 | ? ($self->{c}->req->param($self->htmlname)) |
---|
| 97 | : $self->getValues; |
---|
| 98 | |
---|
[1329] | 99 | if (@val) { |
---|
| 100 | return @val; |
---|
| 101 | } else { |
---|
| 102 | return (grep { $_ } ($self->default_value)) |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | sub attr_field_name { |
---|
| 107 | my ($self) = @_; |
---|
| 108 | |
---|
| 109 | $self->htmlname; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | sub attr_field { |
---|
| 113 | my ($self, $type) = @_; |
---|
| 114 | |
---|
| 115 | # Force Array context: |
---|
[1394] | 116 | my @attr_raw_value = $self->attr_raw_value; |
---|
| 117 | if (!$self->multiple) { |
---|
| 118 | push(@attr_raw_value, undef) unless(@attr_raw_value); |
---|
| 119 | } |
---|
[1329] | 120 | my @htmls = (); |
---|
| 121 | |
---|
| 122 | foreach my $attr_raw_value (@attr_raw_value) { |
---|
| 123 | $self->htmlnewid; |
---|
| 124 | push(@htmls, $self->attr_item($attr_raw_value)); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | if (my $js = $self->attr_js) { |
---|
| 128 | push(@htmls, |
---|
| 129 | qq[ <script> |
---|
| 130 | \$( function() { |
---|
| 131 | $js |
---|
| 132 | }); |
---|
| 133 | </script>] |
---|
| 134 | ); |
---|
| 135 | } |
---|
| 136 | |
---|
[1398] | 137 | my $addempty = ''; |
---|
[1329] | 138 | if ($self->{multiple} && !$self->readonly) { |
---|
| 139 | |
---|
| 140 | my $old = $self->{valcount} + 1; |
---|
| 141 | $self->{valcount} = 999; |
---|
| 142 | my $code = $self->attr_item(''); |
---|
| 143 | $code =~ s/'/\\'/g; |
---|
| 144 | |
---|
[1398] | 145 | $addempty = sprintf('<div id="%s_div"></div>', $self->htmlid); |
---|
[1329] | 146 | $addempty .= "\n"; |
---|
| 147 | $addempty .= sprintf(q| |
---|
| 148 | <button id="%s_add" type="button" style="display:block">+</button> |
---|
| 149 | <script type="text/javascript"> |
---|
| 150 | var cn_%s = %d; |
---|
| 151 | $( '#%s_add' ).click(function(even, ui) { |
---|
| 152 | $('#%s_div').append('%s'.replace(/%d/g, cn_%s)).append('<br>'); |
---|
| 153 | |
---|
| 154 | cn_%s++; |
---|
| 155 | %s |
---|
| 156 | }); |
---|
| 157 | </script> |
---|
| 158 | |, |
---|
| 159 | $self->htmlid, |
---|
| 160 | $self->htmlid, |
---|
| 161 | $old, |
---|
| 162 | $self->htmlid, |
---|
| 163 | $self->htmlid, |
---|
| 164 | $code, |
---|
| 165 | $self->{valcount}, |
---|
| 166 | $self->htmlid, |
---|
| 167 | $self->htmlid, |
---|
| 168 | $self->attr_js, |
---|
| 169 | ); |
---|
| 170 | |
---|
| 171 | $addempty .= "\n"; |
---|
| 172 | |
---|
| 173 | } |
---|
| 174 | |
---|
[1398] | 175 | return join("<br>\n", @htmls) . $addempty; |
---|
[1329] | 176 | } |
---|
| 177 | |
---|
| 178 | sub attr_item { |
---|
| 179 | my ($self, $value) = @_; |
---|
| 180 | |
---|
| 181 | my $html = ''; |
---|
| 182 | |
---|
| 183 | my $type = 'attr_' . uc($self->form_type); |
---|
| 184 | |
---|
| 185 | if ($self->can($type)) { |
---|
| 186 | $html .= $self->$type($value); |
---|
| 187 | } else { |
---|
| 188 | $html .= $self->attr_TEXT($value); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | $html .= q|<span class="inputvalidate" id="|. $self->htmlid . q|_check"></span>|; |
---|
| 192 | |
---|
| 193 | if (my $ref = $self->reference) { |
---|
| 194 | my $uri_part = $self->_uri_part($ref); |
---|
| 195 | if ($self->base->attribute($ref, 'displayName')) { |
---|
| 196 | my $text = ''; |
---|
| 197 | if ($value && |
---|
| 198 | (my $obj = $self->base->get_object($ref, $value))) { |
---|
| 199 | $text = $obj->get_attributes('displayName'); |
---|
| 200 | } |
---|
| 201 | #$html .= sprintf( |
---|
| 202 | #qq|<span id="${htmlid}_ref" style="margin-left: 1em">$text</span>|, |
---|
| 203 | # $self->escape($text), |
---|
| 204 | #); |
---|
| 205 | } elsif($value && $uri_part) { |
---|
| 206 | $html .= sprintf( |
---|
| 207 | '<a href="%s"><img class="attr" src="%s" title="%s"></a>', |
---|
| 208 | $self->{c}->uri_for('/', $uri_part, $value), |
---|
| 209 | $self->{c}->uri_for('/static', 'images', 'arrow-right.png'), |
---|
| 210 | $value, |
---|
| 211 | ); |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | $html |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | sub attr_js { |
---|
| 219 | my ($self, $value) = @_; |
---|
| 220 | |
---|
| 221 | my $type = 'attr_' . uc($self->form_type) . '_js'; |
---|
| 222 | |
---|
| 223 | if ($self->can($type)) { |
---|
| 224 | return $self->$type($value); |
---|
| 225 | } else { |
---|
| 226 | return ''; |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | sub attr_TEXT { |
---|
| 232 | my ($self, $value, $name, $id) = @_; |
---|
| 233 | |
---|
| 234 | return sprintf( |
---|
| 235 | '<input type="text" id="%s" name="%s" value="%s" size="%d">', |
---|
| 236 | $self->escape($id || $self->htmlid), |
---|
| 237 | $self->escape($name || $self->htmlname), |
---|
| 238 | $self->escape($value), |
---|
| 239 | $self->form_option('length') || 30, |
---|
| 240 | ); |
---|
| 241 | |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | sub attr_TEXT_js { |
---|
| 245 | my ($self) = @_; |
---|
| 246 | |
---|
| 247 | my $html = ''; |
---|
| 248 | |
---|
| 249 | if ($self->uniq) { |
---|
| 250 | $html .= sprintf(q| |
---|
| 251 | $( '[name="%s"]' ).on('input', function() { |
---|
| 252 | var id = $(this).attr('id'); |
---|
| 253 | $.ajax({ |
---|
| 254 | method: "POST", |
---|
| 255 | url: '%s', |
---|
| 256 | data: { val: $(this).val() }, |
---|
| 257 | }). done(function( msg ) { |
---|
| 258 | $( '#' + id + '_check' ).html (msg); |
---|
| 259 | }); |
---|
| 260 | return false; |
---|
| 261 | }); |
---|
| 262 | |, |
---|
| 263 | $self->htmlname, |
---|
| 264 | $self->{c}->uri_for('/ajax', 'objattrexist', $self->otype, $self->name), |
---|
| 265 | ); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | $html; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | sub attr_LABEL { |
---|
| 272 | my ($self, $value) = @_; |
---|
| 273 | return sprintf('%s <input id="%s" type=hidden name="%s" value="%s">', |
---|
| 274 | $self->escape($value), |
---|
| 275 | $self->escape($self->htmlid), |
---|
| 276 | $self->escape($self->htmlname), |
---|
| 277 | $self->escape($value), |
---|
| 278 | ); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | sub attr_TEXTAREA { |
---|
| 282 | my ($self, $value) = @_; |
---|
| 283 | return sprintf( |
---|
| 284 | '<textarea id="%s" name="%s" cols="40">%s</textarea>', |
---|
| 285 | $self->escape($self->htmlid), |
---|
| 286 | $self->escape($self->htmlname), |
---|
| 287 | $self->escape($value || ''), |
---|
| 288 | ); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | sub attr_DATE_js { |
---|
| 292 | my ($self, $value) = @_; |
---|
| 293 | return sprintf(q| |
---|
[1348] | 294 | $('[name="%s"]').datepicker( { |
---|
| 295 | changeMonth: true, |
---|
| 296 | changeYear: true, |
---|
| 297 | dateFormat: 'dd/mm/yy' |
---|
| 298 | } ); |
---|
[1329] | 299 | |, $self->escape($self->htmlname)); |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | sub attr_CHECKBOX { |
---|
| 303 | my ($self, $value) = @_; |
---|
| 304 | $value ||= ''; |
---|
| 305 | |
---|
| 306 | return sprintf('<input type="checkbox" id="%s" name="%s"%s>%s', |
---|
| 307 | $self->escape($self->htmlid), |
---|
| 308 | $self->escape($self->htmlname), |
---|
| 309 | ($value ? ' checked="yes"' : ''), |
---|
| 310 | ($self->form_option('rawvalue') ? " $value" : ''), |
---|
| 311 | ); |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | sub attr_LIST { |
---|
| 315 | my ($self, $value) = @_; |
---|
| 316 | |
---|
| 317 | my @values = $self->can_values; |
---|
[1408] | 318 | $value ||= ''; |
---|
[1329] | 319 | |
---|
| 320 | if (@values < 10) { |
---|
| 321 | return sprintf(q|<select id="%s" name="%s">%s%s</select>|, |
---|
| 322 | $self->htmlid, |
---|
| 323 | $self->htmlname, |
---|
| 324 | ($self->mandatory ? '' : '<option value="">--</option>'), |
---|
| 325 | join(",\n", map { $_ = sprintf('<option value="%s"%s>%s</option>', $_, ($_ eq $value ? ' selected="selected"' : ''), $self->display($_)) } sort $self->can_values), |
---|
| 326 | ); |
---|
| 327 | } else { |
---|
| 328 | my $html = $self->attr_TEXT($self->display($value)); |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | sub attr_LIST_js { |
---|
| 333 | my ($self) = @_; |
---|
| 334 | return sprintf(q| |
---|
| 335 | $('[name="%s"]').autocomplete({ |
---|
| 336 | delay: 200, |
---|
| 337 | minLength: 0, |
---|
| 338 | source: '%s', |
---|
| 339 | }) |
---|
| 340 | .autocomplete( "instance" )._renderItem = function( ul, item ) { |
---|
| 341 | return $( "<li>" ) |
---|
| 342 | .append( "<a>" + item.label + " (" + item.value + ")</a>" ) |
---|
| 343 | .appendTo( ul ); |
---|
| 344 | }; |
---|
| 345 | |, |
---|
| 346 | $self->htmlname, |
---|
| 347 | $self->{c}->uri_for('/json/refval', $self->otype, $self->name), |
---|
| 348 | ); |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | sub _uri_part { |
---|
| 352 | my ($self, $ref) = @_; |
---|
| 353 | my $uri_part = { |
---|
| 354 | user => 'users', |
---|
| 355 | group => 'groups', |
---|
| 356 | nethost => 'nethosts', |
---|
| 357 | netzone => 'netzones', |
---|
| 358 | site => 'sites', |
---|
| 359 | aliasess => 'aliases', |
---|
| 360 | }->{$ref}; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | sub _attr_js_item { |
---|
| 364 | my ($self, $value, $htmlid) = @_; |
---|
| 365 | |
---|
| 366 | $htmlid = $self->htmlid; |
---|
| 367 | my (@js); |
---|
| 368 | |
---|
| 369 | if (my $ref = $self->reference) { |
---|
| 370 | my $uri_part = $self->_uri_part($ref); |
---|
| 371 | if ($self->base->attribute($ref, 'displayName')) { |
---|
| 372 | push(@js, |
---|
| 373 | $self->{c}->prototype->observe_field( $htmlid, |
---|
| 374 | { |
---|
| 375 | update => "${htmlid}_ref", |
---|
| 376 | url => $self->{c}->uri_for('/ajax', 'rawattr', $ref), |
---|
| 377 | frequency => 1, |
---|
| 378 | with => "'attr=displayName&id=' + element.options[element.selectedIndex].text", |
---|
| 379 | } |
---|
| 380 | ) |
---|
| 381 | ) if ($self->form_type =~ /list/i); |
---|
| 382 | } |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | return(join("\n", @js)); |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | |
---|
| 389 | =head1 AUTHOR |
---|
| 390 | |
---|
| 391 | Thauvin Olivier |
---|
| 392 | |
---|
| 393 | =head1 LICENSE |
---|
| 394 | |
---|
| 395 | This library is free software, you can redistribute it and/or modify |
---|
| 396 | it under the same terms as Perl itself. |
---|
| 397 | |
---|
| 398 | =cut |
---|
| 399 | |
---|
| 400 | 1; |
---|