<?php
/**
 * Kontroler konfigurator
 *
 */
class ConfigController extends MainController implements ControllerInterface {




	const CONTENT_PER_PAGE = 5;


	/**
	 * Strona glowna
	 *
	 */
	public function IndexAction($param) {
		$dalData = MfStepDAL::GetDalDataObj();
		$arrayObjStep = MfStepDAL::GetResult($dalData);
		//	Utils::ArrayDisplay($arrayObjStep[0]->getStepElement());
		$this->smarty->assign('arrayObj', $arrayObjStep);
	}

	public function AddStructureAction($param) {

	}



	public function AddAction($param) {

		$objStep = MfStepDAL::GetEmptyObj();

		if (Request::GetPost('doCategoryAdd')) {
			//Utils::ArrayDisplay(Request::GetAllPost());

			$out = array();
			$validator  = new Validator(Request::GetAllPost());
			$data = Request::GetAllPost();

			$validator->IsEmpty('name', 'Pole nazwa musi zostać wypełnione.');

			$out = $validator->GetErrorList();

			$publication = Request::Get('publication');
			$publication ? $publication = 1 : $publication = '0';

			$objStep->setPublication($publication);
			$objStep->setWeight($data['weight']);
			$objStep->setLang('pl');
			$objStep->setName($data['name']);
			$objStep->setDescription($data['description']);
			$objStep->setDateAdd(Utils::GetNowDate());

			if(empty($out)) {

				$idStep = MfStepDAL::Save($objStep);

				$this->AddRedirectInfo('Zapisano', 'ok', Router::GenerateUrl('editConfig', array('Config' => 'Index')));
			} else {

				$this->smarty->assign('obj',$objStep);
				$this->smarty->assign('info','Pola obowiązkowe muszą zostać wypełnione.');
				$this->smarty->assign('type','error');

				foreach ($out as $item) {
					$error[$item['field']] = $item['msg'];
				}
				$this->smarty->assign('error',$error);
			}
		}
		$this->smarty->assign('obj',$objStep);
	}


	public function EditAction($param) {

		$objStep = MfStepDAL::GetById($param['id'], $param['lang']);
		$this->smarty->assign('obj', $objStep);

		if (Request::GetPost('doCategoryEdit')) {
			//Utils::ArrayDisplay(Request::GetAllPost());

			$out = array();
			$validator  = new Validator(Request::GetAllPost());
			$data = Request::GetAllPost();

			$validator->IsEmpty('name', 'Pole nazwa musi zostać wypełnione.');

			$out = $validator->GetErrorList();

			$publication = Request::Get('publication');
			$publication ? $publication = 1 : $publication = '0';

			$publication = Request::Get('publication');
			$publication ? $publication = 1 : $publication = '0';

			$objStep->setPublication($publication);
			$objStep->setWeight($data['weight']);
			$objStep->setLang('pl');
			$objStep->setName($data['name']);
			$objStep->setDescription($data['description']);
			$objStep->setDateEdit(Utils::GetNowDate());

			if(empty($out)) {

				//Utils::ArrayDisplay($objStep);
				$idStep = MfStepDAL::Save($objStep);

				$this->AddRedirectInfo('Zapisano', 'ok', Router::GenerateUrl('editConfig', array('Config' => 'Index')));
			} else {

				$this->smarty->assign('obj',$objStep);


				$this->smarty->assign('info','Pola obowiązkowe muszą zostać wypełnione.');
				$this->smarty->assign('type','error');

				foreach ($out as $item) {
					$error[$item['field']] = $item['msg'];


				}
				$this->smarty->assign('error',$error);


			}



		}
	}


