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

include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."_ajaxManipulators/_class/class.gridData.php");


$db->ChangeDatabase($_POST['database']);
$tableName = $_POST['RMtable'];

class projUsersManipulator extends GridDataManipulator {
	function selectAction()
	{
		
		$type = $_POST['type'];
		if($_POST['par'] != 'all')
			$this->addWhere("id_project IN ( ".$_POST['par'] .")" );
		//$this->addWhere("user_type = ".$_POST['type']);
		if($type == 2) 
			$_POST['SQL'] = "SELECT b.*,a.*,c.proj_name FROM ".DB_WARPIT_WEBCATI_BASE."._www_ids a INNER JOIN ".DB_WARPIT_WEBCATI_BASE."._Projects c ON a.id_project = c.id INNER JOIN ".DB_WARPIT_USERSPOOL."._Interviewer b ON a.id_user = b.id AND user_type = 2";
	//	$usersPool = $this->fetchSelect();
		else
			$_POST['SQL'] = "SELECT a.*,b.*,c.proj_name FROM ".DB_WARPIT_WEBCATI_BASE."._www_ids a INNER JOIN _Projects c ON a.id_project = c.id INNER JOIN (SELECT id as origId,username,password,Firstname,Surname,email,0 as id_type FROM ".DB_WARPIT_WEBCATI_BASE.".`_Interviewer` UNION SELECT id as origId,username,password,Firstname,Surname,email,1 as id_type FROM  ".DB_WARPIT_MAIN."._user) b ON a.id_user = b.origId AND a.user_type = b.id_type";
		
		$catiPool = $this->fetchSelect();
		
//		foreach ($usersPool as $user) {
//			$us['Firstname'] = $user['ime'];
//			$us['Surname'] = $user['priimek'];
//			$us['email'] = $user['email'];
//			$us['user_type'] = $user['user_type'];
//			$this->arr[] = $us;
//		}
		
		
		return parent::selectAction();
		
		
	}
	function deleteAction()
	{
		$ids = $_POST['ids'];
		$sql = "DELETE cca FROM ".DB_WARPIT_WEBCATI_BASE."._callCenter_Agents AS cca, ".DB_WARPIT_WEBCATI_BASE."._www_ids www ".
			   "WHERE www.id IN($ids) AND www.id_user=cca.IdAgent AND www.user_type=cca.UserType AND www.id_project=cca.IdProject";
		$this->db->SQLexecute($sql);	
		
		return parent::deleteAction();
//		$type = $_POST['type'];
//		$sql = "DELETE FROM $this->table WHERE id_user IN ( $ids ) AND id_project = $idProject AND user_type = $type";
//		$this->db->SQLexecute($sql);
//		return "{success:true}";
	}
	function createLinksAction()
	{
			$this->db->ChangeDatabase(DB_WEBCATI_WARPIT_BASE);
			$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE."._www_ids WHERE id_project = ".$_POST['id_project'];
			if($ids = $_POST['ids'])
				$sql .= " AND id_user IN ( $ids ) ";
				
			$res = $this->db->SQLexecute($sql);
			$dbConfig = configLoader::loadConfig('paths');
			$path_www = $dbConfig['warpit_www'].'survey/index.php';
		
			require_once PATH_TO_ROOT."_plugins/XMLwriter/ExcelWriterXML.php";
				
			//echo mb_detect_encoding($arr[0][0]);
			
			$xls =new ExcelWriterXML("export.xml");
			//$xls->setVersion(8);
			// Create worksheet
			$cart = $xls->addSheet('izvoz');
			//$cart->setInputEncoding('UTF-8');
			//$titleFormat = $xls->addStyle('lala');
			/*$titleFormat->setFontFamily('Helvetica');
			$titleFormat->setSize('13');
			$titleFormat->setColor('navy');
			$titleFormat->setBottom(2);
			$titleFormat->setBottomColor('navy');*/
			$cart->writeString(1,1,'Izvoz');
			//$cart->write(0,1,'',$titleFormat);
			//$cart->write(0,2,'',$titleFormat);
			//$cart->write(0,3,'',$titleFormat);
			//$cart->mergeCells(0,0,0,3);
			$cart->rowHeight(1,25);
			$cart->columnWidth(1,500);
			
			// An array with the data for the column headings
			$i = 1;
			while ($row = $this->db->fetchAssoc($res))
			{
				$data = $path_www."?loginSID=".$row['loginSID'];
				$cart->writeString(1+$i,1,$data);
				$i++;
			}
			
			$xls->sendHeaders();
			$xls->writeData();
	}
	
}



$manipulator = new projUsersManipulator($db,$tableName);
$comm = $_POST['action'].'Action';
if(method_exists($manipulator,$comm))
 	echo $manipulator->$comm();
else 
{
	
	Echo '{success:false,error:"Unknown action!"}';	
	
}
?>