<%doc>$Id: index.mhtml,v 1.62 2004/02/08 20:50:20 anoncvs Exp $</%doc>
<%args>
$mode => ''
$action => 'get'
$status => $EBLOCKS_OK
$format => 'html'
$state => ''
$volunteerState => ''
$req_chunk_size => ''
$city => ''
$gender => ''
$community => ''
$priorityMin => undef
$priorityMax => undef
$tag => ''
$controlsChanged => ''
</%args>

<%perl>
my ($user, $data) = $m->comp('init.mhtml');
my $uid = $user->getUserID();
my $people;
my $err;

if (!$mode || ($mode ne 'call' && $mode ne 'mail')) {
#     $mode = 'call';
    $mode = ($data->inStateOptimalCallTime($state) || ($data->inWeekend() && $data->inStateCallTime($state))) ? 'call' : 'mail';
}

#See how many voters they want.
our($ADDRESS_QTY,$PHONE_QTY);
our $CHUNK_SIZE;
if($user->hasPermission('eblocks_unlimited'))
{
    ## did the trusted user just change chunk size?
    my $prefChunkSize = $data->getPrefValue($uid, 'eblocks_chunk_size');
    if($req_chunk_size ne '' && $req_chunk_size ne $prefChunkSize)
    {
        $data->clearVoters($user);
        $data->setPrefValue($uid,'eblocks_chunk_size',$req_chunk_size);
	$prefChunkSize = $req_chunk_size;
    }
    ## trusted users can specify their block size.
    $ADDRESS_QTY = $prefChunkSize
                    || $data->getConfVar('eblocks_chunk_addresses')
                    || 10;
    $PHONE_QTY   = $prefChunkSize
                    || $data->getConfVar('eblocks_chunk_phone_numbers')
                    || 10;
}
else
{
    $ADDRESS_QTY = $data->getConfVar('eblocks_chunk_addresses') || 10;
    $PHONE_QTY   = $data->getConfVar('eblocks_chunk_phone_numbers') || 10;
}
my $chunk_complete = 0;
if ($mode eq $call_mode) {
    $CHUNK_SIZE = $PHONE_QTY;
} else {
    $CHUNK_SIZE = $ADDRESS_QTY;
}
if ($CHUNK_SIZE > $data->maxChunkSizeForUser($user)) {
    $CHUNK_SIZE = $data->maxChunkSizeForUser($user);
}

#See what state they want voters from.
my $states = $data->getConfVar("eblocks_states_$mode");
my $prefState = $data->getPrefValue($uid, "eblocks_state_$mode");
if (!$state || ($state ne 'MI' && $state ne 'HI' && $states !~ /,$state/)) {
    my $defaultState = $data->getConfVar("eblocks_default_state_$mode");
    if ($prefState) {
	$state = ($states =~ /,$prefState/) ? $prefState : $defaultState;
    } else {
	$state = $defaultState;
	$prefState = $defaultState;
    }
}
if ($state ne $prefState) {
    $data->setPrefValue($uid, "eblocks_state_$mode", $state);
    $prefState = $state;
}
if ($volunteerState) {
 $state = $volunteerState;
}

# See from what gender pool and city we should draw voters.
my $prefCity = $data->getPrefValue($uid, "eblocks_city_$state") || '';
if (($city || $controlsChanged) && ($city ne $prefCity)) {
    $data->setPrefValue($uid, "eblocks_city_$state", $city);
    $prefCity = $city;
    $data->clearVoters($user, '', $state);
}
$city = $prefCity;

my $prefCommunity = $data->getPrefValue($uid, "eblocks_community_$state") || '';
if (($community || $controlsChanged) && ($community ne $prefCommunity)) {
    $data->setPrefValue($uid, "eblocks_community_$state", $community);
    $prefCommunity = $community;
    $data->clearVoters($user, '');
}
$community = $prefCommunity;

my $prefGender;
if (!$gender && !$controlsChanged) {
    $gender = $data->getPrefValue($uid, 'eblocks_gender') || '';
}
if ($user->hasPermission('eblocks_unlimited')) {
    $prefGender = $data->getPrefValue($uid, "eblocks_gender") || '';
    if (($gender || $controlsChanged) && ($gender ne $prefGender)) {
        $data->setPrefValue($uid, "eblocks_gender", $gender);
	$prefGender = $gender;
        $data->clearVoters($user, '');
    }
    $gender = $prefGender;
}

