<?php
/**
 * trida automatizuje necastejsi pouziti tridy File s tridou DbTable
 */
class MyWeb_File_CreatorFile extends MyWeb_plugin
{
	
	
	private static $_tables;
	public static $_defaultAllFile = array(
					'formNewIni'		=>	null,
					'formEditIni'		=>	null,
					'formSearchIni'		=>	null,
					'formNewPhp'		=>	null,
					'formEditPhp'		=>	null,
					'formSearchPhp'		=>	null,
					'model'				=>	null,
					'modelTable'		=>	null,
					'controller'		=>	null,
					'viewList'			=>	null,
					'viewList2'			=>	null,
					'viewNew'			=>	null,
					'viewEdit'			=>	null,
					'viewIndex'			=>	null,
					'viewSearch'		=>	null,
	
					
					//'formLogoutPhp'		=>	null,
					//'formLoginPhp'		=>	null,
					//'formLoginIni'		=>	null,
					//'viewLogout'		=>	null,
					//'viewLogin'			=>	null
					);
	/*
	[index]['table']	=	table
	[index]['params']['formNewIni']		=	'path'
					['formEditIni']		=	'path'
					['formSearchIni']	=	'path'
					['formNewPhp']		=	'path'
					['formEditPhp']		=	'path'
					['formSearchPhp']	=	'path'
					['model']			=	'path'
					['modelTable']		=	'path'
					['controller']		=	'path'
					['viewList']		=	'path'
					['viewNew']			=	'path'
					['viewEdit']		=	'path'
					['viewIndex']		=	'path'
					['viewSearch']		=	'path'
	*/
					
	/**
	 * 
	 * metoda nastavi defaultni hodnoty pro hromadne pouziti
	 * @param array DbTable $tables
	 * @param string $typeFile
	 * @param string $path
	 */
	public static function createFiles($tables,$typeFile = NULL,$path = NULL)
	{
		/*
		 * $tables 		- 	musi byt vzdy zadano jako pole ve formatu vystupu createTables tj. $tables[index]['table']
		 * 
		 * *** navic moznost nastaveni defaultnich cest
		 * */
		//echo"<p>"."createFiles"."</p>";
		self::addLog('MyWeb_File_CreatorFile',1,4,'ABSTRACT');
        self::showMe(self::showLog(),'LOG2');
        self::$_tables =$tables;
        
        if (is_array($tables)){
        	foreach(self::$_tables as $key => $value){
        		if (!array_key_exists('params', self::$_tables[$key])){
        			//echo"<p>"."createFiles"."</p>";
        			self::$_tables[$key]['params']=self::$_defaultAllFile;
        		}
        	}
        }else {
        		//exception;
        }
        self::createTableFile();
	}
	
	
	/**
	 * 
	 * Metoda vytvari pole file objektu
	 */
	private function createTableFile()
	{
		//echo"<p>"."createTableFile"."</p>";
		self::addLog('MyWeb_File_CreatorFile',2,4,'ABSTRACT');
        self::showMe(self::showLog(),'LOG2');
        
        
		foreach (self::$_tables as $index => $arrayTypeFile){
			foreach (self::$_tables[$index]['params'] as $typeFile => $path){
				//echo"<p>"."$typeFile"."</p>";
				$file[$index][$typeFile] = self::createTypeFile($typeFile,$index,$path);
			}
		}
		
		$object = new MyWeb_File_Languages_Languages($path);
		$object->createContent();
		$object->saveFile();
	}
	
	
	
	/**
	 * 
	 * Metoda vytvari prislusne objekty souboru
	 */
	public function createTypeFile($typeFile,$index,$path)
	{
		//echo "<p>".$typeFile."<p>";
		switch ($typeFile) {
			//form ini
			case 'formNewIni':
				$object = new MyWeb_File_Form_NewIni(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'formEditIni':
				$object = new MyWeb_File_Form_EditIni(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'formSearchIni':
				$object = new MyWeb_File_Form_SearchIni(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'formLoginIni':
				$object = new MyWeb_File_Form_LoginIni(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			// form php(nacteni ini file)
			case 'formNewPhp':
				$object = new MyWeb_File_Form_New(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'formEditPhp':
				$object = new MyWeb_File_Form_Edit(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'formSearchPhp':
				$object = new MyWeb_File_Form_Search(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'formLoginPhp':
				$object = new MyWeb_File_Form_Login(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'formLogoutPhp':
				$object = new MyWeb_File_Form_Logout(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			//model,modelDb,controller
			case 'model':
				$object = new MyWeb_File_Model_Model(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'modelTable':
				$object = new MyWeb_File_DbTable_DbTable(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'controller':
				$object = new MyWeb_File_Controller_Controller(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			//view (new,edit,list,index,search)
			case 'viewList':
				$object = new MyWeb_File_View_List(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'viewList2':
				$object = new MyWeb_File_View_List2(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'viewNew':
				$object = new MyWeb_File_View_New(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'viewEdit':
				$object = new MyWeb_File_View_Edit(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'viewIndex':
				$object = new MyWeb_File_View_Index(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'viewSearch':
				$object = new MyWeb_File_View_Search(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'viewLogin':
				$object = new MyWeb_File_View_Login(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;
			case 'viewLogout':
				$object = new MyWeb_File_View_Logout(self::$_tables[$index]['table'],$path);
				$object->createContent();
				$object->saveFile();
				break;			
			default:self::error();
		}
		return $object;
	}
}