<?php
class MyWeb_JavaScript_Popup_Main
{
	// je nutne aby nastaveni bylo provazane s nastavenim table view helperu
	
	
	private $_nameLinkOpenWindow = "openWin"; //neni potreba nastavovat z venci
	
	private $_linkPopupWindow = array();
	private $_levelDirectory = '../';
	
	private $_sourceAction = "none";
	private $_sourceController = "none";
	
	private $_namePopupWindow = "";
	private $_paramGetLinkOpenPopup = array();
	private $_nameInputs = array();
	private $_controllerLinkOpenWindow = array();
	private $_idHidden = array();
	
	private $_stringWrite;
	private $_stringWriteLinks;
	
	private $_nameFile = "";
	private $_nameFileLinka = "";
	
	
	public function setControllerLinkOpenWindow($value)
	{
		$this->_controllerLinkOpenWindow = $value;
	}
	public function getControllerLinkOpenWindow()
	{
		return $this->_controllerLinkOpenWindow;
	}
	public function addControllerLinkOpenWindow($value)
	{
		$this->_controllerLinkOpenWindow[] = $value;
	}
	
	
	
	
	public function setNameFileLink($name)
	{
		$this->_nameFileLink = $name;
	}
	
	public function getNameFileLink()
	{
		return $this->_nameFileLink;
	}
	
	
	
	
	public function setNameFile($name)
	{
		$this->_nameFile = $name;
	}
	
	public function getNameFile()
	{
		return $this->_nameFile;
	}
	
	
	/**
	 * nastavi id vystupnich poli
	 * @param string $value
	 */
	public function addIdInputs($id)
	{
		// childMsg0 ovlada do kterych policek se vrati hodnota z popup
		// hodnota childMsg0 bude na pevno nastavena pouze se pro kazdy input bude menit index
		// v mainu stejne musi mit kazdy jine jmeno jinak by se do dvou policek vracela jedna hodnota
		$this->_nameInputs[] = $id;
	}
	/**
	 * vrati hodnotu id inputu
	 */
	public function getIdInputs()
	{
		return $this->_nameInputs;
	}
	
	
	public function setIdHidden($value)
	{
		$this->_idHidden = $value;
	}
	public function getIdHidden()
	{
		return $this->_idHidden;
	}
	public function addIdHidden($indexInputs,$value)
	{
		$this->_idHidden[$indexInputs][] = $value;
	}
	
	
	/**
	 * 
	 * nastaveni kontrolleru pro ktery je js urcen
	 * @param string $value
	 */
	public function getLinkPopupWindow()
	{
		return $this->_linkPopupWindow;
	}
	
	/**
	 * metoda vrati kontroler pto ktery je js urcen
	 */
	public function addLinkPopupWindow($name,$link)
	{
		$this->_linkPopupWindow[$name] = $link;
	}
	
	
	
	
	
	
	/**
	 * 
	 * nastaveni kontrolleru pro ktery je js urcen
	 * @param string $value
	 */
	public function setSourceController($value)
	{
		$this->_sourceController = $value;
	}
	
	/**
	 * metoda vrati kontroler pto ktery je js urcen
	 */
	public function getSourceController()
	{
		return $this->_sourceController;
	}
	
	
	/**
	 * 
	 * metoda nastav9 akci pro kterou je js urcen
	 * @param string $value
	 */
	public function setSourceAction($value)
	{
		$this->_sourceAction = $value;
	}
	
	/**
	 * 
	 * matoda vrati akci pro kterou je js urcen
	 */
	public function getSourceAction()
	{
		return $this->sourceAction;
	}
	
	
	
