📄 xajaxresponse.inc.php
字号:
return $this->addCreate($sParent, $sTag, $sId, $sType); } /** * Adds a insert element command message to the XML response. * * <i>Usage:</i> <kbd>$objResponse->addInsert("childDiv", "h3", "myid");</kbd> * * @param string contains the id of the child before which the new element * will be inserted * @param string the tag to be added * @param string the id to be assigned to the new element */ function addInsert($sBefore, $sTag, $sId) { $this->addCommand(array('n'=>'ie','t'=>$sBefore,'p'=>$sId),$sTag); return $this; } function insert($sBefore, $sTag, $sId) { return $this->addInsert($sBefore, $sTag, $sId); } /** * Adds a insert element command message to the XML response. * * <i>Usage:</i> <kbd>$objResponse->addInsertAfter("childDiv", "h3", "myid");</kbd> * * @param string contains the id of the child after which the new element * will be inserted * @param string the tag to be added * @param string the id to be assigned to the new element */ function addInsertAfter($sAfter, $sTag, $sId) { $this->addCommand(array('n'=>'ia','t'=>$sAfter,'p'=>$sId),$sTag); return $this; } function insertAfter($sAfter, $sTag, $sId) { return $this->addInsertAfter($sAfter, $sTag, $sId); } /** * Adds a create input command message to the XML response. * * <i>Usage:</i> <kbd>$objResponse->addCreateInput("form1", "text", "username", "input1");</kbd> * * @param string contains the id of an HTML element to which the new input * will be appended * @param string the type of input to be created (text, radio, checkbox, * etc.) * @param string the name to be assigned to the new input and the variable * name when it is submitted * @param string the id to be assigned to the new input */ function addCreateInput($sParent, $sType, $sName, $sId) { $this->addCommand(array('n'=>'ci','t'=>$sParent,'p'=>$sId,'c'=>$sType),$sName); return $this; } function createInput($sParent, $sType, $sName, $sId) { return $this->addCreateInput($sParent, $sType, $sName, $sId); } /** * Adds an insert input command message to the XML response. * * <i>Usage:</i> <kbd>$objResponse->addInsertInput("input5", "text", "username", "input1");</kbd> * * @param string contains the id of the child before which the new element * will be inserted * @param string the type of input to be created (text, radio, checkbox, * etc.) * @param string the name to be assigned to the new input and the variable * name when it is submitted * @param string the id to be assigned to the new input */ function addInsertInput($sBefore, $sType, $sName, $sId) { $this->addCommand(array('n'=>'ii','t'=>$sBefore,'p'=>$sId,'c'=>$sType),$sName); return $this; } function insertInput($sBefore, $sType, $sName, $sId) { return $this->addInsertInput($sBefore, $sType, $sName, $sId); } /** * Adds an insert input command message to the XML response. * * <i>Usage:</i> <kbd>$objResponse->addInsertInputAfter("input7", "text", "email", "input2");</kbd> * * @param string contains the id of the child after which the new element * will be inserted * @param string the type of input to be created (text, radio, checkbox, * etc.) * @param string the name to be assigned to the new input and the variable * name when it is submitted * @param string the id to be assigned to the new input */ function addInsertInputAfter($sAfter, $sType, $sName, $sId) { $this->addCommand(array('n'=>'iia','t'=>$sAfter,'p'=>$sId,'c'=>$sType),$sName); return $this; } function insertInputAfter($sAfter, $sType, $sName, $sId) { return $this->addInsertInputAfter($sAfter, $sType, $sName, $sId); } /** * Adds an event command message to the XML response. * * <i>Usage:</i> <kbd>$objResponse->addEvent("contentDiv", "onclick", "alert(\'Hello World\');");</kbd> * * @param string contains the id of an HTML element * @param string the event you wish to set ("onclick", "onmouseover", etc.) * @param string the Javascript string you want the event to invoke */ function addEvent($sTarget,$sEvent,$sScript) { $this->addCommand(array('n'=>'ev','t'=>$sTarget,'p'=>$sEvent),$sScript); return $this; } /** * Adds a handler command message to the XML response. * * <i>Usage:</i> <kbd>$objResponse->addHandler("contentDiv", "onclick", "content_click");</kbd> * * @param string contains the id of an HTML element * @param string the event you wish to set ("onclick", "onmouseover", etc.) * @param string the name of a Javascript function that will handle the * event. Multiple handlers can be added for the same event */ function addHandler($sTarget,$sEvent,$sHandler) { $this->addCommand(array('n'=>'ah','t'=>$sTarget,'p'=>$sEvent),$sHandler); return $this; } /** * Adds a remove handler command message to the XML response. * * <i>Usage:</i> <kbd>$objResponse->addRemoveHandler("contentDiv", "onclick", "content_click");</kbd> * * @param string contains the id of an HTML element * @param string the event you wish to remove ("onclick", "onmouseover", * etc.) * @param string the name of a Javascript handler function that you want to * remove */ function addRemoveHandler($sTarget,$sEvent,$sHandler) { $this->addCommand(array('n'=>'rh','t'=>$sTarget,'p'=>$sEvent),$sHandler); return $this; } function removeHandler($sTarget,$sEvent,$sHandler) { return $this->addRemoveHandler($sTarget,$sEvent,$sHandler); } /** * Adds an include script command message to the XML response. * * <i>Usage:</i> <kbd>$objResponse->addIncludeScript("functions.js");</kbd> * * @param string URL of the Javascript file to include */ function addIncludeScript($sFileName) { $this->addCommand(array('n'=>'in'),$sFileName); return $this; } function includeScript($sFilename) { return $this->addIncludeScript($sFileName); } /** * Returns the XML to be returned from your function to the xajax processor * on your page. Since xajax 0.2, you can also return an xajaxResponse * object from your function directly, and xajax will automatically request * the XML using this method call. * * <i>Usage:</i> <kbd>return $objResponse->getXML();</kbd> * * @return string response XML data */ function getOutput() { $xml = ""; if (is_array($this->aCommands)) { foreach($this->aCommands as $aCommand) { $sData = $aCommand['data']; unset($aCommand['data']); $xml .= $this->_getXMLForCommand($aCommand, $sData); } } $charSet = ''; $encoding = ''; if (trim($this->sEncoding)) { $charSet = '; charset="'.$this->sEncoding.'"'; $encoding = ' encoding="'.$this->sEncoding.'"'; } @header('content-type: text/xml'.$charSet); return '<?xml version="1.0"'.$encoding.' ?'.'><xjx>'.$xml.'</xjx>'; } function getXML() { return $this; } /** * Adds the commands of the provided response XML output to this response * object * * <i>Usage:</i> * <code>$r1 = $objResponse1->getXML(); * $objResponse2->loadXML($r1); * return $objResponse2->getXML();</code> * * @param string the response XML (returned from a getXML() method) to add * to the end of this response object */ function loadXML($mCommands) { if (is_a($mCommands, 'xajaxResponse')) { $this->aCommands = array_merge($this->aCommands, $mCommands->aCommands); } else if (is_array($mCommands)) { $this->aCommands = array_merge($this->aCommands, $mCommands); } else if (is_string($mCommands) && strpos($mXML, '<xjx>')!==false) { trigger_error("Using xajaxResponse->loadXML doesn't work with raw XML any more", E_USER_ERROR); } else { if (!empty($mCommands)) trigger_error("The xajax response output could not load other commands as data was not a valid array", E_USER_ERROR); } } function loadCommands($mCommands) { return $this->loadXML($mCommands); } /** * Generates XML from command data * * @access private * @param array associative array of attributes * @param string data * @return string XML command */ function _cmdXML($aAttributes, $sData) { if ($this->bOutputEntities) { if (function_exists('mb_convert_encoding')) { $sData = call_user_func_array('mb_convert_encoding', array(&$sData, 'HTML-ENTITIES', $this->sEncoding)); } else { trigger_error("The xajax XML response output could not be converted to HTML entities because the mb_convert_encoding function is not available", E_USER_NOTICE); } } $xml = "<cmd"; foreach($aAttributes as $sAttribute => $sValue) $xml .= " $sAttribute=\"$sValue\""; if ($sData !== null && !stristr($sData,'<![CDATA[')) $xml .= "><![CDATA[$sData]]></cmd>"; else if ($sData !== null) $xml .= ">$sData</cmd>"; else $xml .= "></cmd>"; return $xml; } /** * Generates XML from command data * * @access private * @param array associative array of attributes * @param mixed data * @return string XML command */ function _getXMLForCommand($aAttributes, $mData) { $xml = '<cmd'; foreach($aAttributes as $sAttribute => $sValue) if ($sAttribute) $xml .= " $sAttribute=\"$sValue\""; if (is_array($mData)) $xml .= '>'.$this->_arrayToXML($mData).'</cmd>'; else $xml .= '>'.$this->_escape($mData).'</cmd>'; return $xml; } /** * Converts an array of data into XML * * @access private * @param mixed associative array of data or string of data * @return string XML command */ function _arrayToXML($mArray) { if (!is_array($mArray)) return $this->_escape($mArray); $xml = '<xjxobj>'; foreach ($mArray as $aKey=>$aKeyValues) { if (is_array($aKeyValues)) { $xml .= '<e>'; foreach($aKeyValues as $sKey => $sValue) { $xml .= '<'.htmlentities($sKey).'>'; $xml .= $this->_arrayToXML($sValue); $xml .= '</'.htmlentities($sKey).'>'; } $xml .= '</e>'; } else { $xml .= '<e><k>'; $xml .= $this->_escape($aKey); $xml .= '</k><v>'; $xml .= $this->_escape($aKeyValues); $xml .= '</v></e>'; } } $xml .= '</xjxobj>'; return $xml; } /** * Adds a commmand to the array of all commands * * @param array associative array of attributes * @param mixed data */ function addCommand($aAttributes, $mData) { $aAttributes['data'] = $mData; $this->aCommands[] = $aAttributes; } /** * Escapes the data. Can be overridden to allow other transports to send * data. * * @access private * @param string data * @return string escaped data */ function _escape($sData) { if (!is_numeric($sData) && !$sData) return ''; $needCDATA = false; if ($this->bOutputEntities) { if (!function_exists('mb_convert_encoding')) trigger_error('The xajax response output could not be converted to HTML entities because the mb_convert_encoding function is not available', E_USER_NOTICE); $sData = call_user_func_array('mb_convert_encoding', array(&$sData, 'HTML-ENTITIES', $this->sEncoding)); } else { if ((strpos($sData, '<![CDATA[') !== false) || (strpos($sData, ']]>') !== false) || (htmlentities($sData) != $sData)) $needCDATA = true; $segments = explode('<![CDATA[', $sData); $sData = ''; foreach ($segments as $key => $segment) { $fragments = explode(']]>', $segment); $segment = ''; foreach ($fragments as $fragment) { if ($segment != '') $segment .= ']]]]><![CDATA[>'; $segment .= $fragment; } if ($sData != '') $sData .= '<![]]><![CDATA[CDATA['; $sData .= $segment; } } if ($needCDATA) $sData = '<![CDATA['.$sData.']]>'; return $sData; } /** * Recursively serializes a data structure in an array so it can be sent to * the client. It could be thought of as the opposite of * {@link xajax::_parseObjXml()}. * * @access private * @param mixed data structure to serialize * @return array data ready for insertion into command list array */ function _buildObj($mData) { if (gettype($mData) == 'object') $mData = get_object_vars($mData); if (is_array($mData)) { $aData = array(); foreach ($mData as $key => $value) $aData[] = array( 'k'=>$this->_buildObj($key), 'v'=>$this->_buildObj($value) ); return $aData; } else return $mData; } }// end class xajaxResponse
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -