<?php
/**
 * $Id$
 * Słownki 
 *
 */
class CalcController extends MainController implements ControllerInterface  {



	const CONTENT_PER_PAGE = 50;

	/**
	 * Domyślna metoda
	 *
	 */
	public function IndexAction($param) {


		if (isset($param['type'])) {
			SessionProxy::SetValue('typeCalc', $param['type']);
		} else {
			$param['type'] = 1;
		}
		SessionProxy::SetValue('typeCalc', $param['type']);
		$dalData = MfParametersDAL::GetDalDataObj();
		$dalData->addCondition('type', $param['type']);
		$dalData->setSortBy('sort');

		$arrayObjParameters = MfParametersDAL::GetResult($dalData);
		//Utils::ArrayDisplay($arrayObjParameters);
		//===grupowanie====
		$arrayParam = array();
		$arrayGroupParam = array();
		foreach ($arrayObjParameters as $objParam) {
			$idLink = $objParam->GetLinkId();
			$arrayGroupParam[$objParam->GetLinkId()][] = $objParam;
		}
		
		$this->smarty->assign('arrayObj', $arrayObjParameters);
		$this->smarty->assign('arrayGroupParam', $arrayGroupParam);
	}
	
    
	
	public function AddAction($param) {

		$objParameters = new MfParameters();
		$objParameters->setType(SessionProxy::GetValue('typeCalc'));
		$this->smarty->assign('obj', $objParameters);

		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';

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

			$objParameters->setPublication($publication);
			$objParameters->setPrice($data['price']);
			$objParameters->setLinkId($data['link_id']);
			$objParameters->setName($data['name']);
			$objParameters->setOpis($data['opis']);
			$objParameters->setPriceProgres($priceProgres);
			$objParameters->setCountProgres($data['count_progres']);
			$objParameters->setUnit($data['unit']);

			if(empty($out)) {

				//Utils::ArrayDisplay($objParameters);
				$idParameters = MfParametersDAL::Save($objParameters);

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

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


				$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 EditAction($param) {

		$objParameters = MfParametersDAL::GetById($param['id']);
		$this->smarty->assign('obj', $objParameters);

		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';

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

			$objParameters->setPublication($publication);
			$objParameters->setPrice($data['price']);
			$objParameters->setLinkId($data['link_id']);
			$objParameters->setName($data['name']);
			$objParameters->setOpis($data['opis']);
			$objParameters->setCountProgres($data['count_progres']);
			$objParameters->setPriceProgres($priceProgres);
			$objParameters->setUnit($data['unit']);

			if(empty($out)) {

				//Utils::ArrayDisplay($objParameters);
				$idParameters = MfParametersDAL::Save($objParameters);

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

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


				$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 RegEditAction($param) {
        $dalData = MfParametersDAL::GetDalDataObj();
        //$dalData->addCondition('type', $type);
        $dalData->addCondition('publication', 1);
        $dalData->setSortBy('sort');
		$arrayObjParameters = MfParametersDAL::GetResult($dalData);
        
       
            
            $objParticipant = MfParticipantDAL::GetById($param['id']);
            $arrayFee = $objParticipant->getFeeFullUnserialize();
            
            //Utils::ArrayDisplay($arrayTextPrice);
            $this->smarty->assign('arrayObjParameters', $arrayObjParameters);
            $this->smarty->assign('discPrice', $arrayFee['disc']);
            $this->smarty->assign('arrayFee', $arrayFee);
            $this->smarty->assign('obj', $objParticipant);
            $this->smarty->assign('objParticipant', $objParticipant);
                    

		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();

			

			$objParticipant->setStatus($data['payment_status']);

			if(empty($out)) {

				//Utils::ArrayDisplay($objParameters);
				$idParameters = MfParticipantDAL::Save($objParticipant);

				$this->AddRedirectInfo('Zapisano', 'ok', Router::GenerateUrl('editReg', array('Calc' => 'Reg')));
			} else {

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


				$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) {


		$arrayObjDict = MfDictionaryDAL::GetResult(array('id_mf_dictionary' => $param['id']), array(), 1, 'mf_dictionary.keyword ASC');
		if (count($arrayObjDict) > 0) {
			MfDictionaryDAL::DeleteByKey($arrayObjDict[0]->GetKeyword());
			
		}

		

		$this->AddRedirect(Router::GenerateUrl('DictLabel', array("Dictionary" => 'Index')), 0);
	}
	public function RegDeleteAction($param) {

                $obj = MfParticipantDAL::GetById($param['id']);
                $dalData = MfParticipantDAL::GetDalDataObj();
                $dalData->setObj($obj);
                MfParticipantDAL::Delete($dalData);
				$this->AddRedirectInfo('Usunięto', 'ok', Router::GenerateUrl('editReg', array('Calc' => 'Reg')));
	}

    public function RegAction($param) {


		
		$dalData = MfParticipantDAL::GetDalDataObj();
		//$dalData->setSortBy();

		$arrayObjReg = MfParticipantDAL::GetResult($dalData);
		//Utils::ArrayDisplay($arrayObjReg);
        
        $dalData = MfParametersDAL::GetDalDataObj();
        $dalData->addCondition('publication', 1);
        $dalData->setSortBy('sort');

        $arrayObjParameters = MfParametersDAL::GetResult($dalData);
		
		$this->smarty->assign('arrayObjParameters', $arrayObjParameters);
		$this->smarty->assign('arrayObj', $arrayObjReg);
	}
	
    
	/**
	 * Metoda wspolna
	 *
	 */
	public function preDispatch($param) {

		$this->RunShared('Auth', $param);
		$this->Run($param);
		$admin = AuthDAL::GetAdmin();
		$this->user = $admin;

		$this->smarty->assign('titleAdmin', 'Formularz');
		$struct = array(
		//'User' => array('User' => 'Index'),
		'Rejestracja' => array('Calc' => 'Reg'),
//		'Parametry' => array('Calc' => 'Index', 'type' => 1),
		//'Parametry mieszkanie' => array('Calc' => 'Index', 'type' => 2),
		'Treści' => array('HomeSite' => 'EditArticle')
		);

		$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) {
	}



}



?>