1# Module of TWiki Enterprise Collaboration Platform, http://TWiki.org/
2#
3# Copyright (C) 2001-2018 Peter Thoeny, peter[at]thoeny.org
4# and TWiki Contributors. All Rights Reserved. TWiki Contributors
5# are listed in the AUTHORS file in the root of this distribution.
6# NOTE: Please extend that file, not this notice.
7#
8# Additional copyrights apply to some or all of the code in this
9# file as follows:
10#
11# This program is free software; you can redistribute it and/or
12# modify it under the terms of the GNU General Public License
13# as published by the Free Software Foundation; either version 3
14# of the License, or (at your option) any later version. For
15# more details read LICENSE in the root of this distribution.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20#
21# As per the GPL, removal of this notice is prohibited.
22
23package TWiki::Form::Radio;
24use base 'TWiki::Form::ListFieldDefinition';
25
26use strict;
27
28my @allowedAttributes = (
29    'form', 'onblur', 'onfocus', 'onchange', 'onselect', 'onmouseover', 'onmouseout',
30    'spellcheck', 'style', 'tabindex', 'title', 'translate'
31  );
32
33sub new {
34    my $class = shift;
35    my $this = $class->SUPER::new( @_ );
36    $this->{size} ||= 0;
37    $this->{size} =~ s/\D//g;
38    $this->{size} ||= 0;
39    $this->{size} = 4 if ( $this->{size} < 1 );
40
41    return $this;
42}
43
44sub renderForEdit {
45    my( $this, $web, $topic, $value ) = @_;
46
47    my $selected = '';
48    my $session = $this->{session};
49    my %attrs;
50    my $classes = $this->cssClasses('twikiRadioButton', 'twikiEditFormRadioField');
51    my $val = $this->{parameters}{class};
52    $classes .= ' ' . $val if( defined $val );
53    foreach my $item ( @{$this->getOptions()} ) {
54        $attrs{$item} =
55          {
56            class => $classes,
57            label => $session->handleCommonTags( $item, $web, $topic )
58          };
59        foreach my $attr ( @allowedAttributes ) {
60            $val = $this->{parameters}{$attr};
61            $attrs{$item}{$attr} = $val if( defined $val );
62        }
63        $selected = $item if( $item eq $value );
64    }
65
66    $value = CGI::radio_group(
67        -name => $this->{name},
68        -values => $this->getOptions(),
69        -default => $selected,
70        -columns => $this->{size},
71        -attributes => \%attrs,
72      );
73    $value = CGI::div( { class => 'twikiCheckboxGroup' }, $value );
74    return ( '', $value );
75}
76
771;
78