<?php
/**

 * Kontroler Demo
 *
 */
class DemoController extends MainController implements ControllerInterface {
	
	

	const CONTENT_PER_PAGE = 30;
	
	
	/**
	 * Strona glowna
	 *
	 */
	public function IndexAction($param) {
		
		
		$this->AddScript('structure.js');
		
		$dalData = MfClientDAL::GetDalDataObj();
		$page = 1;
		$countStart = 0;

		if(isset($param['strona']) && $param['strona'] > 0) {
			$page = $param['strona'];
			$countStart = ($page-1)*self::CONTENT_PER_PAGE;
		}

		SessionProxy::SetValue('__news_page_no__', $page);

		$offset = ($page - 1) * self::CONTENT_PER_PAGE;
		$param['ajax'] = 'GetTableContent($(this).attr(\'href\'), \'#tableContentClient\', $(\'#search\').val(), $(\'#linkedList\').val(), $(\'#sortVal\').val())';
		//$this->smarty->assign('ajax',$param['ajax']);
		$dalData->setCount(true);
		
		$dalData->addCondition('type', 5);
		try {
			$limit = Utils::PageConfigure($this->smarty, $param, ceil(MfClientDAL::GetResult($dalData)), self::CONTENT_PER_PAGE);
		} catch (Exception $e) {
			Utils::ArrayDisplay($e);
		}
		
	
		
		$sortNameASC = '';
		$sortNameDESC = '';
		$sortIdASC = '';
		$sortIdDESC = '';
		
		$sortDateAddDESC = '';
		$sortDateAddASC = '';
		$sortVal = Request::GetPost('sortVal');
		switch (Request::GetPost('sortVal')) {
			case 'last_name DESC':
				$sortNameDESC = 'Act';
				break;				
				case 'last_name ASC':
				$sortNameASC = 'Act';
				break;
				case 'id_mf_client DESC':
				$sortIdDESC = 'Act';
				break;
				case 'id_mf_client ASC':
				$sortIdASC = 'Act';
				break;
			default:
				$sortVal = 'last_name';
				$sortNameASC = 'Act';
		}
		
		
		$dalData->setCount(false);
		$dalData->setSortBy($sortVal);
		$dalData->setLimit($limit);
		$dalData->addCondition('type', 5);
		$arrayObjClient = MfClientDAL::GetResult($dalData, false);
		$this->smarty->assign('arrayObj', $arrayObjClient);
		
		//Utils::ArrayDisplay($arrayObjClient); 
		
		$this->smarty->assign('sortVal', 'last_name');
		
		$this->smarty->assign('sortNameASC', $sortNameASC);
		$this->smarty->assign('sortNameDESC', $sortNameDESC);
		$this->smarty->assign('sortIdASC', $sortIdASC);
		$this->smarty->assign('sortIdDESC', $sortIdDESC);
		$this->smarty->assign('sortDateAddASC', $sortDateAddASC);
		$this->smarty->assign('sortDateAddDESC', $sortDateAddDESC);
		
		$this->smarty->assign('countStart', $countStart);
		
			
	}
	
