<?
// ALTER TABLE `tbl_kka_client` ADD `clt_name_export` VARCHAR( 255 ) NULL AFTER `clt_name` ;

define('CKKA_ERR_INVALID_ARGUMENTS', 401);

define('CKKA_STATE_ERROR', 201);
define('CKKA_STATE_LOGGED_OUT', 202);
define('CKKA_STATE_LOGGED_IN', 203);
define('CKKA_STATE_VOTED', 204);


require_once ('class.stdModule.php');

class ckka extends stdModule {

	function ckka() {
		
		$this->stdModule('ckka0100');

		$this->_settings->templates->actie_intro		= "actie_intro.tpl";
		$this->_settings->templates->choose_projects	= "choose_projects.tpl";
		$this->_settings->templates->thanks				= "thanks.tpl";

	}

	function getCurrentUserState($act_code) {

		if (isset($_SESSION['sess_ckka']) && isset($_SESSION['sess_ckka']['client']['clt_id']) && is_numeric($_SESSION['sess_ckka']['client']['clt_id'])) {

			if ($this->getVote($_SESSION['sess_ckka']['client']['clt_id'])) {
				return CKKA_STATE_VOTED;
			} else {
				return CKKA_STATE_LOGGED_IN;
			}
		}

		return CKKA_STATE_LOGGED_OUT;

	}