	public function DeleteAction($param) {
		$dalData = MfStepElementDAL::GetDalDataObj();
		$dalData->setCount(true);
		$dalData->addCondition('id_mf_step', $param['id']);

		$count = MfStepElementDAL::GetResult($dalData);

		$delete = true;
		$error = '';
		if ($count > 0 ) {
			$delete = false;
		}

		if ($delete) {
			$objStep = MfStepDAL::GetById($param['id']);





			$dalData = MfStepDAL::GetDalDataObj();
			$dalData->setObj($objStep);
			MfStepDAL::Delete($dalData);
			$this->AddRedirectInfo('Element został usunięty', 'ok', Router::GenerateUrl('editCategory', array('Config' => 'Index')));
		} else {
			if ($count) {
				$error .= 'Grupa posiada elementy';
			}
			//			if ($isLang) {
			//				$error .= $error ? '<br/><br/>' : '';
			//				$error .= 'Kategoria posiada wersję jezykową';
			//			}
			$this->AddRedirectInfo($error, 'error', Router::GenerateUrl('editCategory', array('Config' => 'Index')));
		}
	}





	public function IndexElementAction($param) {
		$dalData = MfStepDAL::GetDalDataObj();
		$arrayObjStep = MfStepDAL::GetResult($dalData);
		//	Utils::ArrayDisplay($arrayObjStep[0]->getStepElement());
		$this->smarty->assign('arrayObj', $arrayObjStep);
	}

	public function AddElementAction($param) {
		$idMain = $param['idMain'];
		$objStepElement = MfStepElementDAL::GetEmptyObj();

		if (Request::GetPost('doCategoryAdd')) {
			//Utils::ArrayDisplay(Request::GetAllPost());

			$out = array();
			$validator  = new Validator(Request::GetAllPost());
			$data = Request::GetAllPost();

			$validator->IsEmpty('name', 'Pole nazwa musi zostać wypełnione.');

			$out = $validator->GetErrorList();

			$publication = Request::Get('publication');
			$publication ? $publication = 1 : $publication = '0';
			$other = Request::Get('other');
			$other ? $other = 1 : $other = '0';

			$objStepElement->setOther($other);
			$objStepElement->setIdMfStep($idMain);
			$objStepElement->setPublication($publication);
			$objStepElement->setWeight($data['weight']);
			$objStepElement->setLang('pl');
			$objStepElement->setName($data['name']);
			$objStepElement->setDescription($data['description']);
			$objStepElement->setDateAdd(Utils::GetNowDate());

			if(empty($out)) {

				$idStep = MfStepElementDAL::Save($objStepElement);

				$this->AddRedirectInfo('Zapisano', 'ok', Router::GenerateUrl('editConfig', array('Config' => 'Edit', 'id' => $idMain)));
			} else {

				$this->smarty->assign('obj',$objStepElement);
				$this->smarty->assign('info','Pola obowiązkowe muszą zostać wypełnione.');
				$this->smarty->assign('type','error');

				foreach ($out as $item) {
					$error[$item['field']] = $item['msg'];
				}
				$this->smarty->assign('error',$error);
			}
		}
		$this->smarty->assign('obj',$objStepElement);
		$this->smarty->assign('idMain',$idMain);
	}


	public function EditElementAction($param) {
		$idMain = $param['idMain'];
		$objStepElement = MfStepElementDAL::GetById($param['id'], $param['lang']);
		$this->smarty->assign('obj', $objStepElement);

		$this->smarty->assign('idMain',$idMain);


		if (Request::GetPost('doCategoryEdit')) {
			//Utils::ArrayDisplay(Request::GetAllPost());

			$out = array();
			$validator  = new Validator(Request::GetAllPost());
			$data = Request::GetAllPost();

			$validator->IsEmpty('name', 'Pole nazwa musi zostać wypełnione.');

			$out = $validator->GetErrorList();

			$publication = Request::Get('publication');
			$publication ? $publication = 1 : $publication = '0';

			$other = Request::Get('other');
			$other ? $other = 1 : $other = '0';

			$objStepElement->setOther($other);
			$objStepElement->setIdMfStep($idMain);
			$objStepElement->setPublication($publication);
			$objStepElement->setWeight($data['weight']);
			$objStepElement->setLang('pl');
			$objStepElement->setName($data['name']);
			$objStepElement->setDescription($data['description']);
			$objStepElement->setDateEdit(Utils::GetNowDate());

			if(empty($out)) {

				//Utils::ArrayDisplay($objStep);
				$idStep = MfStepElementDAL::Save($objStepElement);

				$this->AddRedirectInfo('Zapisano', 'ok', Router::GenerateUrl('editConfig', array('Config' => 'Edit', 'id' => $idMain)));
			} else {

				$this->smarty->assign('obj',$objStep);


				$this->smarty->assign('info','Pola obowiązkowe muszą zostać wypełnione.');
				$this->smarty->assign('type','error');

				foreach ($out as $item) {
					$error[$item['field']] = $item['msg'];


				}
				$this->smarty->assign('error',$error);


			}



		}
	}

