class ResponseData {
var $container;
var $data;
var $type;
var $mode;
var $callBack;
var $fadeTo;
var $fadingColor;
function ResponseData() {
$this->container = null;
$this->data = null;
$this->type = 'text';
$this->mode = null;
$this->callBack = null;
$this->fadeTo = null;
$this->fadingColor = null;
}
function setContainer($container) {
$this->container = $container;
}
function getContainer() {
return $this->container;
}
function setData($data) {
$this->data = $data;
}
function getData() {
return $this->data;
}
function setType($type) {
$this->type = $type;
}
function getType() {
return $this->type;
}
function setMode($mode) {
$this->mode = $mode;
}
function setCallBack($callBack) {
$this->callBack = $callBack;
}
function getCallBack() {
return $this->callBack;
}
function getMode() {
return $this->mode;
}
function setFadeTo($fadeTo) {
$this->fadeTo = $fadeTo;
}
function getFadeTo() {
return $this->fadeTo;
}
function setFadingColor($fadingColor) {
$this->fadingColor = $fadingColor;
}
function getFadingColor() {
return $this->fadingColor;
}
}
class ResponseElement {
var $id;
var $attributes;
var $callback;
function setId($id) {
$this->id = $id;
}
function getId() {
return $this->id;
}
function addAttribute($name, $value) {
$this->attributes[$name] = $value;
}
function getAttribute($name) {
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
}
function setAttributes($attributes) {
$this->attributes = $attributes;
}
function getAttributes() {
return $this->attributes;
}
function setCallBack($callBack) {
$this->callBack = $callBack;
}
function getCallBack() {
return $this->callBack;
}
}
class XMLAjaxResponse {
var $status;
var $messages;
var $forms;
var $data;
var $encoding;
var $elements;
function XMLAjaxResponse($encoding = 'UTF-8') {
$this->status = true;
$this->messages = array();
$this->forms = array();
$this->data = array();
$this->encoding = $encoding;
}
function setStatus($status) {
$this->status = $status;
}
function getStatusMessage() {
return $this->status ? 'ok' : 'error';
}
function addMessage($message) {
$this->messages[] = $message;
}
function addForm($name, $form) {
$this->forms[$name] = $form;
}
function addData($container, $data, $fadeTo = null, $dom = false) {
$responseData = &new ResponseData();
$responseData->setContainer($container);
$responseData->setData($data);
$responseData->setFadeTo($fadeTo);
if ($dom) $responseData->setType('dom');;
$this->data[] = $responseData;
}
function addDataAppend($container, $data, $fadeTo = null) {
$responseData = &new ResponseData();
$responseData->setContainer($container);
$responseData->setData($data);
$responseData->setType('dom');
$responseData->setMode('append');
$responseData->setFadeTo($fadeTo);
$this->data[] = $responseData;
}
function addDataPrepend($container, $data, $fadeTo = null) {
$responseData = &new ResponseData();
$responseData->setContainer($container);
$responseData->setData($data);
$responseData->setType('dom');
$responseData->setMode('prepend');
$responseData->setFadeTo($fadeTo);
$this->data[] = $responseData;
}
function addDataRemove($id, $fadeTo = null) {
$responseData = &new ResponseData();
$responseData->setContainer($id);
$responseData->setType('dom');
$responseData->setMode('remove');
$responseData->setFadeTo($fadeTo);
$this->data[] = $responseData;
}
function addDataObject($dataObject) {
$this->data[] = $dataObject;
}
function addElement($id, $attributeName, $attributeValue, $callback = null) {
$element = &new ResponseElement();
$element->setId($id);
$element->addAttribute($attributeName, $attributeValue);
$element->setCallback($callback);
$this->elements[] = $element;
}
function addElementObject($element) {
$this->elements[] = $element;
}
function getResponseXml() {
$messages = $this->messages;
$forms = $this->forms;
$responseData = $this->data;
$elements = $this->elements;
$buffer = ob_get_contents();
if ($buffer) {
$dataItems['phperror'] = $buffer;
ob_end_clean();
}
$xml = array();
$xml[] = '<response status="' . $this->getStatusMessage() . '">';
if ($messages) {
$xml[] = ' <messages>';
foreach($messages as $message) {
$xml[] = ' <message><![CDATA[' . $message . ']]></message>';
}
$xml[] = ' </messages>';
}
if ($forms) {
$xml[] = ' <forms>';
foreach($forms as $formName=>$form) {
$xml[] = ' <form name="' . $formName . '">';
$xml[] = ' <elements>';
foreach($form as $fieldName=>$fieldValue) {
if (!is_array($fieldValue)) {
$xml[] = ' <element name="' . $fieldName . '"><![CDATA[' . $fieldValue . ']]></element>';
} else {
$xml[] = ' <element name="' . $fieldName . '">';
foreach($fieldValue as $value) {
$xml[] = ' <value><![CDATA[' . $value . ']]></value>';
}
$xml[] = ' </element>';
}
}
$xml[] = ' </elements>';
$xml[] = ' </form>';
}
$xml[] = ' </forms>';
}
if ($responseData) {
$xml[] = ' <data>';
foreach($responseData as $data) {
$type = $data->getType();
if ($type == 'text') {
$xml[] = ' <item container="' . $data->getContainer() . '" type="' . $type . '" mode="' . $data->getMode() . '" callBack="' . $data->getCallBack() . '" fadeTo="' . $data->getFadeTo() . '" fadingColor="' . $data->getFadingColor() . '"><![CDATA[' . $data->getData() . ']]></item>';
} else if ($type == 'dom') {
$xml[] = ' <item container="' . $data->getContainer() . '" type="' . $type . '" mode="' . $data->getMode() . '" callBack="' . $data->getCallBack() . '" fadeTo="' . $data->getFadeTo() . '" fadingColor="' . $data->getFadingColor() . '">' . $data->getData() . '</item>';
}
}
$xml[] = ' </data>';
}
if ($elements) {
$xml[] = ' <elements>';
foreach($elements as $element) {
$xml[] = ' <element id="' . $element->getId() . '" callBack="' . $element->getCallBack() . '">';
$xml[] = ' <attributes>';
$attributes = $element->getAttributes();
foreach($attributes as $name=>$value) {
$xml[] = ' <attribute attributeName="' . $name . '" attributeValue="' . $value . '"/>';
}
$xml[] = ' </attributes>';
$xml[] = ' </element>';
}
$xml[] = ' </elements>';
}
$xml[] = '</response>';
return implode("\n", $xml);
}
function doResponse() {
header("Content-Type: text/xml; charset=" . $this->encoding);
echo $this->getResponseXml();
}
function reset() {
$this->XMLAjaxResponse();
}
function destroy() {
$this = null;
}
}
?>
0 comments:
Post a Comment