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



	const CONTENT_PER_PAGE = 50;

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


		//$dalData->setCount(true);


		$limit = Utils::PageConfigure($this->smarty, $param, ceil( MfDictionaryDAL::GetResult(array('lang' => "'pl'"), array(), null, null, true)), self::CONTENT_PER_PAGE);

		$arrayObjDict = MfDictionaryDAL::GetResult(array('lang' => "'pl'"), array(), $limit, 'mf_dictionary.keyword ASC');

		//Utils::ArrayDisplay($arrayObjDict);
		$this->smarty->assign('arrayObjDict', $arrayObjDict);
	}

	public function EditAction($param) {

		if (isset($param['id'])) {


			$arrayObjDict = MfDictionaryDAL::GetResult(array('id_mf_dictionary' => $param['id']), array(), 1, 'mf_dictionary.keyword ASC');
			if (!(count($arrayObjDict) > 0)) {
				$this->AddRedirect(Router::GenerateUrl('DictLabel', array("Dictionary" => 'Index')));
			}



			if (Request::GetPost('doDictEdit')) {
				$post = Request::GetAllPost(false);


				foreach ($post['replacement'] as $langDict => $replacement) {



					$arrayObjDict = MfDictionaryDAL::GetResult(array('keyword' => "'".$post['keyword']."'", 'lang' => "'".$langDict."'"), array(), 1, 'mf_dictionary.keyword ASC');
					$objDict = $arrayObjDict[0];
					$objDict->SetReplacement($replacement);
					MfDictionaryDAL::Save($objDict);

				}


				$this->AddRedirect(Router::GenerateUrl('DictLabel', array("Dictionary" => 'Index')), 0);

			}

			$arrayObjDict = MfDictionaryDAL::GetResult(array('id_mf_dictionary' => $param['id']), array(), 1, 'mf_dictionary.keyword ASC');
			if (!(count($arrayObjDict) > 0)) {
				$this->AddRedirect(Router::GenerateUrl('DictLabel', array("Dictionary" => 'Index')), 0);
			}


			$this->smarty->assign('objDict', $arrayObjDict[0]);
		} else {
			$this->AddRedirect(Router::GenerateUrl('DictLabel', array("Dictionary" => 'Index')), 0);
		}






	}

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

	/**
	 * Metoda wspolna
	 *
	 */
	public function preDispatch($param) {

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

		$this->smarty->assign('titleAdmin', 'Administracja');
		$struct = array(
		//'User' => array('User' => 'Index'),
		'Słowniki' => array('Dictionary' => 'Index'),
		'Zmienne serwisu' => array('Setup' => 'Index'),
		'Ustawienia formularza' => array('FormSettings' => 'Index'),
//		'Kategorie produktów' => array('ProductCategory' => 'Index'),		
//		'Serie produktów' => array('ProductSeries' => 'Index'),
//		'Materiał produktów' => array('ProductSpec' => 'Index', 'type' => 2),
//		'Kolor produktów' => array('ProductSpec' => 'Index', 'type' => 1),
//		'Żarówki produktów' => array('ProductSpec' => 'Index', 'type' => 3),
		//'Paramtery produktów' => array('ProductAttribute' => 'Index')


		);

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



}



?>