my $prefPriorityMin = undef;
my $prefPriorityMax = undef;
if ($user->hasPermission('edit_user')) {
    $prefPriorityMin = $data->getPrefValue($uid, "eblocks_priority_min") || undef;
    $prefPriorityMax = $data->getPrefValue($uid, "eblocks_priority_max") || undef;
    if (($priorityMin || $controlsChanged) && ($priorityMin ne $prefPriorityMin)) {
        $data->setPrefValue($uid, "eblocks_priority_min", $priorityMin);
	$prefPriorityMin = $priorityMin;
 	$data->clearVoters($user, '');
    }
    if (($priorityMax || $controlsChanged) && ($priorityMax ne $prefPriorityMax)) {
        $data->setPrefValue($uid, "eblocks_priority_max", $priorityMax);
	$prefPriorityMax = $priorityMax;
        $data->clearVoters($user, '');
    }
}

# deal with a completed voter
our $THANKS_TXT;
if ($action eq 'setall' && $data->userCanCompleteInBulk($user, $mode)) {
    foreach my $voter (@{$data->getChunkOfVoters($mode, $CHUNK_SIZE, $user, $state, $city, $gender, $community, $priorityMin, $priorityMax)}) {
        my $err1 .= $voter->done($data, $user, $mode, "[Marked done as block]", "[Marked done as block]");
	if ($err1) {
	    $err .= $err1 . "<br>";
	}
    }
    if ($CHUNK_SIZE > 100) {
        $CHUNK_SIZE = 10;
        $data->setPrefValue($uid,'eblocks_chunk_size',$CHUNK_SIZE);
    }
    $THANKS_TXT = $data->getConfVar('thanks_for_' . $mode .'_chunk') . " " . $data->getConfVar('thanks_for_' . $mode .'_chunk_2');
} elsif ($action eq 'set' && defined($ARGS{voterkey})) {
	my $vk = $ARGS{voterkey};
	my $notes = $ARGS{notes};
	my $reaction = $ARGS{reaction};

	my $voter = $data->getVoterByKey($vk);
        $err = $voter->done($data, $user, $mode, $notes, $reaction);
	$people = $data->getChunkOfVoters($mode, $CHUNK_SIZE, $user, $state, $city, $gender, $community, $priorityMin, $priorityMax);
	if ("$err" eq "0") {
	    $chunk_complete = 1 if (scalar(@{$people}) % $CHUNK_SIZE == 0);
	    # its just coincidence that mail / call work here, this will need to be changed if it ever changes
	    if ($chunk_complete) {
		$THANKS_TXT = $data->getConfVar('thanks_for_' . $mode .'_chunk') . " " . $data->getConfVar('thanks_for_' . $mode .'_chunk_2');
	    } else {
		$THANKS_TXT = $data->getConfVar('thanks_for_' . $mode);
		$THANKS_TXT = sprintf($THANKS_TXT, scalar(@{$people}));
	    }
	}
}

our $MAIL_TOTALS = $data->getMailTotals($user->getUserID());
our $CALL_TOTALS = $data->getCallTotals($user->getUserID());

sub plural {
	my $num = shift;
	return if (! defined($num));
	return ((($num > 1) || ($num == 0)) ? 's' : '');
}

my $volunteerStatePropagate = $volunteerState ? "&volunteerState=$state" : "";

</%perl>
<%doc>Actually display something....</%doc>

<table width="100%"><tr><td align="left">
<h3>
% if ($mode eq $mail_mode) {
	<b>Write letters</b> | <a href="<% $r->uri %>?mode=<% $call_mode %><%$volunteerStatePropagate%>">Make calls</a>
% } else {
	<a href="<% $r->uri %>?mode=<% $mail_mode %><%$volunteerStatePropagate%>">Write letters</a> | <b>Make calls</b>
% }
</h3>
<td>
<div align="right">You've written <b><% $MAIL_TOTALS || 0 %></b> letter<% plural($MAIL_TOTALS) %> and made <b><% $CALL_TOTALS || 0 %></b> phone call<% plural($CALL_TOTALS) %>.<br />
%my $grandTotalMail = $data->getTotal('mail');
%my $grandTotalCall = $data->getTotal('call');
As a team we've written <b><% $grandTotalMail %></b> letters and made <b><% $grandTotalCall %></b> phone calls.<br />
You're logged in as <b><%$user->getEmail()%></b>.
</div>
</td>
</tr></table>
%if (length($states) > 3 && !$volunteerState) {
<form action="/eblocks/index.mhtml" method="post">
<input type="hidden" name="mode" value="<%$mode%>">
<input type="hidden" name="format" value="<%$format%>">
<select name="state">
%foreach my $selectState (split /,/, substr($states, 1)) {
%if ($selectState) {
%my $selected = $selectState eq $prefState ? ' selected="selected"' : "";
%my $stateData = $data->getStateData($selectState);
 <option value="<%$selectState%>"<%$selected%>><%$stateData->{name}%> (<%$stateData->{primaryDate}%>)</option>
%}
%}
</select>
<input type="submit" value="Change State"><br />

%if ($mode eq 'mail') {
%} else {
<b>Our highest priority is Tennessee.</b><br />
Maine holds its primary today.
%}

</form>

%#DEBUG: aq: <% $ADDRESS_QTY %> pq: <% $PHONE_QTY %> uid: <% $user->getUserID() %>

%}
% if (($action eq 'set' || $action eq 'setall') && $status eq $EBLOCKS_OK && !$err) {
<p><b><% $THANKS_TXT %></b></p>
% }