	private function validationSettings($method = 'js')
	{
		
		//kontrola nastaveni sourceController
		if (strcmp($this->_sourceController, 'none') === 0){
			$zprava = "MyWeb_JavaScript_Popup_Main: Neni nastaven sourceController: "."\n";
        	throw new Exception($zprava);
		}
		if (strcmp($this->_sourceAction, 'none') === 0){
			$zprava = "MyWeb_JavaScript_Popup_Main: Neni nastaven sourceAction: "."\n";
        	throw new Exception($zprava);
		}
		if (count($this->_linkPopupWindow) <= 0){
			$zprava = "MyWeb_JavaScript_Popup_Main: Neni nastaven zadny link: "."\n";
        	throw new Exception($zprava);
		}
		if (count($this->_linkPopupWindow) !== count($this->_nameInputs)){
			$zprava = "MyWeb_JavaScript_Popup_Main: Nesouhlasi pocet id a pocet odkazu: "."\n";
        	throw new Exception($zprava);
		}
		if (count($this->_controllerLinkOpenWindow) !== count($this->_nameInputs)){
			$zprava = "MyWeb_JavaScript_Popup_Main: Nesouhlasi pocet controlleru a pocet odkazu: "."\n";
        	throw new Exception($zprava);
		}
		
		
		
		if (strcmp($method, 'writeFileJs') === 0){
			if (empty($this->_nameFile)){
				$zprava = "MyWeb_JavaScript_Popup_Main: Neni nastaveno jmeno souboru pro ulozeni: "."\n";
        		throw new Exception($zprava);
			}
		}
	}
	
	
	/**
	 * 
	 * metoda zajisti zavolani validace a vytvoreni javascriptu
	 */
	public function js()
	{
		$js = "";
		try {
			$this->validationSettings();
			$js = $this->generateJs();
		}
		
		catch (Exception $e){
    		echo $e->getMessage();
    	}
		return $js;
	}
	
	
	
	
	
