<?php
/**
 * MarketEvaluation actions.
 *
 * @package    inmproject
 * @subpackage MarketEvaluation
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class ManageLayoutAction extends sfActions
{
	/**
	* Executes index action
	*
	*/
	public function execute()
	{
		$this->setLayout('smanager');
		$this->clientId			= 0;
		$this->arrayClient		= $this->GetEvalClient();

		$this->arrayPageType 	= array('DEFAULT'=>'DEFAULT','DMS_ONLY'=>'DMS_ONLY','EXCLUDE_NON_DEALER_MAKES'=>'EXCLUDE_NON_DEALER_MAKES');
		$this->arrayPage		= array('PAGE_1'=>'PAGE_1','PAGE_2'=>'PAGE_2','PAGE_3'=>'PAGE_3','PAGE_4'=>'PAGE_4','PAGE_5'=>'PAGE_5','SVC_ADS'=>'SVC_ADS','SVC_MAP'=>'SVC_MAP','SVC_TABLE'=>'SVC_TABLE','SALES_OVERALL'=>'SALES_OVERALL','SALES_TABLE'=>'SALES_TABLE','SALES_MAP'=>'SALES_MAP','PAGE_10'=>'PAGE_10','EVAL_PAGE_CONQUEST_SVC'=>'EVAL_PAGE_CONQUEST_SVC','EVAL_PAGE_CONQUEST_SALES_TRG'=>'EVAL_PAGE_CONQUEST_SALES_TRG','EVAL_PAGE_CONQUEST_CURRENT_PROD'=>'EVAL_PAGE_CONQUEST_CURRENT_PROD');
		$this->arrayCondition	= array(''=>'','HAS_CONSUMER_MAP'=>'HAS_CONSUMER_MAP','ROOM_FOR_RIGHT_IMAGE'=>'ROOM_FOR_RIGHT_IMAGE');
		$this->pageType			= 'DEFAULT';
		$this->page				= 'PAGE_1';
		$this->html				= '';

/*		$C = new Criteria;
		$C->addAnd(MarketEvalPageConfigPeer::CLIENT_ID);
		$C->addGroupByColumn(MarketEvalPageConfigPeer::CLIENT_ID);
		$Rs = MarketEvalPageConfigPeer::doSelectRs($C, Propel::GetConnection('dms_main'));
		while($Rs->next()) $this->arrayClient[$Rs->get(1)] = ClientPeer::GetNameById($Rs->get(1));;
*/		
		/*``
		` Switch pages
		```````````````````````````````````````````````````````````*/
		if($_POST['switch'] || $_POST['save'] || $_POST['CopySelected']) {
			$this->clientId = $_POST['clientId'];
			$this->pageType = $_POST['pageType'];
			$this->page 	= $_POST['page'];
			$this->layout	= $_POST['layout'];
		}
		
		/*``
		` Copy selected
		```````````````````````````````````````````````````````````*/
		if($_POST['CopySelected'])
		{
			$arrayPageConfig 	= $this->getPageConfig($this->clientId, $customerId=0, $this->layout);
			$arrayElements		= $arrayPageConfig[$this->pageType][$this->page];

			foreach($_REQUEST['SelectedCell'] as $base64Data)
			{
				$code = unserialize(base64_decode(trim($base64Data)));

				if($code['type'])
				{
					$arrayElements[] = array(
						'type'		=> $code['type'],
						'font'		=> $code['font'],
						'x'			=> $code['x'],
						'y'			=> $code['y'],
						'w'			=> $code['w'],
						'h'			=> $code['h'],
						'align'		=> $code['align'],
						'txt'		=> $code['txt'],
						'newline'	=> $code['newline'],
					);
				}
			}	


			$serialData = serialize($arrayElements);
			$this->savePageConfig($this->clientId, $this->layout, $this->page, $this->pageType, $serialData);
		}

		/*``
		` Saving changes
		```````````````````````````````````````````````````````````*/
		if($_POST['save'])
		{
			$arrayElements = array();
			
			foreach($_POST['type'] as $i => $type)
			{
				if(!$type) continue;
				$font		= $_POST['font'][$i];
				$txt		= $_POST['txt'][$i];
				$align		= $_POST['align'][$i];
				$url		= $_POST['url'][$i];
				$x			= $_POST['x'][$i];
				$y			= $_POST['y'][$i];
				$w			= $_POST['w'][$i];
				$h			= $_POST['h'][$i];
				$newline	= $_POST['newline'][$i];
				$condition	= $_POST['condition'][$i];
				
				switch($type) {
					case 'img':
						$arrayElements[] = array(
							'type'		=> $type,
							'url'		=> $url,
							'x'			=> $x,
							'y'			=> $y,
							'w'			=> $w,
							'h'			=> $h,
							'condition'	=> $condition
						);
					break;
					case 'cell':
						if(!$txt) continue;
						$arrayElements[] = array(
							'type'		=> $type,
							'font'		=> $font,
							'x'			=> $x,
							'y'			=> $y,
							'w'			=> $w,
							'h'			=> $h,
							'align'		=> $align,
							'txt'		=> $txt,
							'newline'	=> $newline
						);
					break;
					case 'txt':
						if(!$txt) continue;
						$arrayElements[] = array(
							'type'		=> $type,
							'font'		=> $font,
							'x'			=> $x,
							'y'			=> $y,
							'txt'		=> $txt,
							'newline'	=> $newline
						);
					break;
					case 'consumer_map_legend':
						$arrayElements[] = array(
							'type'		=> $type,
							'condition'	=> $condition
						);					
					break;
				}
			}
			
			if($_REQUEST['addByCode'])
			{
				$code = unserialize(trim($_REQUEST['addByCode']));

				if($code['type'])
				{
					$arrayElements[] = array(
						'type'		=> $code['type'],
						'font'		=> $code['font'],
						'x'			=> $code['x'],
						'y'			=> $code['y'],
						'w'			=> $code['w'],
						'h'			=> $code['h'],
						'align'		=> $code['align'],
						'txt'		=> $code['txt'],
						'newline'	=> $code['newline'],
					);
				}
			}
			
			$serialData = serialize($arrayElements);
			$this->savePageConfig($this->clientId, $this->layout, $this->page, $this->pageType, $serialData);
			
			#echo '<pre>';
			#print_r($arrayElements);
			#echo '</pre>';
		}
		
		
		
		
		$this->arrayPageConfig 	= $this->getPageConfig($this->clientId, $customerId=0, $this->layout);
		$this->arrayLayout 		= $this->getPageLayout($this->clientId);
		$this->pageElements		= $this->arrayPageConfig[$this->pageType][$this->page];		
	}
	
	/**
	* 
	*********************************************************************************/	
	private function getPageLayout($clientId=0)
	{
		$dbConn	= Propel::getConnection('dms_main');
		$query 	= "SELECT layout FROM market_eval_page_config WHERE client_id='$clientId' ";
		$rs		= $dbConn->executeQuery($query);
		while($rs->next()) {
			$result[$rs->get('layout')] = $rs->get('layout');
		}
		
		$result[''] = 'DEFAULT';
		return $result;
	}
	
	/**
	* Save the eval page config to the database
	*********************************************************************************/	
	private function savePageConfig($clientId=0, $layout='', $page='', $pageType='', $serialData='')
	{
		$serialData	= addslashes($serialData);
		$dbConn		= Propel::getConnection('dms_main');
		$query 		= "INSERT INTO market_eval_page_config (client_id, layout, page_type, page, data) VALUES ('$clientId', '$layout', '$pageType', '$page', '$serialData') ON DUPLICATE KEY UPDATE data=VALUES(data); ";
		$rs			= $dbConn->executeQuery($query);
	}
	
	/**
	* Get the eval page config from the database
	*********************************************************************************/	
	private function getPageConfig($clientId=0, $customerId=0, $layout=false)
	{
		$dbConn	= Propel::getConnection('dms_main');
		$extra	= $layout ? " AND layout='$layout' " : " AND (layout IS NULL OR layout='') "; 
		$query 	= "SELECT customer_id, page_type, page, data FROM market_eval_page_config WHERE client_id='$clientId' AND (customer_id='$customerId' OR customer_id=0) {$extra}";
		$rs		= $dbConn->executeQuery($query);
		while($rs->next()) {
			$pageCustomerId	= $rs->get('customer_id');
			$evalType		= $rs->get('page_type');
			$page			= $rs->get('page');
			$data			= $rs->get('data');
			$arrayPageConfig[$evalType][$page][$pageCustomerId] = unserialize($data);
		}
		
		foreach($arrayPageConfig as $evalType => $arrayPages) {
			foreach($arrayPages as $page => $arrayData) {
				if($arrayData[$customerId]) $result[$evalType][$page] = $arrayData[$customerId];
				else $result[$evalType][$page] = $arrayData[0];
			}
		}
		
		return $result;
	}

	private function GetEvalClient()
	{
		$result = array(0=>'Select Client');

		/**
		* Seem like an incomplete client list
		*/
		$C = new Criteria;
		$C->addSelectColumn(MarketEvaluationClientPeer::CLIENT_ID);
		$Rs = MarketEvaluationClientPeer::doSelectRs($C);

		while($Rs->next())
		{
			$result[$Rs->get(1)] = ClientPeer::GetNameById($Rs->get(1));
		}


		/**
		* Also select list of client from market_evaluation table
		*/
		$C = new Criteria;
		$C->addSelectColumn(MarketEvaluationPeer::CLIENT_ID);
		$C->addGroupByColumn(MarketEvaluationPeer::CLIENT_ID);
		$Rs = MarketEvaluationPeer::doSelectRs($C);

		while($Rs->next())
		{
			$result[$Rs->get(1)] = ClientPeer::GetNameById($Rs->get(1));
		}



		$result[0] = 'Select Client';
		return $result;
	}
}

?>