%#DEBUG: aq: <% $ADDRESS_QTY %> pq: <% $PHONE_QTY %> uid: <% $user->getUserID() %>

<%perl>
if (!$people) {
    $people = $data->getChunkOfVoters($mode, $CHUNK_SIZE, $user, $state, $city, $gender, $community, $priorityMin, $priorityMax);
}
if ($mode eq $call_mode) {
    $m->comp('phone.mhtml', data => $data, people => $people, err => $err, format => $format, state => $state, volunteerState => $volunteerState, user => $user);
} else {
    $m->comp('letter.mhtml', data => $data, people => $people, user => $user, format => $format, state => $state, volunteerState => $volunteerState, err => $err);
}
</%perl>
<%once>
use Clark04::Eblocks::Eblocks qw/:all/;
use Clark04::Eblocks::Data;
use Clark04::Eblocks::User;
our $call_mode = 'call';
our $mail_mode = 'mail';
</%once>

<hr>

<p><b>Customize Your Block:</b>

%my $unlimited = $user->hasPermission('eblocks_unlimited');

%if ($unlimited) {

<p>Here you can change the size of your block, choose to see only male
or only female voters, or see voters who live in a particular city.

%}

<form action="/eblocks/index.mhtml" method="post">
%$m->comp('mantainState.mhtml', mode => $mode, format => $format, state => $state, volunteerState => $volunteerState);
<input type="hidden" name="controlsChanged" value="1">
<table>
%if ($unlimited) {
<tr><td><b>Number:</b></td> <td>Show 
<select name="req_chunk_size">
% foreach my $i (@{$data->chunkSizesForUser($user)}) {
%  my $selected = ($CHUNK_SIZE == $i) ? ' selected="selected"' : "";
<option<%$selected%>><%$i%></option>
% }
</select>
<%$mode eq 'call' ? "phone numbers" : "addresses"%> at a time
</td>
</tr>
<tr><td><b>Gender:</b></td><td>Show
<select name="gender">
 <option value=""<%($prefGender eq '') ? ' selected="selected"' : ''%>>both male and female</option>
 <option value="M"<%($prefGender eq 'M') ? ' selected="selected"' : ''%>>only male</option>
 <option value="F"<%($prefGender eq 'F') ? ' selected="selected"' : ''%>>only female</option>
</select>
voters </td></tr>
%}
<tr><td valign="top"><b>City:</b></td> <td>Show voters in the city of
<input name="city" type="text" size="20" maxlength="20" value="<%$prefCity%>">, <%$state%><br />(leave blank to show voters from everywhere in the state)</td>
</tr>
%my @communities = split(/,/, $data->getConfVar("eblocks_communities_$state") || '');
%if (scalar(@communities) > 1) {
<tr><td valign="top"><b>Community:</b></td>
<td>
<select name="community">
%foreach my $community (@communities) {
 <option value="<%$community%>" <%($prefCommunity eq $community) ? ' selected="selected"' : ''%>><%$community eq '' ? 'Everyone' : $community%></option>
%}
</select>
</td>
</tr>
%if ($user->hasPermission('eblocks_unlimited') && $user->hasPermission('edit_user')) {
<tr><td valign="top"><b>Priority:</b></td> <td>
<input name="priorityMin" type="text" size="4" maxlength="4" value="<%$prefPriorityMin%>"> min, 
<input name="priorityMax" type="text" size="4" maxlength="4" value="<%$prefPriorityMax%>"> max.
</td>
</tr>
%}
%}
</table>
<input type="submit" value="Change Block">
</p>
</form>

<p><a href="/logout">Log out</a> | <a href="/eblocks/resources/letters/">About eBlock Captains</a> | <a href="http://eblockers.forclark.com/">eBlock Captains Community</a>
