]> git.decadent.org.uk Git - maypole.git/blobdiff - lib/Maypole/Model/CDBI/AsForm.pm
Changed View so it uses table name rather than class moniker in template paths.
[maypole.git] / lib / Maypole / Model / CDBI / AsForm.pm
index ef67a06e14ef4c8fd6ec0c2957da8a1e17773876..a5296cd2bbe9be9e8f8057c3d4424e2003c2b06f 100644 (file)
@@ -11,7 +11,6 @@ package Maypole::Model::CDBI::AsForm;
 #                  -- $class->to_field($has_many_col); # foreign inputs  
 #  $class->search_inputs; /
 
-use 5.006;
 
 use strict;
 use warnings;
@@ -32,17 +31,9 @@ our @EXPORT =
                _to_foreign_inputs _to_enum_select _to_bool_select
                _to_hidden _to_link_hidden _rename_foreign_input _to_readonly
                _options_from_objects _options_from_arrays _options_from_hashes 
-               _options_from_scalars _to_select_or_create
+               _options_from_array _options_from_hash _to_select_or_create
     );
                                
-our @EXPORTOK = 
-       qw( 
-               
-        
-       );
-                                 
-                                 
-
 our $VERSION = '.10'; 
 
 =head1 NAME
@@ -118,6 +109,8 @@ example usages.
 
 Not all _to_* methods pay attention to all arguments. For example, '_to_textfield' does not look in $args->{'items'} at all.
  
