"Fossies" - the Fresh Open Source Software Archive

Member "metadot/metadot/Metadot/Form3000/Fields/AbstractSelectionField.pm" (27 Apr 2005, 3169 Bytes) of package /linux/www/old/Metadot6.4.5.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractSelectionField.pm" see the Fossies "Dox" file reference documentation.

    1 package Metadot::Form3000::Fields::AbstractSelectionField;
    2 
    3 use Metadot::Form3000::Fields::BaseFormField;
    4 use Metadot qw($DISPLAY);
    5 
    6 use strict;
    7 use vars qw(@ISA);
    8 
    9 @ISA = qw(Metadot::Form3000::Fields::BaseFormField);
   10 
   11 
   12 use constant MO_INSTANCE_PROPERTIES => {
   13     select_options  => undef,
   14 };
   15 
   16 
   17 # given a stored value, returns the label
   18 sub get_label_for_value {
   19     my $self  = shift;
   20     my $value = shift;
   21 
   22     my $result = undef;
   23 
   24     my $select_options = $self->select_options();
   25     if (defined $select_options) {
   26         my @options = @$select_options;
   27         while (@options) {
   28             my $option_value = shift @options;
   29             my $option_label = shift @options;
   30 
   31             if ($option_value eq $value) {
   32                 $result = $option_label;
   33                 last;
   34             }
   35         }
   36     }
   37 
   38     return $result;
   39 }
   40 
   41 
   42 sub select_options {
   43     my $self  = shift;
   44 
   45     if (@_)
   46     {
   47         my $value = shift;
   48 
   49         if (defined $value)
   50         {
   51             # unflatten the select options if they are flat
   52             if (!ref $value) {
   53                 $DISPLAY->debug_msg(5,"select_options must unflatten '$value'");
   54                 $value = $self->unflatten_select_options($value);
   55             }
   56         }
   57         $self->_select_options($value);
   58     }
   59     else
   60     {
   61         return $self->_select_options();
   62     }
   63 
   64 }
   65 
   66 
   67 # returns a true if the option_value matches the field_value.
   68 # if field_value is an array, it will scan the array
   69 sub value_is_selected
   70 {
   71     my ($class, $option_value, $field_value) = @_;
   72     my $result;
   73 
   74     if (UNIVERSAL::isa($field_value, 'ARRAY'))
   75     {
   76         $result = Utility->array_contains($option_value, $field_value);
   77     }
   78     else
   79     {
   80         $result = ($field_value eq $option_value);
   81     }
   82 
   83     $DISPLAY->debug_msg(5, "value_is_selected($option_value, $field_value) returns $result");
   84     return $result;
   85 }
   86 
   87 use constant OPTION_VALUE_SEPARATOR => ' ';
   88 sub flatten_select_options {
   89     my $self    = shift;
   90     my $options = shift || $self->select_options() || [];
   91     my $separator = shift || OPTION_VALUE_SEPARATOR();
   92 
   93     my $flat;
   94 
   95     my ($value, $label);
   96 
   97     my @option_list = @$options;
   98     while (@$options) {
   99         $value = shift @$options;
  100         $label = shift @$options;
  101 
  102         if (defined $value) {
  103             $flat .= $value . $separator . (defined $label ? $label : $value) . "\n";
  104         }
  105     }
  106     # remove trailing \n
  107     chomp($flat);
  108 
  109     return $flat;
  110 }
  111 
  112 
  113 sub unflatten_select_options {
  114     my $self  = shift;
  115     my $flat  = shift || $self->select_options();
  116     my $separator = shift || OPTION_VALUE_SEPARATOR();
  117 
  118     my @select_options = ();
  119     my @lines = split("\n", $flat);
  120     foreach my $line (@lines) {
  121         my ($value, $label) = split(/\Q$separator\E/, $line, 2);
  122 
  123         $value = Utility->trim($value);
  124         $label = Utility->trim($label);
  125 
  126         # add the option if we have something
  127         if (defined $value) {
  128 
  129            # in case we only got one value
  130             unless (defined $label) {
  131                 $label = $value;
  132             }
  133 
  134             push @select_options, ($value, $label);
  135         }
  136     }
  137 
  138     return \@select_options;
  139 }
  140 
  141 
  142 
  143 1;