CGI-Lazy

 view release on metacpan or  search on metacpan

lib/CGI/Lazy/Widget.pm  view on Meta::CPAN

	}

	delete $incoming->{CGILazyID}; #key/value pair only used at cgi level, will cause problems here (set automatically by Dataset with name of widget)

	if ($like) {
		$bind = " like ? ";

	} else {
		$bind = " = ? ";
	}

	my %likemap = (
			'%?%'	=> sub {return '%'.$_[0].'%';},
			'?%'	=> sub {return $_[0].'%';},
			'%?'	=> sub {return '%'.$_[0];},

	);

	foreach my $field (keys %$incoming) {
		unless ($field =~ /['"&;]\(\)/) {
			if ($incoming->{$field}) {
				(my $fieldname = $field) =~ s/^$widgetID-//;
				push @fields, $fieldname.$bind;
				if (ref $incoming->{$field}) {
					if ($likevars) {
						my $value = $likemap{$likevars}->(${$incoming->{$field}});
						push @$binds, $value;
					} else {
						push @$binds, ${$incoming->{$field}};
					}
				} else {
					if ($like) {
						my $value = $likemap{$like}->($incoming->{$field});
						push @$binds, $value;
					} else {
						push @$binds, $incoming->{$field};
					}
				}
			}
		}
	}
	
	my $bindstring = join ' and ', @fields;	
	
	$self->recordset->where($bindstring);

#	$self->q->util->debug->edump("bindstring: $bindstring binds: @$binds");

	my %parameters = (
			mode => 'select', 
			binds => $binds, 
			vars => $vars, 
			);

	$parameters{nodiv} = 1 unless $div; #pass the div tag if we prefer

	return $self->rawContents(%parameters);
}

#----------------------------------------------------------------------------------------
sub update {
	my $self = shift;
	my %vars = @_;

	if (ref $self eq 'CGI::Lazy::Widget::Composite') {
		foreach (@{$self->memberarray}) {
			$_->update(%vars);
		}
		return;
	}

#	$self->q->util->debug->edump('fromupdate', $self->updates, \%vars);
	$self->recordset->update($self->updates, \%vars);

	return;
}

#----------------------------------------------------------------------------------------
sub updates {
	my $self = shift;

	if (ref $self eq 'CGI::Lazy::Widget::Composite') {
		foreach (@{$self->memberarray}) {
			$_->updates;
		}
		return;
	}

        my $data;
	my $widgetID = $self->widgetID;

        foreach my $key (grep {/^$widgetID-:UPDATE:/} $self->q->param) {
                if ($key =~ /^($widgetID-:UPDATE:)(.+)-:-(.+)::(\d+)$/) {
			my ($pre, $fieldname, $ID, $row) = ($1, $2, $3, $4);
			$data->{$ID}->{$fieldname} = $self->q->param($key);# if $self->q->param($key); #if this is set, won't blank fields deliberately left blank
		} elsif ($key =~ /^($widgetID-:UPDATE:)(.+)-:-(.+)$/) {
			my ($pre, $fieldname, $ID) = ($1, $2, $3);
			$data->{$ID}->{$fieldname} = $self->q->param($key);# if $self->q->param($key);
		}
        }
#	$self->q->util->debug->edump($data);
        return $data;
}

#----------------------------------------------------------------------------------------
sub updateIds {
	my $self = shift;

	my @updates = sort keys %{$self->updates};

	if (wantarray) {
		return @updates;
	} else {
		return \@updates;
	}
}

#----------------------------------------------------------------------------------------
sub validator {
	my $self = shift;

	return $self->{_validator};
}

#----------------------------------------------------------------------------------------
sub vars {
	my $self = shift;

	return $self->{_vars};
}

#----------------------------------------------------------------------------------------
sub widgetID {
	my $self = shift;

	return $self->{_widgetID};
}

1

__END__

=head1 LEGAL

#===========================================================================

Copyright (C) 2008 by Nik Ogura. All rights reserved.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

Bug reports and comments to nik.ogura@gmail.com. 

#===========================================================================

=head1 NAME

CGI::Lazy::Widget

=head1 SYNOPSIS

	use CGI::Lazy;

	my $q = CGI::Lazy->new('/path/to/config');

	my $widget = $q->widget->dataset({...});



( run in 1.088 second using v1.01-cache-2.11-cpan-ceb78f64989 )