	public function AjaxListAction($param) {
		$this->AddScript('structure.js');
		$this->SetAjaxRender();
		$dalData = MfClientDAL::GetDalDataObj();
		$page = 1;
		$countStart = 0;

		if(isset($param['strona']) && $param['strona'] > 0) {
			$page = $param['strona'];
			$countStart = ($page-1)*self::CONTENT_PER_PAGE;
		}

		SessionProxy::SetValue('__news_page_no__', $page);

		$offset = ($page - 1) * self::CONTENT_PER_PAGE;
		$param['ajax'] = 'GetTableContent($(this).attr(\'href\'), \'#tableContentClient\', $(\'#search\').val(), $(\'#linkedList\').val(), $(\'#sortVal\').val())';
		$this->smarty->assign('ajax',$param['ajax']);
		$dalData->setCount(true);
		
		$dalData->addCondition('type', 5);
		try {
			$limit = Utils::PageConfigure($this->smarty, $param, ceil(MfClientDAL::GetResult($dalData, false)), self::CONTENT_PER_PAGE);
		} catch (Exception $e) {
			Utils::ArrayDisplay($e);
		}
		
	
		
		$sortNameASC = '';
		$sortNameDESC = '';
		$sortIdASC = '';
		$sortIdDESC = '';
		$sortDateAddDESC = '';
		$sortDateAddASC = '';
		$sortVal = Request::GetPost('sortVal');
		switch (Request::GetPost('sortVal')) {
			case 'last_name DESC':
				$sortNameDESC = 'Act';
				break;				
				case 'last_name ASC':
				$sortNameASC = 'Act';
				break;
				case 'id_mf_client DESC':
				$sortIdDESC = 'Act';
				break;
				case 'id_mf_client ASC':
				$sortIdASC = 'Act';
				break;
				case 'date_add ASC':
				$sortDateAddASC = 'Act';
				break;
				case 'date_add DESC':
				$sortDateAddDESC = 'Act';
				break;
			default:
				$sortVal = 'last_name';
				$sortNameASC = 'Act';
		}
		
		
		$dalData->setCount(false);
		$dalData->setSortBy($sortVal);
		$dalData->setLimit($limit);
		$arrayObjClient = MfClientDAL::GetResult($dalData, false);
		$this->smarty->assign('arrayObj', $arrayObjClient);
		
		//Utils::ArrayDisplay($arrayObjClient); 
		
		
		$this->smarty->assign('sortNameASC', $sortNameASC);
		$this->smarty->assign('sortNameDESC', $sortNameDESC);
		$this->smarty->assign('sortIdASC', $sortIdASC);
		$this->smarty->assign('sortIdDESC', $sortIdDESC);
		$this->smarty->assign('sortDateAddASC', $sortDateAddASC);
		$this->smarty->assign('sortDateAddDESC', $sortDateAddDESC);
		$this->smarty->assign('countStart', $countStart);
		
		
	}
	
	public function ViewAction($param) {
		$objClient = MfClientDAL::GetById($param['id']);
		$this->smarty->assign('obj', $objClient);
		
		//===FILE=======================================
		$dalData = MfFileDemoLogsDAL::GetDalDataObj();
		$dalData->addCondition('id_mf_client',$param['id']);
		//Utils::ArrayDisplay($_SERVER);
		//$dalData->setJoin(array('objFile' => ' LEFT JOIN mf_file ON mf_article.id_mf_article=mf_article_description.id_mf_article'));
		$arrayObj = MfFileDemoLogsDAL::GetResult($dalData);
		$this->smarty->assign('arrayObjFile', $arrayObj);
		//----------------------------------------------
		
		
	}
	
	public function FileAction($param) {
		
		$dalData = MfFileDemoDAL::GetDalDataObj();		
		$arrayObj = MfFileDemoDAL::GetResult($dalData);
		
		$dalData = MfFileDemoLogsDAL::GetDalDataObj();
		//$dalData->setCount(true);
		foreach ($arrayObj as $obj) {
			$dalData->addCondition('id_mf_file_demo', $obj->getId());
			$arrayByFile[$obj->getId()] = MfFileDemoLogsDAL::GetResult($dalData);						
		}		
		//Utils::ArrayDisplay($arrayByFile);		
		$this->smarty->assign('arrayObj', $arrayObj);
	}
	
	

	public function AddStructureAction($param) {
		$this->SetNoRender();
	}



	/**
	 * Wspolna metoda
	 *
	 */
	public function preDispatch($param) {

		//$this->RunShared('Admin');

		$this->Run($param);
		//$admin = AuthDAL::GetAdmin();
		$this->RunShared('Auth', array());
		$this->smarty->assign('titleAdmin', 'CRM');
		$struct = array(
		//'User' => array('User' => 'Index'),
		'Konfigurator' => array('Config' => 'Index'),
		'-> Historia konfiguracji' => array('Config' => 'Archive'),
		'Demo' => array('Demo' => 'Index'),
		'-> Historia pobierania' => array('Demo' => 'File'),
		'Pliki' => array('File' => 'Index'),
		'Klienci' => array('Client' => 'Index'),
		'Strefa Poradnik' => array('Structure' => 'Edit', 'id' => 30),
		'Strefa Aktualne promocje' => array('Structure' => 'Edit', 'id' => 31),


		);

		$this->smarty->assign('structure',$this->renderStruct($struct));

	}


	private function renderStruct($struct){
		$return = '';

		foreach($struct AS $k => $row){
			$return .= '<li><a href="' . Router::GenerateUrl('dictpig',$row).'">'.$k.'</a></li>';
		}

		$html = '<ul>';
		$html .= $return;
		$html .= '</ul>';


		return $html;
	}


	public function postDispatch($param) {

	}
}
?>