<?php
require_once ('JavaScriptUtils.class.php');
require_once ('InstallationProgress.class.php');

/**
 * @copyright  Aurigma Inc. 2008-2009 All rights reserved.
 * @version    1.0
 */
class BaseControl {
	/* ----------------------------------------------------------------------
	 * Non-public members
	 * ----------------------------------------------------------------------*/	
	static $_controlID = 1;
	
	static $_registeredClientScripts = array();
	static $_renderedClientScripts = array();	
		
	static $_defaultJavaAppletJarFileName = 'ImageUploader6.jar';
	static $_defaultActiveXCabFileName = 'ImageUploader6.cab';	
	static $_IUEmbedMainScriptName = 'iuembed.js';
	static $_IUEmbedInstallationProgressScriptName = 'iuembed.InstallationProgress.js';		

	//>>>AUTOGENERATED
	var $_embeddedActiveXControlVersion = "6.5.35.0";
	var $_embeddedJavaAppletVersion = "6.5.35.0";
	//AUTOGENERATED<<<
		
	var $_javaScriptWriterClassName = '';
	
	var $_id = '';
	var $_width = 600;
	var $_height = 400;
	
	var $_activeXControlEnabled = true;
	var $_activeXControlVersion;
	var $_javaAppletEnabled = true;
	var $_installationProgress;
	var $_clientFullPageLoad = array();
	var $_clientInitWriter = array();
	
	var $_scriptsDirectory = '';
	var $_activeXControlClassId = '';
	var $_activeXControlProgId = '';
	var $_activeXControlCabFileName = '';
	var $_javaAppletJarFileName = '';	
	var $_javaAppletClassName = '';		
	

	function _renderParams() {
		
	}
		
	
	function _renderEvents() {
		
		
	}
	
		
	/**
	 * @param string $path
	 * @return string
	 */
	function _relativePath($path) {
		if (array_key_exists('DOCUMENT_ROOT', $_SERVER)) {
			$docRoot = str_replace("\\", "/", $_SERVER['DOCUMENT_ROOT']);
		} elseif (array_key_exists('SCRIPT_NAME', $_SERVER) && array_key_exists('SCRIPT_FILENAME', $_SERVER)) {
			$scriptName = $_SERVER['SCRIPT_NAME'];
			$scriptFileName = str_replace("\\", "/", $_SERVER['SCRIPT_FILENAME']);			
			$pos = strrpos($scriptFileName, $scriptName);
			if ($pos == strlen($scriptFileName) - strlen($scriptName)) {
				$docRoot = substr($scriptFileName, 0, $pos);
			}
		}

		if ($docRoot) {
			return str_replace($docRoot, '', str_replace("\\", "/", realpath($path)));
		}
		else {
			return $path;
		}
	}
	

	function _renderRegisteredClientScripts() {
		$scriptFileNames = array(BaseControl::$_IUEmbedMainScriptName);
		
		if ($this->_installationProgress->getVisible()) {
			array_push($scriptFileNames, BaseControl::$_IUEmbedInstallationProgressScriptName);
		}

		$this->registerClientScripts($scriptFileNames);

		$scriptsDirectory = $this->getScriptsDirectoryResolved();
		
		foreach (BaseControl::$_registeredClientScripts as $script) {
			if (!in_array($script, BaseControl::$_renderedClientScripts)) {
				array_push(BaseControl::$_renderedClientScripts, $script);

				echo "<script src=\"" . $scriptsDirectory . $script . "\" type=\"text/javascript\"></script>\n";			
			}			
		}		
	}
	
	
	/* ----------------------------------------------------------------------
	 * Public members
	 * ----------------------------------------------------------------------*/	

	
	/**
	 * This is method BaseControl
	 *
	 * @param string $id Creates the instance of control
	 * @param int|string $width The width of control
	 * @param int|string $height The width of control
	 */
	function BaseControl($id, $width, $height) {
		if ($id != null) {
			$this->_id = $id;
		}
		else {
			$this->_id = 'iu' . BaseControl::$_controlID;
			BaseControl::$_controlID = BaseControl::$_controlID + 1;
		}
		
		if ($width != null) {		
			$this->_width = $width;
		}
		
		if ($height != null) {		
			$this->_height = $height;
		}		
		
		$this->_installationProgress = new InstallationProgress($this);	
		
		$this->_activeXControlVersion = $this->_embeddedActiveXControlVersion;
		$this->_javaAppletVersion = $this->_embeddedJavaAppletVersion;		

		$this->_extenders = array();
	}
	
	
	//Common properties
	
	/**
	 * Get ID
	 * 
	 * @return string
	 */
	function getId() {
		return $this->_id;			
	}
	
	/**
	 * Set ID
	 * 
	 * @param string $value
	 */
	function setId($value) {
		$this->_id = $value;			
	}
	
	/** 
	 * Get width
	 * 
	 * @return int|string
	 */
	function getWidth() {
		return $this->_width;			
	}
	
	/**
	 * Set width
	 * 
	 * @param int|string $value
	 */
	function setWidth($value) {
		$this->_width = $value;			
	}
	
