lib/TWiki/Form/Select.pm
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
permissions -rw-r--r--
RELEASE 4.2.0 freetown
colas@0
     1
# See bottom of file for license and copyright details
colas@0
     2
package TWiki::Form::Select;
colas@0
     3
use base 'TWiki::Form::ListFieldDefinition';
colas@0
     4
colas@0
     5
use strict;
colas@0
     6
colas@0
     7
sub new {
colas@0
     8
    my $class = shift;
colas@0
     9
    my $this = $class->SUPER::new( @_ );
colas@0
    10
colas@0
    11
    # Parse the size to get min and max
colas@0
    12
    $this->{size} ||= 1;
colas@0
    13
    if( $this->{size} =~ /^\s*(\d+)\.\.(\d+)\s*$/ ) {
colas@0
    14
        $this->{minSize} = $1;
colas@0
    15
        $this->{maxSize} = $2;
colas@0
    16
    } else {
colas@0
    17
        $this->{minSize} = $this->{size};
colas@0
    18
        $this->{minSize} =~ s/[^\d]//g;
colas@0
    19
        $this->{minSize} ||= 1;
colas@0
    20
        $this->{maxSize} = $this->{minSize};
colas@0
    21
    }
colas@0
    22
colas@0
    23
    return $this;
colas@0
    24
}
colas@0
    25
colas@0
    26
colas@0
    27
=pod
colas@0
    28
colas@0
    29
---++ getDefaultValue() -> $value
colas@0
    30
The default for a select is always the empty string, as there is no way in
colas@0
    31
TWiki form definitions to indicate selected values. This defers the decision
colas@0
    32
on a value to the browser.
colas@0
    33
colas@0
    34
=cut
colas@0
    35
colas@0
    36
sub getDefaultValue {
colas@0
    37
    return '';
colas@0
    38
}
colas@0
    39
colas@0
    40
sub getOptions {
colas@0
    41
    my $this = shift;
colas@0
    42
colas@0
    43
    return $this->{_options} if $this->{_options};
colas@0
    44
colas@0
    45
    my $vals = $this->SUPER::getOptions(@_);
colas@0
    46
    if ($this->{type} =~ /\+values/) {
colas@0
    47
        # create a values map
colas@0
    48
colas@0
    49
        $this->{valueMap} = ();
colas@0
    50
        $this->{_options} = ();
colas@0
    51
        my $str;
colas@0
    52
        foreach my $val (@$vals) {
colas@0
    53
            if ($val =~ /^(.*?[^\\])=(.*)$/) {
colas@0
    54
                $str = $1;
colas@0
    55
                $val = $2;
colas@0
    56
                $str =~ s/\\=/=/g;
colas@0
    57
            } else {
colas@0
    58
                $str = $val;
colas@0
    59
            }
colas@0
    60
            $this->{valueMap}{$val} = TWiki::urlDecode($str);
colas@0
    61
            push @{$this->{_options}}, $val;
colas@0
    62
        }
colas@0
    63
    }
colas@0
    64
colas@0
    65
    return $vals;
colas@0
    66
}
colas@0
    67
colas@0
    68
=begin twiki
colas@0
    69
colas@0
    70
---++ ObjectMethod finish()
colas@0
    71
Break circular references.
colas@0
    72
colas@0
    73
=cut
colas@0
    74
colas@0
    75
# Note to developers; please undef *all* fields in the object explicitly,
colas@0
    76
# whether they are references or not. That way this method is "golden
colas@0
    77
# documentation" of the live fields in the object.
colas@0
    78
sub finish {
colas@0
    79
    my $this = shift;
colas@0
    80
    $this->SUPER::finish();
colas@0
    81
    undef $this->{minSize};
colas@0
    82
    undef $this->{maxSize};
colas@0
    83
    undef $this->{valueMap}
colas@0
    84
}
colas@0
    85
colas@0
    86
sub isMultiValued { return shift->{type} =~ /\+multi/; }
colas@0
    87
colas@0
    88
sub renderForEdit {
colas@0
    89
    my( $this, $web, $topic, $value ) = @_;
colas@0
    90
colas@0
    91
    my $choices = '';
colas@0
    92
colas@0
    93
    my %isSelected = map { $_ => 1 } split(/\s*,\s*/, $value);
colas@0
    94
    foreach my $option ( @{$this->getOptions()} ) {
colas@0
    95
        my %params = (
colas@0
    96
            class => 'twikiEditFormOption',
colas@0
    97
           );
colas@0
    98
        $params{selected} = 'selected' if $isSelected{$option};
colas@0
    99
        if (defined($this->{valueMap}{$option})) {
colas@0
   100
            $params{value} = $option;
colas@0
   101
            $option = $this->{valueMap}{$option};
colas@0
   102
        }
colas@0
   103
        $option =~ s/<nop/&lt\;nop/go;
colas@0
   104
        $choices .= CGI::option( \%params, $option );
colas@0
   105
    }
colas@0
   106
    my $size = scalar( @{$this->getOptions()} );
colas@0
   107
    if( $size > $this->{maxSize} ) {
colas@0
   108
        $size = $this->{maxSize};
colas@0
   109
    } elsif( $size < $this->{minSize} ) {
colas@0
   110
        $size = $this->{minSize};
colas@0
   111
    }
colas@0
   112
    my $params = {
colas@0
   113
        class => $this->cssClasses('twikiSelect', 'twikiEditFormSelect'),
colas@0
   114
        name => $this->{name},
colas@0
   115
        size => $this->{size},
colas@0
   116
    };
colas@0
   117
    if( $this->isMultiValued() ) {
colas@0
   118
        $params->{'multiple'}='on';
colas@0
   119
        $value  = CGI::Select( $params, $choices );
colas@0
   120
        # Item2410: We need a dummy control to detect the case where
colas@0
   121
        #           all checkboxes have been deliberately unchecked
colas@0
   122
        # Item3061:
colas@0
   123
        # Don't use CGI, it will insert the value from the query
colas@0
   124
        # once again and we need an empt field here.
colas@0
   125
        $value .= '<input type="hidden" name="'.$this->{name}.'" value="" />';
colas@0
   126
    }
colas@0
   127
    else {
colas@0
   128
        $value  = CGI::Select( $params, $choices );
colas@0
   129
    }
colas@0
   130
    return ( '', $value );
colas@0
   131
}
colas@0
   132
colas@0
   133
1;
colas@0
   134
__DATA__
colas@0
   135
colas@0
   136
Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
colas@0
   137
colas@0
   138
Copyright (C) 2001-2007 TWiki Contributors. All Rights Reserved.
colas@0
   139
TWiki Contributors are listed in the AUTHORS file in the root of
colas@0
   140
this distribution. NOTE: Please extend that file, not this notice.
colas@0
   141
colas@0
   142
This program is free software; you can redistribute it and/or
colas@0
   143
modify it under the terms of the GNU General Public License
colas@0
   144
as published by the Free Software Foundation; either version 2
colas@0
   145
of the License, or (at your option) any later version. For
colas@0
   146
more details read LICENSE in the root of this distribution.
colas@0
   147
colas@0
   148
This program is distributed in the hope that it will be useful,
colas@0
   149
but WITHOUT ANY WARRANTY; without even the implied warranty of
colas@0
   150
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
colas@0
   151
colas@0
   152
As per the GPL, removal of this notice is prohibited.
colas@0
   153