[Mapbender-commits] r3215 - in trunk/mapbender: core lib

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Tue Nov 11 08:06:56 EST 2008


Author: christoph
Date: 2008-11-11 08:06:56 -0500 (Tue, 11 Nov 2008)
New Revision: 3215

Added:
   trunk/mapbender/lib/ajax.php
Modified:
   trunk/mapbender/core/globalSettings.php
   trunk/mapbender/lib/ajax.js
Log:
http://trac.osgeo.org/mapbender/ticket/322

Modified: trunk/mapbender/core/globalSettings.php
===================================================================
--- trunk/mapbender/core/globalSettings.php	2008-11-11 10:50:08 UTC (rev 3214)
+++ trunk/mapbender/core/globalSettings.php	2008-11-11 13:06:56 UTC (rev 3215)
@@ -68,4 +68,10 @@
 // Do not display PHP errors
 //
 ini_set("display_errors", "0");
+
+//
+// AJAX wrapper
+//
+require_once(dirname(__FILE__)."/../lib/ajax.php");
+
 ?>

Modified: trunk/mapbender/lib/ajax.js
===================================================================
--- trunk/mapbender/lib/ajax.js	2008-11-11 10:50:08 UTC (rev 3214)
+++ trunk/mapbender/lib/ajax.js	2008-11-11 13:06:56 UTC (rev 3215)
@@ -12,7 +12,8 @@
  */
 /**
  * A wrapper for an AJAX request via GET 
- *
+ * 
+ * @deprecated
  * @param {String} url the URL of a (presumably a server side) script.
  * @param {Object} param An object containing parameters, f.e. {name1:value1, name2:value2}
  * @param {Function} callback A function that is called when the server side script has been processed. The function is called with two parameters, result and status. Result is the output of the server side script (XML, HTML, whatever), status is a {String}, either "success" or "error". 
@@ -30,6 +31,7 @@
 /**
  * A wrapper for an AJAX request via POST 
  *
+ * @deprecated
  * @param {String} url the URL of a (presumably a server side) script.
  * @param {Object} param An object containing parameters, f.e. {name1:value1, name2:value2}
  * @param {Function} callback A function that is called when the server side script has been processed. The function is called with two parameters, result and status. Result is the output of the server side script (XML, HTML, whatever), status is a {String}, either "success" or "error". 
@@ -47,6 +49,7 @@
 /**
  * A wrapper for an AJAX request via GET 
  *
+ * @deprecated
  * @param {String} url the URL of a (presumably a server side) script.
  * @param {Object} param An object containing parameters, f.e. {name1:value1, name2:value2}
  * @param {Function} callback A function that is called when the server side script has been processed. The function is called with two parameters, result and status. Result is the output of the server side script (a JavaScript Object, not a String!), status is a {String}, either "success" or "error". 
@@ -61,3 +64,201 @@
 	}
 }	
 
+Mapbender.Ajax = {
+	requestCount: 0,
+	Messages: {
+		"fatalError": "A fatal error occured.",
+		"idMismatchError": "The ID of the response is not equal to the ID of the request."
+	}
+};
+
+Mapbender.Ajax.Notification = function () {
+	/**
+	 * The method to call on the server
+	 */
+	var method = "";
+
+	/**
+	 * The parameters to pass to the above method
+	 */
+	var parameters = {};
+	 
+	this.setMethod = function (aMethod) {
+		method = aMethod.toString();
+	};
+	
+	this.setParameter = function (key, value) {
+		parameters[key] = value;
+		return true;
+	};
+
+	this.send = function (type, url) {
+		switch (type.toUpperCase()) {
+			case "POST" :
+				$.post(url, getParameters(), function () {
+				});
+				break;
+			case "GET" :
+				$.get(url, getParameters(), function () {
+				});
+				break;
+			default:
+				return false;
+		}
+		return true;
+	};
+
+	var getParameters = function () {
+		return {
+			"method": method,
+			"params": $.toJSON([parameters]),
+			"id": null
+		};
+	}
+	
+}
+Mapbender.Ajax.Request = function (options) {
+	/**
+	 * "POST" or "GET"
+	 */
+	var type = (typeof(options) == "object" && options.type) ? options.type.toUpperCase() : "POST";
+	
+	/**
+	 * result of request will be logged as Mb_notice
+	 */
+	var log = (typeof(options) == "object" && options.log) ? options.log : false;
+	
+	/**
+	 * The method to call on the server
+	 */
+	var method = "";
+
+	/**
+	 * The parameters to pass to the above method
+	 */
+	var parameters = {};
+
+	/**
+	 * A unique identifier for this Ajax request
+	 */
+	var id = ++Mapbender.Ajax.requestCount;
+	
+	/**
+	 * The result object, coming from the server
+	 */
+	var result = {};
+
+	/**
+	 * A message coming from the server
+	 */
+	var message = "";
+
+	/**
+	 * Did the request succeed?
+	 */
+	var success = false;
+	
+	/**
+	 * Did an internal error occur
+	 */
+	var internalError = false;
+	
+	/**
+	 * Checks if the response is valid.
+	 * 
+	 * @param {String} json
+	 * @param {String} status
+	 */
+	var receive = function (json, status) {
+		if (!json) {
+			message = Mapbender.Ajax.Messages.fatalError;
+			new Mb_warning(message);
+			internalError = true;
+			return;
+		}
+		var resultObj = eval('(' + json + ')');
+		// some severe error has occured
+		if (typeof(resultObj) != "object" || status != "success") {
+			message = Mapbender.Ajax.Messages.fatalError;
+			new Mb_warning(message);
+			internalError = true;
+			return;
+		}
+		if (resultObj.error !== null) {
+			// the ajax request reports failure  
+			if (resultObj.error.message) {
+				message = resultObj.error.message;
+			}
+			else {
+				message = Mapbender.Ajax.Messages.fatalError;
+			}
+			if (resultObj.error.code == -1) {
+				internalError = true;
+			}
+			new Mb_warning(message);
+			return;
+		}
+
+		// the ajax request reports success
+		if (resultObj.result && typeof(resultObj.result) == "object" &&
+			resultObj.result.data && typeof(resultObj.result.data) == "object") {
+			if (id != resultObj.id) {
+				message = Mapbender.Ajax.Messages.idMismatchError;
+				new Mb_warning(message);
+				internalError = true;
+				return;
+			}
+
+			success = true;
+
+			if (resultObj.result.data.message) {
+				message = resultObj.data.message;
+				status = resultObj.data.message;
+			}
+			result = resultObj.result.data;
+		}
+		return;
+	}
+
+	this.send = function (url, callback, scope) {
+		internalError = false;
+		success = false;
+		
+		var callbackWrapper = function (json, status) {
+			receive(json, status);
+			if (!internalError) {
+				new Mb_notice("REQUEST #" + id + ": " + url + "\n\n" + 
+					"RESULT: " + $.toJSON(result) + "\n\n" + 
+					"SUCCESS: " + success + "\n\n" + 
+					"MESSAGE: " + message);
+				
+				if (scope !== undefined) {
+					callback.apply(scope, [result, success, message]);
+				}
+				else {
+					callback(result, success, message);
+				}
+			}
+		};
+		switch (type) {
+			case "POST" :
+				$.post(url, getParameters(), callbackWrapper);
+				break;
+			case "GET" :
+				$.get(url, getParameters(), callbackWrapper);
+				break;
+			default:
+				return false;
+		}
+		return true;
+	};
+
+	var getParameters = function () {
+		return {
+			"method": method,
+			"params": $.toJSON([parameters]),
+			"id": id
+		};
+	}
+};
+Mapbender.Ajax.Request.prototype = new Mapbender.Ajax.Notification();