	/**
	 * Get height
	 * 
	 * @return int|string
	 */
	function getHeight() {
		return $this->_height;			
	}
	
	/**
	 * Set height
	 * 
	 * @param int|string $value
	 */
	function setHeight($value) {
		$this->_height = $value;			
	}	
	
	
	/**
	 * Get ...
	 * 
	 * @return string
	 */			
	function getScriptsDirectory() {
		return $this->_scriptsDirectory;
	}
	
	/**
	 * Set ...
	 * 
	 * @param string value
	 */
	function setScriptsDirectory($value) {
		$this->_scriptsDirectory = $value;
	}		
	
	
	/**
	 * Get ...
	 * 
	 * @return bool
	 */	
	function getActiveXControlEnabled() {
		return $this->_activeXControlEnabled;
	}
	
	/**
	 * Set ...
	 * 
	 * @param bool $value
	 */
	function setActiveXControlEnabled($value) {
		$this->_activeXControlEnabled = $value;
	}
	
	
	/**
	 * Get ...
	 * 
	 * @return string
	 */		
	function getActiveXControlVersion() {
		return $this->_activeXControlVersion;
	}
	
	/**
	 * Set ...
	 * 
	 * @param string $value
	 */
	function setActiveXControlVersion($value) {
		$this->_activeXControlVersion = $value;
	}
	

	/**
	 * Get ...
	 * 
	 * @return bool
	 */		
	function getJavaAppletEnabled() {
		return $this->_javaAppletEnabled;
	}
	
	/**
	 * Set ...
	 * 
	 * @param bool $value
	 */
	function setJavaAppletEnabled($value) {
		$this->_javaAppletEnabled = $value;
	}
	
	
	/**
	 * Get ...
	 * 
	 * @return string
	 */		
	function getJavaAppletVersion() {
		return $this->_javaAppletVersion;
	}
	
	/**
	 * Set ...
	 * 
	 * @param string $value
	 */
	function setJavaAppletVersion($value) {
		$this->_javaAppletVersion = $value;
	}
	
	
	/**
	 * Get ...
	 * 
	 * @return InstallationProgress
	 */		
	function getInstallationProgress() {
		return $this->_installationProgress;
	}
	
	
	/**
	 * Add ...
	 * 
	 * @param string $clientFunction
	 */		
	function addClientFullPageLoad($clientFunction) {
		if (!in_array($clientFunction, $this->_clientFullPageLoad)) {			
			array_push($this->_clientFullPageLoad, $clientFunction);
		}
	}
	
	/**
	 * Remove ...
	 * 
	 * @param string $clientFunction
	 */
	function removeClientFullPageLoad($clientFunction) {
		$key = array_search($clientFunction, $this->_clientFullPageLoad);  
		if ($key !== false) {			
			array_splice($this->_clientFullPageLoad, $key, 1);
		}
	}
	
	function addExtender($extender) {
		if (!in_array($extender, $this->_extenders)) {
			array_push($this->_extenders, $extender);
		}
	}

	function removeExtender($extender) {
		$key = array_search($extender, $this->_extenders);
		if ($key !== false) {
			array_splice($this->_extenders, $key, 1);
		}
	}
	
	/**
	 * Add ...
	 * 
	 * @param string $clientFunction
	 */	
	function addClientInitWriter($clientFunction) {
		if (!in_array($clientFunction, $this->_clientInitWriter)) {			
			array_push($this->_clientInitWriter, $clientFunction);
		}
	}
	
	/**
	 * Remove ...
	 * 
	 * @param string $clientFunction
	 */
	function removeClientInitWriter($clientFunction) {
		$key = array_search($clientFunction, $this->_clientInitWriter);  
		if ($key !== false) {			
			array_splice($this->_clientInitWriter, $key, 1);
		}
	}
	
	
	//Private label specific properties
	
	
	
	
	/**
	 * Get ...
	 * 
	 * @return string
	 */		
	function getActiveXControlClassId() {
		return $this->_activeXControlClassId;
	}
	
	/**
	 * Set ...
	 * 
	 * @param string $value
	 */
	function setActiveXControlClassId($value) {
		$this->_activeXControlClassId = $value;
	}
	
	
	/**
	 * Get ...
	 * 
	 * @return string
	 */		
	function getActiveXControlProgId() {
		return $this->_activeXControlProgId;
	}
	
	/**
	 * Set ...
	 * 
	 * @param string $value
	 */
	function setActiveXControlProgId($value) {
		$this->_activeXControlProgId = $value;
	}
	
	
	/**
	 * Get ...
	 * 
	 * @return string
	 */		
	function getActiveXControlCabFileName() {
		return $this->_activeXControlCabFileName;
	}
	
	/**
	 * Set ..
	 * 
	 * @param string $value
	 */
	function setActiveXControlCabFileName($value) {
		$this->_activeXControlCabFileName = $value;
	}
	
	
	/**
	 * Get ...
	 * 
	 * @return string
	 */		
	function getJavaAppletJarFileName() {
		return $this->_javaAppletJarFileName;
	}
	
