lib/TWiki/Form/Checkbox.pm
changeset 0 414e01d06fd5
equal deleted inserted replaced
-1:000000000000 0:414e01d06fd5
       
     1 # See bottom of file for license and copyright details
       
     2 package TWiki::Form::Checkbox;
       
     3 use base 'TWiki::Form::ListFieldDefinition';
       
     4 
       
     5 use strict;
       
     6 
       
     7 sub new {
       
     8     my $class = shift;
       
     9     my $this = $class->SUPER::new( @_ );
       
    10     $this->{size} ||= 0;
       
    11     $this->{size} =~ s/\D//g;
       
    12     $this->{size} ||= 0;
       
    13     $this->{size} = 4 if ( $this->{size} < 1 );
       
    14 
       
    15     return $this;
       
    16 }
       
    17 
       
    18 # Checkboxes can't provide a default from the form spec
       
    19 sub getDefaultValue { undef }
       
    20 
       
    21 # Checkbox store multiple values
       
    22 sub isMultiValued { 1 }
       
    23 
       
    24 sub renderForEdit {
       
    25     my( $this, $web, $topic, $value ) = @_;
       
    26 
       
    27     my $session = $this->{session};
       
    28     my $extra = '';
       
    29     if( $this->{type} =~ m/\+buttons/ ) {
       
    30         my $boxes = scalar( @{$this->getOptions()} );
       
    31         $extra = CGI::br();
       
    32         $extra .= CGI::button
       
    33           ( -class => 'twikiCheckBox twikiEditFormCheckboxButton',
       
    34             -value => $session->i18n->maketext('Set all'),
       
    35             -onClick => 'checkAll(this,2,'.$boxes.',true)' );
       
    36         $extra .= '&nbsp;';
       
    37         $extra .= CGI::button
       
    38           ( -class => 'twikiCheckBox twikiEditFormCheckboxButton',
       
    39             -value => $session->i18n->maketext('Clear all'),
       
    40             -onClick => 'checkAll(this,1,'.$boxes.',false)');
       
    41     }
       
    42     $value = '' unless defined( $value ) && length( $value );
       
    43     my %isSelected = map { $_ => 1 } split(/\s*,\s*/, $value);
       
    44     my %attrs;
       
    45     my @defaults;
       
    46     foreach my $item ( @{$this->getOptions()} ) {
       
    47         # NOTE: Does not expand $item in label
       
    48         $attrs{$item} =
       
    49           {
       
    50               class => $this->cssClasses('twikiEditFormCheckboxField'),
       
    51               label => $session->handleCommonTags( $item, $web, $topic ),
       
    52           };
       
    53 
       
    54         if( $isSelected{$item} ) {
       
    55             $attrs{$item}{checked} = 'checked';
       
    56             push( @defaults, $item );
       
    57         }
       
    58     }
       
    59     $value = CGI::checkbox_group( -name => $this->{name},
       
    60                                   -values => $this->getOptions(),
       
    61                                   -defaults => \@defaults,
       
    62                                   -columns => $this->{size},
       
    63                                   -attributes => \%attrs );
       
    64     # Item2410: We need a dummy control to detect the case where
       
    65     #           all checkboxes have been deliberately unchecked
       
    66     # Item3061:
       
    67     # Don't use CGI, it will insert the sticky value from the query
       
    68     # once again and we need an empty field here.
       
    69     $value .= '<input type="hidden" name="'.$this->{name}.'" value="" />';
       
    70     return ( $extra, $value );
       
    71 }
       
    72 
       
    73 1;
       
    74 __DATA__
       
    75 
       
    76 Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
       
    77 
       
    78 Copyright (C) 2001-2007 TWiki Contributors. All Rights Reserved.
       
    79 TWiki Contributors are listed in the AUTHORS file in the root of
       
    80 this distribution. NOTE: Please extend that file, not this notice.
       
    81 
       
    82 This program is free software; you can redistribute it and/or
       
    83 modify it under the terms of the GNU General Public License
       
    84 as published by the Free Software Foundation; either version 2
       
    85 of the License, or (at your option) any later version. For
       
    86 more details read LICENSE in the root of this distribution.
       
    87 
       
    88 This program is distributed in the hope that it will be useful,
       
    89 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    90 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
       
    91 
       
    92 As per the GPL, removal of this notice is prohibited.
       
    93