<?php

include_once ("_dirinfo.php");

require_once (PATH_TO_ROOT."CRM/_lib/excel/PHPExcel.php");
include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."config.php");
include_once (PATH_TO_ROOT."CRM/class/class.checkString.php");

include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
include_once(PATH_TO_ROOT."CRM/class/class.Language-Pack.php");
$LangPack = new LanguagePack($db, $aLanguage);
ini_set("display_errors",1);

$dan = isset($_GET['dan']) ? intval(StringChecker::Check($_GET['dan'])) : NULL;
$ura = isset($_GET['ura']) ? intval(StringChecker::Check($_GET['ura'])) : NULL;
$filterId = isset($_GET['filter_id']) ? intval(StringChecker::Check($_GET['filter_id'])) : 0;
$projectId = isset($_GET['project_id']) ? intval(StringChecker::Check($_GET['project_id'])) : NULL;
$printId = isset($_GET['printId']) ? intval(StringChecker::Check($_GET['printId'])) : NULL;

if($_GET['action'] == 'export'){
	
	//prodobitev project name
	$sqlProjName = "SELECT proj_name FROM ".DB_WARPIT_WEBCATI_BASE.".`_Projects` WHERE id = $projectId";
	$res = $db->SQLexecute($sqlProjName);
	$dat = $db->fetchAssoc($res);
	$ProjectName = $dat['proj_name'];
	//id tel array
	if(isset($filterId) && $filterId != 0){
		$sqlLog = "SELECT * FROM ".WARPIT_CRM."._actionLogger WHERE id_filter = $filterId AND id_project = $projectId AND id_action = 1";
		$res = $db->SQLexecute($sqlLog);
		while($dat = $db->fetchAssoc($res)){
			$log[] = $dat['id_tel'];
		}
	}
	if(isset($_GET['history'])){
		$sqlLog = "SELECT * FROM ".WARPIT_CRM."._actionLogger WHERE id_print = $printId AND id_project = $projectId AND id_action = 1";
		$res = $db->SQLexecute($sqlLog);
		while($dat = $db->fetchAssoc($res)){
			$log[] = $dat['id_tel'];
		}
	}
	//dan, ura
	$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI.".`".$ProjectName."_language` WHERE `id_question` IN ( 9, 10 ) AND `id_text_type` = 3";
	$res = $db->SQLexecute($sql);
	while($row = $db->fetchAssoc($res)){
		$lang[] = explode("/", $row['lngText']);
	}
	foreach ($lang[0] as $day) {
		$tmp = explode(" ", $day);
		$days[$tmp[0]]['label'] = str_replace($tmp[0]." ", "", $day);	
	}
	foreach ($lang[1] as $t) {
		$tmp = explode(" ", $t);
		$time[$tmp[0]]['label'] = str_replace($tmp[0]." ", "", $t);
	}	
	
	//podatki tabele
	if(is_array($log)){
		$notIn = isset($_GET['history']) ? " IN " : " NOT IN";
		if(isset($filterId) && $filterId != 0)
			$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE dan = '". $dan ."' AND ura = '". $ura ."' AND smpl_var = ".$filterId." AND id_tel $notIn (".implode(",", $log).") AND status_rec = 1 AND `status` = 1";	
		else
			$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE id_tel $notIn (".implode(",", $log).") AND status_rec = 1 AND `status` = 1";
	} else {
		if(isset($filterId) && $filterId != 0)
			$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE dan = '". $dan ."' AND ura = '". $ura ."' AND smpl_var = ".$filterId." AND status_rec = 1 AND `status` = 1";
		else
			$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE status_rec = 1 AND `status` = 1";
	}
	$sql.= " ORDER BY dan, ura, smpl_var, smpl_var";
	
	$res = $db->SQLexecute($sql);
	$osebe = array();
	
	while($row = $db->fetchAssoc($res)){
		$osebe[] = $row;
	}
	
	$sql = "SELECT DISTINCT(presentation_location) FROM ".WARPIT_CRM."._PresentatorPermissions WHERE cati_location = ".$filterId." AND id_project=".$projectId;
	
	$res = $db->SQLexecute($sql);
	$data = $db->fetchAssoc($res);
	$tmp = explode(",", $data['presentation_location']);
	$hotel = $tmp[0];
	//izdelava excella
	$objPHPExcel = new PHPExcel();
	
	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('25');
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('25');
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('20');
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('35');
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('30');
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('10');
	
	$objPHPExcel->getActiveSheet()->setCellValue("A1", $LangPack->GetLabel("CRM_export", "name_surname","Ime in priimek"));
	$objPHPExcel->getActiveSheet()->getStyle("A1")->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->setCellValue("B1", $LangPack->GetLabel("CRM_export", "address","Naslov"));
	$objPHPExcel->getActiveSheet()->getStyle("B1")->getFont()->setBold(true);
	$objPHPExcel->getActiveSheet()->setCellValue("C1", $LangPack->GetLabel("CRM_export", "post_code","Pošta"));
	$objPHPExcel->getActiveSheet()->getStyle("C1")->getFont()->setBold(true);
	$objPHPExcel->getActiveSheet()->setCellValue("D1", $LangPack->GetLabel("CRM_export", "event","Dogodek"));
	$objPHPExcel->getActiveSheet()->getStyle("D1")->getFont()->setBold(true);
	$objPHPExcel->getActiveSheet()->setCellValue("E1", $LangPack->GetLabel("CRM_export", "day","Dan"));
	$objPHPExcel->getActiveSheet()->getStyle("E1")->getFont()->setBold(true);
	$objPHPExcel->getActiveSheet()->setCellValue("F1", $LangPack->GetLabel("CRM_export", "hour","Ura"));
	$objPHPExcel->getActiveSheet()->getStyle("F1")->getFont()->setBold(true);
	
	$i = 2;
 	
	foreach( $osebe as $key )
	{
	        $objPHPExcel->getActiveSheet()->setCellValue("A".$i, $key['oseba_1']);
	        $objPHPExcel->getActiveSheet()->setCellValue("B".$i, $key['oseba_2']);
			$objPHPExcel->getActiveSheet()->setCellValue("C".$i, $key['oseba_3']);
			$objPHPExcel->getActiveSheet()->setCellValue("D".$i, $key['dogodek_2']);
			$objPHPExcel->getActiveSheet()->setCellValue("E".$i, $days[$key['dan']]['label']);
			$objPHPExcel->getActiveSheet()->setCellValue("F".$i, $time[$key['ura']]['label']);
	        $i++;
	}
	$fileName = $LangPack->GetLabel("CRM_export", "file_name","Vabila");
	if(isset($filterId) && $filterId != 0)
		$exportName = $fileName." - " . $ProjectName . " - ". $days[$dan]['label'] ." - ". $time[$ura]['label'] ." - " . $hotel . ".xls";
	else
		$exportName = $fileName." - " . $ProjectName.".xls";
	header('Content-Type: application/vnd.ms-excel');
	header('Content-Disposition: attachment; filename="' . $exportName . '"');
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	$objWriter->save('php://output');
		
}
 
?>


