Revision 296 (by ahitrov, 2013/03/26 17:59:01) Promosite (anthill) project source
<pre><% Dumper(\%ARGS) %></pre>
<pre><% Dumper($voistruct) %></pre>
<%once>

  use Apache::Cookie;

</%once>
<%args>

	$vote	=> undef

</%args>
<%init>

  &abort404		unless $vote && $vote =~ /^\d+$/;
  my $cs = Apache::Cookie->parse($r->header_in('Cookie') || '');
  my $sid = ref $cs && ref $cs->{ruid} && $cs->{ruid}->value ? $cs->{ruid}->value : $r->header_in('X-Real-IP');
  my ($voting, $voice, $vstruct, $voistruct);
  my $answers = 0;
  if ( $sid ) {
	$voting = $keeper->get_document_by_id ( $vote,
			class	=> 'promosuite::Voting',
			status	=> 1,
		);
	&abort404	unless ref $voting;
	$vstruct = $voting->get_image ('voting');
	$voice = promosuite::Voice->new($keeper);
	$voice->session_id($sid);
	$voice->status(1);
	$voice->object_id($vote);

	$voistruct = {};
	while ( my ($key, $value) = each %ARGS ) {
		next	unless $key =~ /^question/;
		$answers++;
		if ( $key =~ /question(\d+)_(\d+)/ ) {
			my $qnum = $1;
			my $anum = $2;
			$voistruct->{$qnum}{$anum} = $value;
		} elsif ( $key =~ /question(\d+)/ ) {
			my $qnum = $1;
			my $anum = $value;
			if ( ref $anum eq 'ARRAY' ) {
				map { $voistruct->{$qnum}{$_} = 1 } @$anum;
			} else {
				$voistruct->{$qnum}{$anum} = 1;
			}
		}
	}
	local $Data::Dumper::Indent = 0;
	$voice->voting(Dumper($voistruct));
	my $dbh = $keeper->SQL;
	if ( $answers ) {
		my $previous_handler = $dbh->{HandleError};
		$dbh->{HandleError} = sub { return 1	if $_[0] =~ /duplicate key/; goto &$previous_handler(@_)	if $previous_handler; };
		my $sth = $dbh->prepare("INSERT INTO enquesta (class, session_id, status, object_id, voting) VALUES (?,?,?,?,?)");
		my $json = JSON::XS->new;
		$sth->execute('promosuite::Voice', $sid, 1, $vote, $json->encode($voistruct));
	}
  }
  if ( $answers ) {
	$m->redirect('./?voted=1');
  } else {
	$m->redirect('./');
  }

</%init>