/** * Registers a PHP variable/class in JavaScript. * * <p>Example:</p> * <code> * <script type="text/javascript"> * <?php require_once('xoad.php'); ?> * * var arr = <?= XOAD_Client::register(array(1, 2, "string", array("Nested"))) ?>; * * alert(arr); * * </script> * </code> * * @access public * * @param mixed $var Variable/Class name to register. * * @param mixed $params When registering a variable/class you can * provide extended parameters, like class name * and callback URL. * * @return string JavaString code that represents the variable/class. * * @static * */ public static function register($var, $params = null) { $type = XOAD_Utilities::getType($var); if ($type == 'object') { $paramsType = XOAD_Utilities::getType($params); if ($paramsType != 'string') { $callbackUrl = XOAD_Utilities::getRequestUrl(); if ($paramsType == 'a_array') { if (!empty($params['class'])) { $className = $params['class']; } if (!empty($params['url'])) { $callbackUrl = $params['url']; } } } else { $callbackUrl = $params; } if (method_exists($var, XOAD_CLIENT_METADATA_METHOD_NAME)) { call_user_func_array(array(&$var, XOAD_CLIENT_METADATA_METHOD_NAME), array()); } $objectCode = array(); if (empty($className)) { $className = XOAD_Utilities::caseConvert(get_class($var)); } $meta = get_object_vars($var); $objectMeta = null; if (isset($meta['xoadMeta'])) { if (XOAD_Utilities::getType($meta['xoadMeta']) == 'object') { if (strcasecmp(get_class($meta['xoadMeta']), 'XOAD_Meta') == 0) { $objectMeta = $meta['xoadMeta']; unset($meta['xoadMeta']); unset($var->xoadMeta); } } } if (sizeof($meta) > 0) { $attachMeta = array(); foreach ($meta as $key => $value) { if (!empty($objectMeta)) { if (!$objectMeta->isPublicVariable($key)) { unset($meta[$key]); unset($var->{$key}); continue; } } $valueType = XOAD_Utilities::getType($value); if ($valueType == 'object' || $valueType == 's_array' || $valueType == 'a_array') { $var->{$key} = XOAD_SERIALIZER_SKIP_STRING . XOAD_Client::register($var->{$key}, $callbackUrl); } $attachMeta[$key] = $valueType; } $var->__meta = $attachMeta; $var->__size = sizeof($attachMeta); } else { $var->__meta = null; $var->__size = 0; } $var->__class = $className; $var->__url = $callbackUrl; $var->__uid = md5(uniqid(rand(), true)); $var->__output = null; $var->__timeout = null; $serialized = XOAD_Serializer::serialize($var); $objectCode[] = substr($serialized, 1, strlen($serialized) - 2); $objectCode[] = '"__clone":function(obj){xoad.clone(this,obj)}'; $objectCode[] = '"__serialize":function(){return xoad.serialize(this)}'; $objectCode[] = '"catchEvent":function(){return xoad.catchEvent(this,arguments)}'; $objectCode[] = '"ignoreEvent":function(){return xoad.ignoreEvent(this,arguments)}'; $objectCode[] = '"postEvent":function(){return xoad.postEvent(this,arguments)}'; $objectCode[] = '"fetchOutput":function(){return this.__output}'; $objectCode[] = '"setTimeout":function(miliseconds){this.__timeout=miliseconds}'; $objectCode[] = '"getTimeout":function(){return this.__timeout}'; $objectCode[] = '"clearTimeout":function(){this.__timeout=null}'; $classMethods = get_class_methods($var); for ($iterator = sizeof($classMethods) - 1; $iterator >= 0; $iterator--) { if (strcasecmp($className, $classMethods[$iterator]) == 0) { unset($classMethods[$iterator]); continue; } if (strcasecmp($classMethods[$iterator], XOAD_CLIENT_METADATA_METHOD_NAME) == 0) { unset($classMethods[$iterator]); continue; } if (!empty($objectMeta)) { if (!$objectMeta->isPublicMethod($classMethods[$iterator])) { unset($classMethods[$iterator]); continue; } } } if (sizeof($classMethods) > 0) { $index = 0; $length = sizeof($classMethods); $returnValue = ''; foreach ($classMethods as $method) { $methodName = XOAD_Utilities::caseConvert($method); if (!empty($objectMeta)) { $mapMethodName = $objectMeta->findMethodName($methodName); if (strcmp($mapMethodName, $methodName) != 0) { $methodName = $mapMethodName; } } $serialized = XOAD_Serializer::serialize($methodName); $returnValue .= $serialized; $returnValue .= ':'; $returnValue .= 'function(){return xoad.call(this,' . $serialized . ',arguments)}'; if ($index < $length - 1) { $returnValue .= ','; } $index++; } $objectCode[] = $returnValue; } $returnValue = '{' . join(',', $objectCode) . '}'; return $returnValue; } else { if ($type == 's_array' || $type == 'a_array') { foreach ($var as $key => $value) { $valueType = XOAD_Utilities::getType($value); if ($valueType == 'object' || $valueType == 's_array' || $valueType == 'a_array') { $var[$key] = XOAD_SERIALIZER_SKIP_STRING . XOAD_Client::register($var[$key], $params); } } } else { if ($type == 'string') { $paramsType = XOAD_Utilities::getType($params); if ($paramsType == 'string') { if (class_exists($var)) { $classObject = new $var(); $classCode = XOAD_Client::register($classObject, array('class' => $var, 'url' => $params)); $classCode = $var . '=function(){return ' . $classCode . '}'; return $classCode; } } } } } return XOAD_Serializer::serialize($var); }
/** * Registers a custom client control. * * <p>Each control includes a JS file that defines the class that * will handle the control. Additionally, you can attach a server class and * a HTML code.</p> * * @access public * * @param string $tagPrefix The tag prefix for the control, required. * @param mixed $tagName The tag name for the control, required. * @param string $jsFile The relative path to the JS file that * defines the class that will handle the * control, required. * @param string $phpFile The relative/absolute path to the PHP file * that defines the server class that is * associated with the control, optional. * @param string $url The callback URL for the server class, * optional. * @param string $htmlFile The relative/absolute path to the HTML file * that defines the code that is associated * with the control, optional. * * @return string HTML code to register the custom control. * * @static * */ public static function register($tagPrefix = 'xoad', $tagName = null, $jsFile = null, $phpFile = null, $url = null, $htmlFile = null) { if (empty($phpFile) && empty($jsFile)) { return null; } if (XOAD_Utilities::getType($tagName) == 's_array') { $returnValue = ''; foreach ($tagName as $name) { $returnValue .= XOAD_Controls::register($tagPrefix, $name, $jsFile, $phpFile, $url, $htmlFile); } return $returnValue; } if (XOAD_Utilities::getType($tagName) == 'a_array') { $returnValue = ''; foreach ($tagName as $prefix => $name) { $returnValue .= XOAD_Controls::register($prefix, $name, $jsFile, $phpFile, $url, $htmlFile); } return $returnValue; } if (empty($tagPrefix) || empty($tagName)) { return null; } $registerAttribute = $tagName == '@'; $controlName = strtolower($tagPrefix) . ':' . strtolower($tagName); if (!$registerAttribute && in_array($controlName, $GLOBALS['_XOAD_CONTROLS_LIST'])) { return null; } $phpControlName = $tagPrefix . '_Controls_' . $tagName; $jsControlName = $tagPrefix . '.controls.' . $tagName; $includeScript = ''; $returnValue = ''; if (!empty($phpFile)) { require_once XOAD_Controls::getFileName($phpFile); if (empty($url)) { $url = XOAD_Utilities::getRequestUrl(); } $phpObject = new $phpControlName(); XOAD_Client::privateMethods($phpObject, array('getJSCode', 'getHtmlCode')); $returnValue .= XOAD_Client::register($phpControlName, $url) . ';'; } if (!empty($jsFile)) { if (!in_array($jsFile, $GLOBALS['_XOAD_CONTROLS_SCRIPT'])) { $includeScript .= '<script type="text/javascript" src="' . htmlspecialchars($jsFile) . '"></script>'; $GLOBALS['_XOAD_CONTROLS_SCRIPT'][] = $jsFile; } } else { if (isset($phpObject) && method_exists($phpObject, 'getJSCode')) { $returnValue .= $phpObject->getJSCode($jsControlName, $phpControlName); } } $controlHtml = null; if (!empty($htmlFile)) { $controlHtml = @join(null, @file(XOAD_Controls::getFileName($htmlFile))); } else { if (isset($phpObject) && method_exists($phpObject, 'getHtmlCode')) { $controlHtml = $phpObject->getHtmlCode($jsControlName, $phpControlName); } } if (!$registerAttribute) { $returnValue .= 'xoad.controls.list[' . sizeof($GLOBALS['_XOAD_CONTROLS_LIST']) . '] = {'; $returnValue .= 'tagName:' . XOAD_Client::register($controlName); if (!empty($jsFile)) { $returnValue .= ',clientClass:' . XOAD_Client::register($jsControlName); } if (!empty($phpFile)) { $returnValue .= ',serverClass:' . XOAD_Client::register($phpControlName); } if (!empty($controlHtml)) { $returnValue .= ',html:' . XOAD_Client::register($controlHtml); } $returnValue .= '};'; } if (!empty($returnValue)) { $returnValue = '<script type="text/javascript">' . $returnValue . '</script>'; } $returnValue = $includeScript . $returnValue; $GLOBALS['_XOAD_CONTROLS_LIST'][] = $controlName; return $returnValue; }
/** * Registers XOAD Events header data. * * <p>You should call this method after {@link XOAD_Utilities::header}.</p> * <p>XOAD Events header data includes server time and callback URL.</p> * * @access public * * @param string $callbackUrl XOAD Events callback URL. * * @return string HTML code to initialize XOAD Events. * * @static * */ public static function eventsHeader($callbackUrl = null) { if ($callbackUrl == null) { $callbackUrl = XOAD_Utilities::getRequestUrl(); } $returnValue = '<script type="text/javascript">'; $returnValue .= 'xoad.events.callbackUrl = ' . XOAD_Client::register($callbackUrl) . ';'; $returnValue .= 'xoad.events.lastRefresh = ' . XOAD_Client::register(XOAD_Utilities::getMicroTime()) . ';'; $returnValue .= '</script>'; return $returnValue; }