<?php
/**
 * $Id: SetupController.php 639 2008-06-18 10:51:32Z mija $
 * Kontroler Ustawien
 *
 */
class SetupController extends MainController implements ControllerInterface {
	/**
	 * Strona glowna
	 *
	 */
	public function IndexAction($param) {
		if (  isset($_REQUEST["addVariable"])  ) {
			//Utils::ArrayDisplay($_REQUEST);
			SetupDAL::SaveVariable($_REQUEST["variable"], $_REQUEST["value"], $_REQUEST["description"]);
		}
		$this->smarty->assign('arrayVariables', SetupDAL::GetAllVariables(false) );


		//Utils::ArrayDisplay(SetupDAL::GetAllVariables(false));

	}

	/**
	 * Nowy parametr
	 *
	 */
	public function NewAction() {
		$this->partialTemplate = 'Edit.tpl';
		$this->smarty->assign('objVariable', NULL );
	}


	/**
	 * Edycja parametru
	 *
	 * @param array $param
	 */
	public function EditAction($param) {
		//$this->partialTemplate = 'Edit.tpl';


		$objVariable = SetupDAL::GetVariableByName($param["variable"]);

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

	/**
	 * Czyszczenie cache strony
	 *
	 */
	public function ClearCacheAction() {

			
		$directory = PATH_SITE_CACHE;

		if( !$dirhandle = @opendir($directory) )
		return;

		while( false !== ($filename = readdir($dirhandle)) ) {
			if( $filename != "." && $filename != ".." ) {
				$filename = $directory. "/". $filename;
				if(is_file($filename)) {
					@unlink($filename);
				}
			}
		}
	}

	public function ManualPathInstallerAction($param) {
		if(Request::IsPost()) {
			$post = Request::GetAllPost();
			$controller = $post['controller'];
			$method = $post['method'];
			$matchUri = $post['matchUri'];
			$actionSetText = $post['actionSet'];
			$paramSet = $post['param'];
			$module = $post['module'];
			$actionSet = array();
			$actionSetText = explode(',', $actionSetText);
			foreach($actionSetText as $item) {
				$item = explode(':', $item);
				if(isset($item[0]) && isset($item[1]) && isset($item[2]))
				$actionSet[] = array('module'=>$item[0], 'controller'=>$item[1], 'method'=>$item[2]);
			}
			$paramText = array();
			$paramText = explode(',', $paramSet);
			//Utils::ArrayDisplay($paramText);
			foreach($paramText as $item) {
				$item = explode(':', $item);
				if(isset($item[0]) && isset($item[1]))
				$paramArray[] = array('var'=>$item[0], 'value'=>$item[1]);
			}

			$router = new MfRouter();
			$router->SetController($controller);
			$router->SetMethod($method);
			$router->SetUrl($matchUri);
			$router->SetParam(serialize($paramArray));
			$router->SetConfig(serialize(array('module'=>$module, 'actionSet'=>$actionSet)));

			MfRouterDAL::Save($router);

			Router::GenerateDbRoutes();
		}
	}

	public function PackageInstallerAction($param) {
		$dir    = Config::Get('PATH_CORE').'/modules';
		$files = scandir($dir);

		$modules = array();
		foreach($files as $file) {
			if(preg_match('((.*)(.zip))', $file)) {
				$modules[] = str_replace('.zip', '', $file);
			}
		}

		$this->smarty->assign('modulesList', $modules);

		if(isset($param['package'])) {
			$installer = new ModuleInstaler($param['package']);
		}

		
	}

	


	/**
	 * Wspolna metoda
	 *
	 */
	public function preDispatch($param) {
		
		//$this->RunShared('Admin');
		
		$this->Run($param);
		//$admin = AuthDAL::GetAdmin();
		$this->RunShared('Auth', array());
		$this->smarty->assign('titleAdmin', 'Administracja');
		$struct = array(
		//'User' => array('User' => 'Index'),
		'Słowniki' => array('Dictionary' => 'Index'),
		'Zmienne serwisu' => array('Setup' => '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),
		//'Parametry 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) {
		
	}
}
?>