|
|
|
|
File: [Development] / advokit-installer / Context.class.php
(download)
/
(as text)
Revision: 1.52, Thu May 18 15:27:21 2006 UTC (4 years, 3 months ago) by patdunlavey Branch: MAIN CVS Tags: HEAD Changes since 1.51: +4 -1 lines Fixed bad join when selecting the tasks when setting a teamjob - Thanks to David Donachie for this one. |
<?
# ======================================================================
# AdvoKit -- a campaign managment tool
# Copyright (C) 2004 OrchidSuites, Inc. (info@orchidsuites.net)
# Copyright (C) 2004 Dan Robinson
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the AFFERO GENERAL PUBLIC LICENSE
# as published by Affero, Inc.; either version 1
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# AFFERO GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the AFFERO GENERAL PUBLIC LICENSE
# along with this program; if not, write to Affero, Inc. at
# 510 Third Street - Suite 225, San Francisco, CA 94107, USA
# or visit <http://www.affero.org>.
# ======================================================================
# ======================================================================
# Context class. A wrapper around some other classes, plus
# a way for actions to convey information to displays.
#
# Maintains state for current campaign information.
# ======================================================================
class Context
{
# Configuration information
var $config;
# Logger for writing messages to log files
var $logger;
# Wrapper around HTTP request
var $request;
# DB access object
var $db;
# Template Engine, if configured
var $te;
# Session handler (for login sessions)
var $sesshandler;
# Current user object, possibly anonymous user
var $user;
# User state object.
var $ustate;
# Information that may be displayed to the end user.
var $displayinfo;
# Display to render, if non-NULL.
var $displayname;
# Message that should be rendered prominently in the
# display. Typically set by the action.
var $message;
# TRUE => The last action was denied
var $actiondenied = false;
# TRUE => Don't display anything, the action is
# handling the output.
var $nodisplay = false;
# The ID of the current teamjob, if any.
#
# A "teamjob" is a job within the context of a particular team.
#
# This is what most people speak of as simply a "job", but if you
# think about it, they're really talking about a "teamjob" -- a
# position in the context of a group of people (team).
#
var $teamjob_id;
# The current team.
var $team;
# The current job.
var $job;
# The contactlists for the current teamjob.
var $contactlists;
# The tasks for the current personteamjob.
var $tasks;
# The upline for the current team.
var $upline;
#.................................................. Context
#
# Constructor. Pass in all needed info.
#
function Context( &$config, &$logger, &$db, &$request, &$te, &$sesshandler, &$user, &$ustate )
{
$this->config =& $config;
$this->logger =& $logger;
$this->request =& $request;
$this->db =& $db;
$this->te =& $te;
$this->sesshandler =& $sesshandler;
$this->user =& $user;
$this->ustate =& $ustate;
$this->initContext();
}
# split this out so that we can call it from LoginAction
function initContext() {
$ustate = $this->ustate; $user = $this->user; $logger = $this->logger;
$ownjob = $this->getOwnJob();
if( ! $ownjob )
{
$this->resetOwnJob();
}
# This is somewhat inefficient, but it works.
$current_teamjob_id = $ustate->getValue( $user->getId(), 0, '', '', 'current_teamjob_id' );
$assumed_teamjob_id = $ustate->getValue( $user->getId(), 0, '', '', 'assumed_teamjob_id' );
if( $assumed_teamjob_id )
{
$this->assumeTeamJob( $assumed_teamjob_id, FALSE );
$current_job_contacts_count = $this->db->select_one( "select count(*) as count from ".W_TBL."voterteamjob where teamjob_id=".$assumed_teamjob_id );
}
elseif( $current_teamjob_id )
{
$this->setTeamJob( $current_teamjob_id, FALSE );
$current_job_contacts_count = $this->db->select_one( "select count(*) as count from ".W_TBL."voterteamjob where teamjob_id=".$current_teamjob_id );
}
$this->setJobs();
$current_team_id = $ustate->getValue( $user->getId(), 0, '', '', 'current_team_id' );
if( ! $current_team_id )
{
$this->resetCurrentTeam();
$current_team_id = $ustate->getValue( $user->getId(), 0, '', '', 'current_team_id' );
}
if( $current_team_id )
{
$this->setTeam( $current_team_id, FALSE );
}
$logger->debug( __FILE__, 'Context object created.' );
$this->te->set( 'current_job_contacts_count', $current_job_contacts_count["count"] );
# get value for simple interface flag from configuration
$use_simple_interface = $this->config->get( "use_simple_interface" );
$this->te->set( 'use_simple_interface', $use_simple_interface );
}
#.................................................. setJobs
#
# Set current user's jobs.
#
function setJobs()
{
$user_id = $this->user->getId();
# Set user's jobs. These don't depend on "current" job.
$ajobs = $this->db->select_all( "select tj.id as teamjob_id, j.name as job_name, t.name as team_name, ptj.approved as approved from ".W_TBL."personteamjob ptj left join ".W_TBL."teamjob tj on (tj.id = ptj.teamjob_id) left join ".W_TBL."job j on (j.id = tj.job_id) left join ".W_TBL."team t on (t.id = tj.team_id) where ptj.person_id = $user_id and j.leader <> 'Y' and j.teamleader <> 'Y' order by j.name, t.name " );
$this->te->set( 'user_ajobs', $ajobs );
$ljobs = $this->db->select_all( "select tj.id as teamjob_id, j.teamleader as teamleader, j.leader as leader, j.o_leader, j.c_leader, j.name as job_name, t.name as team_name, t.id as team_id, ptj.approved as approved from ".W_TBL."personteamjob ptj left join ".W_TBL."teamjob tj on (tj.id = ptj.teamjob_id) left join ".W_TBL."job j on (j.id = tj.job_id) left join ".W_TBL."team t on (t.id = tj.team_id) where ptj.person_id = $user_id and (j.leader = 'Y' or j.teamleader = 'Y') order by t.depth, j.name, t.name " );
$this->te->set( 'user_ljobs', $ljobs );
}
#.................................................. getOwnJob
#
# Get current user's ownjob.
#
function getOwnJob()
{
$ownjob = $this->ustate->getValue( $this->user->getId(), 0, '', '', 'ownjob' );
return $ownjob;
}
#.................................................. resetOwnJob
#
# Set current user's ownjob - call if there is no current ownjob.
#
function resetOwnJob()
{
$user_id = $this->user->getId();
# Set user's jobs. These don't depend on "current" job.
$ljob = $this->db->select_one( "select ptj.teamjob_id as teamjob_id from ".W_TBL."personteamjob ptj left join ".W_TBL."teamjob tj on (tj.id = ptj.teamjob_id) left join ".W_TBL."job j on (j.id = tj.job_id) left join ".W_TBL."team t on (t.id = tj.team_id) where ptj.person_id = $user_id and (j.leader = 'Y' or j.teamleader = 'Y') order by t.depth, j.name, t.name " );
if($ljob)
{
$ownjob = $ljob['teamjob_id'];
}
else
{
$ajob = $this->db->select_one( "select ptj.teamjob_id as teamjob_id from ".W_TBL."personteamjob ptj left join ".W_TBL."teamjob tj on (tj.id = ptj.teamjob_id) left join ".W_TBL."job j on (j.id = tj.job_id) left join ".W_TBL."team t on (t.id = tj.team_id) where ptj.person_id = $user_id and j.leader <> 'Y' and j.teamleader <> 'Y' order by j.name, t.name " );
if($ajob)
{
$ownjob = $ajob['teamjob_id'];
}
}
if( $ownjob )
{
$this->ustate->setValue( $user_id, 0, '', '', 'ownjob', $ownjob );
$this->ustate->setValue( $user_id, 0, '', '', 'current_teamjob_id', $ownjob );
$current_teamjob = $this->db->select_one( "select * from ".W_TBL."teamjob tj where tj.id = $ownjob " );
$current_team_id = $current_teamjob['team_id'];
$this->ustate->setValue( $user_id, 0, '', '', 'current_team_id', $current_team_id );
}
else
{ # if no ptj, delete ownjob, dump current job
$this->db->execute( "delete from ".W_TBL."ustate where person_id = $user_id and name='ownjob'" );
$this->db->execute( "delete from ".W_TBL."ustate where person_id = $user_id and name='current_teamjob_id'" );
$this->db->execute( "delete from ".W_TBL."ustate where person_id = $user_id and name='current_team_id'" );
}
#clean up invalid ustates
$this->db->execute( "delete from ".W_TBL."ustate where person_id = $user_id and name='assumed_teamjob_id'" );
$this->ustate->setValue( $user_id, 0, '', '', "lastdisplay", $this->config->get( "default_display" ) );
}
#.................................................. resetCurrentTeam
#
# Set current user's current team - call if there is no current team.
#
function resetCurrentTeam()
{
$user_id = $this->user->getId();
$ljob = $this->db->select_one( "select t.id as team_id from ".W_TBL."personteamjob ptj left join ".W_TBL."teamjob tj on (tj.id = ptj.teamjob_id) left join ".W_TBL."job j on (j.id = tj.job_id) left join ".W_TBL."team t on (t.id = tj.team_id) where ptj.person_id = $user_id and (j.leader = 'Y' or j.teamleader = 'Y') order by t.depth, j.name, t.name " );
if($ljob)
{
$currentteam = $ljob['team_id'];
}
else
{
$ajob = $this->db->select_one( "select t.id as team_id from ".W_TBL."personteamjob ptj left join ".W_TBL."teamjob tj on (tj.id = ptj.teamjob_id) left join ".W_TBL."job j on (j.id = tj.job_id) left join ".W_TBL."team t on (t.id = tj.team_id) where ptj.person_id = $user_id and j.leader <> 'Y' and j.teamleader <> 'Y' order by j.name, t.name " );
if($ajob)
{
$currentteam = $ajob['team_id'];
}
}
if( $currentteam )
{
$this->ustate->setValue( $user_id, 0, '', '', 'current_team_id' , $currentteam);
}
}
#.................................................. setTeam
#
# The user is requesting a switch of teams (nodes in
# hierarchy). Do it, if possible.
#
function setTeam( $team_id, $setustate=TRUE )
{
$team = NULL;
$user_id = $this->user->getId();
if( $team_id )
{
$assume_team = $this->db->select_one( "select t.*, t.id as team_id from ".W_TBL."team t where t.id = $team_id" );
if (!$assume_team) {
return NULL;
}
# Now find out if the user can see the team or the teamjob
# See if user is a super admin
if( IS_ADMIN && IS_TECH && IS_SPECIAL )
{
# User is a super admin
$team = $assume_team;
}
# See if the person holds a teamjob on the team in question
elseif( $this->db->select_one( "select * from ".W_TBL."teamjob tj left join ".W_TBL."personteamjob ptj on (ptj.teamjob_id = tj.id) left join ".W_TBL."person p on (p.id = ptj.person_id) where tj.team_id = $team_id and ptj.person_id = $user_id" ) )
{
# Person holds the teamjob.
$team = $assume_team;
}
# See if person is an upline leader for the team.
# Must hold a leader position upline.
elseif( IS_ADMIN && IS_TECH )
{
$parent_team = $assume_team;
$index = 0;
while( $parent_team["parent_id"] && ! $team )
{
$parent_team = $this->db->select_one( "select t.*, t.id as team_id from ".W_TBL."team t where t.id = ".$parent_team["parent_id"] );
if( $this->db->select_one( "select * from ".W_TBL."teamjob tj left join ".W_TBL."job j on (j.id = tj.job_id) left join ".W_TBL."personteamjob ptj on (ptj.teamjob_id = tj.id) left join ".W_TBL."person p on (p.id = ptj.person_id) where tj.team_id = ".$parent_team["team_id"]." and j.leader = 'Y' and ptj.person_id = $user_id" ) )
{
# Person is a leader on a team upline from the teamjob.
$team = $assume_team;
break;
}
$index++;
if( $index > 20 )
{
echo "<h1>max iterations exceeded.</h1>";
break;
}
}
}
}
if( $team )
{
# Set team info.
$this->team = $team;
$this->te->set( 'current_team', $team );
$this->te->set( 'current_team_id', $team['team_id'] );
$this->te->set( 'current_campaign_id', $team['campaign_id'] );
$this->te->set( 'current_operation_id', $team['operation_id'] );
if( $setustate )
{
$this->ustate->setValue( $user_id, 0, '', '', 'current_team_id', $team['team_id'] );
}
# Recalculate upline
$this->resetUpline();
}
return $team;
}
#.................................................. setTeamJob
#
# The user is requesting a switch of teamjobs (roles).
# Do it, if possible.
#
function setTeamJob( $teamjob_id, $setustate=TRUE )
{
$team = NULL;
$user_id = $this->user->getId();
if( $teamjob_id )
{
$assume_team = $this->db->select_one( "select tj.id as teamjob_id, tj.vfilterdesc as tjvfilterdesc, tj.vfilter as tjvfilter, tj.*, t.id as team_id, t.* from ".W_TBL."teamjob tj left join ".W_TBL."team t on (t.id = tj.team_id) where tj.id = $teamjob_id" );
# Now find out if the user can see the team or the teamjob
# See if user is a super admin
if( IS_ADMIN && IS_TECH && IS_SPECIAL )
{
# User is a super admin
$team = $assume_team;
}
# See if the person holds the teamjob in question
elseif( $this->db->select_one( "select * from ".W_TBL."teamjob tj left join ".W_TBL."personteamjob ptj on (ptj.teamjob_id = tj.id) left join ".W_TBL."person p on (p.id = ptj.person_id) where ptj.teamjob_id = $teamjob_id and ptj.person_id = $user_id and ptj.approved = 'Y'" ) )
{
# Person holds the teamjob.
$team = $assume_team;
}
# See if the person is a leader or team leader for the team containing the requested teamjob
elseif( $this->db->select_one( "select * from ".W_TBL."teamjob tj left join ".W_TBL."job j on (j.id = tj.job_id) left join ".W_TBL."personteamjob ptj on (ptj.teamjob_id = tj.id) left join ".W_TBL."person p on (p.id = ptj.person_id) where tj.team_id = ".$assume_team["team_id"]." and ( j.leader = 'Y' or j.teamleader = 'Y' ) and ptj.person_id = $user_id" ) )
{
# Person is the team leader for the team containing the requested teamjob
$team = $assume_team;
}
# See if person is an upline leader for the teamjob.
elseif( IS_ADMIN && IS_TECH )
{
$parent_team = $assume_team;
$index = 0;
while( $parent_team["parent_id"] && ! $team )
{
$parent_team = $this->db->select_one( "select t.*, t.id as team_id from ".W_TBL."team t where t.id = ".$parent_team["parent_id"] );
if( $this->db->select_one( "select * from ".W_TBL."teamjob tj left join ".W_TBL."job j on (j.id = tj.job_id) left join ".W_TBL."personteamjob ptj on (ptj.teamjob_id = tj.id) left join ".W_TBL."person p on (p.id = ptj.person_id) where tj.team_id = ".$parent_team["team_id"]." and j.leader = 'Y' and ptj.person_id = $user_id" ) )
{
# Person is a leader on a team upline from the teamjob.
$team = $assume_team;
break;
}
$index++;
if( $index > 20 )
{
echo "<h1>max iterations exceeded.</h1>";
break;
}
}
}
}
if( $team )
{
# Set team info.
$this->team = $team;
$this->te->set( 'current_team', $team );
$this->te->set( 'current_team_id', $team['team_id'] );
$this->te->set( 'current_campaign_id', $team['campaign_id'] );
$this->te->set( 'current_operation_id', $team['operation_id'] );
$this->teamjob_id = $teamjob_id;
$this->te->set( 'current_teamjob_id', $teamjob_id );
if( $setustate )
{
$this->ustate->setValue( $user_id, 0, '', '', 'current_teamjob_id', $teamjob_id );
$this->ustate->setValue( $user_id, 0, '', '', 'current_team_id', $team['team_id'] );
}
# Set job-related info.
$this->job = $this->db->select_one( "select j.*, c.name as cat_name from ".W_TBL."job j left join ".W_TBL."cat c on (j.cat_id = c.id) where j.id = ".$team['job_id'] );
$this->te->set( 'current_job', $this->job );
$this->te->set( 'current_job_id', $team['job_id'] );
# Set contact lists.
if( "Y" == $this->job["voters"] )
{
$current_job_contacts_count = $this->db->select_one( "select count(*) as count from ".W_TBL."voterteamjob where teamjob_id=".$teamjob_id );
$this->te->set( 'current_job_contacts_count', $current_job_contacts_count["count"] );
$this->contactlists = $this->db->select_all( "select * from ".W_TBL."contactlist where teamjob_id = $teamjob_id order by name" );
$this->te->set( 'current_contactlists', $this->contactlists );
if( 1 == count( $this->contactlists ) )
{
$this->te->set( 'only_contactlist_id', $this->contactlists[0]["id"] );
}
}
# Set tasks
if( $this->job["cat_id"] )
{
$sql = "select * from ".W_TBL."task t
left join ".W_TBL."team tm on (tm.id = t.team_id)
left join ".W_TBL."campaign c on (c.id = tm.campaign_id)
where tm.campaign_id = ".$team['campaign_id'];
$sql .= " and t.cat_id = ".$this->job["cat_id"];
$sql .= " and t.completedon is null ";
$sql .= " order by t.priority_id";
$this->tasks = $this->db->select_all( $sql );
$sql = "select name from ".W_TBL."cat where id = {$this->job["cat_id"]}";
//$this->logger->info(__FILE__,"task name = $sql");
$taskdef = $this->db->select_one( $sql );
$this->te->set( 'current_task', $taskdef['name'] );
$this->te->set( 'current_tasks', $this->tasks );
$this->te->set( 'current_taskscount', count( $this->tasks ) );
}
# Recalculate upline
$this->resetUpline();
}
return $team;
}
#.................................................. assumeTeamJob
#
# The user wishes to assume a job that he doesn't hold.
# Do it, if possible.
#
function assumeTeamJob( $teamjob_id, $setustate=TRUE )
{
$team = NULL;
$user_id = $this->user->getId();
if( $teamjob_id )
{
$team = $this->setTeamJob( $teamjob_id, $setustate );
}
if( $team )
{
if( ! $this->db->select_one( "select * from ".W_TBL."personteamjob ptj where ptj.teamjob_id=$teamjob_id and ptj.person_id=$user_id" ) )
{
$assumed_job = $this->db->select_one( "select count(p.id) as activist_count, tj.id as teamjob_id, j.leader as leader, j.name as job_name, t.name as team_name, p.nickname as activist_nickname, p.lastname as activist_lastname from ".W_TBL."teamjob tj left join ".W_TBL."job j on (j.id = tj.job_id) left join ".W_TBL."team t on (t.id = tj.team_id) left join ".W_TBL."personteamjob ptj on (ptj.teamjob_id = tj.id) left join ".W_TBL."person p on (ptj.person_id = p.id) where tj.id = $teamjob_id group by tj.id" );
$this->te->set( "current_assumed_job", $assumed_job );
$current_job_contacts_count = $this->db->select_one( "select count(*) as count from ".W_TBL."voterteamjob where teamjob_id=".$teamjob_id );
$this->te->set( 'current_job_contacts_count', $current_job_contacts_count["count"] );
if( $setustate )
{
$this->ustate->setValue( $user_id, 0, '', '', 'assumed_teamjob_id', $teamjob_id );
$this->ustate->setValue( $user_id, 0, '', '', 'current_teamjob_id', $teamjob_id );
}
}
else
{ # this person owns this teamjob, so set ownjob
$this->ustate->setValue( $user_id, 0, '', '', 'ownjob', $teamjob_id );
}
}
return $team;
}
#.................................................. getTeamJobId
#
# Return the current teamjob id, if any.
#
function getTeamJobId()
{
return $this->teamjob_id;
}
#.................................................. getJob
#
# Return the current job, if any.
#
function getJob()
{
return $this->job;
}
#.................................................. getJobId
#
# Return the current job id, if any.
#
function getJobId()
{
if( $this->job )
{
return $this->job['id'];
}
return NULL;
}
#.................................................. getTeam
#
# Return the current team, if any.
#
function getTeam()
{
return $this->team ;
}
#.................................................. getTeamId
#
# Return the current team id, if any.
#
function getTeamId()
{
if( $this->team )
{
return $this->team['team_id'];
}
return NULL;
}
#.................................................. getOperationId
#
# Return the current operation id, if any.
#
function getOperationId()
{
if( $this->team )
{
return $this->team['operation_id'];
}
return NULL;
}
#.................................................. getCampaignId
#
# Return the current campaign id, if any.
#
function getCampaignId()
{
if( $this->team )
{
return $this->team['campaign_id'];
}
return NULL;
}
#.................................................. getContactLists
#
# Return any contact lists associated with the current teamjob.
#
function getContactLists()
{
return $this->contactlists;
}
#.................................................. getCumulativeFilter
#
# Return the voter filter from the voter to the current team
# to any parent teams, on up.
#
function getCumulativeFilter()
{
$upline = $this->getUpline( TRUE );
$wheres = array();
# Now get any vfilters from the upline.
for( $i = 0; $i < count( $upline ); $i++ )
{
if( $upline[$i]['vfilter'] )
{
$wheres[] = '( '.$upline[$i]['vfilter'].' )';
}
}
$f = $this->getTeamJobFilter();
if( $f['vfilter'] )
{
$wheres[] = '( '.$f['vfilter'].' )';
}
return count( $wheres ) > 0 ? "(".join( " and ", $wheres ).")" : "";
}
#.................................................. getIterativeFilter
#
# Return the voter filter from the current team
# on up, in a form that can be looped through.
#
function getIterativeFilter()
{
$upline = $this->getUpline( TRUE );
$filters = array();
# Now get any vfilters from the upline.
for( $i = 0; $i < count( $upline ); $i++ )
{
if( $upline[$i]['vfilter'] )
{
$f = array();
$f["levelname"] = $upline[$i]['name'];
$f["vfilterdesc"] = $upline[$i]['vfilterdesc'];
$f["vfilter"] = $upline[$i]['vfilter'];
$filters[] = $f;
}
}
$f = $this->getTeamJobFilter();
if( $f )
{
$filters[] = $f;
}
return $filters;
}
#.................................................. getTeamJobFilter
#
# Generate filter text from the teamjob, if any.
#
function getTeamJobFilter()
{
# Get restrictions for this teamjob
if( $this->team["tjvfilter"] )
{
$f = array();
$f["levelname"] = $this->job["name"];
$f["vfilterdesc"] = $this->team["tjvfilterdesc"];
$f["vfilter"] = $this->team["tjvfilter"];
}
return $f;
}
#.................................................. resetUpline
#
# The team has probably changed, so recalculate upline.
#
function resetUpline()
{
if( $this->team )
{
$this->upline = array();
$current_team = $this->team;
# Hard limit of 10 for uplines...that's an exponent, folks.
while( $current_team['parent_id'] && $index < 10 )
{
$t = $this->db->select_one( "select t.*, t.id as team_id from ".W_TBL."team t where t.id=".$current_team["parent_id"] );
array_unshift( $this->upline, $t );
$current_team = $t;
}
}
}
#.................................................. getUpline
#
# Return the upline list for the current team.
#
# If $inclusive is FALSE, then do not include
# the current team in the list.
#
# The list always goes from "left to right", meaning
# deeper teams (nodes) are later in the array.
#
function getUpline( $inclusive=TRUE )
{
if( $inclusive )
{
$result = $this->upline;
$result[] = $this->team;
return $result;
}
return $this->upline;
}
#.................................................. isUplineLeader
#
# Return TRUE if the current user is an upline
# leader, relative to the current_team_id. To be
# an upline leader, the user must be a downline leader
# somewhere in the upline, get it?
#
function isUplineLeader()
{
$upline = $this->getUpline();
# Special case for campaign leader.
if( 1 == count( $upline ) )
{
return TRUE;
}
# General case.
$upline_team_ids = array();
for( $i = 0; $i < count( $upline ) - 1; $i++ )
{
$upline_team_ids[] = $upline[$i]["team_id"];
}
$upline_in_clause = "(".join( ",", $upline_team_ids ).")";
return $this->db->select_one( "select * from ".W_TBL."teamjob tj left join ".W_TBL."job j on (j.id = tj.job_id) left join ".W_TBL."personteamjob ptj on (ptj.teamjob_id = tj.id) where ptj.person_id = ".$this->user->getId()." and tj.team_id in $upline_in_clause and j.leader='Y' " );
}
#.................................................. nextUpline
#
# Given a team, return the next team in the upline.
#
function nextUpline( $team )
{
for( $i = 0; $i < count( $this->upline ); $i++ )
{
if( $team['parent_id'] == $this->upline[$i]['id'] )
{
return $this->upline[$i];
}
}
return NULL;
}
#.................................................. set
#
# Set the value within the displayinfo, if $key has
# an interesting value.
#
function set( $key, $value )
{
if( $key )
{
$this->displayinfo[$key] = $value;
}
}
#.................................................. get
#
# Get the value from the displayinfo, if it exists.
#
function get( $key )
{
return $this->displayinfo[$key];
}
#.................................................. getAgeClause
#
# Get the SQL age clause for display
#
function getAgeClause()
{
if ( $this->config->get( 'age_relative_to_election_date' ) )
{
$result = $this->db->select_one( "select o.election_date from ".W_TBL."operation o left join ".W_TBL."team t on (t.operation_id = o.id) and o.election_date >= now() and t.id = ".$this->getTeamId() );
if ( $result )
{
$election_date = $result['election_date'];
return "if( v.birthdate is not null and v.birthdate != '', date_format('$election_date', '%Y') - date_format(v.birthdate, '%Y') - (date_format('$election_date', '00-%m-%d') < date_format(v.birthdate, '00-%m-%d')), null) ";
}
}
return "if( v.birthdate is not null and v.birthdate != '', date_format(now(), '%Y') - date_format(v.birthdate, '%Y') - (date_format(now(), '00-%m-%d') < date_format(v.birthdate, '00-%m-%d')), null) ";
}
#.................................................. CheckPermissions
#
# Do Permissions Checking
#
function CheckPermissions($funcName)
{
$this->logger->debug( __FILE__, "CheckPermissions -- checking - $funcName" );
if ( (strcmp($funcName,"DeleteContactList") == 0) OR
(strcmp($funcName,"ContactList") == 0) )
{
$this->logger->debug( __FILE__, "CheckPermissions -- found ContactList Function" );
return $this->CheckContactListOwnership($funcName);
}
elseif ( (strcmp($funcName,"EditVoterAdvanced") == 0) or
(strcmp($funcName,"EditVoter") == 0) or
(strcmp($funcName,"EditVoterAdvancedAction") == 0) or
(strcmp($funcName,"EditVoterAction") == 0) )
{
$this->logger->debug( __FILE__, "CheckPermissions -- found EditVoter Function" );
return $this->CheckVoterOwnership($funcName);
}
$this->logger->debug( __FILE__, "CheckPermissions -- found Different Function" );
return TRUE;
}
#.................................................. CheckVoterOwnership
#
# Do Permissions Checking
#
function CheckVoterOwnership($funcName) {
$this->logger->debug( __FILE__, "CheckVoterOwnership -- checking Voter Ownership" );
$team_id = $this->getTeamId();
$user_id = $this->user->getId();
$voter_id = $this->request->get("id");
$contactList_id = $this->request->get("contactlist_id");
$this->logger->debug( __FILE__, "CheckVoterOwnership -- Team: $team_id, User: $user_id, Voter: $voter_id, ContactList: $contactList_id" );
if ( ( $contactList_id == NULL ) or
( $user_id == NULL ) or
( $voter_id == NULL ) or
( $team_id == NULL ) )
{
$this->logger->debug( __FILE__, "CheckVoterOwnership -- NULL Value" );
return FALSE;
}
if( IS_ADMIN && IS_TECH && IS_SPECIAL ) {
$this->logger->debug( __FILE__, "CheckVoterOwnership -- Special User" );
return TRUE;
}
if ( ! $this->isVoterOnList($voter_id, $contactList_id) )
{
$this->logger->debug( __FILE__, "CheckVoterOwnership -- Voter / List Mismatch" );
return FALSE;
}
if ( $this->isPersonOnTeamOfList($user_id, $contactList_id) )
{
$this->logger->debug( __FILE__, "CheckContactListOwnership -- Owned Voter" );
return TRUE;
}
if ( $this->isLeaderOfTeam( $team_id, $user_id ) )
{
$this->logger->debug( __FILE__, "CheckContactListOwnership -- Team Leader" );
return TRUE;
}
$this->logger->debug( __FILE__, "CheckContactListOwnership -- Voter not owned" );
return FALSE;
}
function isVoterOnList($voter_id, $clist_id) {
$this->logger->debug( __FILE__, "isVoterOnList -- voter: $voter_id, list: $clist_id" );
if ( $this->db->select_one( "SELECT voter_id, contactlist_id FROM ".W_TBL."voterteamjob where voter_id = $voter_id and contactlist_id = $clist_id" ) )
{
$this->logger->debug( __FILE__, "isVoterOnList -- Voter on list" );
return TRUE;
}
else
{
$this->logger->debug( __FILE__, "isVoterOnList -- Voter not on list" );
return FALSE;
}
}
function isPersonOnTeamOfList($user_id, $clist_id ) {
$this->logger->debug( __FILE__, "isPersonOnTeamOfList -- voter: $voter_id, list: $clist_id" );
if ( $this->db->select_one( "SELECT cl.id as ContactListID, cl.name as ContactListName, ptj.person_id as PersonID FROM ".W_TBL."contactlist cl, ".W_TBL."teamjob tj, ".W_TBL."personteamjob ptj Where cl.teamjob_id=tj.id and ptj.teamjob_id=tj.id and ptj.person_id = $user_id and cl.id = $clist_id" ) )
{
$this->logger->debug( __FILE__, "isPersonOnTeamOfList -- user owns list" );
return TRUE;
}
else
{
$this->logger->debug( __FILE__, "isPersonOnTeamOfList -- user does not own list" );
return FALSE;
}
}
#.................................................. CheckContactListOwnership
#
# Do Permissions Checking
#
function CheckContactListOwnership($funcName)
{
$team_id = $this->getTeamId();
$user_id = $this->user->getId();
if (strcmp($funcName,"DeleteContactList") == 0 ) {
$clist_id = $this->request->getInteger( "contactlist_id" );
} else {
$clist_id = $this->request->get( "id" );
}
$this->logger->debug( __FILE__, "CheckContactListOwnership ContactList: $clist_id, Person_id: $user_id, Team_id: $team_id" );
if ( ! $this->db->select_one( "SELECT cl.id as ContactListID FROM ".W_TBL."contactlist cl Where cl.id = $clist_id" ) )
{
$this->logger->debug( __FILE__, "CheckContactListOwnership -- non existant contact list" );
return TRUE;
}
if( IS_ADMIN && IS_TECH && IS_SPECIAL ) {
$this->logger->debug( __FILE__, "CheckContactListOwnership -- Special User" );
return TRUE;
}
# See if the person holds a teamjob on the team that owns the list
if ( $this->isPersonOnTeamOfList($user_id, $clist_id) )
{
$this->logger->debug( __FILE__, "CheckContactListOwnership -- Team member" );
return TRUE;
}
if ( IS_ADMIN && IS_TECH )
{
$this->logger->debug( __FILE__, "CheckContactListOwnership -- check tech" );
$clist_team = $this->db->select_one("SELECT tj.team_id FROM ".W_TBL."contactlist cl, ".W_TBL."teamjob tj where cl.teamjob_id = tj.id and cl.id = $clist_id");
$clist_team_id = $clist_team["team_id"];
$index = 0;
if ( $this->isLeaderOfTeam($clist_team_id, $user_id) )
{
return TRUE;
}
}
$this->logger->debug( __FILE__, "CheckContactListOwnership -- Attempted delete of non-owned contact list" );
return FALSE;
}
function isLeaderOfTeam( $team_id, $user_id )
{
if ( $this->user->isAdmin() && $this->user->isTech() && $this->user->isSpecial() )
{
return true;
}
# get the user's ownjob
$ownjob = $this->ustate->getValue( $user_id, 0, '', '', 'ownjob' );
#get corresponding team_id
$ownteamjob = $this->db->select_one( "select * from ".W_TBL."teamjob where id = $ownjob" );
$ownteam_id = $ownteamjob['team_id'];
$this->logger->debug( __FILE__, "isLeaderOfTeam: team_id is $team_id; ownteam is $ownteam_id" );
# check to see if this is a leader of this team
if ( $ownteam_id == $team_id ) { #new
return TRUE;
}
while( $team_id )
{
# now get the next team
$teaminfo = $this->db->select_one( "select depth, parent_id from ".W_TBL."team where id = $team_id" );
if ( $teaminfo["depth"] < 1 )
{
return false;
}
$team_id = $teaminfo["parent_id"];
if ( $ownteam_id == $team_id ) { #new
{
# they are a leader - but this is the parent team so they must be a downline leader
return TRUE;
}
}
# cur_leader_team may have returned false - this person could be an upline leader and have no job at this level
$index++;
if( $index > 20 )
{
echo "<h1>max iterations exceeded.</h1>";
return;
}
}
}
function daysToElection($id)
{ # $id is any valid team_id
# get days to election - may return a negative number!
$election_date = $this->db->select_one( "select o.election_date as date from ".W_TBL."operation o left join ".W_TBL."team t on (t.operation_id = o.id) where t.id = $id");
$election_date = $election_date['date'];
if($election_date)
{
$time_correction = 3600 * $this->config->get("time_correction"); #in seconds
$year = substr($election_date, 0, 4);
$month = substr($election_date, 5, 2);
$day = substr($election_date, 8, 2);
$hour = substr($election_date, 11, 2);
$this->te->set("hour", $hour);
$election_unixtime = mktime($hour, 0, 0, $month, $day, $year)/86400;
$now = (time()+ $time_correction)/86400 ;
$days_to_election = $election_unixtime - $now;
return $days_to_election;
}
}
}
?>
| cvsadmin@voter2voter.org | CVS Snapshots (updated daily) |