<?php
include_once ("_dirinfo.php");

include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."webcati/_class/class.projTreeData.php");


class mailWizManipulator extends projectTreeManipulator
{
	
	function selectAction()
	{
		//let's fetch the projects that this group has already send to!
		if($_POST['node'] == 0)
		{
		$this->db->ChangeDatabase(DB_WARPIT_USERSPOOL);
		
			if($_POST['id_group'] && $_POST['showRedirect'] != 'true')
			{
				$sql = "SELECT DISTINCT id_project FROM _SendMailSet WHERE id_project > 0 AND id_project IN (SELECT id FROM ".DB_WARPIT_WEBCATI_BASE."._Projects) AND project_type = 1  AND id_group = ".$_POST['id_group'];
				$res = $this->db->SQLexecute($sql);
				while ($row = $this->db->fetchRow($res))
				{
					$projs[] = $row[0];
				}
			}
			$this->db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);	
			if($projs)
			{
				$curNode['text'] = 'Last sent';
				$curNode['leaf'] = false;
				$curNode['expanded'] = true;
				
				$sql = $this->db->SQLexecute("SELECT * FROM _Projects WHERE id IN(".implode(',',$projs).')' );
				while ($row = $this->db->fetchAssoc($sql))
				{
					$node = array();
					$this->projIgnore[] = $row['id'];
					$node['text'] =	$row['proj_name'];
					$node['id'] = 'lastSent_'.$row['id'];
					$node['id_project'] = $row['id'];
					$node['project_type'] = 1;
					$node['leaf'] = true;
					$node['sendAlready'] = true;
					$curNode['children'][] = $node;
				}
				//safety pin...if no children are selected this becomes an infinite loop!
				if(!$curNode['children']) $curNode['children'][] = null;
			
				$this->nodes[] = $curNode;
			}
			
		$this->db->ChangeDatabase(DB_WARPIT_USERSPOOL);
			unset($projs);
			unset($curNode);
			if($_POST['id_group'] && $_POST['showAll'] != 'true')
			{
				$sql = "SELECT DISTINCT id_project FROM _SendMailSet WHERE id_project > 0 AND id_project IN (SELECT id FROM ".DB_WARPIT_WEBCATI_BASE."._Projects) AND project_type = 2  AND id_group = ".$_POST['id_group'];
				$res = $this->db->SQLexecute($sql);
				while ($row = $this->db->fetchRow($res))
				{
					$projs[] = $row[0];
				}
			}
			if($projs)
			{
				$curNode['text'] = 'Last redirect sent';
				$curNode['leaf'] = false;
				$curNode['expanded'] = true;
				
				$sql = $this->db->SQLexecute("SELECT * FROM _RedirectProjects WHERE id IN(".implode(',',$projs).')' );
				while ($row = $this->db->fetchAssoc($sql))
				{
					$node = array();
					$this->projIgnore[] = $row['id'];
					$node['text'] =	$row['idLab'];
					$node['id'] = 'lastRedSent_'.$row['id'];
					$node['id_project'] = $row['id'];
					$node['project_type'] = 2;
					$node['leaf'] = true;
					$node['sendAlready'] = true;
					$curNode['children'][] = $node;
				}
				//safety pin...if no children are selected this becomes an infinite loop!
				if(!$curNode['children']) $curNode['children'][] = null;
			
				$this->nodes[] = $curNode;
			}
			$this->db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);	
			
		}
		if($_POST['showAll'] == 'true')
			return parent::selectAction();
		else if($_POST['showRedirect'] == 'true')
		{
			$this->db->ChangeDatabase(DB_WARPIT_USERSPOOL);
			$this->table = "_RedirectProjects";
			$_POST['SQL'] = "SELECT *,id as id_project FROM $this->table";
			return parent::selectAction();
		}

		return json_encode($this->nodes);
	}
	public function getStatusAction()
	{
		$id_group = $_POST['id_group'];
		$id_project = $_POST['id_project'];
		//all
		$this->db->ChangeDatabase(DB_WARPIT_USERSPOOL);
		$res = $this->db->SQLexecute("SELECT * FROM _group_link a INNER JOIN _Interviewer b ON b.id = a.id WHERE id_group = $id_group");
		$all = $this->db->NumRows($res);
		$unfin = 0;
		$fin = 0;
		
		$this->db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
		$res = $this->db->SQLexecute("SELECT count(*),qStatus FROM _www_interviewer_statistics INNER JOIN ".DB_WARPIT_USERSPOOL."._Interviewer b ON id_user = b.id INNER JOIN ".DB_WARPIT_USERSPOOL."._group_link c ON b.id = c.id WHERE id_group = $id_group AND id_project = $id_project GROUP BY qStatus ");
		while ($row = $this->db->fetchRow($res))
		{
			if($row[1] == 99)
				$unfin = $row[0];
			else 
				$fin = $row[0];
		}
		$rem = $all-$unfin-$fin;
		
		$data[] = array(1,"All ($all)");
		$data[] = array(2,"Reminder ($rem)");
		$data[] = array(3,"Unfinished ($unfin)");
		$data[] = array(4,"Finished ($fin)");
		
		
		return json_encode($data);
	}
	

}



$db->ChangeDatabase($_REQUEST['database']);

$manipulator = new mailWizManipulator($db,$_REQUEST['RMtable']);

$comm = $_REQUEST['action'].'Action';
if(method_exists($manipulator,$comm))
	echo $manipulator->$comm();
else 
{
	
	echo '{success:false,error:"Unknown action!"}';	
	
}


?>