	public function ArchiveAction($param) {

		$dalData = MfConfigArchiveDAL::GetDalDataObj();
		$page = 1;

		if(isset($param['p']) && $param['p'] > 0) {
			$page = $param['p'];

		}

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

		$offset = ($page - 1) * self::CONTENT_PER_PAGE;
		$param['ajax'] = 'GetTableContent($(this).attr(\'href\'), \'#tableContentClient\', $(\'#search\').val(), $(\'#linkedList\').val())';
		//$this->smarty->assign('ajax',$param['ajax']);
		$dalData->setCount(true);
		try {
			$limit = Utils::PageConfigure($this->smarty, $param, ceil(MfConfigArchiveDAL::GetResult($dalData)), self::CONTENT_PER_PAGE);
		} catch (Exception $e) {
			Utils::ArrayDisplay($e);
		}
		$dalData->setCount(false);
		$dalData->setSortBy('date_add DESC');
		$dalData->setLimit($limit);
		$arrayObj = MfConfigArchiveDAL::GetResult($dalData, false);
		$this->smarty->assign('arrayObj', $arrayObj);


	}


	public function AjaxArchiveAction($param) {
		$this->SetAjaxRender();
		$dalData = MfConfigArchiveDAL::GetDalDataObj();
		$page = 1;


		$search= trim(Request::GetPost('search'));
		$status = Request::GetPost('linked');
		$arraySearch = explode(' ', $search);
		if (count($arraySearch) > 0) {
			$where = ' ( ';
			foreach ($arraySearch as $key => $search) {
				$where .= $key == 0 ? '' : ' OR ';
				$where .= ' company LIKE "%'.Utils::AddSlashes($search).'%" ';
			}
			$where .= ' ) ';

			$dalData->addCondition(' ', $where, ' ');
		}
		$dalData->addCondition('status', $status);

		if(isset($param['p']) && $param['p'] > 0) {
			$page = $param['p'];
		}
		SessionProxy::SetValue('__news_page_no__', $page);

		$offset = ($page - 1) * self::CONTENT_PER_PAGE;
		$param['ajax'] = 'GetTableContent($(this).attr(\'href\'), \'#tableContentClient\', $(\'#search\').val(), $(\'#linkedList\').val())';
		//$this->smarty->assign('ajax',$param['ajax']);
		$dalData->setCount(true);
		try {
			$limit = Utils::PageConfigure($this->smarty, $param, ceil(MfConfigArchiveDAL::GetResult($dalData)), self::CONTENT_PER_PAGE);
		} catch (Exception $e) {
			Utils::ArrayDisplay($e);
		}
		$dalData->setCount(false);
		$dalData->setSortBy('date_add DESC');
		$dalData->setLimit($limit);
		$arrayObj = MfConfigArchiveDAL::GetResult($dalData, false);
		$this->smarty->assign('arrayObj', $arrayObj);


	}
	
	public function ShowArchiveAction($param) {
		$obj = MfConfigArchiveDAL::GetById($param['id']);
		$this->smarty->assign('obj', $obj);
	}
	
	
	public function AjaxArchiveStatusAction($param) {
		$this->SetNoRender();
		$data = Request::GetAllPost();
		if (Request::GetPost('id') > 0) {			
			$obj = MfConfigArchiveDAL::GetById($data['id']);		
			$obj->setStatus($data['status']);			
			MfConfigArchiveDAL::Save($obj);			
		}
	}


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

		//$this->RunShared('Admin');
		$this->AddScript('structure.js');
		$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) {

	}
}
?>