<?php
abstract class MyWeb_File_Form_Abstract extends MyWeb_File_File
{
	protected $_stringAddsLink;
	protected $_selectForeignColumn;
	protected $_controllerLink;
	protected $_selectColumn;
	protected $_selectColumnAll;
	
	private $_getValue = 'Value';
	protected $_getID = 'ID';
	protected  $_listAddedElements = array();
	
	
	
	
	protected function foreigenKey(){
		if (is_array($this->_selectForeignColumn)){
			$index = 0;
			foreach ($this->_selectForeignColumn as $key => $value) {
				$array = explode('_', $value);
				if (3 == count($array)){
					
					$this->_controllerLink[$this->_selectForeignColumn[$key]] = mb_strtolower($array[0]);
					// hlubsi zanoreni neni mozne
					//	/minuly kontroler/article/minula akce/edit/
					//$path = '.\'/'.'sourceController'.'/'.$this->_dbTable->get_modelName().'/'.'sourceAction'.'/'.$this->_prefix[$this->_currentPrefix].'/';
					
					//link pro klasicky odkaz
					//$url = '$url->url(array(\'controller\' => \''.$this->_controllerLink[$this->_selectForeignColumn[$key]].'\', \'action\' => \'list2\'), \'default\', true)'.$path;
					//link pro otevreni noveho okna
					$urlOpenWindow = '"javascript:load'.$index.'()"';
					
					//$link = '\'<a href = "\'."'.$urlOpenWindow.'".\'. onclick="window.open('.'\\'.'\'\'.'.$urlOpenWindow.'\\'.'\'\'.\'); return false"'.'>'.$this->_controllerLink[$this->_selectForeignColumn[$key]].''.'</a>\';';
					$link = '\'<a href = "\'."'.$urlOpenWindow.'".\'">'.$this->_controllerLink[$this->_selectForeignColumn[$key]].''.'</a>\';';
					
					$this->_stringAddsLink .=
					"\n" .
					"//***************************************************************************" . "\n" .
					'$link = \'<a href = "\'.'.$urlOpenWindow.'.\'">\'.$translate->translate(\''.ucfirst($this->_controllerLink[$this->_selectForeignColumn[$key]]).'\').\'</a>\';'. "\n" .
					'$element = $this->getElement(\''.$this->_selectForeignColumn[$key].'Value'.'\');' . "\n" .
					'$element->setLabel($translate->translate(\''.ucfirst($this->_controllerLink[$this->_selectForeignColumn[$key]]).'\'));' . "\n" .
					'$element->setDescription( $link );'. "\n" .
					'$element->setDecorators(	array(\'ViewHelper\',array(\'Description\', array(\'escape\' => false, \'tag\' => false)),' . "\n" .
												'array(\'HtmlTag\', array(\'tag\' => \'dd\')),' . "\n" .
												'array(\'Label\', array(\'tag\' => \'dt\')),' . "\n" .
									'\'Errors\')); ' . "\n\n" .
					
					$this->createHidden($value);
					//nastaveni hodnoty do pole input z pole GET pokud existuje v poli GET spravny klic
					/*
					'if (key_exists(\''.$value.$this->_getValue.'\', $getValue)){' . "\n" .
        			"\t".	'$element->setValue($getValue[\''.$value.$this->_getValue.'\']);' . "\n" .
					'}'. "\n" .
					"\n" .
					//vytvori pole hidden 
					'$'.$value.$this->_getID.' = new Zend_Form_Element_Hidden(\''.$value.$this->_getID.'\');' . "\n" .
					'$'.$value.$this->_getID.'->setValue(NULL);' . "\n" .
					
					//pokud existuje v poli GET odpovidajici klic ulozi se hodnota do pole hidden
					'if (key_exists(\''.$value.'\', $getValue)){' . "\n" .
        			"\t".	'$'.$value.$this->_getID.'->setValue($getValue[\''.$value.'\']);' . "\n" .
        			'}'. "\n" .
					"\n" .*/
					"\n";
				}
				$index++;
			}
		}
	}
	
	
	/**
	 * metoda vytvori pro kazde pole input s odkazem 4 pole hidden
	 * Enter description here ...
	 */
	protected  function createHidden($idInput)
	{
		// $idInput id prvku
		// na zaklade $idInput se vyvori 4 pole hidden s id $idInput + index
		// kde prvni pole je nazev hodnoty v druhem poli stejne tak pro 3 a 4 pole
		
		$stringHidden ="";
		$nameHidden = "Input";
		$prefixInput = "Input";
		$prefixInputName = 'InputName';
		$prefixInputValue = 'InputValue';
		$indexForeignColumn = NULL;
		
		$stringHidden .= '//*** vytvoreni hidden poli pro jmeno hodnoty a hodnotu'	. "";
		
		for($index = 1; $index <= 4; $index++){
			if ($index > 2) {
				$indexForeignColumn++;
			}
			//vytvori pole hidden 
			if (($index%2) === 1){
				$stringHidden .= "\n";
				$stringHidden .= '$'.$idInput.$prefixInput."Name".$indexForeignColumn.' = new Zend_Form_Element_Hidden(\''.$idInput.$prefixInput."Name".$indexForeignColumn.'\');' . "\n";
				$stringHidden .= '$'.$idInput.$prefixInput."Name".$indexForeignColumn.'->setValue(NULL);' . "\n";
				$stringHidden .= '//$'.$idInput.$prefixInput.'Name'.$indexForeignColumn.'->setLabel(\''.$idInput.$prefixInput.'Name'.$indexForeignColumn.'\');' . "\n";
				$this->_listAddedElements[] = '$'.$idInput.$prefixInput."Name".$indexForeignColumn;
				
				// kontrolni policka input text pro kontrolu hidden poli
				//$stringHidden .= '$'.$idInput.$nameInput.'Name'.$index.' = new Zend_Form_Element_Text(\''.$idInput.$nameInput."Name".$index.'\');' . "\n";
				//$stringHidden .= '$'.$idInput.$nameInput.'Name'.$index.'->setLabel(\''.$idInput.$nameInput.'Name'.$index.'\');' . "\n";
				//$this->_listAddedElements[] = '$'.$idInput.$prefixInput."Name";
				
			}else {
				$stringHidden .= '$'.$idInput.$prefixInput."Value".$indexForeignColumn.' = new Zend_Form_Element_Hidden(\''.$idInput.$prefixInput."Value".$indexForeignColumn.'\');' . "\n";
				$stringHidden .= '$'.$idInput.$prefixInput."Value".$indexForeignColumn.'->setValue(NULL);' . "\n";
				$stringHidden .= '//$'.$idInput.$prefixInput.'Value'.$indexForeignColumn.'->setLabel(\''.$idInput.$prefixInput.'Value'.$indexForeignColumn.'\');' . "\n";
				$this->_listAddedElements[] = '$'.$idInput.$prefixInput."Value".$indexForeignColumn;
				
				// kontrolni policka input text pro kontrolu hidden poli
				//$stringHidden .= '$'.$idInput.$nameInput.'Value'.$index.' = new Zend_Form_Element_Text(\''.$idInput.$nameInput."Value".$index.'\');' . "\n";
				//$stringHidden .= '$'.$idInput.$nameInput.'Value'.$index.'->setLabel(\''.$idInput.$nameInput.'Value'.$index.'\');' . "\n";
				//$this->_listAddedElements[] = '$'.$idInput.$prefixInput."Value";
			}
		}
		$stringHidden .= "\n";
		return $stringHidden;
	}
	
	
	
	
	/**
	 * metoda vybere sloupce tabulky netvorici cizy klic a tvorici
	 */
	protected  function set_defaultColumn()
	{
		$this->_selectColumn[] = current($this->_dbTable->get__modelPrimary());
		foreach ($this->_dbTable->get_describeTable() as $key => $value){
			//if ((strcmp($this->_describeTable[$key]['Key'], 'PRI') !== 0) && (strcmp($this->_describeTable[$key]['Key'], 'MUL') !== 0)){
			if ((strcmp($this->_describeTable[$key]['Key'], 'PRI') !== 0)){
				//&& (strcmp($this->_describeTable[$key]['Key'], 'MUL') !== 0)
				$this->_selectColumn[] = $this->_describeTable[$key]['Field']; //pole se vsemi sloupci vyjma primarniho
			}
			if ((strcmp($this->_describeTable[$key]['Key'], 'MUL') == 0)){
				$this->_selectForeignColumn[] = $this->_describeTable[$key]['Field'];//pole s pouze cizimi klici 
			}
			$this->_selectColumnAll[] = $this->_describeTable[$key]['Field'];//pole se vsemi slouci
		}
	}
	
	
	
	/**
	 * metoda vytvori seznam pridavanych prvku do formulare
	 * 
	 */
	protected function listAddedElements($value)
	{
		$this->_listAddedElements[] = '$'.$value;
	}
}