+=over
+
 =item name -- the name the element will have , this trumps the derived name.
 
   $beer->to_field('brewery', 'readonly', {
@@ -205,6 +198,7 @@ static values. You can also specify these in the relationship arguments.
 Tell AsForm not to make hidden inputs for relationship constraints. It does
 this  sometimes when making foreign inputs . 
 
+=back
 
 =head2 to_cgi
 
@@ -363,7 +357,12 @@ sub search_inputs {
 
 
 
-#
+
+=head2 unselect_element
+
+  unselect any selected elements in a HTML::Element select list widget
+
+=cut
 sub unselect_element {
    my ($self, $el) = @_;
    #unless (ref $el eq 'HTML::Element') {
@@ -653,7 +652,7 @@ sub _to_select {
     my ($self, $col, $args) = @_;
     $args ||= {};
 # Do we have items already ? Go no further. 
-    if ($args->{items} and @{$args->{items}}) {  
+    if ($args->{items} and ref $args->{items}) {  
                my $a = $self->_select_guts($col,  $args);
        $OLD_STYLE && return $a->as_HTML;
                if ($args->{multiple}) { $a->attr('multiple', 'multiple');}
@@ -940,13 +939,13 @@ sub _to_link_hidden {
          $name = $args->{name} || $obj->primary_column->name;
     }
     elsif ($obj = $args->{items}->[0]) {
-       # cool) 
-        $name = $args->{name} || $obj->primary_column->name; # TODO make use meta data
+        $name = $args->{name} || $accessor || $obj->primary_column->name; 
+               # TODO use meta data above maybe
     }
-       
     else {           # hiding linking related object with id in args
         $obj  = $self->related_class($r, $accessor)->retrieve($args->{id});
-        $name = $args->{name} || $obj->primary_column->name; # TODO make use meta data
+        $name = $args->{name} || $accessor ; #$obj->primary_column->name;
+               # TODO use meta data above maybe
     }
     $self->_croak("_to_link_hidden has no object") unless ref $obj;
     my $href =  $uri || $r->config->{uri_base} . "/". $obj->table."/view/".$obj->id;
@@ -957,8 +956,6 @@ sub _to_link_hidden {
     $a;
 }
 
-
-
 =head2 _to_foreign_inputs
 
 $html_els = $class_or_obj->_to_foreign_inputs($accssr, [$fields, $accssr_meta]);
@@ -1083,9 +1080,12 @@ sub _hash_selected {
 Internal api  method to make the actual select box form elements.
 
 3 types of lists making for -- 
+  Hash, Array, 
   Array of CDBI objects.
   Array of scalars , 
-  Array or  Array refs with cols from class.
+  Array or  Array refs with cols from class,
+  Array of hashes 
+
 =cut
 
 
@@ -1106,36 +1106,51 @@ sub _select_guts {
         $a->push_content($null_element);
     }
 
-    my $items = $args->{items};
-    my $proto = $items->[0];
-    my $type  = ref $proto || '';
-
-    # Objects 
-    if (not $type) {
-               $a->push_content($self->_options_from_scalars($items, $args));
-       }
-       elsif($type !~ /ARRAY|HASH/i) {
-               # make select  of objects
-               $a->push_content($self->_options_from_objects($items, $args));
-       }
-    elsif ($type =~ /ARRAY/i) {
-               $a->push_content($self->_options_from_arrays($items, $args));
+       my $items = $args->{items};
+    my $type = ref $items;
+       my $proto = eval { ref $items->[0]; } || "";
+       my $optgroups = $args->{optgroups} || '';
+       
+       # Array of hashes, one for each optgroup
+       if ($optgroups) {
+               my $i = 0;
+               foreach (@$optgroups) {
+                       my $ogrp=  HTML::Element->new('optgroup', label => $_);
+                       $ogrp->push_content($self->_options_from_hash($items->[$i], $args));
+                       $a->push_content($ogrp);
+                       $i++;
+               }
+       }               
+    # Single Hash
+    elsif ($type eq 'HASH') {
+        $a->push_content($self->_options_from_hash($items, $args));
     }
-    elsif ($type =~ /HASH/i) { 
-               $a->push_content($self->_options_from_hashes($items, $args));
+    # Single Array
+    elsif ( $type eq 'ARRAY' and not ref $items->[0] ) {
+        $a->push_content($self->_options_from_array($items, $args));
     }
-    else { 
-               die "You passed a weird type of data structure to me. Here it is: $type";
+    # Array of Objects
+    elsif( $type eq 'ARRAY' and $proto !~ /ARRAY|HASH/i ) {
+        # make select  of objects
+        $a->push_content($self->_options_from_objects($items, $args));
+    }
+    # Array of Arrays
+    elsif ( $type eq 'ARRAY' and $proto eq 'ARRAY' ) {
+        $a->push_content($self->_options_from_arrays($items, $args));
+    }
+    # Array of Hashes
+    elsif ( $type eq 'ARRAY' and $proto eq 'HASH' ) {
+        $a->push_content($self->_options_from_hashes($items, $args));
+    }
+    else {
+        die "You passed a weird type of data structure to me. Here it is: " .
+       Dumper($items );
     }
 
     return $a;
-}
-
-               
-  
-  
 
 
+}
 
 =head2 _options_from_objects ( $objects, $args);
 
@@ -1181,20 +1196,39 @@ sub _options_from_arrays {
     return @res;
 }
 
-sub _options_from_scalars {
+
+sub _options_from_array {
     my ($self, $items, $args) = @_;
-       my $selected = $args->{selected} || {};
+    my $selected = $args->{selected} || {};
     my @res;
-       for (@$items) {
-               my $opt = HTML::Element->new("option", value => $_ );
-               #$opt->attr(selected => "selected") if $selected =~/^$id$/;
-               $opt->attr(selected => "selected") if $selected->{$_};
-               $opt->push_content( $_ );
-        push @res, $opt; 
+    for (@$items) {
+        my $opt = HTML::Element->new("option", value => $_ );
+        #$opt->attr(selected => "selected") if $selected =~/^$id$/;
+        $opt->attr(selected => "selected") if $selected->{$_};
+        $opt->push_content( $_ );
+        push @res, $opt;
+    }
+    return @res;
+}
+
+sub _options_from_hash {
+    my ($self, $items, $args) = @_;
+    my $selected = $args->{selected} || {};
+    my @res;
+
+    my @values = values %$items;
+    # hash Key is the option content  and the hash value is option value
+    for (sort keys %$items) {
+        my $opt = HTML::Element->new("option", value => $items->{$_} );
+        #$opt->attr(selected => "selected") if $selected =~/^$id$/;
+        $opt->attr(selected => "selected") if $selected->{$items->{$_}};
+        $opt->push_content( $_ );
+        push @res, $opt;
     }
     return @res;
 }
 
+
 sub _options_from_hashes {
     my ($self, $items, $args) = @_;
        my $selected = $args->{selected} || {};
@@ -1206,7 +1240,7 @@ sub _options_from_hashes {
                my $val = $_->{$pk};
                my $opt = HTML::Element->new("option", value => $val );
                $opt->attr(selected => "selected") if $selected->{$val};
-               my $content = $fclass and $stringify and $fclass->can($stringify) ? 
+               my $content = ($fclass and $stringify and $fclass->can($stringify)) ? 
                              $fclass->$stringify($_) : 
                                  join(' ', @$_);
                $opt->push_content( $content );
@@ -1223,11 +1257,8 @@ sub _to_select_or_create {
        my $create = $self->to_field($col, 'foreign_inputs', $args);
        $create->{'__select_or_create__'} = 
                $self->to_field('__select_or_create__',{ name => '__select_or_create__' , value => 1 } );
-               
        return ($select, $create);
 }
-               
-       
        
 # 
 # checkboxes: if no data in hand (ie called as class method), replace
@@ -1241,7 +1272,6 @@ sub _to_select_or_create {
 sub _to_checkbox {
     my ($self, $col, $args) = @_;
     my $nullable = eval {self->column_nullable($col)} || 0; 
-    
     return $self->_to_radio($col) if !ref($self) || $nullable;
     my $value = $self->$col;
     my $a = HTML::Element->new("input", type=> "checkbox", name => $col);