<?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 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) {
            $id = $param['id'];
		if ($id) {
			try {
				$obj = MfParticipantDAL::GetById($id);
                                $dalData = MfParticipantDAL::GetDalDataObj();
                                
                                $dalData->setId($id);
                                $dalData->setObj($obj);
				MfParametersDAL::Delete($dalData);

			} catch (Exception $e) {
				//Utils::ArrayDisplay($e);
				$logger->error('bład usunięcia wpisu o id: '.$id);
			}

		}

		$this->AddRedirect(Router::GenerateUrl('red', array('Calc' => 'Reg', 'location' => $param['location'])), 0);
        }

        public function RegEditAction($param) {
            $id = isset($param['id']) ? $param['id'] : Request::GetPost('id');
            $objParticipant = MfParticipantDAL::GetById($id);

            if (Request::GetPost('doCategoryEdit')) {
                $objParticipant->setStatus(Request::GetPost('payment_status'));
                MfParticipantDAL::Save($objParticipant);
                $this->AddRedirectInfo('Zapisano', 'ok', Router::GenerateUrl('red', array('Calc' => 'Reg', 'location' => $objParticipant->getLocation() ? $objParticipant->getLocation() : 1)), 0);
            }

            $arrayFee = $objParticipant->getFeeFullUnserialize();
            if (!is_array($arrayFee)) {
                $arrayFee = array();
            }
            $discPrice = isset($arrayFee['disc']) ? $arrayFee['disc'] : 1;

            $dalData = MfParametersDAL::GetDalDataObj();
            $dalData->addCondition('publication', 1);
            $dalData->setSortBy('sort');
            $arrayObjParameters = MfParametersDAL::GetResult($dalData);

            $this->smarty->assign('obj', $objParticipant);
            $this->smarty->assign('objParticipant', $objParticipant);
            $this->smarty->assign('arrayFee', $arrayFee);
            $this->smarty->assign('discPrice', $discPrice);
            $this->smarty->assign('arrayObjParameters', $arrayObjParameters);
        }

        public function RegAction($param) {
        $location = 1;

        if (isset($param['location'])) {
          $location = $param['location'];
        } 
        
        if ($location == 2) {
            $this->partialTemplate = 'RegPAN.tpl';
        }
          
		
		$dalData = MfParticipantDAL::GetDalDataObj();
        $dalData->addCondition('location', $location);
        $dalData->setSortBy('id_mf_participant DESC');

		$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);
		$this->smarty->assign('location', $location);
	}
	
    
	/**
	 * 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 - aktualia' => array('Calc' => 'Reg', 'location' => 1),
		//'Rejestracja - NAWA' => array('Calc' => 'Reg', 'location' => 2),
//		'Parametry' => array('Calc' => 'Index', 'type' => 1),
		//'Parametry mieszkanie' => array('Calc' => 'Index', 'type' => 2),
		'Treści - aktualia' => array('HomeSite' => 'EditArticle', 'location' => 1),
		//'Treści - NAWA' => array('HomeSite' => 'EditArticle', 'location' => 2)
		);

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



}



?>
