<?php
class MyWeb_File_File extends MyWeb_plugin
{
	protected $_contentFile;
	protected $_dbTable;
	
	protected $_filename = 'none';
	private $_typeFile;//*** smazat
	protected $_path;
	
	protected $_describeTable;//***smazat
	protected $prefix = array(0 => null, 1 => 'new', 2 => 'edit', 3 => 'search', 4 => 'list', 5 => 'index',6 => 'controller', 7 => 'list2');
	protected $_prefix = array(0 => null, 1 => 'new', 2 => 'edit', 3 => 'search', 4 => 'list', 5 => 'index',6 => 'controller', 7 => 'list2');
	protected $_currentPrefix;
	protected $_prefixModelPath;//***smazat
	
	//melo by to byt pole kvuli pridavani
	protected $_default_path_form_ini = '../application/forms/';//na konci cesty mus� b�t lom�tko
	protected $_default_path_form_php = '../application/models/Form/';//na konci cesty mus� b�t lom�tko
	protected $_default_path_model_php = '../application/models/';//na konci cesty mus� b�t lom�tko
	protected $_default_path_model_table_php	=	'../application/models/DbTable/';//model
	protected $_default_path_controller_php	=	'../application/controllers/';//cesta ke controllers
	protected $_default_path_view_php			=	'../application/views/scripts/';//cesta ke views
	
	
	public function get_contentFile()
	{
		return $this->_contentFile;
	}
	public function set_contentFile($content)
	{
		$this->_contentFile = $content;
	}
	/*
	public function add_contentFile($content)
	{
		//*** tahle trida by mohla byt abstraktni protoze se bude implementovat az v List,Edit ...
		$this->_contentFile = $this->_contentFile.'\n\n'.$content;
	}*/
	
	
	public function get_defaultPathFormIni()
	{
		return $this->_default_path_form_ini;
	}
	public function set_defaultPathFormIni($value)
	{
		$this->_default_path_form_ini = $value;
	}
	
	public function get_defaultPathFormPhp()
	{
		return $this->_default_path_form_php;
	}
	public function set_defaultPathFormPhp($value)
	{
		$this->_default_path_form_php = $value;
	}
	
	public function get_defaultPathModelTablePhp()
	{
		return $this->_default_path_model_table_php;
	}
	public function set_defaultPathModelTablePhp($value)
	{
		$this->_default_path_model_table_php = $value;
	}
	
	public function get_defaultPathControllerPhp()
	{
		return $this->_default_path_controller_php;
	}
	public function set_defaultPathControllerPhp($value)
	{
		$this->_default_path_controller_php = $value;
	}
	
	public function get_defaultPathModelPhp()
	{
		return $this->_default_path_model_php;
	}
	public function set_defaultPathModelPhp($value)
	{
		$this->_default_path_model_php = $value;
	}
	
	public function get_defaultPathViewPhp()
	{
		return $this->_default_path_view_php;
	}
	public function set_defaultPathViewPhp($value)
	{
		$this->_default_path_view_php = $value;
	}

	
	
	
	
	
	
	
	
	public function get_currentPrefix()
	{
		return $this->_currentPrefix;
	}
	public function set_currentPrefix($value)
	{
		$this->_currentPrefix = $value;
	}
	
	
	public function get_dbTables()
	{
		return $this->_dbTable;
	}
	public function set_dbTable($dbTable)
	{
		$this->_dbTable = $dbTable;
	}
	
	public function get_typeFile()
	{
		return $this->_typeFile();
	}
	public function set_typeFile($typeFile)
	{
		$this->_typeFile = $typeFile;
	}
	
	
	public function createFile()
	{
		$this->_describeTable = $this->_dbTable->get_describeTable();
		$this->_default_path_view_php .= $this->_dbTable->get_modelName()."/";
		//require_once 'MyWeb/File/Exception.php';
		try {
			$this->typeFile();
		}catch (MyWeb_File_Exception $e) {
            echo $e->getMessage();
		}
	}
	
	
	
	/**
	 * 
	 * Metoda nastavi defaultni cestu pokud nebyla zvolena
	 * @param $defaultPath
	 */
	public function defaultSettingsPath($defaultPath)
	{
		if (empty($this->_path)){
			$this->_path = $defaultPath;
		}
		if (!file_exists($this->_path)) {
            throw new MyWeb_File_Exception('Neexistuje adresář pro: '.$this->_path);
		}
	}
	
	/**
	 * 
	 * Metoda nahradi cestu /application/models/DbTable/  cestou /application/model/DbTable/
	 */
	protected function createPrefixPath()
	{
		$path=str_replace('models','model',$this->_path) ;
		$partsPath = explode('/',$path);
		foreach ($partsPath as $key => $value){
			$partsPath[$key] = ucfirst($value);
		}
		unset($partsPath[0]);
		reset($partsPath);
		$partsPath=implode('_',$partsPath);
		return $partsPath;
	}
	
	/**
	 * 
	 * Ulozi soubor
	 * @param string $typeFile|ini|phtml|php
	 */
	public function saveFile()
	{
		
		$pos = strpos($this->_filename, '_has_');
		if ($pos !== false){
			$stringArray = explode('_', $this->_filename);
			$stringArray[1] = ucfirst($stringArray[1]);
			$stringArray[2] = ucfirst($stringArray[2]);
			$this->_filename = ucfirst(implode('', $stringArray));
		}
		if (0 !== strcmp($this->_filename, 'none')) {
			$this->_filename = ucfirst($this->_filename);
		}
		
		
		
		
		if ((0 === strcmp($this->_filename, 'none')) && ($this->_currentPrefix > 0) && (0 === strcmp($this->_typeFile, 'phtml'))){
			$path = $this->_path.$this->prefix[$this->_currentPrefix].".".$this->_typeFile;
		}else {
			$path = $this->_path.$this->_filename.ucfirst($this->prefix[$this->_currentPrefix]).".".$this->_typeFile;
		}
		echo "<p>save file".$path."</p>";
		$file=fopen($path, "w");
		fwrite($file, $this->_contentFile);
		fclose($file);
	}

	
	
	/**
	 * Prvni pismeno prevede na male
	 * @param $string
	 */
	function lcfirst($string)
	{
		$this->addLog($this,11,4);
        $this->showMe($this->showLog(),'MyWeb_SourceFileCreator_DbTable_DbTable');
        
		$string[0] = strtolower($string[0]);   
        return $string;   
	}
}