	/**
	 * Set ...
	 * 
	 * @param string $value
	 */
	function setJavaAppletJarFileName($value) {
		$this->_javaAppletJarFileName = $value;
	}
	
	
	/**
	 * Get ...
	 * 
	 * @return string
	 */		
	function getJavaAppletClassName() {
		return $this->_javaAppletClassName;
	}
	
	/**
	 * Set ...
	 * 
	 * @param string $value
	 */
	function setJavaAppletClassName($value) {
		$this->_javaAppletClassName = $value;
	}
	
	
	//Rendering
	
	/**
	 * Enter description here...
	 */
	function render() {		
		$this->_renderRegisteredClientScripts();
		
		$varName = $this->getJavaScriptWriterVariableName();
		$scriptsDirectory = $this->getScriptsDirectoryResolved();
		
		echo "<script type=\"text/javascript\">\n";
		echo "//<![CDATA[\n";
		echo 'var ' . $varName . ' = new ' . $this->_javaScriptWriterClassName . '("' 
			. JavaScriptUtils::javaScriptEncode($this->getId()) . '", '
			. $this->getWidth() . ', ' . $this->getHeight() . ");\n";
		
		if ($this->getActiveXControlEnabled()) {
			if ($this->getActiveXControlCabFileName() != '') {
				$cabFileName = $this->getActiveXControlCabFileName();
			}
			else {
				$cabFileName = BaseControl::$_defaultActiveXCabFileName;
			}
			
			echo $varName . '.activeXControlCodeBase = "' . $scriptsDirectory . $cabFileName . "\";\n";			
			
			if ($this->getActiveXControlVersion() != '') {
				//Version in ActiveX should be with commas
				echo $varName . '.activeXControlVersion = "' 
					. str_replace(".", ",", $this->getActiveXControlVersion()) . "\";\n";
			}
			
			if ($this->getActiveXControlClassId() != "") {
				echo $varName . '.activeXClassId = "' 
					. $this->getActiveXControlClassId() .  "\";\n";
			}
			
			if ($this->getActiveXControlProgId() != "") {
				echo $varName . '.activeXProgId = "' 
					. $this->getActiveXControlProgId() . "\";\n";
			}		
		}
		else {
			echo $varName . ".activeXControlEnabled = false;\n";
		}
		
		if ($this->getJavaAppletEnabled())
		{
			if ($this->getJavaAppletJarFileName() != '') {
				echo $varName . '.javaAppletJarFileName = "'
					. $this->getJavaAppletJarFileName() . "\";\n";
			}
			
			echo $varName . '.javaAppletCodeBase = "' . $scriptsDirectory . "\";\n";
			
			if ($this->getJavaAppletVersion() != '') {
				//Version in Java should be with points
				echo $varName . '.javaAppletVersion = "' .
					str_replace(',', '.', $this->getJavaAppletVersion()) . "\";\n";
			}
			
			if ($this->getJavaAppletClassName() != "") {
				echo $varName . '.javaAppletClassName = "'
					. $this->getJavaAppletClassName() . "\";\n";
			}
			
		}
		else {
			echo $this->getJavaScriptWriterVariableName() . ".javaAppletEnabled = false;\n";
		}
		
		$this->_renderParams();
		
		$this->_renderEvents();
		
		JavaScriptUtils::renderEvent($varName, 'FullPageLoad', $this->_clientFullPageLoad);
		
		$this->_installationProgress->render();
		
		foreach ($this->_clientInitWriter as $listener) {
			echo $listener . "(" . $varName . ");\n";
		}
		
		//render extenders
		foreach ($this->_extenders as $extender) {
			$extender->render();
		}

		echo $this->getJavaScriptWriterVariableName() . ".writeHtml();\n";

		echo "//]]>\n";
		echo "</script>\n";
	}	
	
		
	/**
	 * This is method getJavaScriptWriterVariableName
	 *
	 * @return string This is the return value description
	 */
	function getJavaScriptWriterVariableName() {
		return $this->_id . '_w';
	}	
	
	
	/**
	 * This is method registerClientScripts
	 *
	 * @param mixed $scriptFiles This is a description
	 */
	function registerClientScripts($scriptFiles) {
		foreach ($scriptFiles as $script) {			
			if (!in_array($script, BaseControl::$_registeredClientScripts)) {
				array_push(BaseControl::$_registeredClientScripts, $script);
			}
		}
	}
	
			
	/**
	 * This is method getScriptsDirectoryResolved
	 *
	 * @return string This is the return value description
	 */
	function getScriptsDirectoryResolved() {
		if ($this->getScriptsDirectory() == '') {
			$dir = $this->_relativePath(dirname(__FILE__) . '/Scripts');			
		}
		else {
			$dir = $this->getScriptsDirectory();			
		}
		
		if ((strlen($dir) > 0) && ($dir[strlen($dir) - 1] != '/')) {
			$dir = $dir . '/';
		}
		
		return $dir;
	}	
}

?>