Added: trunk/mapbender/lib/ajax.php
===================================================================
--- trunk/mapbender/lib/ajax.php	                        (rev 0)
+++ trunk/mapbender/lib/ajax.php	2008-11-11 13:06:56 UTC (rev 3215)
@@ -0,0 +1,200 @@
+<?php
+# $Id:database-pgsql.php 2619 2008-07-08 15:46:11Z christoph $
+# http://www.mapbender.org/index.php/database-pgsql.php
+# Copyright (C) 2002 CCGIS
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+require_once(dirname(__FILE__) . "/../http/classes/class_json.php");
+
+/**
+ * Represents an incoming JSON-RPC. It will NOT send a response to the client!
+ * (Use AjaxResponse if you also want to send a response.)
+ * 
+ * Usually called like this
+ * 
+ * $incomingAjax = new AjaxRequest($_REQUEST)
+ * 
+ * get parameters of request via
+ * 
+ * echo $incomingAjax->getParameter("someAttribute");
+ */
+class AjaxRequest {
+
+	protected $method = "";
+	protected $json = null;
+	protected $id = null;
+	protected $paramObject = array();
+	
+	public function __construct ($requestArray) {
+		$this->json = new Mapbender_JSON();
+		
+		if (is_array($requestArray)) {
+			$this->initializeFromArray($requestArray);
+		}
+	}
+
+	protected function initializeFromArray ($requestArray) {
+		if ($requestArray["id"]) {
+			$this->id = $requestArray["id"];
+		}
+		
+		if ($requestArray["method"]) {
+			$this->method = $requestArray["method"];
+			
+			if ($requestArray["params"]) {
+				
+				$obj = $this->json->decode($requestArray["params"]);
+				if (is_array($obj) && count($obj) == 1) {
+					$this->paramObject = $obj[0];
+				}
+			}
+		}
+	}
+
+	public function getParameter ($key) {
+		if (is_object($this->paramObject) && $this->paramObject[$key]) {
+			return $this->paramObject[$key];
+		}
+		return null;
+	}
+	
+	public function getId () {
+		return $this->id;
+	}
+}
+
+/**
+ * Represents an incoming JSON-RPC, which will send a response to the client.
+ * 
+ * Usually called like this
+ * 
+ * $ajaxResponse = new AjaxResponse($_REQUEST)
+ * 
+ * 
+ * get parameters of request via
+ * 
+ * echo $ajaxResponse->getParameter("someAttribute");
+ * 
+ * 
+ * set data to be sent back to the client
+ * 
+ * $ajaxResponse->setResult("key", "value");
+ * 
+ * or
+ * 
+ * $ajaxResponse->setResult($assocArray);
+ * 
+ * 
+ * set the status of this RPC
+ * 
+ * $ajaxResponse->setSuccess(false);
+ * 
+ * and supply a message
+ * 
+ * $ajaxResponse->setMessage("I didn't do it.");
+ * 
+ * 
+ * Finally send the response
+ * 
+ * $ajaxResponse->send();
+ */
+class AjaxResponse extends AjaxRequest {
+	private $data = array();
+	private $success = true;
+	private $error = null;
+	private $message = "";
+	
+	public function __construct ($ajaxRequest) {
+		$this->json = new Mapbender_JSON();
+
+		if (is_array($ajaxRequest)) {
+			$this->initializeFromArray($ajaxRequest);
+		}		
+
+		// in addition to AjaxRequest, immediately send an
+		// error message to the client, if the request
+		// could not be identified
+		if ($this->id === null) {
+			$this->success = false;
+			$this->message = _mb("Fatal error: Could not detect ID of AJAX request.");
+			$this->send();
+		}
+	}
+	
+	/**
+	 * Set a message to be sent back to the client.
+	 * 
+	 * @param $aMessage String
+	 */
+	public function setMessage ($aMessage) {
+		$this->message = $aMessage;
+	}
+	
+	/**
+	 * Set status of the RPC.
+	 * 
+	 * @param $trueOrFalse Boolean
+	 */
+	public function setSuccess ($trueOrFalse) {
+		$this->success = $trueOrFalse;
+	}
+	
+	/**
+	 * Compose data to be sent back to the client.
+	 * Either by key and value, or by passing the complete associative array.
+	 */
+	public function setResult () {
+		if (func_num_args() == 1) {
+			$this->data = func_get_arg(0);
+		}
+		else if (func_num_args() == 2) {
+			$key = func_get_arg(0);
+			$value = func_get_arg(1);
+			$this->data[$key] = $value;
+		}
+	}
+	
+	/**
+	 * Send the response to the client.
+	 */
+	public function send () {
+		header("Content-type:application/json; charset=" . CHARSET);
+		echo $this->getData();
+		die;
+	}
+	
+	private function getData () {
+		$dataObject = array();
+		$dataObject["data"] = $this->data;
+		if ($this->success) {
+			$dataObject["success"] = true;
+			$dataObject["message"] = $this->message;
+		}
+		else {
+			$this->error = array(
+				"code" => -1,
+				"message" => $this->message
+			);
+		}
+		$obj = array(
+			"result" => $dataObject,
+			"error" => $this->error,
+			"id" => $this->id
+		);
+		return $this->json->encode($obj);
+	}
+}
+?>
\ No newline at end of file



More information about the Mapbender_commits mailing list