<?php
class MyWeb_File_View_List extends MyWeb_File_View_View
{
	
	private $_listModul;
	private $_addContent;
	private $_tableHead = NULL;
	
	private $_controller;
	private $_action;
	private $_modul = 'default';
	
	public function set_controller($controller)
	{
		$this->_controller = $controller;
	}
	
	public function get_controller()
	{
		return $this->_controller;
	}
	
	public function set_action($action)
	{
		$this->_action = $action;
	}
	
	public function get_action()
	{
		return $this->_action;
	}
	
	public function set_listModul($listModul)
	{
		$this->_listModul = $listModul;
	}
	
	public function get_listModul()
	{
		return $this->_listModul;
	}
	
	/**
	 * metoda vytvori nadpisy sloupcu tabulky
	 * Enter description here ...
	 */
	private function set_defaultTableHead()
	{
		//nastaveni prvniho primarniho klice (primarnich klicu muze byt vic)
		$this->_currentPrefix = 4;
		$this->_tableHead .= "'".current($this->_dbTable->get__modelPrimary());
		$this->_action = $this->prefix[$this->_currentPrefix];
		$this->_controller = $this->_dbTable->get_modelName();
		
		foreach ($this->_dbTable->get_describeTable() as $key => $value){
			if ((strcmp($this->_describeTable[$key]['Key'], 'PRI') !== 0)){
				$this->_tableHead .= "','".$this->_describeTable[$key]['Field'];
			}
		}
		$this->_tableHead .= "'";
	}
	
	public function get_tableHead()
	{
		return $this->_tableHead;
	}
	
	public function set_tableHead($tableHead)
	{
		$this->_tableHead = $tableHead;
	}
	
	
	private function defaultSettings()
	{
		if (is_null($this->_describeTable)){
			$this->_describeTable = $this->_dbTable->get_describeTable();
		}
		if (is_null($this->_listModul)){
			$this->_listModul = 'list'.ucfirst($this->_dbTable->get_modelName());
		}
		$this->set_defaultTableHead();
		if (is_null($this->get_cssLink())){
			$cssLink = '$this->headLink()->appendStylesheet($this->DinamicPath().'.'\'css/table.css\''.',\'all\');';
			$this->set_cssLink($cssLink);
		}
	}
	
	
	
	
	public function __construct($dbTable,$path = null)
	{
		$this->_dbTable = $dbTable;
		$this->defaultSettings();
		$this->_path = $path;	//*** pridano zajistuje pouziti nove cesty od uzivatele misto default
		$this->set_defaultDirectoryPath($this->_dbTable->get_modelName());
		$this->defaultSettingsPath($this->_default_path_view_php);
		$this->set_typeFile('phtml');
		$this->_currentPrefix = 4;
	}
	
	/**
	 * 
	 * Metoda prida retezec do vysledneho souboru
	 * @param $content
	 */
	public function add_contentFile($content)
	{
		$this->_addContent = $content;
	}
	
	
	public function createContent()
	{
		$stringFile = "";
		$stringFile .=	'<?php' . "\n" .
								$this->get_cssLink().
								'$translate = Zend_Registry::get(\'Zend_Translate\');'				. "\n" .
								'$request = Zend_Controller_Front::getInstance()->getRequest();'	. "\n\n" .
								
								'if (!is_null($this->pager)){'													. "\n" .
									"\t".'try {'																. "\n" .
									"\t\t".'$paginator = $this->PagerToPaginator($this->pager,$this->select);'	. "\n\n" .
								
									"\t\t".'$head = NULL;' . "\n" .
									"\t\t".'//$head[\'nameHeader\'] = array($translate->translate(\'ID\'),$translate->translate(\'Title\'),$translate->translate(\'Date\'),$translate->translate(\'Username\'),$translate->translate(\'State\'));'. "\n" .
									"\t\t".'$selectData = array('.$this->_tableHead .');'						. "\n" .
									"\t\t".'$links[\''.current($this->_dbTable->get__modelPrimary()).'\'][\'link\'] = $this->url(array(\'controller\' => $request->getControllerName(), \'action\' => \'edit\'), $request->getModuleName(), true);' . "\n" .
									"\t\t".'$links[current($selectData)][\'get\'][0] = current($selectData);'		. "\n" .
									"\t\t".'$columnValue = NULL;'													. "\n" .
									"\t\t".'$hidden = NULL;'														. "\n" .
									"\t\t".'$caption = array($translate->translate(\''.$this->_dbTable->get_modelName().'\'));'						. "\n" .
									"\t\t".'print $this->Table($paginator,$selectData,$columnValue,$head,$hidden,$caption,$links);' . "\n" .
									"\t\t".'print $this->paginationControl($this->pager,\'Sliding\',\'paginator.phtml\');' . "\n" .
									"\t\t".$this->_addContent 			. "\n" .
									"\t".	'}catch (Exception $e){'	. "\n" .
									"\t\t".		'echo $e->getMessage();'. "\n" .
									"\t".	'}'							. "\n" .
								'}else {'. "\n" .
									"\t".'echo "Nebyly nalezeny zadne zaznamy.";'. "\n" .
								'}'. "\n" .
								'?>' . "\n";
		$this->set_contentFile($stringFile);
	}
}