	/**
	 * 
	 * Metoda vytvori javascript kod
	 */
	private function generateJs()
	{
		reset($this->_linkPopupWindow);
		
		$this->_stringWrite = "";
		
		$this->_stringWrite .= 
						"\t".		'$(function(){'.						"\n" .
						"\t"."\t".		'// don\\\'t forget this init!'.	"\n" .
						"\t"."\t".		'$.initWindowMsg();'.				"\n" .
						"\t"."\t".		'var childWin;'.					"\n"."\n";
		
		
		
		// funkce javascript pro nalezeni substringu a ziskani url
		$this->_stringWrite .= 
		"\t".		'//funkce slouzi k overeni existence substringu'.							"\n" .
		"\t".		'jQuery.isSubstring = function(haystack, needle) {'.						"\n" .
		"\t".		'return haystack.indexOf(needle) !== -1; };'.								"\n" ."\n" .
		
		"\t".		'var pathname;'.															"\n" .
		"\t".		'// funkce naplni promenou pathname hodnotou url'.							"\n" .
		"\t".		'$(document).ready(function() {'.											"\n" .
		"\t"."\t".		'pathname = window.location.pathname;'.									"\n" .
		"\t"."\t".		'//document.write(pathname);'.											"\n" .
		"\t".	'});'.																			"\n"."\n";
		
		$this->_stringWrite .= 'if( $.isSubstring(pathname, "'.$this->_sourceAction.'") && $.isSubstring(pathname, "'.$this->_sourceController.'")) {'.	"\n";
		
		$indexLinkOpenWindow = 0;
		foreach ($this->_linkPopupWindow as $key => $value){
        	$this->_stringWrite .= 
        		"\t".		'$(\'#'.$this->_nameLinkOpenWindow.$indexLinkOpenWindow.'\').click(function(){'.						"\n" .
        		"\t"."\t".		'childWin = window.open(\''.$value.'\',\''.$key.'\', "width=500, height=500, location=no, menubar=no, scrollbars=no, status=no, toolbar=no");'.	"\n" .
        		"\t"."\t".		'if (window.focus) {childWin.focus()}});'.							"\n" ."\n";
        	
        $indexLinkOpenWindow++;
		}
        $this->_stringWrite .= '};'.	"\n"."\n"."\n"."\n";
        
        
        
        $indexInputs = 0;
        foreach ($this->_nameInputs as $key => $value){
        	$this->_stringWrite .=	"\t".		'$.windowMsg("childMsg'.$indexInputs.'", function(message) {'.		"\n" .
        							"\t"."\t".		'$(\'#'.$value.'\').val(message);'.					"\n" .
        							"\t".		'});'.												"\n" ."\n";
        	
        	foreach ($this->_idHidden[$key] as $key => $value){
        		$this->_stringWrite .=	"\t".		'$.windowMsg("childHidden'.$indexInputs.$key.'", function(message) {'.		"\n" .
        							"\t"."\t".		'$(\'#'.$value.'\').val(message);'.					"\n" .
        							"\t".		'});'.												"\n" ."\n";
        		//
        		//.$indexInputs.$key
        	}
        $indexInputs++;
        }
		$this->_stringWrite .= '});'.	"\n" ."\n";
		return $this->_stringWrite;
	}
	
	
	/**
	 * 
	 * Metoda zajisti ulozani javascriptu do souboru
	 */
	public function writeFileJs()
	{
		$js = "";
		try {
			$this->validationSettings('writeFileJs');
			$js = $this->generateJs();
			
			$filename = $this->_nameFile.'.js';
			$file = fopen($filename, 'w');
			
			fwrite($file, $js);
			fclose($file);
		}
		
		catch (Exception $e){
    		echo $e->getMessage();
    	}
	}
	
	
	/**
	 * 
	 * Metoda zajisti ulozani javascriptu(main links) do souboru
	 */
	public function writeFileJsLinks()
	{
		$jsLink = "";
		try {
			$this->validationSettings('writeFileJs');
			$jsLink = $this->jsOpenLinks();
			
			$filename = $this->_nameFileLink.'.js';
			$file = fopen($filename, 'w');
			
			fwrite($file, $jsLink);
			fclose($file);
		}
		
		catch (Exception $e){
    		echo $e->getMessage();
    	}
	}
	
	
	/**
	 * 
	 * Metoda vytvori javascript pro odkazy otevirajici popup window
	 */
	public function jsOpenLinks()
	{
		$this->_stringWriteLinks = "";
		$indexLinkOpenWindow = 0;
		
		$arrayPopupParam = $this->createStringParamGetLinkPopup();
		$stringPopupParam = "";
		foreach ($this->_nameInputs as $key => $value){
			$stringPopupParam = "";
			$this->_stringWriteLinks .= 	
			"\t".		'function load'.$indexLinkOpenWindow.'() {'	."\n".
			"\t"."\t".		'var load'.$indexLinkOpenWindow.' = window.open("'.$this->_levelDirectory.$this->_controllerLinkOpenWindow[$key].'/list2/idInput/'.$key.'/';
			
			if (key_exists($key, $arrayPopupParam)){
				$stringPopupParam = implode('/',$arrayPopupParam[$key])."/";
			}
			
			$this->_stringWriteLinks .= $stringPopupParam.'",\'gg\',\'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no\');'."\n".
			"\t".		'}'					."\n"."\n";
       		$indexLinkOpenWindow++;
		}
		return $this->_stringWriteLinks;
	}
	
	
	/**
	 * metoda pridava do odkazu otevirajici popup window dalsi parametry
	 * @param string $nameParam
	 * @param string $valueParam
	 */
	public function addParamGetLinkOpenPopup($indexLink,$nameParam,$valueParam)
	{
		$this->_paramGetLinkOpenPopup[$indexLink][$nameParam] = $valueParam;
	}
	
	
	/**
	 * metoda vytvori pole ve kterem jsou klice jako hodnoty
	 * metoda vytvori pole ze ktereho se pomoci implode vitvori string parametru get
	 */
	private function createStringParamGetLinkPopup()
	{
		//each
		$paramArray = array();
		
		foreach ($this->_paramGetLinkOpenPopup as $indexLink => $paramArrayLink) {
			foreach ($paramArrayLink as $key => $value) {
				$paramArray[$indexLink][] = $key;
				$paramArray[$indexLink][] = $value;
			}
		}
		return $paramArray;
	}
	
	/**
	 * metoda prida zadany pocet prechodu z adresare ../ 
	 * Enter description here ...
	 * @param $count
	 */
	public function levelDirectory($count){
		//echo "<p>".$count."</p>";
		$this->_levelDirectory = '';
		for ($level = 0; $level < $count; $level++){
			$this->_levelDirectory .= '../';
		}
	}
}