	function getVote($clt_id) {
		if (!is_numeric($clt_id)) return $this->triggerError(CKKA_ERR_INVALID_ARGUMENTS);

		$rs = $this->_db->Query("SELECT tbl_kka_project.* 
								FROM tbl_kka_project 
								INNER JOIN tbl_kka_client ON prj_id = clt_prj_id AND clt_id = ".$clt_id);
		
		$vote = rs2arr($rs, false, true);
		return $vote ? $vote : false;

	}

	function getActie($act_code) {
		if (empty($act_code)) return $this->triggerError(CKKA_ERR_INVALID_ARGUMENTS);

		$sql = "SELECT * 
				FROM tbl_kka_actie 
				WHERE act_code = '".mysql_escape_string($act_code)."' 
				AND act_org_id = ".$this->_variables->public->org_id;
		$rs = $this->_db->Query($sql);
		return rs2arr($rs, false, true);		
	}

	function getProjects($act_id=null, $act_code=null) {
		if (!is_numeric($act_id) && empty($act_code)) return $this->triggerError(CKKA_ERR_INVALID_ARGUMENTS);

		$sql = "SELECT prj.*, ROUND((prj.prj_votecount/SUM(prj_sum.prj_votecount))*100) as votes_perc
				FROM tbl_kka_project as prj
				INNER JOIN tbl_kka_project as prj_sum ON prj_sum.prj_act_id = prj.prj_act_id";

		if (is_numeric($act_id)) {
			$sql .= " WHERE prj.prj_act_id = ".$act_id;
		} else {
			$sql .= " INNER JOIN tbl_kka_actie ON act_id = prj.prj_act_id AND act_code = '".mysql_escape_string($act_code)."' AND act_org_id = ".$this->_variables->public->org_id;
		}
		$sql .= " GROUP BY prj.prj_id ORDER BY prj.prj_order";

		$rs = $this->_db->Query($sql);

		return rs2arr($rs, 'prj_id');

	}


	function saveVote($act_code, $prj_id) {
		if (empty($act_code) || !is_numeric($prj_id)) return $this->triggerError(CKKA_ERR_INVALID_ARGUMENTS);

		if (isset($_SESSION['sess_ckka']) && isset($_SESSION['sess_ckka']['client']['clt_id']) && is_numeric($_SESSION['sess_ckka']['client']['clt_id'])) {
			if (!is_numeric($_SESSION['sess_ckka']['client']['clt_prj_id'])) {
				$this->_db->Execute("UPDATE tbl_kka_client SET clt_prj_id = ".$prj_id.", clt_changedate = NOW() WHERE clt_id = ".$_SESSION['sess_ckka']['client']['clt_id']);
				$this->_db->Execute("UPDATE tbl_kka_project SET prj_votecount = prj_votecount + 1 WHERE prj_id = ".$prj_id);
			}

			$_SESSION['sess_ckka']['client']['clt_prj_id'] = $prj_id;
		}
	}

	function login($act_code, $login_code) {

		$sql = "SELECT tbl_kka_client.*, tbl_kka_region_manager.*
				FROM tbl_kka_client
				INNER JOIN tbl_kka_actie ON act_id = clt_act_id AND act_code = '".mysql_escape_string($act_code)."' AND act_org_id = ".$this->_variables->public->org_id."
				LEFT JOIN tbl_kka_region_manager ON rma_code = clt_rma_code
				WHERE clt_login_code = '".mysql_escape_string($login_code)."'";

		$rs = $this->_db->Query($sql);
		$client = rs2arr($rs, false, true);

		if ($client && is_numeric($client['clt_id'])) {
			
			$_SESSION['sess_ckka']['client'] = $client;
			return true;

		} else {

			return false;

		}

	}

	function _updateSessionData() {
		if (isset($_SESSION['sess_ckka']['client']) && is_numeric($_SESSION['sess_ckka']['client']['clt_id'])) {

			$sql = "SELECT tbl_kka_client.*, tbl_kka_region_manager.*
					FROM tbl_kka_client
					LEFT JOIN tbl_kka_region_manager ON rma_code = clt_rma_code
					WHERE clt_id = ".$_SESSION['sess_ckka']['client']['clt_id'];
			$rs = $this->_db->Query($sql);
			$_SESSION['sess_ckka']['client'] = rs2arr($rs, false, true);

		}
	}

	function updateUserData($data) {
		
		if (isset($_SESSION['sess_ckka']['client']) && is_numeric($_SESSION['sess_ckka']['client']['clt_id'])) {
		
			$fields = array();

			if (isset($data['clt_email']) && strlen($data['clt_email']) > 5) {
				$fields['clt_email'] = "'".mysql_escape_string($data['clt_email']) ."'";
			}
			if (isset($data['clt_newsletter'])) {
				$fields['clt_newsletter'] = $data['clt_newsletter'] ? 1 : 0;;
			}

			if (isset($data['clt_name_export']) && strlen($data['clt_name_export']) > 1) {
				$fields['clt_name_export'] = "'".mysql_escape_string($data['clt_name_export']) ."'";
			}


			if ($fields) {
				$fields['clt_id'] = $_SESSION['sess_ckka']['client']['clt_id'];
				$fields['clt_changedate'] = 'NOW()';

				$sql = generate_query('tbl_kka_client', $fields, 'clt_id');
				$this->_db->Execute($sql);
				$this->_updateSessionData();
			}
		}
	}

	function printActieIntro($act_code) {
		if (empty($act_code)) return $this->triggerError(CKKA_ERR_INVALID_ARGUMENTS);

		$this->_smrt->assign('actie', $this->getActie($act_code));

		$this->assignTemplateGlobals();	
		$this->_smrt->display($this->_settings->templates->actie_intro);

	}

	function printProjects($act_code) {
		if (empty($act_code)) return $this->triggerError(CKKA_ERR_INVALID_ARGUMENTS);


		$this->_smrt->assign('projects', $this->getProjects(null, $act_code));

		$this->assignTemplateGlobals();	
		$this->_smrt->display($this->_settings->templates->choose_projects);

	}

	function printBedankt($act_code) {
		if (empty($act_code)) return $this->triggerError(CKKA_ERR_INVALID_ARGUMENTS);

		$projects = $this->getProjects(null, $act_code);

		$this->_smrt->assign('projects', $projects);

		$this->assignTemplateGlobals();	
		$this->_smrt->display($this->_settings->templates->thanks);

	}

}

?>