[Mapbender-commits] r2502 - trunk/mapbender/http/classes

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Fri Jun 13 09:03:41 EDT 2008


Author: christoph
Date: 2008-06-13 09:03:41 -0400 (Fri, 13 Jun 2008)
New Revision: 2502

Added:
   trunk/mapbender/http/classes/class_map.php
   trunk/mapbender/http/classes/class_wmcToXml.php
Modified:
   trunk/mapbender/http/classes/class_administration.php
   trunk/mapbender/http/classes/class_user.php
   trunk/mapbender/http/classes/class_wmc.php
   trunk/mapbender/http/classes/class_wms.php
Log:
merged with beck_dev

Modified: trunk/mapbender/http/classes/class_administration.php
===================================================================
--- trunk/mapbender/http/classes/class_administration.php	2008-06-13 12:57:16 UTC (rev 2501)
+++ trunk/mapbender/http/classes/class_administration.php	2008-06-13 13:03:41 UTC (rev 2502)
@@ -19,6 +19,7 @@
 require_once(dirname(__FILE__)."/../../conf/mapbender.conf");
 require_once(dirname(__FILE__)."/class_mb_exception.php");
 require_once(dirname(__FILE__)."/class_user.php");
+
 $con = db_connect(DBSERVER,OWNER,PW);
 db_select_db(DB,$con);
 
@@ -28,8 +29,8 @@
  * class to wrap administration methods
  *
  * @uses phpmailer
- */
-class administration{
+ */ 
+class administration {
     /**
      * checks whether the passed email-address is valid / following a pattern
      * @todo is this an exact representation of the RFC 2822?
@@ -109,6 +110,54 @@
 		}
 	}
 
+	/**
+	 * Removes the namespace from a tag name
+	 * @return String like "gml"
+	 * @param $s String like "ogc:gml"
+	 */
+	public static function sepNameSpace($s) {
+		$c = strpos($s,":"); 
+		if ($c > 0) {
+			return substr($s,$c+1);	
+		}
+		return $s;
+	}
+
+	/**
+	 * Parses an XML with PHP XML parser, see 
+	 * http://de2.php.net/manual/de/book.xml.php
+	 * 
+	 * @return Array an associative array of tags, values, attributes and types
+	 * @param $someXml String The actual XML as string.
+	 */
+	public static function parseXml ($someXml) {
+		$values = null;
+		$tags = null;
+		
+		$parser = xml_parser_create(CHARSET);
+
+		// set parsing options
+		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
+		xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
+		xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, CHARSET);
+
+		// this is the actual parsing process
+		xml_parse_into_struct($parser, $someXml, $values, $tags);
+
+		// check if an error occured
+		$code = xml_get_error_code ($parser);
+		if ($code) {
+			// report error
+			$line = xml_get_current_line_number($parser); 
+			$errorMessage = xml_error_string($code) . " in line " . $line;
+			$mb_exception = new mb_exception($errorMessage);
+			return false;
+		}
+		xml_parser_free($parser);
+	
+		return $values;	
+	}
+
     /**
      * returns a random password with numbers and chars both lowercase and uppercase (0-9a-zA-Z)
      *
@@ -346,19 +395,13 @@
      * @param	string		the wmc_id
      * @return	boolean		Did the query run succesfull? This does not necessarily mean that 
      * 						an entry was deleted.
+     * @deprecated
      */
  	function deleteWmc($wmc_id, $user_id){
-		$sql = "DELETE FROM mb_user_wmc ";
-		$sql .= "WHERE fkey_user_id = $1 AND wmc_id = $2";
-		$v = array($user_id,$wmc_id);
-		$t = array('i','s');
-		$res = db_prep_query($sql,$v,$t);
-		if ($res) {
-			return true;
-		}
-		else {
-			return false;
-		}
+		$e = new mb_notice("administration->deleteWmc is deprecated, use wmc->delete instead!"); 
+		
+		$wmc = new wmc();
+		return $wmc->delete($wmc_id, $user_id);
  	}
 
     /**
@@ -517,19 +560,13 @@
      *
      * @param integer			the wms id
      * @return string|boolean	either the wmc as string or false when none exists
+     * @deprecated
      */
 	function getWmcById($id){
-		$sql = "SELECT wmc FROM mb_user_wmc WHERE wmc_id = $1 ";
-		$v = array($id);
-		$t = array('s');
-		$res = db_prep_query($sql,$v,$t);
-		$row = db_fetch_array($res);
-		if ($row) {
-			return $row["wmc"];
-		}
-		else {
-			return false;
-		}
+		$e = new mb_notice("administration->getWmcById is deprecated, use wmc->getDocument instead!"); 
+
+		$wmc = new wmc();
+		return $wmc->getDocument($id);
 	}
 
     /**
@@ -618,19 +655,14 @@
 		return $arrayGuis;
  	}
 
+	/**
+	 * @deprecated
+	 */
  	function getWmcByOwner($user_id){
-		$sql_wmc = "SELECT wmc_id FROM mb_user_wmc ";
-		$sql_wmc .= "WHERE fkey_user_id = $1 GROUP BY wmc_id";
-		$v = array($user_id);
-		$t = array('i');
-		$res_wmc = db_prep_query($sql_wmc,$v,$t);
-  		$count_g = 0;
-  		$arrayWmc = array();
-		while($row = db_fetch_array($res_wmc)){
-			$arrayWmc[$count_g] = $row["wmc_id"];
-			$count_g++;
-		}
-		return $arrayWmc;
+		$e = new mb_notice("administration->getWmcByOwner is deprecated, use user->getWmcByOwner instead!"); 
+
+		$user = new User($user_id);
+		return $user->getWmcByOwner();
  	}
 
 	/**

Added: trunk/mapbender/http/classes/class_map.php
===================================================================
--- trunk/mapbender/http/classes/class_map.php	                        (rev 0)
+++ trunk/mapbender/http/classes/class_map.php	2008-06-13 13:03:41 UTC (rev 2502)
@@ -0,0 +1,417 @@
+<?php
+require_once(dirname(__FILE__)."/../classes/class_bbox.php");
+/**
+ * Representing a map object, identical to the JS object in javascripts/map.js
+ * @class
+ */
+class Map {
+
+	private $width;
+	private $height;
+	private $frameName;
+	private $elementName = "maps";
+	private $extent;
+	private $isOverview = false;
+	private $wmsArray = array();
+	
+	/**
+	 * @destructor
+	 * @param
+	 */
+	function __destruct() {
+	}
+
+	/**
+	 * @constructor
+	 * @param
+	 */
+	function __construct() {
+	}
+	
+	//-------------------------------------------------------------------------
+	// getter and setter
+	//-------------------------------------------------------------------------
+	/**
+	 * @param $value Integer
+	 */
+	public function setWidth ($value) {
+		$this->width = $value;
+	}
+
+	/**
+	 * 
+	 * @return 
+	 */
+	public function getWidth () {
+		return $this->width;
+	}
+
+	/**
+	 * @param $value Integer
+	 */
+	public function setHeight ($value) {
+		$this->height = $value;
+	}
+
+	/**
+	 * 
+	 * @return 
+	 */
+	public function getHeight () {
+		return $this->height;
+	}
+
+	/**
+	 * @param $value String
+	 */
+	public function setFrameName ($value) {
+		$this->frameName = $value;
+	}
+
+	/**
+	 * 
+	 * @return String
+	 */
+	public function getFrameName () {
+		return $this->frameName;	
+	}
+	
+	/**
+	 * @param $value String
+	 */
+	public function setExtent ($aMapbenderBbox) {
+		$this->extent = $aMapbenderBbox;
+	}
+
+	/**
+	 * 
+	 * @return Mapbender_bbox 
+	 */
+	public function getExtent () {
+		return $this->extent;	
+	}
+	
+	/**
+	 * 
+	 * @return String EPSG code of the map.
+	 */
+	public function getEpsg () {
+		return $this->extent->epsg;	
+	}
+	
+	/**
+	 * 
+	 * @return 
+	 */
+	public function getWmsArray () {
+		return $this->wmsArray;	
+	}
+	
+	/**
+	 * 
+	 * @return 
+	 * @param $wmsArray Object
+	 */
+	public function setWmsArray ($wmsArray) {
+		$this->wmsArray = $wmsArray;
+	}
+	
+	/**
+	 * 
+	 * @return 
+	 */
+	public function isOverview () {
+		return $this->isOverview;
+	}
+	
+	public function setIsOverview ($bool) {
+		$this->isOverview = $bool;
+	}
+	
+	/**
+	 * @param $value Object
+	 */
+	public function addWms ($value) {
+		array_push($this->wms, $value);
+	}	
+
+
+	// ------------------------------------------------------------------------
+	// map manipulation
+	// ------------------------------------------------------------------------
+
+	/**
+	 * Appends the WMS of another map to this map.
+	 * 
+	 * @param $anotherMap Map
+	 */
+	public function append ($anotherMap) {
+		$this->wmsArray = array_merge($anotherMap->getWmsArray(), $this->wmsArray);
+	}
+		
+	/**
+	 * Merges this map with another map: Copies the map settings from the 
+	 * other map and merges the WMS (keeping the settings of the other
+	 * map if there are duplicates)
+	 * 
+	 * @param $anotherMap Map
+	 */
+	public function merge ($anotherMap) {
+		$this->width = $anotherMap->width;
+		$this->height = $anotherMap->height;
+		$this->frameName = $anotherMap->frameName;
+		$this->elementName = $anotherMap->elementName;
+		$this->extent = $anotherMap->extent;
+		$this->isOverview = $anotherMap->isOverview;
+		$this->wmsArray = wms::merge(array_merge($anotherMap->getWmsArray(), $this->wmsArray));
+	}
+
+	// ------------------------------------------------------------------------
+	// Instantiation
+	// ------------------------------------------------------------------------
+	/**
+	 * 
+	 * @return 
+	 * @param $jsMapObject Object
+	 */
+	public function createFromJs ($jsMapObject) {
+		$arrayBBox = explode(",", $jsMapObject->extent);
+		$minx = floatval($arrayBBox[0]);
+		$miny = floatval($arrayBBox[1]);
+		$maxx = floatval($arrayBBox[2]);
+		$maxy = floatval($arrayBBox[3]);
+		$srs = $jsMapObject->epsg;
+		$bbox = new Mapbender_bbox($minx, $miny, $maxx, $maxy, $srs);
+
+		$this->width = $jsMapObject->width;
+		$this->height = $jsMapObject->height;
+		$this->frameName = $jsMapObject->frameName;
+		$this->extent = $bbox;
+		
+		if (isset($jsMapObject->isOverview) && $jsMapObject->isOverview == "1") {
+			$this->isOverview = true;
+			$e = new mb_notice("ov: " . $this->isOverview);
+		}
+
+		for ($i=0; $i < count($jsMapObject->wms); $i++){
+	
+			$currentWms = $jsMapObject->wms[$i];
+			$wms = new wms();
+
+			//
+			// set WMS data
+			//
+			$wms->wms_id = $currentWms->wms_id;
+			$wms->wms_version = $currentWms->wms_version;
+			$wms->wms_title = $currentWms->wms_title;
+			$wms->wms_abstract = $currentWms->wms_abstract;
+			$wms->wms_getmap = $currentWms->wms_getmap;
+			$wms->wms_getfeatureinfo = $currentWms->wms_getfeatureinfo;
+			$wms->wms_getlegendurl = $currentWms->wms_getlegendurl;
+			$wms->wms_filter = $currentWms->wms_filter;
+			$wms->gui_epsg = $currentWms->gui_epsg;
+			$wms->gui_minx = $currentWms->gui_minx;
+			$wms->gui_miny = $currentWms->gui_miny;
+			$wms->gui_maxx = $currentWms->gui_maxx;
+			$wms->gui_maxy = $currentWms->gui_maxy;
+			$wms->gui_wms_mapformat = $currentWms->gui_wms_mapformat;
+			$wms->gui_wms_featureinfoformat = $currentWms->gui_wms_featureinfoformat;
+			$wms->gui_wms_exceptionformat = $currentWms->gui_wms_exceptionformat;
+			$wms->gui_wms_opacity = $currentWms->wms_opacity;
+			$wms->gui_wms_sldurl = $currentWms->gui_wms_sldurl;
+			$wms->gui_wms_visible = $currentWms->gui_wms_visible;
+			$wms->gui_wms_epsg = $currentWms->gui_wms_epsg;
+			$wms->data_type = $currentWms->data_type;
+			$wms->data_format = $currentWms->data_format;
+
+			for ($k = 0; $k < count($currentWms->objLayer); $k++){
+				// the current layer of the JSON map object
+				$currentLayer = $currentWms->objLayer[$k];
+
+				// add new layer to WMS
+				$pos = $currentLayer->layer_pos;
+				$parent = $currentLayer->layer_parent;
+				$wms->addLayer($pos, $parent); 
+
+				$newLayerIndex = count($wms->objLayer) - 1;
+				// $newLayer is a short cut to the layer we just added
+				$newLayer = $wms->objLayer[$newLayerIndex];
+				
+				// set layer data
+				$newLayer->layer_uid = $currentLayer->layer_uid;
+				$newLayer->layer_name = $currentLayer->layer_name;
+				$newLayer->layer_title = $currentLayer->layer_title;
+				$newLayer->layer_dataurl_href = $currentLayer->layer_dataurl_href;
+				$newLayer->layer_pos = $currentLayer->layer_pos;
+				$newLayer->layer_queryable = $currentLayer->layer_queryable;
+				$newLayer->layer_minscale = $currentLayer->layer_minscale;
+				$newLayer->layer_maxscale = $currentLayer->layer_maxscale;
+				$newLayer->layer_metadataurl = $currentLayer->metadataurl;
+				$newLayer->gui_layer_wms_id = $wms->objLayer[0]->layer_uid;
+				$newLayer->gui_layer_status = $currentLayer->gui_layer_status;
+				$newLayer->gui_layer_style = $currentLayer->gui_layer_style;
+				$newLayer->gui_layer_selectable = $currentLayer->gui_layer_selectable;
+				$newLayer->gui_layer_visible = $currentLayer->gui_layer_visible;
+				$newLayer->gui_layer_queryable = $currentLayer->gui_layer_queryable;
+				$newLayer->gui_layer_querylayer = $currentLayer->gui_layer_querylayer;
+				$newLayer->gui_layer_minscale = $currentLayer->gui_layer_minscale;
+				$newLayer->gui_layer_maxscale = $currentLayer->gui_layer_maxscale;
+				$newLayer->gui_layer_wfs_featuretype = $currentLayer->gui_layer_wfs_featuretype;
+
+				// BEWARE THIS IS SUPER UGLY CODE
+				$newLayer->layer_epsg = array();
+				for ($z = 0; $z < count($currentLayer->layer_epsg); $z++) {
+					$newLayer->layer_epsg[$z] = array();
+					$newLayer->layer_epsg[$z]["epsg"] = $currentLayer->layer_epsg[$z]->epsg;
+					$newLayer->layer_epsg[$z]["minx"] = $currentLayer->layer_epsg[$z]->minx;
+					$newLayer->layer_epsg[$z]["miny"] = $currentLayer->layer_epsg[$z]->miny;
+					$newLayer->layer_epsg[$z]["maxx"] = $currentLayer->layer_epsg[$z]->maxx;
+					$newLayer->layer_epsg[$z]["maxy"] = $currentLayer->layer_epsg[$z]->maxy;
+				}
+
+				// BEWARE THIS IS SUPER UGLY CODE
+				$newLayer->layer_style = array();
+				for ($z = 0; $z < count($currentLayer->layer_style); $z++) {
+					$newLayer->layer_style[$z] = array();
+					$newLayer->layer_style[$z]["name"] = $currentLayer->layer_style[$z]->name;
+					$newLayer->layer_style[$z]["title"] = $currentLayer->layer_style[$z]->title;
+					$newLayer->layer_style[$z]["legendurl"] = $currentLayer->layer_style[$z]->legendurl;
+					$newLayer->layer_style[$z]["legendurlformat"] = $currentLayer->layer_style[$z]->legendurlformat;
+				}
+
+			}
+			array_push($this->wmsArray, $wms);
+		}
+		return true;
+	}
+	
+	
+	// ------------------------------------------------------------------------
+	// database functions
+	// ------------------------------------------------------------------------
+	public static function selectMainMapByApplication ($appId) {
+		return map::selectByApplication($appId, "mapframe1");
+	}
+	
+	public static function selectOverviewMapByApplication ($appId) {
+		$currentMap = map::selectByApplication($appId, "overview");
+		if ($currentMap !== null) {
+			$currentMap->setIsOverview(true);
+		}
+		return $currentMap;
+	}
+
+
+	// ------------------------------------------------------------------------
+	// Output
+	// ------------------------------------------------------------------------
+	/**
+	 * Returns an array of string, which are JS statements.
+	 * @return String[]
+	 */
+	public function toJavaScript ($wmsIndex) {
+		$jsCodeArray = array();
+
+		// initialise map object
+		if ($wmsIndex === null) {
+			$wmsIndex = "null";
+		}
+		$registerMapString = "mb_registerMapObj('" . 
+			$this->frameName . "', " . 
+			"'maps', " . 
+			$wmsIndex . ", " . 
+			$this->width . ", " . 
+			$this->height . ");"; 
+		array_push($jsCodeArray, $registerMapString);
+
+		$e = new mb_notice("Map to JS: ov? " . $this->isOverview);
+
+		// if map is overview...
+		if ($this->isOverview) {
+			// ...set overview flag
+			$setOverviewFlagString = "mb_mapObj[mb_mapObj.length-1].isOverview = true;";
+			array_push($jsCodeArray, $setOverviewFlagString);
+		}
+		
+		// set width
+		$setMapframeWidth = "document.getElementById('" . $this->frameName . "')." . 
+			"style.width = " . $this->width . ";";
+		array_push($jsCodeArray, $setMapframeWidth);
+
+		// set height
+		$setMapframeHeight = "document.getElementById('" . $this->frameName . "')." . 
+			"style.height = " . $this->height . ";";
+		array_push($jsCodeArray, $setMapframeHeight);
+
+		// calculate extent
+		$calcExtentString = "mb_calculateExtent('" . 
+			$this->frameName . "', " .
+			$this->extent->min->x . ", " . 
+			$this->extent->min->y . ", " . 
+			$this->extent->max->x . ", " . 
+			$this->extent->max->y . ");"; 
+		array_push($jsCodeArray, $calcExtentString);
+		return $jsCodeArray;
+	}
+
+
+	// ------------------------------------------------------------------------
+	// PRIVATE FUNCTIONS
+	// ------------------------------------------------------------------------
+	
+	private static function selectByApplication ($appId, $frameName) {
+		// find the mapframe in the application elements...
+		$sql = "SELECT * FROM gui_element WHERE fkey_gui_id = $1 AND " . 
+				"e_id = $2 LIMIT 1";
+		$v = array($appId, $frameName);
+		$t = array('s', 's');
+		$res = db_prep_query($sql,$v,$t);
+		$row = db_fetch_array($res);
+		
+		// if found...
+		if ($row) {
+			$currentMap = new Map();
+
+			// use settings from database
+			$currentMap->setWidth($row["e_width"]);
+			$currentMap->setHeight($row["e_height"]);
+			$currentMap->setFrameName($row["e_id"]);
+			
+			// get the WMS 
+			$wmsArray = wms::selectMyWmsByApplication($appId);
+			
+//			$e = new mb_notice("WMS in this map: " . implode(",", $wmsArray));
+			
+			// if this is the overview, find the WMS index and 
+			// reset the WMS array
+			// BEWARE, SUPER UGLY CODE AHEAD!!
+			// (BUT THERE IS NO OTHER WAY TO DO IT)
+			if (strpos($row["e_src"], "mod_mapOV.php?wms") !== false) {
+//				$e = new mb_exception("guess this is the OV");
+				$pattern = "/[\.\/a-zA-Z_]*\?wms=([0-9]*)[^0-9]*/";
+				$ovIndex = preg_replace($pattern, "\$1", $row["e_src"]);
+				$e = new mb_exception("OV index: " . $ovIndex);
+				if (!$ovIndex) {
+					$ovIndex = 0;
+				}
+				$wmsArray = array($wmsArray[$ovIndex]);	
+//				$e = new mb_notice("WMS in this map (corrected): " . implode(",", $wmsArray));
+			}
+			else {
+//				$e = new mb_exception("guess this is NOT the OV");
+			}
+
+			$currentMap->wmsArray = $wmsArray;
+			
+			// TO DO: EXTENT!
+			
+			return $currentMap;			
+		}
+		else {
+			return null;
+		}
+	}
+	
+
+}
+?>
\ No newline at end of file

Modified: trunk/mapbender/http/classes/class_user.php
===================================================================
--- trunk/mapbender/http/classes/class_user.php	2008-06-13 12:57:16 UTC (rev 2501)
+++ trunk/mapbender/http/classes/class_user.php	2008-06-13 13:03:41 UTC (rev 2502)
@@ -38,6 +38,13 @@
 	}	
 	
 	/**
+	 * @return String the ID of this user
+	 */
+	public function __toString () {
+		return (string) $this->id;	
+	}
+	
+	/**
 	 * Returns an array of application IDs that the user is allowed to access.
 	 * 
 	 * @return Array an array of application IDs
@@ -129,5 +136,24 @@
 		}
 		return $ownWFSconfs;
 	}
+	
+	/**
+	 * Returns all WMCs that this user owns
+	 * 
+	 * @return integer[] an array of WMC ids; ids from table mb_user_wmc
+	 */
+	public function getWmcByOwner () {
+		$sql = "SELECT wmc_id FROM mb_user_wmc ";
+		$sql .= "WHERE fkey_user_id = $1 GROUP BY wmc_id";
+		$v = array($this->id);
+		$t = array("i");
+		$res_wmc = db_prep_query($sql, $v, $t);
+
+  		$wmcArray = array();
+		while($row = db_fetch_array($res_wmc)){
+			array_push($wmcArray, $row["wmc_id"]);
+		}
+		return $wmcArray;
+	}
 }
 ?>
\ No newline at end of file

Modified: trunk/mapbender/http/classes/class_wmc.php
===================================================================
--- trunk/mapbender/http/classes/class_wmc.php	2008-06-13 12:57:16 UTC (rev 2501)
+++ trunk/mapbender/http/classes/class_wmc.php	2008-06-13 13:03:41 UTC (rev 2502)
@@ -16,59 +16,87 @@
 # 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__) . "/../../conf/mapbender.conf");
+
 require_once(dirname(__FILE__) . "/../classes/class_wms.php");
-require_once(dirname(__FILE__) . "/../classes/class_mb_exception.php");
 require_once(dirname(__FILE__) . "/../classes/class_layer_monitor.php");
 require_once(dirname(__FILE__) . "/../classes/class_point.php");
 require_once(dirname(__FILE__) . "/../classes/class_bbox.php");
 require_once(dirname(__FILE__) . "/../classes/class_json.php");
+require_once(dirname(__FILE__) . "/../classes/class_map.php");
+require_once(dirname(__FILE__) . "/../classes/class_administration.php");
+require_once(dirname(__FILE__) . "/../classes/class_wmcToXml.php");
 
-$con = db_connect(DBSERVER,OWNER,PW);
-db_select_db(DB,$con);
-
-function mb_utf8_encode ($str) {
-	if(CHARSET=="UTF-8") return utf8_encode($str);
-	return $str;
-}
-function mb_utf8_decode ($str) {
-	if(CHARSET=="UTF-8") return utf8_decode($str);
-	return $str;
-}
-function sepNameSpace($s){
-	$c = strpos($s,":"); 
-	if($c>0)return substr($s,$c+1);
-	return $s;
-}
-
 /**
  * Implementation of a Web Map Context Document, WMC 1.1.0
+ * 
+ * Use cases:
+ * 
+ * Instantiation (1) create a WMC object from a WMC XML document
+ * 		$myWmc = new wmc();
+ * 		$myWmc->createFromXml($xml);
+ * 
+ *    If you want to create a WMC object from a WMC in the database
+ *    	$xml = wmc::getDocument($wmcId);
+ * 		$myWmc = new wmc();
+ * 		$myWmc->createFromXml($xml);
+ *    
+ * 
+ * Instantiation (2) create a WMC from the client side
+ * 		$myWmc = new wmc();
+ * 		$myWmc->createFromJs($mapObject, $generalTitle, $extensionData);
+ * 	
+ * 	  	(creates a WMC from the JS data and then creates an object from that WMC)
+ * 
+ * Output (1) (do Instantiation first) Load a WMC into client
+ * 		This will return an array of JS statements
+ *  
+ * 		$myWmc->toJavaScript();
+ * 
+ * Output (2) (do Instantiation first) Merge with another WMC, then load
+ * 	
+ * 		$myWmc->merge($anotherWmcXml);
+ * 		$myWmc->toJavaScript();
+ * 
  */
 class wmc {
+	/**
+	 * Representing the main map in a map application
+	 * @var Map
+	 */
+	var $mainMap;
 
+	/**
+	 * Representing an (optional) overview map in a map application
+	 * @var Map
+	 */
+	var $overviewMap;
+
+	/**
+	 * @var Array
+	 */
+	var $generalExtensionArray = array();
+
+	/**
+	 * The XML representation of this WMC.
+	 * @var String
+	 */
 	var $xml;
 	
+	// constants
+	var $monitoringIsOn = false;
+	var $saveWmcAsFile = true;
+	var $extensionNamespace = "mapbender";
+	var $extensionNamespaceUrl = "http://www.mapbender.org";
+		
+	// set in constructor
 	var $wmc_id;
+	var $userId;
+
+	// set during parsing
 	var $wmc_version;
-	var $wmc_windowWidth;
-	var $wmc_windowHeight;
-	var $wmc_bBox_SRS;
-	var $wmc_bBox_minx;
-	var $wmc_bBox_maxx;
-	var $wmc_bBox_miny;
-	var $wmc_bBox_maxy;
 	var $wmc_name;
 	var $wmc_title;
 	var $wmc_abstract;
-	var $wmc_general_extension = array();
-	var $wmc_logourl;
-	var $wmc_logourl_format;
-	var $wmc_logourl_type;
-	var $wmc_logourl_width;
-	var $wmc_logourl_height;
-	var $wmc_descriptionurl;
-	var $wmc_descriptionurl_format;
-	var $wmc_descriptionurl_type;
 	var $wmc_keyword = array();
 	var $wmc_contactposition;
 	var $wmc_contactvoicetelephone;
@@ -82,1294 +110,904 @@
 	var $wmc_contactstateorprovince;
 	var $wmc_contactpostcode;
 	var $wmc_contactcountry;
-				
-				
-	var $wmc_wms_title = array();
-	var $wmc_layer_queryable = array();
-	var $wmc_layer_hidden = array();
-	var $wmc_wms_id = array();
-	var $wmc_wms_service = array();
-	var $wmc_wms_version = array();
-	var $wmc_wms_layer_id = array();
-	var $wmc_layer_wfs_featuretype = array();
-	var $wmc_layer_id = array();
-	var $wmc_layer_pos = array();
-	var $wmc_layer_parent = array();
-	var $wmc_layer_querylayer = array();
-	var $wmc_layer_title = array();
-	var $wmc_layer_name = array();
-	var $wmc_layer_abstract = array();
-	var $wmc_layer_srs = array();
-	var $wmc_wms_serviceURL = array();
-	var $wmc_layer_format_current = array();
-	var $wmc_layer_dataurl = array();
-	var $wmc_layer_metadataurl = array();
-	var $wmc_layer_minscale = array();
-	var $wmc_layer_maxscale = array();
-	var $wmc_gui_layer_minscale = array();
-	var $wmc_gui_layer_maxscale = array();
-	var $wmc_layer_format = array();
-	var $wmc_layer_style_current = array();
-	var $wmc_layer_style_name = array();
-	var $wmc_layer_style_title = array();
-	var $wmc_layer_style_legendurl = array();
-	var $wmc_layer_style_legendurl_width = array();
-	var $wmc_layer_style_legendurl_height = array();
-	var $wmc_layer_style_legendurl_format = array();
-	var $wmc_layer_style_legendurl_type = array();
-	var $wmc_layer_style_sld_url = array();
-	var $wmc_layer_style_sld_type = array();
-	var $wmc_layer_style_sld_title = array();
-	
-	var $wmc_wms_count = 0;
-			
-	/*	
-	 	var $data_type = array(); 
-		var $data_format = array();
-		
-		var $objLayer = array(); 
-		  
-		var $gui_wms_mapformat;
-		var $gui_wms_featureinfoformat;
-		var $gui_wms_exceptionformat;
-		var $gui_wms_epsg;
-		  
-		var $default_epsg = 0;
-  */
-	var $monitoringIsOn = false;
-  		
-function wmc() {
-} 
+	var $wmc_logourl;
+	var $wmc_logourl_format;
+	var $wmc_logourl_type;
+	var $wmc_logourl_width;
+	var $wmc_logourl_height;
+	var $wmc_descriptionurl;
+	var $wmc_descriptionurl_format;
+	var $wmc_descriptionurl_type;
 
-/**
- * Saves the current WMC in the log folder.
- * 
- * @return string the filename of the WMC document.
- */
-function saveAsFile() {
-	$filename = "wmc_" . date("Y_m_d_H_i_s") . ".log";
-	$logfile = "../../log/" . $filename;
+	public function __construct () {
+		$this->userId = $_SESSION["mb_user_id"];
+		$this->wmc_id = time();
+	} 
 	
-	if($h = fopen($logfile,"a")){
-		$content = $this->xml;
-		if(!fwrite($h,$content)){
-			$e = new mb_exception("class_wmc.php: failed to write wmc.");
-			return false;
-		}
-		fclose($h);
+	// ---------------------------------------------------------------------------
+	// INSTANTIATION
+	// ---------------------------------------------------------------------------
+
+	/**
+	 * Parses the XML string and instantiates the WMC object.
+	 * 
+	 * @param $xml String
+	 */
+	public function createFromXml ($xml) {
+		return $this->createObjFromWMC_xml($xml);
 	}
-	return $filename;
-}
 
-/**
- * @return string the title of the WMC.
- */
-function getTitle() {
-	return $this->wmc_title;
-}
+	/**
+	 * Loads a WMC from the database.
+	 * 
+	 * @param integer $wmc_id the ID of the WMC document in the database table "mb_user_wmc"
+	 */
+	function createFromDb($wmcId){
+		$this->monitoringIsOn = true;
+		
+		$doc = wmc::getDocument($wmcId);
+		$this->createObjFromWMC_xml($doc);
+	}
 
-/**
- * @return Integer the number of (unique?) WMS contained in this WMC.
- */
-function getNumberOfWms () {
-	return $this->wmc_wms_count;
-}
+	public function createFromApplication ($appId) {
+		// get the map objects "overview" and "mapframe1"
+		$this->mainMap = map::selectMainMapByApplication($appId);
+		$this->overviewMap = map::selectOverviewMapByApplication($appId);
+	}
 
-/**
- * Creates a WMC object from a JS map object {@see map.js}
- * 
- * @param object $mapObject a map object
- * @param integer $user_id the ID of the current user
- * @param string $generalTitle the desired title of the WMC
- * @param object $extensionData data exclusive to Mapbender, which will be 
- * 								mapped into the extension part of the WMC
- */
-function createWMCFromObj($mapObject, $user_id, $generalTitle, $extensionData) {
-	$this->wmc_id = $user_id . '_' . time();
+	/**
+	 * Creates a WMC object from a JS map object {@see map_obj.js}
+	 * 
+	 * @param object $mapObject a map object
+	 * @param integer $user_id the ID of the current user
+	 * @param string $generalTitle the desired title of the WMC
+	 * @param object $extensionData data exclusive to Mapbender, which will be 
+	 * 								mapped into the extension part of the WMC
+	 */
+	public function createFromJs($mapObject, $generalTitle, $extensionData) {
 	
-	$generalWidth = $mapObject->width;
-	$generalHeight = $mapObject->height;
-	$generalBboxSrs = $mapObject->epsg;
-	
-	$arrayBBox = explode(",", $mapObject->extent);
-	$generalBboxMinx = floatval($arrayBBox[0]);
-	$generalBboxMiny = floatval($arrayBBox[1]);
-	$generalBboxMaxx = floatval($arrayBBox[2]);
-	$generalBboxMaxy = floatval($arrayBBox[3]);
-	
-	$generalName = "Mapbender WMC"; // TO do : insert proper data
-	$generalKeywords = array("Mapbender", "WMC"); // TO do : insert proper data
-	$generalAbstract = ""; // TO do : insert proper data
-	$generalLogoUrl = ""; // TO do : insert proper data
-	$generalLogoUrlWidth = ""; // TO do : insert proper data
-	$generalLogoUrlHeight = ""; // TO do : insert proper data
-	$generalLogoUrlFormat = ""; // TO do : insert proper data
-	$generalDescriptionUrl = ""; // TO do : insert proper data
-	$generalContactPerson = "";
-	$generalContactOrganization = "";
-	$generalContactPosition = "";
-	$generalContactAddressType = "";
-	$generalContactAddress = "";
-	$generalContactCity = "";
-	$generalContactStateOrProvince = "";
-	$generalContactPostCode = "";
-	$generalContactCountry = "";
-	$generalContactVoiceTelephone = "";
-	$generalContactFacsimileTelephone = "";
-	$generalContactElectronicMailAddress = "";
-			
-	$extension_namespace = "mapbender";
-	
-	// LayerList variables
-	$layerHidden = "";
-	$layerQueryable = "";
-	$layerAbstract = "";
-	$layerName = "";
-	$layerSrs = "";
-	$layerDataUrl = "";
-	$layerMetadataUrl = "";
-	$layerFormat = "";
-	$layerFormat_current = "";
-	$layerStyle_current = "";
-	$layerStyle_name = "";
-	$layerStyle_title = "";
-	$layerStyle_legendURL = "";
-	$layerStyle_legendURL_width = "";
-	$layerStyle_legendURL_height = "";
-	$layerStyle_legendURL_format = "";		
-	
-	// generate XML
-	$doc = new DOMDocument("1.0", CHARSET);
-	$doc->preserveWhiteSpace = false;
-	
-	// ViewContext
-	$e_view_context = $doc->createElementNS("http://www.opengis.net/context", "ViewContext");
-	
-	
-	$e_view_context->setAttribute("version", "1.0.0");
-	$e_view_context->setAttribute("id", $this->wmc_id);
-	$e_view_context->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
-	$e_view_context->setAttribute("xmlns:mapbender", "http://www.mapbender.org");
-	$e_view_context->setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
-	$e_view_context->setAttribute("xsi:SchemaLocation", "http://schemas.opengis.net/context/1.0.0/context.xsd");
-	
-		// General
-		$e_general = $doc->createElement("General");
-	
-			$e_window = $doc->createElement("Window");
-			if (!empty($generalWidth) && !empty($generalHeight)) {
-				$e_window->setAttribute("width", $generalWidth);
-				$e_window->setAttribute("height", $generalHeight);
+		if (count($mapObject) > 2) {
+			$e = new mb_exception("Save WMC only works for two concurrent map frames (overview plus main) at the moment.");
+		}
+		
+		// set extension data		
+		$this->generalExtensionArray = $extensionData;
+
+		// set title
+		$this->wmc_title = $generalTitle;
+		
+		// create the map objects
+		for ($i = 0; $i < count($mapObject); $i++) {
+			$currentMap = new Map();
+			$currentMap->createFromJs($mapObject[$i]);
+
+			if (isset($mapObject[$i]->isOverview)) {
+				$this->overviewMap = $currentMap;
 			}
-			$e_general->appendChild($e_window);
-			
-			$e_bbox = $doc->createElement("BoundingBox");
-			$e_bbox->setAttribute("SRS", $generalBboxSrs);
-			$e_bbox->setAttribute("minx", $generalBboxMinx);
-			$e_bbox->setAttribute("miny", $generalBboxMiny);
-			$e_bbox->setAttribute("maxx", $generalBboxMaxx);
-			$e_bbox->setAttribute("maxy", $generalBboxMaxy);
-			$e_general->appendChild($e_bbox);
-			
-			$e_name = $doc->createElement("Name", $generalName);
-			$e_general->appendChild($e_name);
-			
-			$e_title = $doc->createElement("Title", $generalTitle);
-			$e_general->appendChild($e_title);
-			
-			$e_keyword_list = $doc->createElement("KeywordList");
-			for ($i=0; $i < count($generalKeywords); $i++) {
-				$e_keyword = $doc->createElement("Keyword", $generalKeywords[$i]);
-				$e_keyword_list->appendChild($e_keyword);
+			else {
+				$this->mainMap = $currentMap;
 			}
-			$e_general->appendChild($e_keyword_list);
-			
-			if ($generalAbstract){
-				$e_abstract = $doc->createElement("Abstract", $generalAbstract);
-				$e_general->appendChild($e_abstract);
-			}
-			
-			if ($generalLogoUrlWidth && $generalLogoUrlHeight && $generalLogoUrlFormat && $generalLogoUrl){
-				$e_logo_url = $doc->createElement("LogoURL");
-				$e_logo_url->setAttribute("width", $generalLogoUrlWidth);
-				$e_logo_url->setAttribute("height", $generalLogoUrlHeight);
-				$e_logo_url->setAttribute("format", $generalLogoUrlFormat);
-			
-				$e_logo_url_or = $doc->createElement("OnlineResource");
-				$e_logo_url_or->setAttributeNS("http://www.opengis.net/context", "xmlns:xlink", "http://www.w3.org/1999/xlink");
-				$e_logo_url_or->setAttribute("xlink:type", "simple");
-				$e_logo_url_or->setAttribute("xlink:href", $generalLogoUrl);
-				$e_logo_url->appendChild($e_logo_url_or);
+		}
+
+		
+		// create XML
+		$this->createXml();
+
+		$this->saveAsFile();
+		return true;
+	}
 	
-				$e_general->appendChild($e_logo_url);
-			}
-				
-			if ($generalDescriptionUrl){
-				$e_description_url = $doc->createElement("DescriptionURL");
-	
-				$e_description_url_or = $doc->createElement("OnlineResource");
-				$e_description_url_or->setAttributeNS("http://www.opengis.net/context", "xmlns:xlink", "http://www.w3.org/1999/xlink");
-				$e_description_url_or->setAttribute("xlink:type", "simple");
-				$e_description_url_or->setAttribute("xlink:href", $generalDescriptionUrl);
-				$e_description_url->appendChild($e_description_url_or);
-	
-				$e_general->appendChild($e_description_url);
-			}
+	// ---------------------------------------------------------------------------
+	// DATABASE FUNCTIONS
+	// ---------------------------------------------------------------------------
+
+	/**
+	 * Stores this WMC in the database. The WMC has to be instantiated first, see above.
+	 * 
+	 * @return mixed[] an assoc array with attributes "success" (boolean) and "message" (String).
+	 */
+	public function insert () {
+		$result = array();
+		if ($this->userId && $this->xml && $this->wmc_title) {
+			$sql = "INSERT INTO mb_user_wmc VALUES ($1, $2, $3, $4, $5)";
+			$v = array($this->wmc_id, $this->userId, $this->xml, $this->wmc_title, time());
+			$t = array("s", "i", "s", "s", "s");
 			
-			if ($generalContactElectronicMailAddress || $generalContactOrganization ||
-				$generalContactPerson || $generalContactPosition || $generalContactAddressType ||
-				$generalContactAddress || $generalContactCity || $generalContactStateOrProvince ||
-				$generalContactPostCode || $generalContactCountry || $generalContactVoiceTelephone ||
-				$generalContactFacsimileTelephone || $generalContactElectronicMailAddress) {
-			
-				$e_contact = $doc->createElement("ContactInformation");
-			
-				if ($generalContactPerson || $generalContactOrganization){
-					$e_contact_person_primary = $doc->createElement("ContactPersonPrimary");
-			
-					if ($generalContactPerson){
-						$e_contact_person = $doc->createElement("ContactPerson", $generalContactPerson);
-						$e_contact_person_primary->appendChild($e_contact_person);
-					}
-					if ($generalContactOrganization){
-						$e_contact_organization = $doc->createElement("ContactOrganization", $generalContactOrganization);
-						$e_contact_person_primary->appendChild($e_contact_organization);
-					}
-					$e_contact->appendChild($e_contact_person_primary);
-				}
-			
-				if ($generalContactPosition){
-					$e_contact_position = $doc->createElement("ContactPosition", $generalContactPosition);
-					$e_contact->appendChild($e_contact_position);
-				}
-			
-				if ($generalContactAddressType || $generalContactAddress || 
-					$generalContactCity || $generalContactStateOrProvince ||
-					$generalContactPostCode || $generalContactCountry) {
-			
-					$e_contact_address = $doc->createElement("ContactAddress");
-			
-					if ($generalContactAddressType){
-						$e_address_type = $doc->createElement("AddressType", $generalContactAddressType);
-						$e_contact_address->appendChild($e_address_type);
-					}
-					if ($generalContactAddress){
-						$e_address = $doc->createElement("Address", $generalContactAddress);
-						$e_contact_address->appendChild($e_address);
-					}
-					if ($generalContactCity){
-						$e_city = $doc->createElement("City", $generalContactCity);
-						$e_contact_address->appendChild($e_city);
-					}
-					if ($generalContactStateOrProvince){
-						$e_state = $doc->createElement("StateOrProvince", $generalContactStateOrProvince);
-						$e_contact_address->appendChild($e_state);
-					}
-					if ($generalContactPostCode){
-						$e_postcode = $doc->createElement("PostCode", $generalContactPostCode);
-						$e_contact_address->appendChild($e_postcode);
-					}
-					if ($generalContactCountry){
-						$e_country = $doc->createElement("Country", $generalContactCountry);
-						$e_contact_address->appendChild($e_country);
-					}
-					$e_contact->appendChild($e_contact_address);
-				}
-				
-				if ($generalContactVoiceTelephone){
-					$e_voice_telephone = $doc->createElement("ContactVoiceTelephone", $generalContactVoiceTelephone);
-					$e_contact->appendChild($e_voice_telephone);
-				}
-				if ($generalContactFacsimileTelephone){
-					$e_facsimile_telephone = $doc->createElement("ContactFacsimileTelephone", $generalContactFacsimileTelephone);
-					$e_contact->appendChild($e_facsimile_telephone);
-				}
-				if ($generalContactElectronicMailAddress){
-					$e_email = $doc->createElement("ContactElectronicMailAddress", $generalContactElectronicMailAddress);
-					$e_contact->appendChild($e_email);
-				}
-				$e_general->appendChild($e_contact);
+			$res = db_prep_query($sql, $v, $t);
+			if (db_error()) {
+				$errMsg = "Error while saving WMC document '" . $this->wmc_title . "': " . db_error();
+				$result["success"] = false;
+				$result["message"] = $errMsg;
+				$e = new mb_exception("mod_insertWMCIntoDB: " . $errMsg);
 			}
-			
-			
-			if (count($extensionData) > 0) {
-				//$e = new mb_exception("writing wmc...");
-				$e_extensionGeneral = $doc->createElement("Extension");
-				
-				foreach ($extensionData as $keyExtensionData => $valueExtensionData) {
-					$e_currentExtensionTag = $doc->createElement($extension_namespace.":".$keyExtensionData, $valueExtensionData);
-					$e_extensionGeneral->appendChild($e_currentExtensionTag);
-				}
-				$e_general->appendChild($e_extensionGeneral);
+			else {
+				$result["success"] = true;
+				$msg = "WMC document '" . $this->wmc_title . "' has been saved.";
+				$result["message"] = $msg;
+				$e = new mb_notice("mod_insertWMCIntoDB: WMC  '" . $this->wmc_title . "' saved successfully.");
 			}
-		$e_view_context->appendChild($e_general);
+		}
+		else {
+			$result["success"] = false;
+			$errMsg = "missing parameters (user_id: ".$this->userId.", title: " . $this->wmc_title . ")";
+			$result["message"] = $errMsg;
+			$e = new mb_exception("mod_insertWMCIntoDB: " . $errMsg .")");
+		}
+		return $result;
+	}
 	
+	/**
+	 * deletes a {@link http://www.mapbender.org/index.php/WMC WMC} 
+	 * entry specified by wmc_id and user_id
+	 *
+	 * @param	integer		the user_id
+	 * @param	string		the wmc_id
+	 * @return	boolean		Did the query run successful?
+	 */
+	public static function delete ($wmcId, $userId) {
+		if (!isset($userId) || $userId === null) {
+			$userId = $_SESSION["mb_user_id"];
+		}
+		
+		$sql = "DELETE FROM mb_user_wmc ";
+		$sql .= "WHERE fkey_user_id = $1 AND wmc_id = $2";
+		$v = array($userId, $wmcId);
+		$t = array('i', 's');
+		$res = db_prep_query($sql, $v, $t);
+		if ($res) {
+			return true;
+		}
+		return false;
+	}
+
+	/**
+	 * Returns a WMC document
+	 * @return String|boolean The document if it exists; else false
+	 * @param $id String the WMC id
+	 */
+	public static function getDocument ($id) {
+		$sql = "SELECT wmc FROM mb_user_wmc WHERE wmc_id = $1";
+		$v = array($id);
+		$t = array('s');
+		$res = db_prep_query($sql,$v,$t);
+		$row = db_fetch_array($res);
+		if ($row) {
+			return $row["wmc"];
+		}
+		return false;
+	}
+
+	// ---------------------------------------------------------------------------
+	// GETTER FUNCTIONS
+	// ---------------------------------------------------------------------------
 	
-		// LayerList
-		$e_layer_list = $doc->createElement("LayerList");
+	/**
+	 * @return string the title of the WMC.
+	 */
+	public function getTitle() {
+		return $this->wmc_title;
+	}
+
+	// ---------------------------------------------------------------------------
+	// OUTPUT FUNCTIONS
+	// ---------------------------------------------------------------------------
 	
-		for ($i=0; $i < count($mapObject->wms); $i++){
-			$wmsId = $mapObject->wms[$i]->wms_id;
-			$wms_epsg = array();
-			$wms_epsg[0] = $mapObject->epsg;
-		
-			if ($mapObject->wms[$i]->gui_wms_epsg != $mapObject->epsg){
-				$wms_epsg[1] = $mapObject->wms[$i]->gui_wms_epsg;
+	/**
+	 * Wrapper function, returns XML at the moment
+	 * @return String
+	 */
+	public function __toString() {
+		return $this->toXml();
+	}
+
+	/**
+	 * Returns the XML document if available
+	 * 
+	 * @return String The XML document; if unavailable, null is returned.
+	 */
+	public function toXml () {
+		if (!$this->xml) {
+			$this->createXml();
+		}
+		return $this->xml;
+	}
+
+	/**
+	 * Returns an array of JavaScript statements
+	 * 
+	 * @return String[]
+	 */
+	public function toJavaScript () {
+/*
+		// counts how often a layer has been loaded
+		if ($this->monitoringIsOn) {
+			$monitor = new Layer_load_count();
+			for ($i = 0; $i < count($this->wmc_layer_id); $i++) {
+				$monitor->increment($this->wmc_layer_id[$i]);
 			}
-	
-			for ($q = 0; $q < count($mapObject->wms[$i]->gui_epsg); $q++){
-				$isInArray = false;
-				
-				for ($r=0 ; $r < count($wms_epsg); $r++){
-					if ($wms_epsg[$r] == $mapObject->wms[$i]->gui_epsg[$q]){
-						$isInArray = true;
-					}
-				}
-				if ($isInArray == false){
-					array_push($wms_epsg, $mapObject->wms[$i]->gui_epsg[$q]);
-				}
-			}
-			for ($j = 0; $j < count($mapObject->wms[$i]->objLayer); $j++){
-				if ($mapObject->wms[$i]->objLayer[$j]->layer_parent != ''){
-					if ($mapObject->wms[$i]->objLayer[$j]->gui_layer_visible == "1"){
-						$layerHidden = 0;
-					}
-					else{
-						$layerHidden = 1;
-					}
-					$layerQueryable = $mapObject->wms[$i]->objLayer[$j]->layer_queryable;
-					$layerQuerylayer = $mapObject->wms[$i]->objLayer[$j]->gui_layer_querylayer;
-					$layerId = $mapObject->wms[$i]->objLayer[$j]->layer_uid;
-					$layerName = $mapObject->wms[$i]->objLayer[$j]->layer_name;
-					$layerTitle = $mapObject->wms[$i]->objLayer[$j]->layer_title;
-					$layerAbstract = $mapObject->wms[$i]->wms_abstract; //To Do: insert actual abstract
-					$layerDataUrl = $mapObject->wms[$i]->objLayer[$j]->layer_dataurl_href; 
-					$layerMetadataUrl = $mapObject->wms[$i]->objLayer[$j]->layer_metadataurl;
-					$layerMinscale = $mapObject->wms[$i]->objLayer[$j]->layer_minscale; 
-					$layerMaxscale = $mapObject->wms[$i]->objLayer[$j]->layer_maxscale; 
-					$guiLayerMinscale = $mapObject->wms[$i]->objLayer[$j]->gui_layer_minscale; 
-					$guiLayerMaxscale = $mapObject->wms[$i]->objLayer[$j]->gui_layer_maxscale; 
-					$wmsVersion = $mapObject->wms[$i]->wms_version;
-					$wmsTitle = $mapObject->wms[$i]->wms_title;
-					$wmsLayerId = $mapObject->wms[$i]->objLayer[0]->layer_uid;
-					$wmsOnlineResource = $mapObject->wms[$i]->wms_getmap;
-					$layerPos = $mapObject->wms[$i]->objLayer[$j]->layer_pos;
-					$layerParent = $mapObject->wms[$i]->objLayer[$j]->layer_parent;
-					$queryLayer = $mapObject->wms[$i]->objLayer[$j]->gui_layer_querylayer;
-					$wfsFeatureType = $mapObject->wms[$i]->objLayer[$j]->gui_layer_wfs_featuretype;
-					
-					$e_layer = $doc->createElement("Layer");
-					$e_layer->setAttribute("queryable", $layerQueryable);
-					$e_layer->setAttribute("hidden", $layerHidden);
+		}
+*/		
+		// will contain the JS code to create the maps
+		// representing the state stored in this WMC
+		$wmcJsArray = array();
 		
-						$e_service = $doc->createElement("Server");
-						$e_service->setAttribute("service", "OGC:WMS");
-						$e_service->setAttribute("version", $wmsVersion);
-						$e_service->setAttribute("title", $wmsTitle);
+		// set general extension data
+		if (count($this->generalExtensionArray) > 0) {
+			$json = new Mapbender_JSON();
+			array_push($wmcJsArray, "restoredWmcExtensionData = " . $json->encode($this->generalExtensionArray) . ";"); 
+		}
 		
-						$e_service_or = $doc->createElement("OnlineResource");
-						$e_service_or->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
-						$e_service_or->setAttribute("xlink:type", "simple");
-						$e_service_or->setAttribute("xlink:href", $wmsOnlineResource);
-						
-						$e_service->appendChild($e_service_or);
-						$e_layer->appendChild($e_service);
-		
-						$e_layer_name = $doc->createElement("Name", $layerName);
-						$e_layer->appendChild($e_layer_name);
-		
-						$e_layer_title = $doc->createElement("Title", $layerTitle);
-						$e_layer->appendChild($e_layer_title);
-		
-						if ($layerAbstract){
-							$e_layer_abstract = $doc->createElement("Abstract", $layerAbstract);
-							$e_layer->appendChild($e_layer_abstract);
-						}
-		
-						$e_layer_srs = $doc->createElement("SRS", implode(" ", $wms_epsg));
-						$e_layer->appendChild($e_layer_name);
-		
-						if ($layerDataUrl){
-							$e_layer_data_url = $doc->createElement("DataURL");
-			
-							$e_layer_data_url_or = $doc->createElement("OnlineResource");
-							$e_layer_data_url_or->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
-							$e_layer_data_url_or->setAttribute("xlink:type", "simple");
-							$e_layer_data_url_or->setAttribute("xlink:href", $layerDataUrl);
-							
-							$e_layer_data_url->appendChild($e_layer_data_url_or);
-							$e_layer->appendChild($e_layer_data_url);
-						}
-		
-						if ($layerMetadataUrl){
-							$e_layer_metadata_url = $doc->createElement("MetadataURL");
-			
-							$e_layer_metadata_url_or = $doc->createElement("OnlineResource");
-							$e_layer_metadata_url_or->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
-							$e_layer_metadata_url_or->setAttribute("xlink:type", "simple");
-							$e_layer_metadata_url_or->setAttribute("xlink:href", $layerMetadataUrl);
-							
-							$e_layer_metadata_url->appendChild($e_layer_metadata_url_or);
-							$e_layer->appendChild($e_layer_metadata_url);
-						}
-		
-						$e_extension = $doc->createElement("Extension");
-		
-						$e_scalehint = $doc->createElement($extension_namespace.":ScaleHint");
-						$e_scalehint->setAttribute("min", $layerMinscale);
-						$e_scalehint->setAttribute("max", $layerMaxscale);
-						$e_extension->appendChild($e_scalehint);
-						
-						$e_gui_scalehint = $doc->createElement($extension_namespace.":guiScaleHint");
-						$e_gui_scalehint->setAttribute("min", $guiLayerMinscale);
-						$e_gui_scalehint->setAttribute("max", $guiLayerMaxscale);
-						$e_extension->appendChild($e_gui_scalehint);
-						
-						$e_layer_id = $doc->createElement($extension_namespace.":layer_id", $layerId);
-						$e_extension->appendChild($e_layer_id);
-						
-						$e_wms_layer_id = $doc->createElement($extension_namespace.":wms_layer_id", $wmsLayerId);
-						$e_extension->appendChild($e_wms_layer_id);
-						
-						$e_layer_pos = $doc->createElement($extension_namespace.":layer_pos", $layerPos);
-						$e_extension->appendChild($e_layer_pos);
-		
-						$e_layer_parent = $doc->createElement($extension_namespace.":layer_parent", $layerParent);
-						$e_extension->appendChild($e_layer_parent);
-		
-						$e_wms_id = $doc->createElement($extension_namespace.":wms_id", $wmsId);
-						$e_extension->appendChild($e_wms_id);
-		
-						$e_querylayer = $doc->createElement($extension_namespace.":querylayer", $layerQuerylayer);
-						$e_extension->appendChild($e_querylayer);
-						
-						if ($wfsFeatureType) {
-							$e_wfsFeatureType = $doc->createElement($extension_namespace.":wfsFeatureType", $wfsFeatureType);
-							$e_extension->appendChild($e_wfsFeatureType);
-						}
+		// reset WMS data
+		array_push($wmcJsArray, "wms = [];");
+		array_push($wmcJsArray, "wms_layer_count = 0;");
 
-						$e_layer->appendChild($e_extension);
-		
-						//layerFormat
-						$e_layer_format = $doc->createElement("FormatList");
-		
-						$data_format_current = false;
-		
-						for ($k = 0; $k < count($mapObject->wms[$i]->data_format); $k++){
-		
-							if ($mapObject->wms[$i]->data_type[$k] == "map") {
-								$layerFormat = $mapObject->wms[$i]->data_format[$k];
-				
-								$e_format = $doc->createElement("Format", $layerFormat);
-		
-								if ($data_format_current == false && ( 
-										($mapObject->wms[$i]->data_format[$k] == $mapObject->wms[$i]->gui_wms_mapformat) ||
-										($k == (count($mapObject->wms[$i]->data_format)-1))
-								)){
-		
-									$e_format->setAttribute("current", "1");
-									$data_format_current = true;
-								}
-								$e_layer_format->appendChild($e_format);
-							}
-						}
-						$e_layer->appendChild($e_layer_format);
-		
-		
-						// LayerStyle
-						$e_layer_stylelist = $doc->createElement("StyleList");
-			
-						for ($k = 0; $k < count($mapObject->wms[$i]->objLayer[$j]->layer_style); $k++){
-		
-							if ($k == 0){
-								$layerStyle_current = 1; // To do: insert proper data
-							}
-							else{
-								$layerStyle_current = 0; // To do: insert proper data
-							}
-		
-							$e_layer_style = $doc->createElement("Style");
-		
-							$layerStyleSLD = "";
-		
-							if ($layerStyleSLD){
-								$layerStyleSLDUrl = ""; // To Do: Insert Proper Data
-								
-								$e_layer_style_or = $doc->createElement("OnlineResource");
-								$e_layer_style_or->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
-								$e_layer_style_or->setAttribute("xlink:type", "simple");
-								$e_layer_style_or->setAttribute("xlink:href", $layerStyleSLDUrl);
-								$e_layer_style->appendChild($e_layer_style_or);
-							}
-							else{
-								//TODO: determine correct layer style entries
-								$layerStyle_name = $mapObject->wms[$i]->objLayer[$j]->layer_style[$k]->name;
-								$layerStyle_title = $mapObject->wms[$i]->objLayer[$j]->layer_style[$k]->title;
-								$layerStyle_legendUrl = $mapObject->wms[$i]->objLayer[$j]->layer_style[$k]->legendurl;
-								$layerStyle_legendUrl_width = ""; // To Do: add proper data
-								$layerStyle_legendUrl_height = ""; // To Do: add proper data
-								$layerStyle_legendUrl_format = ""; // To Do: add proper data
-		
-								if ($layerStyle_current == 1){
-									$e_layer_style->setAttribute("current", "1");
-								}
-	
-								$e_layer_style_name = $doc->createElement("Name", $layerStyle_name);
-								$e_layer_style->appendChild($e_layer_style_name);
-	
-								$e_layer_style_title = $doc->createElement("Title", $layerStyle_title);
-								$e_layer_style->appendChild($e_layer_style_title);
-								
-								
-								$e_layer_style_legendurl = $doc->createElement("LegendUrl");
-								$e_layer_style_legendurl->setAttribute("width", $layerStyle_legendUrl_width);
-								$e_layer_style_legendurl->setAttribute("height", $layerStyle_legendUrl_height);
-								$e_layer_style_legendurl->setAttribute("format", $layerStyle_legendUrl_format);
-	
-								$e_layer_style_legendurl_or = $doc->createElement("OnlineResource");
-								$e_layer_style_legendurl_or->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
-								$e_layer_style_legendurl_or->setAttribute("xlink:type", "simple");
-								$e_layer_style_legendurl_or->setAttribute("xlink:href", $layerStyle_legendUrl);
-								$e_layer_style_legendurl->appendChild($e_layer_style_legendurl_or);
-								$e_layer_style->appendChild($e_layer_style_legendurl);
-							}
-							$e_layer_stylelist->appendChild($e_layer_style);
-						}	
-						$e_layer->appendChild($e_layer_stylelist);
-						
-					$e_layer_list->appendChild($e_layer);
+		// add WMS for main map frame
+		$wmsArray = $this->mainMap->getWmsArray();
+		// for all wms...
+		for ($i = 0; $i < count($wmsArray); $i++) {
+			// ..add wms and set properties
+			array_push($wmcJsArray, $wmsArray[$i]->createJsObjFromWMS_());
+		}
+
+		// delete existing map objects...
+		array_push($wmcJsArray, "mb_mapObj = [];");
+
+		// .. and add the overview map (if exists)
+		if ($this->overviewMap !== null) {
+			// find the WMS in the main map which is equal to the WMS
+			// in the overview map
+			$ovWmsArray = $this->overviewMap->getWmsArray();
+			$overviewWmsIndex = 0;
+			for ($i = 0; $i < count($ovWmsArray); $i++) {
+				for ($j = 0; $j < count($wmsArray); $j++) {
+					if ($ovWmsArray[$i]->equals($wmsArray[$j])) {
+						$overviewWmsIndex = $j;						
+						break;
+					}
 				}
 			}
+			$wmcJsArray = array_merge($wmcJsArray, $this->overviewMap->toJavaScript($overviewWmsIndex));
+
 		}
-		$e_view_context->appendChild($e_layer_list);
-	
-	
-	$doc->appendChild($e_view_context);
-	$this->xml = $doc->saveXML();
-	
-	// for debugging: saving WMC as file
-	// (comment when no longer needed)
-	$filename = $this->saveAsFile();
-	if ($filename) {
-		$e = new mb_notice("class_wmc: saving WMC as file " . $filename);
-	}
-}
 
-/**
- * Loads a WMC from the database.
- * 
- * @param integer $wmc_id the ID of the WMC document in the database table "mb_user_wmc"
- */
-function createObjFromWMC_id($wmc_id){
+		// .. and add main map ..
+		$wmcJsArray = array_merge($wmcJsArray, $this->mainMap->toJavaScript(null));
+
+		// Finally, request the maps
+		array_push($wmcJsArray, "setMapRequest('" . $this->mainMap->getFrameName() . "');");
+		if ($this->overviewMap !== null) {
+			array_push($wmcJsArray, "setMapRequest('" . $this->overviewMap->getFrameName() . "');");
+		}
+
+		array_push($wmcJsArray, "eventAfterLoadWMS.trigger()");
+		return $wmcJsArray;
+	}
 	
-	global $DBSERVER,$DB,$OWNER,$PW;
-	$con = db_connect($DBSERVER,$OWNER,$PW);
-	db_select_db(DB, $con);
-	
-	$sql = "SELECT wmc FROM mb_user_wmc WHERE wmc_id = $1";
-	$v = array($wmc_id);
-	$t = array("i");
-	$res = db_prep_query($sql, $v, $t);
-	$wmc = db_fetch_row($res);
-	$this->createObjFromWMC_xml($wmc[0]);
-	$this->monitoringIsOn = true;
-}
+	// ------------------------------------------------------------------------
+	// manipulation
+	// ------------------------------------------------------------------------
+	/**
+	 * Merges this WMC with another WMC.
+	 * The settings of the other WMC overwrite the settings of this WMC.
+	 * 
+	 * @return void
+	 * @param $xml2 Object
+	 */
+	public function merge ($xml2) {
+		$someWmc = new wmc();
+		$someWmc->createFromXml($xml2);
 
-/**
- * Loads a WMC from an actual WMC XML document.
- * 
- * @param string $data the data from the XML file
- */
-function createObjFromWMC_xml($data){
-#	$data = str_replace("&#38;", "&amp;", $data);
+		$this->mainMap->merge($someWmc->mainMap);
+		if (isset($this->overviewMap) && isset($someWmc->overviewMap)) {
+			$this->overviewMap->merge($someWmc->overviewMap);
+		}
+	}
 
-	// store xml 
-	$this->xml = $data;
+	/**
+	 * Appends the layers of another WMC to this WMC.
+	 * 
+	 * @return void
+	 * @param $xml2 Object
+	 */
+	public function append ($xml2) {
+		$someWmc = new wmc();
+		$someWmc->createFromXml($xml2);
 
-	// for debugging: saving WMC as file
-	// (comment when no longer needed)
-	$filename = $this->saveAsFile();
-	if ($filename) {
-		$e = new mb_notice("class_wmc: saving WMC as file " . $filename);
+		$this->mainMap->append($someWmc->mainMap);
+		if (isset($this->overviewMap) && isset($someWmc->overviewMap)) {
+			// There is only one WMS in the overview map; merge, not append
+			$this->overviewMap->merge($someWmc->overviewMap);
+		}
 	}
 
 
+// ---------------------------------------------------------------------------
+// private functions
+// ---------------------------------------------------------------------------
 
-	$values = NULL;
-	$tags = NULL;
-	$parser = xml_parser_create(CHARSET);
-	xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
-	xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
-	xml_parser_set_option($parser,XML_OPTION_TARGET_ENCODING,CHARSET);
-	xml_parse_into_struct($parser,$data,$values,$tags);
-	$code = xml_get_error_code ($parser);
-	if ($code) {
-		$line = xml_get_current_line_number($parser); 
-		$mb_exception = new mb_exception(xml_error_string($code) .  " in line " . $line);
-		return false;
-	}
-	xml_parser_free($parser);
+	/**
+	 * Loads a WMC from an actual WMC XML document.
+	 * Uses WMS class.
+	 * 
+	 * @param string $data the data from the XML file
+	 */
+	private function createObjFromWMC_xml($data){
+		// store xml 
+		$this->xml = $data;
 	
-	$section = NULL;
-	$format = NULL;
-	$cnt_format = 0;
-	$parent = array();
-	$myParent = array();
-	$cnt_layer = -1;
-	$request = NULL; 
-	$layer_style = array();
-	$extension = false;
-	
-	$general = false;
-	$layerlist = false;
-	$layer = false;
-	$formatlist = false;
-	$dataurl = false;
-	$metadataurl = false; 
-	$stylelist = false;
-	$cnt_style = -1;
-		 
-	foreach ($values as $element) {
-					$verbose .= ".";
-		if(strtoupper($element[tag]) == "VIEWCONTEXT" && $element[type] == "open"){
-				$this->wmc_id = $element[attributes]["id"];
-				$this->wmc_version = $element[attributes]["version"];
-		}
-		if(strtoupper($element[tag]) == "GENERAL" && $element[type] == "open"){
-		   $general = true;
-		}
-		if(strtoupper($element[tag]) == "LAYERLIST" && $element[type] == "open"){
-		   $layerlist = true;
-		}
-		if ($general) {
-				if(strtoupper($element[tag]) == "WINDOW"){
-						$this->wmc_windowWidth = $element[attributes]["width"];
-						$this->wmc_windowHeight = $element[attributes]["height"];
+		$values = administration::parseXml($data);
+
+		//
+		// Local variables that indicate which section of the WMC
+		// is currently parsed.
+		//
+		$extension = false;		$general = false;		$layerlist = false;
+		$layer = false;  		$formatlist = false;	$dataurl = false;
+		$metadataurl = false; 	$stylelist = false;
+
+		//
+		// reset WMC data
+		//
+		$this->mainMap = new Map();
+		$this->overviewMap = null;
+		$this->generalExtensionArray = array();
+		
+		$layerlistArray = array();
+		$layerlistArray["main"] = array();
+		$layerlistArray["overview"] = array();
+			 
+		foreach ($values as $element) {
+			$tag = strtoupper(administration::sepNameSpace($element[tag]));
+			$tagLowerCase = administration::sepNameSpace($element[tag]);
+			$type = $element[type];
+			$attributes = $element[attributes];
+			$value = mb_utf8_decode(html_entity_decode($element[value])); // TODO: not sure if utf decoding is necessary
+			
+			if ($tag == "VIEWCONTEXT" && $type == "open") {
+				$this->wmc_id = $attributes["id"];
+				$this->wmc_version = $attributes["version"];
+			}
+			if ($tag == "GENERAL" && $type == "open") {
+				$general = true;
+			}
+			if ($tag == "LAYERLIST" && $type == "open") {
+				$layerlist = true;
+			}
+			if ($general) {
+				if ($tag == "WINDOW") {
+					$this->mainMap->setWidth($attributes["width"]);
+					$this->mainMap->setHeight($attributes["height"]);
 				}
-				if(strtoupper($element[tag]) == "BOUNDINGBOX"){
-						$this->wmc_bBox_SRS = $element[attributes]["SRS"];
-						$this->wmc_bBox_minx = $element[attributes]["minx"];
-						$this->wmc_bBox_miny = $element[attributes]["miny"];
-						$this->wmc_bBox_maxx = $element[attributes]["maxx"];
-						$this->wmc_bBox_maxy = $element[attributes]["maxy"];
+				if ($tag == "BOUNDINGBOX") {
+					$bbox = new Mapbender_bbox($attributes["minx"], $attributes["miny"], $attributes["maxx"], $attributes["maxy"], $attributes["SRS"]);
+					$this->mainMap->setExtent($bbox);
 				}
-				if(strtoupper($element[tag]) == "NAME"){
-						$this->wmc_name = mb_utf8_decode(html_entity_decode($element[value]));
+				if ($tag == "NAME") {
+					$this->wmc_name = $value;
 				}
-				if(strtoupper($element[tag]) == "TITLE"){
-						$this->wmc_title = mb_utf8_decode(html_entity_decode($element[value]));
+				if ($tag == "TITLE") {
+					$this->wmc_title = $value;
 				}
-				if(strtoupper($element[tag]) == "ABSTRACT"){
-						$this->wmc_abstract = mb_utf8_decode(html_entity_decode($element[value]));
+				if ($tag == "ABSTRACT") {
+					$this->wmc_abstract = $value;
 				}
-				if(strtoupper($element[tag]) == "CONTACTINFORMATION" && $element['type'] == "open"){
-						$contactinformation = true;
+				if ($tag == "CONTACTINFORMATION" && $type == "open") {
+					$contactinformation = true;
 				}
 				if ($contactinformation) {
-					if(strtoupper($element[tag]) == "CONTACTPOSITION"){
-							$this->wmc_contactposition = mb_utf8_decode(html_entity_decode($element[value]));
+					if ($tag == "CONTACTPOSITION") {
+						$this->wmc_contactposition = $value;
 					}
-					if(strtoupper($element[tag]) == "CONTACTVOICETELEPHONE"){
-							$this->wmc_contactvoicetelephone = $element[value];
+					if ($tag == "CONTACTVOICETELEPHONE") {
+						$this->wmc_contactvoicetelephone = $value;
 					}
-					if(strtoupper($element[tag]) == "CONTACTFACSIMILETELEPHONE"){
-							$this->wmc_contactfacsimiletelephone = $element[value];
+					if ($tag == "CONTACTFACSIMILETELEPHONE") {
+						$this->wmc_contactfacsimiletelephone = $value;
 					}
-					if(strtoupper($element[tag]) == "CONTACTELECTRONICMAILADDRESS"){
-							$this->wmc_contactemail = mb_utf8_decode(html_entity_decode($element[value]));
+					if ($tag == "CONTACTELECTRONICMAILADDRESS") {
+						$this->wmc_contactemail = $value;
 					}
-					if(strtoupper($element[tag]) == "CONTACTPERSONPRIMARY" && $element['type'] == "open"){
-							$contactpersonprimary = true;
+					if ($tag == "CONTACTPERSONPRIMARY" && $type == "open") {
+						$contactpersonprimary = true;
 					}
 					if ($contactpersonprimary) {
-						if(strtoupper($element[tag]) == "CONTACTPERSON"){
-								$this->wmc_contactperson = mb_utf8_decode(html_entity_decode($element[value]));
+						if ($tag == "CONTACTPERSON") {
+							$this->wmc_contactperson = $value;
 						}
-						if(strtoupper($element[tag]) == "CONTACTORGANIZATION"){
-								$this->wmc_contactorganization = mb_utf8_decode(html_entity_decode($element[value]));
+						if ($tag == "CONTACTORGANIZATION") {
+							$this->wmc_contactorganization = $value;;
 						}
-						if(strtoupper($element[tag]) == "CONTACTPERSONPRIMARY" && $element['type'] == "close"){
-								$contactpersonprimary = false;
+						if ($tag == "CONTACTPERSONPRIMARY" && $type == "close") {
+							$contactpersonprimary = false;
 						}
 					}
-					if(strtoupper($element[tag]) == "CONTACTADDRESS" && $element['type'] == "open"){
-							$contactaddress = true;
+					if ($tag == "CONTACTADDRESS" && $type == "open") {
+						$contactaddress = true;
 					}
 					if ($contactaddress) {
-						if(strtoupper($element[tag]) == "ADDRESSTYPE"){
-								$this->wmc_contactaddresstype = mb_utf8_decode(html_entity_decode($element[value]));
+						if ($tag == "ADDRESSTYPE") {
+							$this->wmc_contactaddresstype = $value;
 						}
-						if(strtoupper($element[tag]) == "ADDRESS"){
-								$this->wmc_contactaddress = mb_utf8_decode(html_entity_decode($element[value]));
+						if ($tag == "ADDRESS") {
+							$this->wmc_contactaddress = $value;
 						}
-						if(strtoupper($element[tag]) == "CITY"){
-								$this->wmc_contactcity = mb_utf8_decode(html_entity_decode($element[value]));
+						if ($tag == "CITY") {
+							$this->wmc_contactcity = $value;
 						}
-						if(strtoupper($element[tag]) == "STATEORPROVINCE"){
-								$this->wmc_contactstateorprovince = mb_utf8_decode(html_entity_decode($element[value]));
+						if ($tag == "STATEORPROVINCE") {
+							$this->wmc_contactstateorprovince = $value;
 						}
-						if(strtoupper($element[tag]) == "POSTCODE"){
-								$this->wmc_contactpostcode = $element[value];
+						if ($tag == "POSTCODE"){
+							$this->wmc_contactpostcode = $value;
 						}
-						if(strtoupper($element[tag]) == "COUNTRY"){
-								$this->wmc_contactcountry = mb_utf8_decode(html_entity_decode($element[value]));
+						if ($tag == "COUNTRY") {
+							$this->wmc_contactcountry = $value;
 						}
-						if(strtoupper($element[tag]) == "CONTACTADDRESS" && $element['type'] == "close"){
-								$contactaddress = false;
+						if ($tag == "CONTACTADDRESS" && $type == "close") {
+							$contactaddress = false;
 						}
 					}
 				}
-				if(strtoupper($element[tag]) == "LOGOURL" && $element['type'] == "open"){
+				if ($tag == "LOGOURL" && $type == "open") {
 						$logourl = true;
-						$this->wmc_logourl_width = $element[attributes]["width"];
-						$this->wmc_logourl_height = $element[attributes]["height"];
-						$this->wmc_logourl_format = $element[attributes]["format"];
+						$this->wmc_logourl_width = $attributes["width"];
+						$this->wmc_logourl_height = $attributes["height"];
+						$this->wmc_logourl_format = $attributes["format"];
 				}
 				if ($logourl) {
-					if(strtoupper($element[tag]) == "LOGOURL" && $element['type'] == "close"){
-							$logourl = false;
+					if ($tag == "LOGOURL" && $type == "close") {
+						$logourl = false;
 					}
-					if(strtoupper($element[tag]) == "ONLINERESOURCE"){
-						$this->wmc_logourl_type = $element[attributes]["xlink:type"];
-						$this->wmc_logourl = $element[attributes]["xlink:href"];
+					if ($tag == "ONLINERESOURCE") {
+						$this->wmc_logourl_type = $attributes["xlink:type"];
+						$this->wmc_logourl = $attributes["xlink:href"];
 					}
 				}
-				if(strtoupper($element[tag]) == "DESCRIPTIONURL" && $element['type'] == "open"){
+				if ($tag == "DESCRIPTIONURL" && $type == "open") {
 						$descriptionurl = true;
-						$this->wmc_descriptionurl_format = $element[attributes]["format"];
+						$this->wmc_descriptionurl_format = $attributes["format"];
 				}
 				if ($descriptionurl) {
-					if(strtoupper($element[tag]) == "DESCRIPTIONURL" && $element['type'] == "close"){
-							$descriptionurl = false;
+					if ($tag == "DESCRIPTIONURL" && $type == "close"){
+						$descriptionurl = false;
 					}
-					if(strtoupper($element[tag]) == "ONLINERESOURCE"){
-						$this->wmc_descriptionurl_type = $element[attributes]["xlink:type"];
-						$this->wmc_descriptionurl = $element[attributes]["xlink:href"];
+					if ($tag == "ONLINERESOURCE") {
+						$this->wmc_descriptionurl_type = $attributes["xlink:type"];
+						$this->wmc_descriptionurl = $attributes["xlink:href"];
 					}
 				}
-				if(strtoupper($element[tag]) == "KEYWORDLIST" && $element['type'] == "open"){
-						$keywordlist = true;
+				if ($tag == "KEYWORDLIST" && $type == "open") {
+					$keywordlist = true;
 				}
 				if ($keywordlist) {
-					if(strtoupper($element[tag]) == "KEYWORDLIST" && $element['type'] == "close"){
-							$keywordlist = false;
-							$cnt_keyword = -1;
+					if ($tag == "KEYWORDLIST" && $type == "close") {
+						$keywordlist = false;
+						$cnt_keyword = -1;
 					}
-					if(strtoupper($element[tag]) == "KEYWORD"){
+					if ($tag == "KEYWORD") {
 						$cnt_keyword++;
-						$this->wmc_keyword[$cnt_keyword] = mb_utf8_decode(html_entity_decode($element[value]));
+						$this->wmc_keyword[$cnt_keyword] = $value;
 					}
 				}
-						
-				if(strtoupper($element[tag]) == "EXTENSION" && $element['type'] == "close"){
+				if ($tag == "EXTENSION" && $type == "close") {
 					$generalExtension = false;
+					//
+					// After the general extension tag is closed,
+					// we have all necessary information to CREATE
+					// the map objects that are contained in this
+					// WMC.
+					//
+					$this->setMapData();
 				}
 				if ($generalExtension) {
-					$this->wmc_general_extension[sepNameSpace($element[tag])] = $element[value];
-//					$e = new mb_exception("WMC: " . $element[tag] . ": " . $element[value]);
+					$this->generalExtensionArray[$tag] = $value;
 				}
-				if(strtoupper($element[tag]) == "EXTENSION" && $element['type'] == "open"){
+				if ($tag == "EXTENSION" && $type == "open") {
 					$generalExtension = true;
 				}
-
-				if(strtoupper($element[tag]) == "GENERAL" && $element['type'] == "close"){
+				if ($tag == "GENERAL" && $type == "close") {
 		   			$general = false;
 			 	}
-		}
-		if ($layerlist) {
-				if(strtoupper($element[tag]) == "LAYERLIST" && $element['type'] == "close"){
+			}
+			if ($layerlist) {
+				if ($tag == "LAYERLIST" && $type == "close") {
 				   $layerlist = false;
 				}
-				if(strtoupper($element[tag]) == "LAYER" && $element[type] == "open"){
-					 $cnt_layer++;
-					 $this->wmc_layer_queryable[$cnt_layer] = $element[attributes]["queryable"];
-					 $this->wmc_layer_hidden[$cnt_layer] = $element[attributes]["hidden"];
-					 $layer = true;
-      		 		 $cnt_epsg = 0;
+				if ($tag == "LAYER" && $type == "open") {
+					//
+					// The associative array currentLayer holds all 
+					// data of the currently processed layer.
+					// The data will be set in the classes' WMS
+					// object when the layer tag is closed.
+					//
+					$currentLayer = array();
+					
+					$currentLayer["queryable"] = $attributes["queryable"];
+					if ($attributes["hidden"] == "1") {
+						$currentLayer["visible"] = 0;
+					}
+					else {
+						$currentLayer["visible"] = 1;
+					}
+					$currentLayer["format"] = array();
+					$currentLayer["style"] = array();
+					$layer = true;
 				}
 				if ($layer) {
-					 if(strtoupper($element[tag]) == "LAYER" && $element[type] == "close"){
-					 		$layer = false;
-					 }
-					 if ($formatlist) {
-						 if(strtoupper($element[tag]) == "FORMAT"){
-						 	$cnt_format++;
-						 	$this->wmc_layer_format_current[$cnt_layer][$cnt_format] = $element[attributes]["current"];
-						 	$this->wmc_layer_format[$cnt_layer][$cnt_format] = $element[value];
+					if ($tag == "LAYER" && $type == "close") {
+
+						//
+						// After a layer tag is closed,
+						// we have all necessary information to CREATE
+						// a layer object and append it to the WMS object
+						//
+						if ($currentLayer["extension"]["ISOVERVIEWLAYER"] == "1") {
+							array_push($layerlistArray["overview"], $currentLayer);		
+						}
+						else {
+							array_push($layerlistArray["main"], $currentLayer);											
+						}
+				 		$layer = false;
+					}
+					if ($formatlist) {
+						if ($tag == "FORMAT") {
+						 	array_push($currentLayer["format"], array("current" => $attributes["current"], "name" => $value));
+							if ($attributes["current"] == "1") {
+								$currentLayer["formatIndex"] = count($currentLayer["format"]) - 1;
+							}
 						 }
-						 if(strtoupper($element[tag]) == "FORMATLIST" && $element[type] == "close"){
+						 if ($tag == "FORMATLIST" && $type == "close") {
 							 $formatlist = false;
 						 }
 					 }
 					 elseif ($metadataurl) {
-						 if(strtoupper($element[tag]) == "ONLINERESOURCE"){
-						 	$this->wmc_layer_metadataurl[$cnt_layer] = $element[attributes]["xlink:href"];
-						 }
-						 if(strtoupper($element[tag]) == "METADATAURL" && $element[type] == "close"){
-							 $metadataurl = false;
-						 }
+						if ($tag == "ONLINERESOURCE") {
+							$currentLayer["metadataurl"] = $attributes["xlink:href"];
+						}
+						if ($tag == "METADATAURL" && $type == "close") {
+							$metadataurl = false;
+						}
 					 }
 					 elseif ($dataurl) {
-						 if(strtoupper($element[tag]) == "ONLINERESOURCE"){
-						 	$this->wmc_layer_dataurl[$cnt_layer] = $element[attributes]["xlink:href"];
+						if ($tag == "ONLINERESOURCE") {
+						 	$currentLayer["dataurl"] = $attributes["xlink:href"];
+						}
+						 if ($tag == "DATAURL" && $type == "close") {
+							$dataurl = false;
 						 }
-						 if(strtoupper($element[tag]) == "DATAURL" && $element[type] == "close"){
-							 $dataurl = false;
-						 }
 					 }
 					 elseif ($stylelist) {
-						 if(strtoupper($element[tag]) == "STYLE" && $element[type] == "open"){
-						 	$cnt_style++;
-						 	$style = true;
-						 	$this->wmc_layer_style_current[$cnt_layer][$cnt_style] = $element[attributes]["current"];
-						 }
 						 if ($style) {
-							 if(strtoupper($element[tag]) == "STYLE" && $element[type] == "close"){
-							 	$style = false;
-							 }
-							 if(strtoupper($element[tag]) == "SLD" && $element[type] == "open"){
-							 	$sld = true;
-							 }
-							 if ($sld) {
-								 if(strtoupper($element[tag]) == "SLD" && $element[type] == "close"){
-								 	$sld = false;
-								 }
-								 if(strtoupper($element[tag]) == "ONLINERESOURCE"){
-								 	$this->wmc_layer_style_sld_type[$cnt_layer][$cnt_style] = $element[attributes]["xlink:type"];
-								 	$this->wmc_layer_style_sld_url[$cnt_layer][$cnt_style] = $element[attributes]["xlink:href"];
-								 }
-								 if(strtoupper($element[tag]) == "TITLE"){
-								 	$this->wmc_layer_style_sld_title[$cnt_layer][$cnt_style] = mb_utf8_decode(html_entity_decode($element[value]));
-								 }
-							 }
-							 else {
-								 if(strtoupper($element[tag]) == "NAME"){
-								 	$this->wmc_layer_style_name[$cnt_layer][$cnt_style] = mb_utf8_decode(html_entity_decode($element[value]));
-								 }
-								 if(strtoupper($element[tag]) == "TITLE"){
-								 	$this->wmc_layer_style_title[$cnt_layer][$cnt_style] = $element[value];
-								 }
-								 if(strtoupper($element[tag]) == "LEGENDURL" && $element[type] == "open"){
+							$index = count($currentLayer["style"]) - 1;
+							if ($tag == "STYLE" && $type == "close") {
+								$style = false;
+							}
+							if ($tag == "SLD" && $type == "open") {
+								$sld = true;
+							}
+							if ($sld) {
+								if ($tag == "SLD" && $type == "close") {
+									$sld = false;
+								}
+								if ($tag == "ONLINERESOURCE") {
+									$currentLayer["style"][$index]["sld_type"] = $attributes["xlink:type"];
+									$currentLayer["style"][$index]["sld_url"] = $attributes["xlink:href"];
+								}
+								if ($tag == "TITLE") {
+									$currentLayer["style"][$index]["sld_title"] = $value;
+								}
+							}
+							else {
+								if ($tag == "NAME"){
+									$currentLayer["style"][$index]["name"] = $value;
+								}
+								if ($tag == "TITLE") {
+									$currentLayer["style"][$index]["title"] = $value;
+								}
+								if ($legendurl) {
+									if ($tag == "LEGENDURL" && $type == "close") {
+										$legendurl = false;
+									}
+									if ($tag == "ONLINERESOURCE") {
+									 	$currentLayer["style"][$index]["legendurl_type"] = $attributes["xlink:type"];
+									 	$currentLayer["style"][$index]["legendurl"] = $attributes["xlink:href"];
+									}
+								}
+								if ($tag == "LEGENDURL" && $type == "open") {
 								 	$legendurl = true;
-								 	$this->wmc_layer_style_legendurl_width[$cnt_layer][$cnt_style] = $element[attributes]["width"];
-								 	$this->wmc_layer_style_legendurl_height[$cnt_layer][$cnt_style] = $element[attributes]["height"];
-								 	$this->wmc_layer_style_legendurl_format[$cnt_layer][$cnt_style] = $element[attributes]["format"];
-								 }
-								 if ($legendurl) {
-									 if(strtoupper($element[tag]) == "LEGENDURL" && $element[type] == "close"){
-									 	$legendurl = false;
-									 }
-									 if(strtoupper($element[tag]) == "ONLINERESOURCE"){
-									 	$this->wmc_layer_style_legendurl_type[$cnt_layer][$cnt_style] = $element[attributes]["xlink:type"];
-									 	$this->wmc_layer_style_legendurl[$cnt_layer][$cnt_style] = $element[attributes]["xlink:href"];
-									 }
-								 }
-							 }
-						 }
-						 if(strtoupper($element[tag]) == "STYLELIST" && $element[type] == "close"){
-							 $stylelist = false;
-						 }
-					 }
-					 else {
-						 if(strtoupper($element[tag]) == "SERVER" && $element[type] == "open"){
-						 	 $server = true;
-						 	 $this->wmc_wms_service[$cnt_layer] = $element[attributes]["service"];
-						 	 $this->wmc_wms_version[$cnt_layer] = $element[attributes]["version"];
-						 	 $this->wmc_wms_title[$cnt_layer] = mb_utf8_decode(html_entity_decode($element[attributes]["title"]));
-						 }
-						 if ($server) {
-							 if(strtoupper($element[tag]) == "SERVER" && $element[type] == "close"){
-							 	 $server = false;
-							 }
-							 if(strtoupper($element[tag]) == "ONLINERESOURCE"){
-						 		 $this->wmc_wms_serviceURL[$cnt_layer] = $element[attributes]["xlink:href"];
-							 }
-						 }
-						 if(strtoupper($element[tag]) == "NAME"){
-					 		 $this->wmc_layer_name[$cnt_layer] = mb_utf8_decode(html_entity_decode($element[value]));
-						 }
-						 if(strtoupper($element[tag]) == "TITLE"){
-					 		 $this->wmc_layer_title[$cnt_layer] = mb_utf8_decode(html_entity_decode($element[value]));
-						 }
-						 if(strtoupper($element[tag]) == "ABSTRACT"){
-					 		 $this->wmc_layer_abstract[$cnt_layer] = mb_utf8_decode(html_entity_decode($element[value]));
-						 }
-						 if(strtoupper($element[tag]) == "SRS"){
-							 $epsgArray = explode(" ", $element[value]);						 	
-					 		 
-					 		 for ($c = 0 ; $c < count($epsgArray) ; $c ++) {
-						 		 $this->wmc_layer_srs[$cnt_layer][$cnt_epsg] = $epsgArray[$c];
-								 $cnt_epsg++;
-					 		 }
-						 }
-						 if (strtoupper($element[tag]) == "EXTENSION" && $element[type] == "open") {
-						 	$extension = true;
-						 }
-						 if (strtoupper($element[tag]) == "EXTENSION" && $element[type] == "close") {
-						 	$extension = false;
-						 }
-						 if($extension == true && strtoupper(sepNameSpace($element[tag])) == "SCALEHINT"){
-					 		 $this->wmc_layer_minscale[$cnt_layer] = $element[attributes]["min"];
-					 		 $this->wmc_layer_maxscale[$cnt_layer] = $element[attributes]["max"];
-						 }
-						 if($extension == true && strtoupper(sepNameSpace($element[tag])) == "GUISCALEHINT"){
-					 		 $this->wmc_gui_layer_minscale[$cnt_layer] = $element[attributes]["min"];
-					 		 $this->wmc_gui_layer_maxscale[$cnt_layer] = $element[attributes]["max"];
-						 }
-						 if($extension == true && strtoupper(sepNameSpace($element[tag])) == "LAYER_ID"){
-					 		 $this->wmc_layer_id[$cnt_layer] = $element[value];
-						 }
-						 if($extension == true && strtoupper(sepNameSpace($element[tag])) == "WMS_LAYER_ID"){
-					 		 $this->wmc_wms_layer_id[$cnt_layer] = $element[value];
-						 }
-						 if($extension == true && strtoupper(sepNameSpace($element[tag])) == "LAYER_POS"){
-					 		 $this->wmc_layer_pos[$cnt_layer] = $element[value];
-						 }
-						 if($extension == true && strtoupper(sepNameSpace($element[tag])) == "LAYER_PARENT"){
-					 		 $this->wmc_layer_parent[$cnt_layer] = $element[value];
-						 }
-						 if($extension == true && strtoupper(sepNameSpace($element[tag])) == "QUERYLAYER"){
-					 		 $this->wmc_layer_querylayer[$cnt_layer] = $element[value];
-						 }
-						 if($extension == true && strtoupper(sepNameSpace($element[tag])) == "WMS_ID"){
-					 		 $this->wmc_wms_id[$cnt_layer] = $element[value];
-						 }
-						 if($extension == true && strtoupper(sepNameSpace($element[tag])) == "WFSFEATURETYPE"){
-					 		 $this->wmc_layer_wfs_featuretype[$cnt_layer] = $element[value];
-						 }
-						 if(strtoupper($element[tag]) == "METADATAURL" && $element[type] == "open"){
-							 $metadataurl = true;
-						 }
-						 if(strtoupper($element[tag]) == "DATAURL" && $element[type] == "open"){
-							 $dataurl = true;
-						 }
-						 if(strtoupper($element[tag]) == "FORMATLIST" && $element[type] == "open"){
-							 $formatlist = true;
-							 $cnt_format = -1;
-						 }
-						 if(strtoupper($element[tag]) == "STYLELIST" && $element[type] == "open"){
-							 $stylelist = true;
-							 $cnt_style = -1;
-						 }
-					 }
+								 	$currentLayer["style"][$index]["legendurl_width"] = $attributes["width"];
+								 	$currentLayer["style"][$index]["legendurl_height"] = $attributes["height"];
+								 	$currentLayer["style"][$index]["legendurl_format"] = $attributes["format"];
+								}
+							}
+						}
+						if ($tag == "STYLE" && $type == "open") {
+							$style = true;
+							array_push($currentLayer["style"], array("current" => $attributes["current"]));
+							if ($attributes["current"] == "1") {
+								$currentLayer["styleIndex"] = count($currentLayer["style"]) - 1;
+							}
+						}
+						if ($tag == "STYLELIST" && $type == "close") {
+							$stylelist = false;
+						}
+					}
+					else {
+						if ($tag == "SERVER" && $type == "open") {
+							 $server = true;
+							 $currentLayer["service"] = $attributes["service"];
+							 $currentLayer["version"] = $attributes["version"];
+							 $currentLayer["wms_title"] = $attributes["title"];
+						}
+						if ($server) {
+							if ($tag == "SERVER" && $type == "close") {
+						 		$server = false;
+						 	}
+							if ($tag == "ONLINERESOURCE") {
+						 		$currentLayer["url"] = $attributes["xlink:href"];
+							}
+						}
+						if ($tag == "NAME") {
+					 		$currentLayer["name"] = $value;
+						}
+						if ($tag == "TITLE") {
+					 		$currentLayer["title"] = $value;
+						}
+						if ($tag == "ABSTRACT") {
+					 		$currentLayer["abstract"] = $value;
+						}
+						if ($tag == "SRS") {
+							$currentLayer["epsg"] = explode(" ", $value);						 	
+						}
+						if ($tag == "EXTENSION" && $type == "close") {
+							$extension = false;
+						}
+						if ($extension == true){
+							$currentLayer["extension"][$tag] = $value;
+						}
+						if ($tag == "EXTENSION" && $type == "open") {
+							$currentLayer["extension"] = array();
+							$extension = true;
+						}
+						if ($tag == "METADATAURL" && $type == "open") {
+							$metadataurl = true;
+						}
+						if ($tag == "DATAURL" && $type == "open") {
+							$dataurl = true;
+						}
+						if ($tag == "FORMATLIST" && $type == "open") {
+							$formatlist = true;
+						}
+						if ($tag == "STYLELIST" && $type == "open") {
+							$stylelist = true;
+						}
+					}
 				}
 			}
 		}
+		
+		// set WMS data
+		
+		$layerlistCompleteArray = array_merge($layerlistArray["main"], $layerlistArray["overview"]);
+		
+		for ($i = 0; $i < count($layerlistCompleteArray); $i++) {
+			$this->setLayerData($layerlistCompleteArray[$i]);
+		}
+
 		return true;
-	  //return $verbose;
 	}
 
 	/**
-	 * Creates JS code manipulating the map and wms objects, 
-	 * by this displaying the WMC
+	 * Saves the current WMC in the log folder.
 	 * 
-	 * @param string $target the link to the map object, f.e. "parent." or "window.opener."
-	 * @param string $mapObj the name of the map object, f.e. "mapframe1"
-	 * @param string $action "load", "merge" or "append"
-	 * 
-	 * @return string the JS code
+	 * @return string the filename of the WMC document.
 	 */
-	function createJsObjFromWMC($target, $mapObj, $action){
-		
-		/*
-		 * counts how often a layer has been loaded
-		 */
-		if ($this->monitoringIsOn) {
-			$monitor = new Layer_load_count();
-			for ($i = 0; $i < count($this->wmc_layer_id); $i++) {
-				$monitor->increment($this->wmc_layer_id[$i]);
+	private function saveAsFile() {
+		if ($this->saveWmcAsFile) {
+			$filename = "wmc_" . date("Y_m_d_H_i_s") . ".xml";
+			$logfile = "../tmp/" . $filename;
+			
+			if($h = fopen($logfile,"a")){
+				$content = $this->xml;
+				if(!fwrite($h,$content)){
+					$e = new mb_exception("class_wmc.php: failed to write wmc.");
+					return false;
+				}
+				fclose($h);
 			}
+			$e = new mb_notice("class_wmc: saving WMC as file " . $filename . "; You can turn this behaviour off in class_wmc.php");
+			return $filename;
 		}
-		
-		$wmc_string = "";
-		$validActions = array("load", "merge", "append");
-		if (!in_array($action, $validActions)) {
-			$wmc_string .= "alert('invalid action: ".$action."');";			
+		return null;
+	}
+	
+	/**
+	 * Called during WMC parsing; sets the data of a single layer.
+	 * 
+	 * @return 
+	 * @param $currentLayer Array an associative array with layer data
+	 */
+	private function setLayerData ($currentLayer) {
+		$currentMap = $this->mainMap;
+		$currentMapIsOverview = false;
+
+		if ($currentLayer["extension"]["ISOVERVIEWLAYER"] == "1") {
+			$currentMap = $this->overviewMap;
+			$currentMapIsOverview = true;
 		}
-		else {
-			
-			// general extension
-			if (count($this->wmc_general_extension) > 0) {
-				$json = new Mapbender_JSON();
-				$wmc_string .= $target . "restoredWmcExtensionData = " . $json->encode($this->wmc_general_extension) . ";\n"; 
+
+		$wmsArray = $currentMap->getWmsArray();
+
+		//
+		// check if current layer belongs to an existing WMS.
+		// If yes, store the index of this WMS in $wmsIndex.
+		// If not, set the value to null.
+		//
+		$wmsIndex = null;
+
+		// find last WMS with the same online resource
+		for ($i = count($wmsArray) - 1; $i >= 0; $i--) {
+			if (isset($currentLayer["url"]) && 
+				$currentLayer["url"] == $wmsArray[$i]->wms_getmap) {
+					$wmsIndex = $i;
+					break;
 			}
+		}
+
+		// Even if this WMS has been found before it could still
+		// be a duplicate! We would have to create a new WMS and 
+		// not append this layer to that WMS.
+		// For the overview layer we never add a new wms.
+		// check if this layer is an overview layer. If yes, skip this layer.
+		if ($wmsIndex !== null && !$currentMapIsOverview) {
+
+			// check if this WMS has a layer equal to the current layer.
+			// If yes, this is a new WMS. If not, append this layer
+			// to the existing WMS.
+			$matchingWmsLayerArray = $this->wmsArray[$wmsIndex]->objLayer;
 			
-			$wmc_string .= "var index = " . $target . "getMapObjIndexByName('" . $mapObj . "');\n";
-			if ($action == "load") {
-				// delete all previous wms
-				$wmc_string .= "while(" . $target . "mb_mapObj[index].wms.length > 0){" . $target . "mb_mapObjremoveWMS(index,0);}";
-				$wmc_string .= $target . "deleteWmsObject();\n";
+			for ($i = 0; $i < count($matchingWmsLayerArray); $i++) {
+				if ($matchingWmsLayerArray[$i]->layer_name == $currentLayer["name"]) {
+
+					// by re-setting the index to null, a new WMS will be 
+					// added below.
+					$wmsIndex = null;
+					break;
+				}
 			}
-			if ($action == "merge") {
-				$wmc_string .= "var wms_exists = false;\n";				// true if this wms exists in the mapObj
-				$wmc_string .= "var current_wms_index = null;\n";		// if wms_exists: index of the wms in the map obj; else: null
-				$wmc_string .= "var layer_exists = false;\n";			// true if this layer exists in an existing wms of the mapObj
-				$wmc_string .= "var current_layer_index = null;\n";		// if layer_exists: index of the layer of the wms in the mapObj; else: null
-			}
-			$new_wms = "";
-			$cnt_wms = -1;
-			$added_wms = array();
+		}
+
+		// if yes, create a new WMS ...
+		if ($wmsIndex === null) {
+			$wmsIndex = 0;
+			$wms = new wms();
+
+			//
+			// set WMS data
+			//
+			$wms->wms_id = $currentLayer["extension"]["WMS_ID"]; // TO DO: how about WMS without ID?
+			$wms->wms_version = $currentLayer["version"];
+			$wms->wms_title = $currentLayer["wms_title"];
+			$wms->wms_abstract = $currentLayer["abstract"];
+			$wms->wms_getmap = $currentLayer["url"];
+			$wms->wms_getfeatureinfo = $currentLayer["url"]; // TODO : Add correct data
 			
-			// for all layers in wmc, find individual wms...
-			for ($i = 0; $i < count($this->wmc_layer_title); $i++) {
-				$current_wms = $this->wmc_wms_serviceURL[$i];
-				// ...this is something like 'for every wms'
-				if (!in_array($current_wms , $added_wms)) {
-					$layerlist = "";
-					$querylayerlist = "";
-					$srs_array = array();
-		
-					if ($action == "merge") {
-						$wmc_string .= "wms_exists = false;\n";
-						$wmc_string .= "current_wms_index = null;\n";
-						$wmc_string .= "for (var m=0; m < " . $target . "mb_mapObj[index].wms.length; m++) {\n";
-						$wmc_string .= "\tif ('" . $this->wmc_wms_serviceURL[$i] . "' ==  " . $target . "mb_mapObj[index].wms[m].wms_getmap) {\n";
-						$wmc_string .= "\t\twms_exists = true;\n";
-						$wmc_string .= "\t\tcurrent_wms_index = m;\n";
-						$wmc_string .= "\t}\n";
-						$wmc_string .= "}\n";
-						$wmc_string .= "if (!wms_exists) {\n";
-					}				
-					 
-					$mywms = new wms();
-			
-			  		if(!$this->wmc_layer_title[$i] || $this->wmc_layer_title[$i] == ""){
-						echo "alert('Error: no valid capabilities-document !!');\n";
-						die; exit;
-					}
+			$styleIndex = $currentLayer["styleIndex"];
+			$e = new mb_notice("StyleIndex: " . $styleIndex);
+			$wms->wms_getlegendurl = $currentLayer["style"][$styleIndex]["legendurl"];
 
-					for($j=0;$j<count($this->wmc_layer_format[$i]);$j++){
-						if ($this->wmc_layer_format_current[$i][$j] == 1) {
-							$wms_data_format = $this->wmc_layer_format[$i][$j];
-						}
-					}
-					// add wms
-					$wmc_string .= "\t" . $target . "add_wms('".
-						$this->wmc_wms_id[$i]."','".
-						$this->wmc_wms_version[$i] ."','".
-						$this->wmc_wms_title[$i] ."','".
-						$this->wmc_layer_abstract[$i] ."','".
-						$this->wmc_wms_serviceURL[$i] ."','" .
-						$this->wmc_wms_serviceURL[$i] ."','" .
-						$this->wmc_layer_style_legendurl[$i][0] ."','','". 
-						$wms_data_format ."','text/html','application/vnd.ogc.se_xml','". 
-						$this->wmc_bBox_SRS ."','1','100','');\n";
-		
-					$added_wms[count($added_wms)] = $current_wms;
-					$cnt_wms++;
-					$cnt_layers = 0;
-					$cnt_query_layers = 0;
-					if ($action == "merge") {
-						$wmc_string .= "}\n";
-					}
-	
-					// add epsg
-					$wmc_string .= $target . "wms_addSRS('". 
-						$this->wmc_bBox_SRS ."','". 
-						$this->wmc_bBox_minx ."','". 
-						$this->wmc_bBox_miny ."','". 
-						$this->wmc_bBox_maxx ."','". 
-						$this->wmc_bBox_maxy ."','". 
-						"');\n";
+			$wms->wms_filter = ""; // TODO : Add correct data
 
-					// for each layer...
-					for ($ii = 0; $ii < count($this->wmc_layer_title); $ii++) {
-						$layer_wms = $this->wmc_wms_serviceURL[$ii];
-						// ... of this wms
-						if ($current_wms == $layer_wms) {
-							
-							// add format (FIXME: is this working?)
-							$z = count($this->wmc_layer_format[$ii]);
-							for($j=0;$j<$z;$j++){
-								$wmc_string .= $target . "wms_add_data_type_format('map','". $this->wmc_layer_format[$ii][$j] ."');\n";
-							}
-							
-							if ($cnt_layers == 0) {
-								if ($action == "merge") {
-									$wmc_string .= "if (!wms_exists) {\n\t";
-								} 
-								// add parent layer
-								$wmc_string .= $target . "wms_add_layer('','".
-									$this->wmc_wms_layer_id[$i]."','','". 
-									$this->wmc_wms_title[$i] ."','','0','0','0','0','','".
-									$this->wmc_wms_id[$i]."','1','', '1','1','0','0','0','0','');\n";
-								if ($action == "merge") {
-									$wmc_string .= "}\n";
-								} 
-							}
-	
-							$cnt_layers++;
-							
-							if ($action == "merge") {
-								$wmc_string .= "if (wms_exists) {\n";
-								
-								// check if this layer already exists in this wms
-								$wmc_string .= "\tlayer_exists = false;\n";
-								$wmc_string .= "\tcurrent_layer_index = null;\n";
-								$wmc_string .= "\tfor (var m=0; m < " . $target . "mb_mapObj[index].wms[current_wms_index].objLayer.length; m++) {\n";
-								$wmc_string .= "\t\tif ('" . $this->wmc_layer_name[$ii] . "' ==  " . $target . "mb_mapObj[index].wms[current_wms_index].objLayer[m].layer_name) {\n";
-								$wmc_string .= "\t\t\tlayer_exists = true;\n";
-								$wmc_string .= "\t\t\tcurrent_layer_index = m;\n";
-								$wmc_string .= "\t\t}\n";
-								$wmc_string .= "\t}\n"; 
+			$formatIndex = $currentLayer["formatIndex"];
+			$wms->gui_wms_mapformat = $currentLayer["format"][$formatIndex]["name"];
 
-								if ($this->wmc_layer_querylayer[$ii]!="") {
-									$querylayer_yn = $this->wmc_layer_querylayer[$ii];
-								}
-								else {
-									$querylayer_yn = $this->wmc_layer_queryable[$ii];
-								}			
-								$wmc_string .= "\tif (layer_exists) {\n";
-								// check if the visibility or the queryability are different to the existing layer
-								$wmc_string .= "\t\tif (" . $target . "mb_mapObj[index].wms[current_wms_index].objLayer[current_layer_index].gui_layer_visible != '" . intval(!$this->wmc_layer_hidden[$ii]) . "'";
-								$wmc_string .= " || " . $target . "mb_mapObj[index].wms[current_wms_index].objLayer[current_layer_index].gui_layer_querylayer != '" . $querylayer_yn . "') {\n";
-		
-								// if yes, update the visibility and queryability
-								$wmc_string .= "\t\t\t" . $target . "mb_mapObj[index].wms[current_wms_index].objLayer[current_layer_index].gui_layer_visible = " . intval(!$this->wmc_layer_hidden[$ii]) . ";\n"; 
-								$wmc_string .= "\t\t\t" . $target . "mb_mapObj[index].wms[current_wms_index].objLayer[current_layer_index].gui_layer_querylayer = " . $querylayer_yn . ";\n"; 
-								$wmc_string .= "\t\t}\n";
-								$wmc_string .= "\t}\n"; 
-								$wmc_string .= "}\n"; 
-								$wmc_string .= "\telse {\n";
-							} 
+			$wms->gui_wms_featureinfoformat = "text/html"; // TODO : Add correct data
+			$wms->gui_wms_exceptionformat = "application/vnd.ogc.se_xml"; // TODO : Add correct data
+			$wms->gui_wms_epsg = $this->mainMap->getEpsg();
+			$wms->gui_wms_visible = 1; // TODO : Add correct data
+			$wms->gui_wms_opacity = 100; // TODO : Add correct data
+			$wms->gui_wms_sldurl = $currentLayer["style"][$styleIndex]["sld_url"];
 
-							// add layer
-							$wmc_string .= "\t" . $target . "wms_add_layer('".
-								($this->wmc_layer_parent[$ii]!=""?$this->wmc_layer_parent[$ii]:"0") . "','". 
-								$this->wmc_layer_id[$ii] . "','". 
-								$this->wmc_layer_name[$ii] . "','". 
-								$this->wmc_layer_title[$ii] ."','". 
-								$this->wmc_layer_dataurl[$ii] . "','". 
-								($this->wmc_layer_pos[$ii]!=""?$this->wmc_layer_pos[$ii]:intval($cnt_layers)) ."','". 
-								$this->wmc_layer_queryable[$ii] ."','".
-								$this->wmc_layer_minscale[$ii]  ."','". 
-								$this->wmc_layer_maxscale[$ii]  ."','". 
-								$this->wmc_layer_metadataurl[$ii] ."','". 
-								$this->wmc_wms_id[$i] ."','1','', '1','". 
-								intval(!$this->wmc_layer_hidden[$ii]) ."','". 
-								$this->wmc_layer_queryable[$ii] ."','". 
-								($this->wmc_layer_querylayer[$ii]!=""?$this->wmc_layer_querylayer[$ii]:$this->wmc_layer_queryable[$ii]) ."','".
-								($this->wmc_gui_layer_minscale[$ii]!=""?$this->wmc_gui_layer_minscale[$ii]:$this->wmc_layer_minscale[$ii]) ."','".
-								($this->wmc_gui_layer_maxscale[$ii]!=""?$this->wmc_gui_layer_maxscale[$ii]:$this->wmc_layer_maxscale[$ii]) ."','".
-								$this->wmc_layer_wfs_featuretype[$ii] . "');\n";
-	
-							if ($action == "merge") {
-								$wmc_string .= "\t}\n";
-							} 
-												
-							// if layer is queryable, add it to querylayerlist
-							if (($this->wmc_layer_querylayer[$ii]!=""?$this->wmc_layer_querylayer[$ii]:$this->wmc_layer_queryable[$ii])) {
-								$cnt_query_layers++;
-								if (!in_array($this->wmc_layer_name[$ii], explode(",",$querylayerlist))) {
-									if ($querylayerlist == "") {$querylayerlist = $this->wmc_layer_name[$ii];} else {$querylayerlist .= "," . $this->wmc_layer_name[$ii];} 
-								}
-							}
-							// if layer is visible, add it to layerlist 
-							if (intval(!$this->wmc_layer_hidden[$ii]) && !in_array($this->wmc_layer_name[$ii], explode(",",$layerlist))) {
-								if ($layerlist == "") {$layerlist = $this->wmc_layer_name[$ii];} else {$layerlist .= "," . $this->wmc_layer_name[$ii];}
-							}
-	
-							// add layer style (FIXME: is this working?)
-							for($j=0; $j<count($this->wmc_layer_style_name[$ii]);$j++){
-								$wmc_string .= $target . "wms_addLayerStyle('".$this->wmc_layer_style_name[$ii][$j] ."','".$this->wmc_layer_style_title[$ii][$j] ."','".$j."','".$cnt_layers."', '" . $this->wmc_layer_style_legendurl[$ii][$j] . "', '" . $this->wmc_layer_style_legendurl_format[$ii][$j] . "');\n";
-							}
-						}
-					}
-					// add wms to mapObj with all layers and querylayers
-					if ($action == "merge") {
-						$wmc_string .= "if (!wms_exists) {\n";
-					} 
-					$wmc_string .= $target. "mb_mapObjaddWMSwithLayers('" . $mapObj . "', '" . $layerlist . "', '" . $querylayerlist . "');\n";
-					if ($action == "merge") {
-						$wmc_string .= "}\n";
-						$wmc_string .= "else {\n";
-						$wmc_string .= $target. "mb_mapObj[index].layers[current_wms_index] = \"" . $layerlist . "\";\n";
-						$wmc_string .= $target. "mb_mapObj[index].querylayers[current_wms_index] = \"" . $querylayerlist . "\";\n";
-						$wmc_string .= "}\n";
-					}
-				}
+			$wms->gui_epsg = $currentLayer["epsg"];
+			//
+			// set data formats
+			//
+			for ($i = 0; $i < count($currentLayer["format"]); $i++) {
+				array_push($wms->data_type, "map");							
+				array_push($wms->data_format, $currentLayer["format"][$i]["name"]);							
 			}
-			$wmc_string .= "var old_mapObj = ".$target."cloneObject(".$target."mb_mapObj);\n";
-			$wmc_string .= $target . "deleteMapObj();\n";
-			$wmc_string .= "for (var i=0; i<old_mapObj.length; i++) {\n";
-			$wmc_string .= "\tif (old_mapObj[i].frameName != 'overview') {\n";
-			$wmc_string .= "\t\t" . $target . "mb_registerMapObj(old_mapObj[i].frameName, old_mapObj[i].elementName, null, " . $this->wmc_windowWidth . ", " . $this->wmc_windowHeight . ");\n"; 
-			$wmc_string .= "\t\t" . $target . "document.getElementById(old_mapObj[i].frameName).style.width = " . $this->wmc_windowWidth . ";\n";
-			$wmc_string .= "\t\t" . $target . "document.getElementById(old_mapObj[i].frameName).style.height = " . $this->wmc_windowHeight . ";\n";
-			$wmc_string .= "\t}\n";
-			$wmc_string .= "\telse {\n";
-			$wmc_string .= "\t\tvar found = false;\n";
-			$wmc_string .= "\t\tfor (var j=0; j < " . $target . "wms.length && found == false; j++) {\n";
-			$wmc_string .= "\t\t\tif (" . $target . "wms[j].wms_getmap == old_mapObj[i].wms[0].wms_getmap) {\n";
-			$wmc_string .= "\t\t\t\t" . $target . "mb_registerMapObj('overview', old_mapObj[i].elementName, j, old_mapObj[i].width,  old_mapObj[i].width);\n"; 
-			$wmc_string .= "\t\t\t\tfound = true;\n"; 
-			$wmc_string .= "\t\t\t}\n";
-			$wmc_string .= "\t\t}\n";
-			$wmc_string .= "\t\tif (!found) {\n";
-			$wmc_string .= "\t\t\t" . $target . "mb_registerMapObj('overview', old_mapObj[i].elementName, 0, old_mapObj[i].width,  old_mapObj[i].width);\n"; 
-			$wmc_string .= "\t\t}\n";
-			$wmc_string .= "\t}\n";
-			$wmc_string .= "}\n";
-			
-			$ov_bbox = array();
-			// compute the union of the overview and the mapframe bbox for the new overview bbox
-			if ($this->wmc_general_extension["ov_minx"] && $this->wmc_general_extension["ov_miny"] && 
-				$this->wmc_general_extension["ov_maxx"] && $this->wmc_general_extension["ov_maxy"]) {
 
-				// box for overview
-				$ov_min = new Mapbender_point($this->wmc_general_extension["ov_minx"], $this->wmc_general_extension["ov_miny"], $this->wmc_bBox_SRS);
-				$ov_max = new Mapbender_point($this->wmc_general_extension["ov_maxx"], $this->wmc_general_extension["ov_maxy"], $this->wmc_bBox_SRS);
-				$ov_box = new Mapbender_bbox($ov_min, $ov_max, $this->wmc_bBox_SRS);
-				
-				// box for mapframe
-				$mf_min = new Mapbender_point($this->wmc_bBox_minx, $this->wmc_bBox_miny, $this->wmc_bBox_SRS);
-				$mf_max = new Mapbender_point($this->wmc_bBox_maxx, $this->wmc_bBox_maxy, $this->wmc_bBox_SRS);
-				$mf_box = new Mapbender_bbox($mf_min, $mf_max, $this->wmc_bBox_SRS);
-				
-				$unionBox = Mapbender_bbox::union(array($ov_box, $mf_box));
-				
-				array_push($ov_bbox, $unionBox->min->x); 				
-				array_push($ov_bbox, $unionBox->min->y); 				
-				array_push($ov_bbox, $unionBox->max->x); 				
-				array_push($ov_bbox, $unionBox->max->y); 				
-			}
-			else {
-/*
-				$sql = "SELECT minx, miny, maxx, maxy FROM layer_epsg WHERE fkey_layer_id = $1 AND epsg = $2 LIMIT 1";
-				$v = array($this->wmc_layer_id[0], $this->wmc_bBox_SRS);
-				$t = array('i', 's');
-				$res = db_prep_query($sql, $v, $t);
-				$row = db_fetch_array($res);
-				if ($row["minx"] && $row["miny"] && $row["maxx"] && $row["maxy"]) {
-					$ov_bbox = array($row["minx"],$row["miny"],$row["maxx"],$row["maxy"]);
-				}
-				else if ($this->wmc_layer_id[0] && $this->wmc_bBox_SRS){
-					$ov_bbox = array($this->wmc_bBox_minx, $this->wmc_bBox_miny, $this->wmc_bBox_maxx, $this->wmc_bBox_maxy);
-				}
-				else {
-*/
-					$ov_bbox = array(2412139.175257732, 5365000, 2767860.824742268, 5700000);		
-//				}
-			}
+			// set root layer
+			$wms->addLayer(0, "");
+			$wms->objLayer[0]->layer_uid = $currentLayer["extension"]["WMS_LAYER_ID"];
+			$wms->objLayer[0]->layer_name = $currentLayer["extension"]["WMS_NAME"];
+			$wms->objLayer[0]->layer_title = $currentLayer["wms_title"];
+			$wms->objLayer[0]->layer_pos = 0;
+			$wms->objLayer[0]->layer_queryable = 0;
+			$wms->objLayer[0]->layer_minscale = 0;
+			$wms->objLayer[0]->layer_maxscale = 0;
+			$wms->objLayer[0]->gui_layer_wms_id = $currentLayer["extension"]["WMS_LAYER_ID"];
+			$wms->objLayer[0]->gui_layer_status = 1;
+			$wms->objLayer[0]->gui_layer_selectable = 1; 
+			$wms->objLayer[0]->gui_layer_visible = 1;
+			$wms->objLayer[0]->gui_layer_queryable = 0;
+			$wms->objLayer[0]->gui_layer_querylayer = 0;
+			$wms->objLayer[0]->gui_layer_minscale = 0;
+			$wms->objLayer[0]->gui_layer_maxscale = 0;
+
+			// add WMS
+			array_push($wmsArray, $wms);
+
+			// the index of the WMS we just added
+			$wmsIndex = count($wmsArray) - 1;
+		}
+
+		// add layer to existing WMS ...
+		$currentWms = $wmsArray[$wmsIndex];
+		$currentWms->newLayer($currentLayer, null);
+		$currentMap->setWmsArray($wmsArray);
+		return true;
+	}
+	
+	/**
+	 * Called during WMC parsing; sets the maps within a WMC.
+	 * 
+	 * @return 
+	 */
+	private function setMapData () {
+		if ($this->generalExtensionArray["OV_WIDTH"] && 
+			$this->generalExtensionArray["OV_HEIGHT"] &&
+			$this->generalExtensionArray["OV_FRAMENAME"] &&
+			$this->generalExtensionArray["OV_MINX"] &&
+			$this->generalExtensionArray["OV_MINY"] &&
+			$this->generalExtensionArray["OV_MAXX"] &&
+			$this->generalExtensionArray["OV_MAXY"] &&
+			$this->generalExtensionArray["OV_SRS"]) {
 			
-			$wmc_string .= "for (var i=0; i<old_mapObj.length; i++) {\n";
-			$wmc_string .= "\tif (old_mapObj[i].frameName != 'overview') {\n";
-			$wmc_string .= "\t\t".$target."mb_calculateExtent(old_mapObj[i].frameName, ";
-			$wmc_string .= $this->wmc_bBox_minx .",".$this->wmc_bBox_miny .",";
-			$wmc_string .= $this->wmc_bBox_maxx .",".$this->wmc_bBox_maxy.");\n";
-			$wmc_string .= "\t}\n";
-			$wmc_string .= "\telse {\n";
-			$wmc_string .= "\t\t".$target."mb_calculateExtent(old_mapObj[i].frameName, ";
-			$wmc_string .= $ov_bbox[0] .",".$ov_bbox[1] .",";
-			$wmc_string .= $ov_bbox[2] .",".$ov_bbox[3] .");\n";
-			$wmc_string .= "\t}\n";
-			$wmc_string .= "\t". $target . "setMapRequest(old_mapObj[i].frameName);\n";
-			$wmc_string .= "}\n";
-			$wmc_string .= $target . "mb_execloadWmsSubFunctions();\n";
+			$this->overviewMap = new Map();
+			$this->overviewMap->setWidth($this->generalExtensionArray["OV_WIDTH"]);
+			$this->overviewMap->setHeight($this->generalExtensionArray["OV_HEIGHT"]);
+			$this->overviewMap->setFrameName($this->generalExtensionArray["OV_FRAMENAME"]);
+			$this->overviewMap->setIsOverview(true);
+
+			$bbox = new Mapbender_bbox($this->generalExtensionArray["OV_MINX"], $this->generalExtensionArray["OV_MINY"], $this->generalExtensionArray["OV_MAXX"], $this->generalExtensionArray["OV_MAXY"], $this->generalExtensionArray["OV_SRS"]);
+			$this->overviewMap->setExtent($bbox);
 		}
-//		$e = new mb_exception("js code: " . $wmc_string);
-		return $wmc_string;
+		if ($this->generalExtensionArray["MAIN_FRAMENAME"]) {
+			$this->mainMap->setFrameName($this->generalExtensionArray["MAIN_FRAMENAME"]);
+		}
+		else {
+			$this->mainMap->setFrameName("mapframe1");
+		}
+		return true;
 	}
+
+	/**
+	 * Creates a WMC document (XML) from the current object
+	 * 
+	 * @return String XML
+	 */
+	private function createXml() {
+		$wmcToXml = new WmcToXml($this);
+		$this->xml = $wmcToXml->getXml();
+	}
+
+	
 } 
-// end class
-?>
+
+/**
+ * @deprecated
+ */
+function mb_utf8_encode ($str) {
+	if(CHARSET=="UTF-8") return utf8_encode($str);
+	return $str;
+}
+
+/**
+ * @deprecated
+ */
+function mb_utf8_decode ($str) {
+	if(CHARSET=="UTF-8") return utf8_decode($str);
+	return $str;
+}
+?>
\ No newline at end of file

Added: trunk/mapbender/http/classes/class_wmcToXml.php
===================================================================
--- trunk/mapbender/http/classes/class_wmcToXml.php	                        (rev 0)
+++ trunk/mapbender/http/classes/class_wmcToXml.php	2008-06-13 13:03:41 UTC (rev 2502)
@@ -0,0 +1,536 @@
+<?php
+# $Id: class_wmc.php 2466 2008-05-20 08:55:03Z christoph $
+# http://www.mapbender.org/index.php/class_wmc.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.
+
+/**
+ * The XML representation of a WMC object.
+ * 
+ * Usage:
+ * 
+ * $wmcToXml = new WmcToXml($wmc);
+ * $xml = $wmcToXml->getXml();
+ * 
+ */
+class WmcToXml {
+
+	private $wmc = null;
+	private $doc;
+	private $xml = "";
+	
+	/**
+	 * Constructor. Computes the XML of the WMC object given as parameter.
+	 * 
+	 * @param $someWmc wmc
+	 */
+	public function __construct ($someWmc) {
+		$this->wmc = $someWmc;
+		$this->toXml();
+	} 
+
+	// ---------------------------------------------------------------------
+	// public functions
+	// ---------------------------------------------------------------------
+	
+	public function getXml () {
+		return $this->xml;
+	}
+	
+	// ---------------------------------------------------------------------
+	// private functions
+	// ---------------------------------------------------------------------
+	
+	private function toXml () {
+
+		// add main map and overview (if exists) to $mapArray
+		$mapArray = array($this->wmc->mainMap);
+		if ($this->wmc->overviewMap !== null) {
+			array_push($mapArray, $this->wmc->overviewMap);
+		}
+
+		// generate XML
+		$this->doc = new DOMDocument("1.0", CHARSET);
+		$this->doc->preserveWhiteSpace = false;
+		
+		// ViewContext
+		$e_view_context = $this->doc->createElementNS("http://www.opengis.net/context", "ViewContext");
+		$e_view_context->setAttribute("version", "1.1.0");
+		$e_view_context->setAttribute("id", $this->wmc->wmc_id);
+		$e_view_context->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
+		$e_view_context->setAttribute("xmlns:" . $this->wmc->extensionNamespace, $this->wmc->extensionNamespaceUrl);
+		$e_view_context->setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
+		$e_view_context->setAttribute("xsi:SchemaLocation", "http://schemas.opengis.net/context/1.0.0/context.xsd");
+		
+		// General
+		$e_general = $this->createGeneralNode();	
+		if ($e_general !== null) {
+			$e_view_context->appendChild($e_general);
+		}	
+		
+		// Layerlist
+		$e_layer_list = $this->doc->createElement("LayerList");
+		while (count($mapArray) > 0) {
+			
+			$currentMap = array_pop($mapArray);
+			$currentWmsArray = $currentMap->getWmsArray();
+	
+			for ($i = 0; $i < count($currentWmsArray); $i++) {
+				$currentWms = $currentWmsArray[$i];
+				
+				for ($j = 0; $j < count($currentWms->objLayer); $j++) {
+					$currentLayer = $currentWms->objLayer[$j];
+					
+					$layerNode = $this->createLayerNode($currentMap, $currentWms, $currentLayer);
+					if ($layerNode !== null) {
+						$e_layer_list->appendChild($layerNode);
+					}
+				}
+			}
+		}
+		$e_view_context->appendChild($e_layer_list);
+
+		$this->doc->appendChild($e_view_context);
+		$this->xml = $this->doc->saveXML();
+	}
+
+	private function createGeneralNode () {
+		if ($this->wmc->overviewMap !== null) {
+			$ovExtent = $this->wmc->overviewMap->getExtent();
+			$extensionData->ov_minx = $ovExtent->min->x;
+			$extensionData->ov_miny = $ovExtent->min->y;
+			$extensionData->ov_maxx = $ovExtent->max->x;
+			$extensionData->ov_maxy = $ovExtent->max->y;
+			$extensionData->ov_srs = $ovExtent->epsg;
+			$extensionData->ov_framename = $this->wmc->overviewMap->getFrameName();
+			$extensionData->ov_width = $this->wmc->overviewMap->getWidth();
+			$extensionData->ov_height = $this->wmc->overviewMap->getHeight();
+		}
+				
+		// General
+		$e_general = $this->doc->createElement("General");
+		
+		// Window
+		$e_window = $this->doc->createElement("Window");
+		if ($this->wmc->mainMap->getWidth() && $this->wmc->mainMap->getHeight()) {
+			$e_window->setAttribute("width", $this->wmc->mainMap->getWidth());
+			$e_window->setAttribute("height", $this->wmc->mainMap->getHeight());
+		}
+		$e_general->appendChild($e_window);
+		
+		// BoundingBox
+		$mainExtent = $this->wmc->mainMap->getExtent();
+		$e_bbox = $this->doc->createElement("BoundingBox");
+		$e_bbox->setAttribute("SRS", $mainExtent->epsg);
+		$e_bbox->setAttribute("minx", $mainExtent->min->x);
+		$e_bbox->setAttribute("miny", $mainExtent->min->y);
+		$e_bbox->setAttribute("maxx", $mainExtent->max->x);
+		$e_bbox->setAttribute("maxy", $mainExtent->max->y);
+		$e_general->appendChild($e_bbox);
+				
+		// Name
+		$e_name = $this->doc->createElement("Name", $this->wmc->wmc_name);
+		$e_general->appendChild($e_name);
+				
+		// Title
+		$e_title = $this->doc->createElement("Title", $this->wmc->wmc_title);
+		$e_general->appendChild($e_title);
+				
+		// Keywords
+		$e_keyword_list = $this->doc->createElement("KeywordList");
+		for ($i=0; $i < count($this->wmc->wmc_keyword); $i++) {
+			$e_keyword = $this->doc->createElement("Keyword", $this->wmc->wmc_keyword[$i]);
+			$e_keyword_list->appendChild($e_keyword);
+		}
+		$e_general->appendChild($e_keyword_list);
+				
+		// Abstract
+		if ($this->wmc->wmc_abstract) {
+			$e_abstract = $this->doc->createElement("Abstract", $this->wmc->wmc_abstract);
+			$e_general->appendChild($e_abstract);
+		}
+	
+		// Logo URL			
+		if ($this->wmc->wmc_logourl_width && $this->wmc->wmc_logourl_height && 
+			$this->wmc->wmc_logourl_format && $this->wmc->wmc_logourl){
+			
+			$e_logo_url = $this->doc->createElement("LogoURL");
+			$e_logo_url->setAttribute("width", $this->wmc->wmc_logourl_width);
+			$e_logo_url->setAttribute("height", $this->wmc->wmc_logourl_height);
+			$e_logo_url->setAttribute("format", $this->wmc->wmc_logourl_format);
+		
+			$e_logo_url_or = $this->doc->createElement("OnlineResource");
+			$e_logo_url_or->setAttributeNS("http://www.opengis.net/context", "xmlns:xlink", "http://www.w3.org/1999/xlink");
+			$e_logo_url_or->setAttribute("xlink:type", "simple");
+			$e_logo_url_or->setAttribute("xlink:href", $this->wmc->wmc_logourl);
+			$e_logo_url->appendChild($e_logo_url_or);
+
+			$e_general->appendChild($e_logo_url);
+		}
+				
+		// Description URL	
+		if ($this->wmc->wmc_descriptionurl){
+			$e_description_url = $this->doc->createElement("DescriptionURL");
+
+			$e_description_url_or = $this->doc->createElement("OnlineResource");
+			$e_description_url_or->setAttributeNS("http://www.opengis.net/context", "xmlns:xlink", "http://www.w3.org/1999/xlink");
+			$e_description_url_or->setAttribute("xlink:type", "simple");
+			$e_description_url_or->setAttribute("xlink:href", $this->wmc->wmc_descriptionurl);
+			$e_description_url->appendChild($e_description_url_or);
+
+			$e_general->appendChild($e_description_url);
+		}
+				
+		// Contact information
+		$e_contact = $this->createContactInformationNode();
+		if ($e_contact !== null) {
+			$e_general->appendChild($e_contact);
+		}
+
+		// Extension data				
+		if (count($extensionData) > 0) {
+			$e_extensionGeneral = $this->doc->createElement("Extension");
+			
+			foreach ($extensionData as $keyExtensionData => $valueExtensionData) {
+				$e_currentExtensionTag = $this->doc->createElement($this->wmc->extensionNamespace.":".$keyExtensionData, $valueExtensionData);
+				$e_extensionGeneral->appendChild($e_currentExtensionTag);
+			}
+			$e_general->appendChild($e_extensionGeneral);
+		}
+		return $e_general;
+	}
+	
+	private function createLayerNode ($currentMap, $currentWms, $currentLayer) {
+		if ($currentLayer->layer_parent != '') {
+
+			// Layer
+			$e_layer = $this->doc->createElement("Layer");
+			$e_layer->setAttribute("queryable", $currentLayer->layer_queryable);
+			$e_layer->setAttribute("hidden", ($currentLayer->gui_layer_visible ? 0 : 1));
+
+			// Server
+			$e_service = $this->doc->createElement("Server");
+			$e_service->setAttribute("service", "OGC:WMS");
+			$e_service->setAttribute("version", $currentWms->wms_version);
+			$e_service->setAttribute("title", $currentWms->wms_title);
+
+			// Online resource
+			$e_service_or = $this->doc->createElement("OnlineResource");
+			$e_service_or->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
+			$e_service_or->setAttribute("xlink:type", "simple");
+			$e_service_or->setAttribute("xlink:href", $currentWms->wms_getmap);
+			$e_service->appendChild($e_service_or);
+			$e_layer->appendChild($e_service);
+
+			// Name
+			$e_layer_name = $this->doc->createElement("Name", $currentLayer->layer_name);
+			$e_layer->appendChild($e_layer_name);
+
+			// Title
+			$e_layer_title = $this->doc->createElement("Title", $currentLayer->layer_title);
+			$e_layer->appendChild($e_layer_title);
+
+			// Abstract
+			if ($currentWms->wms_abstract){
+				$e_layer_abstract = $this->doc->createElement("Abstract", $currentWms->wms_abstract);
+				$e_layer->appendChild($e_layer_abstract);
+			}
+
+			// SRS		
+			$srsNode = $this->createSrsNode($currentMap, $currentWms);
+			if ($srsNode !== null) {
+				$e_layer->appendChild($srsNode);
+			}
+	
+			// Data URL
+			if ($currentLayer->layer_dataurl_href){
+				$e_layer_data_url = $this->doc->createElement("DataURL");
+	
+				$e_layer_data_url_or = $this->doc->createElement("OnlineResource");
+				$e_layer_data_url_or->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
+				$e_layer_data_url_or->setAttribute("xlink:type", "simple");
+				$e_layer_data_url_or->setAttribute("xlink:href", $currentLayer->layer_dataurl_href);
+				
+				$e_layer_data_url->appendChild($e_layer_data_url_or);
+				$e_layer->appendChild($e_layer_data_url);
+			}
+
+			// Metadata URL
+			if ($currentLayer->layer_metadataurl){
+				$e_layer_metadata_url = $this->doc->createElement("MetadataURL");
+
+				// Metadata URL online resource
+				$e_layer_metadata_url_or = $this->doc->createElement("OnlineResource");
+				$e_layer_metadata_url_or->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
+				$e_layer_metadata_url_or->setAttribute("xlink:type", "simple");
+				$e_layer_metadata_url_or->setAttribute("xlink:href", $currentLayer->layer_metadataurl);
+				$e_layer_metadata_url->appendChild($e_layer_metadata_url_or);
+				$e_layer->appendChild($e_layer_metadata_url);
+			}
+
+			// Layer format
+			$formatListNode = $this->createLayerFormatListNode($currentWms);
+			if ($formatListNode !== null) {
+				$e_layer->appendChild($formatListNode);
+			}
+
+			// Layer style
+			$layerStyleListNode = $this->createLayerStyleNode($currentWms, $currentLayer);
+			if ($layerStyleListNode !== null) {
+				$e_layer->appendChild($layerStyleListNode);
+			}
+
+			// Extension
+			$extensionNode = $this->createLayerExtensionNode($currentMap, $currentWms, $currentLayer);
+			if ($extensionNode !== null) {
+				$e_layer->appendChild($extensionNode);
+			}
+
+			return $e_layer;
+		}
+		return null;	
+	}
+
+	private function createSrsNode ($currentMap, $currentWms) {
+		$wms_epsg = array();
+		$wms_epsg[0] = $currentMap->getEpsg();
+	
+		if ($currentWms->gui_wms_epsg != $currentMap->getEpsg()) {
+			$wms_epsg[1] = $currentWms->gui_wms_epsg;
+		}
+
+		for ($j = 0; $j < count($currentWms->gui_epsg); $j++) {
+			if (!in_array($currentWms->gui_epsg[$j], $wms_epsg)){
+				array_push($wms_epsg, $currentWms->gui_epsg[$j]);
+			}
+		}
+
+		$e_layer_srs = $this->doc->createElement("SRS", implode(" ", $wms_epsg));
+		return $e_layer_srs;
+	}
+
+	private function createLayerFormatListNode ($currentWms) {
+		$e_layer_format = $this->doc->createElement("FormatList");
+
+		$data_format_current = false;
+		
+		for ($k = 0; $k < count($currentWms->data_format); $k++){
+
+			if ($currentWms->data_type[$k] == "map") {
+				$layerFormat = $currentWms->data_format[$k];
+
+				$e_format = $this->doc->createElement("Format", $layerFormat);
+
+				if ($data_format_current === false && ( 
+						$currentWms->data_format[$k] == $currentWms->gui_wms_mapformat ||
+						$k == (count($currentWms->data_format)-1)
+				)){
+
+					$e_format->setAttribute("current", "1");
+					$data_format_current = true;
+				}
+				$e_layer_format->appendChild($e_format);
+			}
+		}
+		return $e_layer_format;
+	}
+
+	private function createLayerExtensionNode ($currentMap, $currentWms, $currentLayer) {
+		$layerExtensionData = array();
+		$layerExtensionData["wms_name"] = $currentWms->objLayer[0]->layer_name;
+		$layerExtensionData["minscale"] = $currentLayer->layer_minscale;
+		$layerExtensionData["maxscale"] = $currentLayer->layer_maxscale;
+		$layerExtensionData["gui_minscale"] = $currentLayer->gui_layer_minscale;
+		$layerExtensionData["gui_maxscale"] = $currentLayer->gui_layer_maxscale;
+		$layerExtensionData["layer_id"] = $currentLayer->layer_uid;
+		$layerExtensionData["wms_layer_id"] = $currentWms->objLayer[0]->layer_uid;
+		$layerExtensionData["layer_pos"] = $currentLayer->layer_pos;
+		$layerExtensionData["layer_parent"] = $currentLayer->layer_parent;
+		$layerExtensionData["wms_id"] = $currentWms->wms_id;
+		$layerExtensionData["querylayer"] = $currentLayer->gui_layer_querylayer;
+		$layerExtensionData["gui_selectable"] = $currentLayer->gui_layer_selectable;
+		$layerExtensionData["gui_queryable"] = $currentLayer->gui_layer_queryable;
+		$layerExtensionData["gui_status"] = $currentLayer->gui_layer_status;
+		if ($currentMap->isOverview()) {
+			$layerExtensionData["isOverviewLayer"] = 1;
+		}
+		if ($currentLayer->gui_layer_wfs_featuretype) {
+			$layerExtensionData["wfsFeatureType"] = $currentLayer->gui_layer_wfs_featuretype;
+		}
+
+		if (count($layerExtensionData) > 0) {
+			$e_extension = $this->doc->createElement("Extension");
+			
+			foreach ($layerExtensionData as $keyExtensionData => $valueExtensionData) {
+				$e_currentExtensionTag = $this->doc->createElement($this->wmc->extensionNamespace.":".$keyExtensionData, $valueExtensionData);
+				$e_extension->appendChild($e_currentExtensionTag);
+			}
+			return $e_extension;
+		}
+		return null;
+	}
+	
+	private function createLayerStyleNode ($currentWms, $currentLayer) {
+		$e_layer_stylelist = $this->doc->createElement("StyleList");
+		
+		for ($k = 0; $k < count($currentLayer->layer_style); $k++) {
+
+			$currentStyle = $currentLayer->layer_style[$k];
+			
+			$layerStyle_current = 0;
+			if ($k === 0){
+				$layerStyle_current = 1; // To do: insert proper data
+			}
+
+			$e_layer_style = $this->doc->createElement("Style");
+
+			$layerStyleSLD = "";
+
+			if ($layerStyleSLD) {
+				$e_layer_style_or = $this->doc->createElement("OnlineResource");
+				$e_layer_style_or->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
+				$e_layer_style_or->setAttribute("xlink:type", "simple");
+				$e_layer_style_or->setAttribute("xlink:href", $currentWms->gui_wms_sldurl);
+				$e_layer_style->appendChild($e_layer_style_or);
+			}
+			else{
+				
+				if ($layerStyle_current == 1){
+					$e_layer_style->setAttribute("current", "1");
+				}
+
+				$e_layer_style_name = $this->doc->createElement("Name", $currentStyle->name);
+				$e_layer_style->appendChild($e_layer_style_name);
+
+				$e_layer_style_title = $this->doc->createElement("Title", $currentStyle->title);
+				$e_layer_style->appendChild($e_layer_style_title);
+				
+				
+				$e_layer_style_legendurl = $this->doc->createElement("LegendUrl");
+
+				//TODO: determine correct layer style entries
+				$layerStyle_legendUrl_width = ""; // To Do: add proper data
+				$layerStyle_legendUrl_height = ""; // To Do: add proper data
+				$layerStyle_legendUrl_format = ""; // To Do: add proper data
+				$e_layer_style_legendurl->setAttribute("width", $layerStyle_legendUrl_width);
+				$e_layer_style_legendurl->setAttribute("height", $layerStyle_legendUrl_height);
+				$e_layer_style_legendurl->setAttribute("format", $layerStyle_legendUrl_format);
+
+				$e_layer_style_legendurl_or = $this->doc->createElement("OnlineResource");
+				$e_layer_style_legendurl_or->setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
+				$e_layer_style_legendurl_or->setAttribute("xlink:type", "simple");
+				$e_layer_style_legendurl_or->setAttribute("xlink:href", $currentWms->wms_getlegendurl);
+				$e_layer_style_legendurl->appendChild($e_layer_style_legendurl_or);
+				$e_layer_style->appendChild($e_layer_style_legendurl);
+			}
+			$e_layer_stylelist->appendChild($e_layer_style);
+		}	
+		return $e_layer_stylelist;
+	}
+	
+	/**
+	 * 
+	 * @return 
+	 */
+	private function createContactInformationNode () {
+		
+		if ($this->wmc->wmc_contactemail || $this->wmc->wmc_contactorganization ||
+			$this->wmc->wmc_contactperson || $this->wmc->wmc_contactposition || 
+			$this->wmc->wmc_contactaddresstype || $this->wmc->wmc_contactaddress || 
+			$this->wmc->wmc_contactcity || $this->wmc->wmc_contactstateorprovince ||
+			$this->wmc->wmc_contactpostcode || $this->wmc->wmc_contactcountry || 
+			$this->wmc->wmc_contactvoicetelephone || $this->wmc->wmc_contactfacsimiletelephone) {
+				
+			$e_contact = $this->doc->createElement("ContactInformation");
+			$e_contact_person_primary = $this->wmc->createContactPersonPrimaryNode();
+			if ($e_contact_person_primary !== null) {
+				$e_contact->appendChild($e_contact_person_primary);
+			}
+		
+			if ($this->wmc->wmc_contactposition){
+				$e_contact_position = $this->doc->createElement("ContactPosition", $this->wmc->wmc_contactposition);
+				$e_contact->appendChild($e_contact_position);
+			}
+		
+			if ($this->wmc->wmc_contactaddresstype || $this->wmc->wmc_contactaddress || 
+				$this->wmc->wmc_contactcity || $this->wmc->wmc_contactstateorprovince ||
+				$this->wmc->wmc_contactpostcode || $this->wmc->wmc_contactcountry) {
+		
+				$e_contact_address = $this->doc->createElement("ContactAddress");
+		
+				if ($this->wmc->wmc_contactaddresstype){
+					$e_address_type = $this->doc->createElement("AddressType", $this->wmc->wmc_contactaddresstype);
+					$e_contact_address->appendChild($e_address_type);
+				}
+				if ($this->wmc->wmc_contactaddress){
+					$e_address = $this->doc->createElement("Address", $this->wmc->wmc_contactaddress);
+					$e_contact_address->appendChild($e_address);
+				}
+				if ($this->wmc->wmc_contactcity){
+					$e_city = $this->doc->createElement("City", $this->wmc->wmc_contactcity);
+					$e_contact_address->appendChild($e_city);
+				}
+				if ($this->wmc->wmc_contactstateorprovince){
+					$e_state = $this->doc->createElement("StateOrProvince", $this->wmc->wmc_contactstateorprovince);
+					$e_contact_address->appendChild($e_state);
+				}
+				if ($this->wmc->wmc_contactpostcode){
+					$e_postcode = $this->doc->createElement("PostCode", $this->wmc->wmc_contactpostcode);
+					$e_contact_address->appendChild($e_postcode);
+				}
+				if ($this->wmc->wmc_contactcountry){
+					$e_country = $this->doc->createElement("Country", $this->wmc->wmc_contactcountry);
+					$e_contact_address->appendChild($e_country);
+				}
+				$e_contact->appendChild($e_contact_address);
+			}
+			
+			if ($this->wmc->wmc_contactvoicetelephone){
+				$e_voice_telephone = $this->doc->createElement("ContactVoiceTelephone", $this->wmc->wmc_contactvoicetelephone);
+				$e_contact->appendChild($e_voice_telephone);
+			}
+			if ($this->wmc->wmc_contactfacsimiletelephone){
+				$e_facsimile_telephone = $this->doc->createElement("ContactFacsimileTelephone", $this->wmc->wmc_contactfacsimiletelephone);
+				$e_contact->appendChild($e_facsimile_telephone);
+			}
+			if ($this->wmc->wmc_contactemail){
+				$e_email = $this->doc->createElement("ContactElectronicMailAddress", $this->wmc->wmc_contactemail);
+				$e_contact->appendChild($e_email);
+			}
+			return $e_contact;
+		}		
+		return null;
+	}
+	
+	private function createContactPersonPrimaryNode () {
+		if ($this->wmc->wmc_contactperson || $this->wmc->wmc_contactorganization){
+			$e_contact_person_primary = $this->doc->createElement("ContactPersonPrimary");
+	
+			if ($this->wmc->wmc_contactperson){
+				$e_contact_person = $this->doc->createElement("ContactPerson", $this->wmc->wmc_contactperson);
+				$e_contact_person_primary->appendChild($e_contact_person);
+			}
+			if ($this->wmc->wmc_contactorganization){
+				$e_contact_organization = $this->doc->createElement("ContactOrganization", $this->wmc->wmc_contactorganization);
+				$e_contact_person_primary->appendChild($e_contact_organization);
+			}
+			return $e_contact_person_primary;
+		}
+		return null;		
+	}
+
+}
+?>
\ No newline at end of file

Modified: trunk/mapbender/http/classes/class_wms.php
===================================================================
--- trunk/mapbender/http/classes/class_wms.php	2008-06-13 12:57:16 UTC (rev 2501)
+++ trunk/mapbender/http/classes/class_wms.php	2008-06-13 13:03:41 UTC (rev 2502)
@@ -19,12 +19,12 @@
 
 include_once(dirname(__FILE__)."/../../conf/mapbender.conf");
 require_once(dirname(__FILE__)."/class_connector.php");
+require_once(dirname(__FILE__)."/class_user.php");
 require_once(dirname(__FILE__)."/class_mb_exception.php");
 require_once(dirname(__FILE__)."/class_administration.php");
 
 $con = db_connect(DBSERVER,OWNER,PW);
 db_select_db(DB,$con);
-
   
 class wms {
 	var $lastURL;
@@ -75,7 +75,122 @@
 	  
 	function wms() {
 	} 
-	  
+	
+	/**
+	 * Compares this WMS to another WMS.
+	 * 
+	 * @return boolean false, if
+	 * 					- the capabilities URLs don't match
+	 * 					- the layer count is different
+	 * 					- the layer names are different
+	 * 
+	 * @param $anotherWms wms this is just another WMS object
+	 */
+	public function equals ($anotherWms) {
+		// If the getMap URLs are not equal, the WMS are not equal.
+		if ($this->wms_getmap != $anotherWms->wms_getmap) {
+//			$e = new mb_notice($this . " != " . $anotherWms . " (getMap URL)");
+			return false;
+		}
+
+		// If the layer count is different, the WMS are not equal.
+		if (count($this->objLayer) != count($anotherWms->objLayer)) {
+//			$e = new mb_notice($this . " != " . $anotherWms . " (layer count: " . count($this->objLayer) . ":" . count($anotherWms->objLayer). ")");
+			return false;
+		}
+		
+		// If the layer names are different, the WMS are not equal.
+		for ($i = 0; $i < count($this->objLayer); $i++) {
+			$name1 = $this->objLayer[$i]->layer_name;
+			$name2 = $anotherWms->objLayer[$i]->layer_name;
+			
+			if ($name1 != $name2) {
+//				$e = new mb_notice($this . " != " . $anotherWms . " (layer names, " . $name1 . " vs. " . $name2 . ")");
+				return false;
+			}
+		}
+//		$e = new mb_notice($this . " == " . $anotherWms);
+		return true;
+	}  
+	
+	/**
+	 * The other WMS must be the same as this WMS, but with different
+	 * application settings. These application settings are copied,
+	 * the local settings are overwritten.
+	 * 
+	 * @return boolean true if the settings could be copied; false 
+	 * 					when an error occured.
+	 * @param $anotherWms wms The same WMS with possibly other settings
+	 */
+/*
+	public function copyConfiguration ($anotherWms) {
+		if (!$this->equals($anotherWms)) {
+			$e = new mb_exception("class_wms.php: copyConfiguration(): parameters cannot be copied, it's a different WMS.");
+			return false;
+		}
+		for ($i = 0; $i < count($this->objLayer); $i++) {
+			$myCurrentLayer = $this->objLayer[$i];
+			$theirCurrentLayer = $anotherWms->objLayer[$i];
+
+			$myCurrentLayer->gui_layer_selectable = $theirCurrentLayer->gui_layer_selectable;
+			$myCurrentLayer->gui_layer_visible    = $theirCurrentLayer->gui_layer_visible;
+			$myCurrentLayer->gui_layer_queryable  = $theirCurrentLayer->gui_layer_queryable;
+			$myCurrentLayer->gui_layer_querylayer = $theirCurrentLayer->gui_layer_querylayer;
+			$myCurrentLayer->gui_layer_style      = $theirCurrentLayer->gui_layer_style;
+		}
+	}
+*/	
+	
+	/**
+	 * Removes duplicate WMS from an array of WMS. To find duplicates,
+	 * two WMS are compared via equals().
+	 * 
+	 * @return wms[]
+	 * @param $wmsArray wms[]
+	 */
+	public static function merge ($wmsArray) {
+		$e = new mb_notice("before: " . implode(", ", $wmsArray));
+		if (!is_array($wmsArray)) {
+			$e = new mb_exception("class_wms.php: merge(): parameter is NOT an array.");
+			return array();
+		}
+		if (count($wmsArray) == 0) {
+			$e = new mb_exception("class_wms.php: merge(): parameter is an EMPTY array.");
+			return array();
+		}
+		
+		$newWmsArray = array();
+		
+		while (count($wmsArray) > 0) {
+			$currentWms = array_pop($wmsArray);
+			
+			$isNewWms = true;
+
+			if (get_class($currentWms) != "wms") {
+				$e = new mb_exception("class_wms.php: merge(): current WMS is not a WMS object, but a " . get_class($currentWms));
+			}
+			else {
+				for ($i = 0; $i < count($newWmsArray) && $isNewWms; $i++) {
+					if ($currentWms->equals($newWmsArray[$i])) {
+						$isNewWms = false;
+					}
+				}
+				if ($isNewWms) {
+//					$e = new mb_notice("adding WMS " . $currentWms);
+					array_push($newWmsArray, $currentWms);
+				}
+			}
+		}
+		// reversal of the array, because the elements were popped 
+		// from another array before.
+//		$e = new mb_notice("after: " . implode(", ", array_reverse($newWmsArray)));
+		return array_reverse($newWmsArray);
+	}
+	
+	public function __toString () {
+		return $this->wms_title;
+	}
+	
 	function createObjFromXML($url){
 	
 		$x = new connector($url);
@@ -681,13 +796,80 @@
 	  function stripEndlineAndCarriageReturn($string) {
 	  	return preg_replace("/\n/", "", preg_replace("/\r/", " ", $string));
 	  }
-	  function createJsObjFromWMS($parent=0){
+		function createJsObjFromWMS($parent=0){
+			echo $this->createJsObjFromWMS_();
+		}
+		
+
+	function newLayer ($currentLayer, $currentExtent) {
+		$pos = $currentLayer["extension"]["LAYER_POS"];
+		$parent = $currentLayer["extension"]["LAYER_PARENT"];
+		$this->addLayer($pos, $parent); 
+
+		// set layer data
+		$layerIndex = count($this->objLayer) - 1;
+		$newLayer = $this->objLayer[$layerIndex];
+		$newLayer->layer_uid = $currentLayer["extension"]["LAYER_ID"];
+		$newLayer->layer_name = $currentLayer["name"];
+		$newLayer->layer_title = $currentLayer["title"];
+		$newLayer->layer_dataurl_href = $currentLayer["dataurl"];
+		$newLayer->layer_pos = $currentLayer["extension"]["LAYER_POS"];
+		$newLayer->layer_queryable = $currentLayer["queryable"];
+		$newLayer->layer_minscale = $currentLayer["extension"]["MINSCALE"];
+		$newLayer->layer_maxscale = $currentLayer["extension"]["MAXSCALE"];
+		$newLayer->layer_metadataurl = $currentLayer["metadataurl"];
+		$newLayer->gui_layer_wms_id = $currentLayer["extension"]["WMS_LAYER_ID"];
+		$newLayer->gui_layer_status = $currentLayer["extension"]["GUI_STATUS"];
+		$newLayer->gui_layer_style = ""; // TODO: Add correct data
+		$newLayer->gui_layer_selectable = $currentLayer["extension"]["GUI_SELECTABLE"];
+		$newLayer->gui_layer_visible = $currentLayer["visible"];
+		$newLayer->gui_layer_queryable = $currentLayer["extension"]["GUI_QUERYABLE"];
+		$newLayer->gui_layer_querylayer = $currentLayer["extension"]["QUERYLAYER"];
+		$newLayer->gui_layer_minscale = $currentLayer["extension"]["GUI_MINSCALE"];
+		$newLayer->gui_layer_maxscale = $currentLayer["extension"]["GUI_MAXSCALE"];
+		if (isset($currentLayer["extension"]["WFSFEATURETYPE"])) {
+			$newLayer->gui_layer_wfs_featuretype = $currentLayer["extension"]["WFSFEATURETYPE"];
+		}
+		$newLayer->layer_abstract = $currentLayer["abstract"];
+
+
+		//
+		// set layer epsg
+		//
+		$newLayer->layer_epsg = array();
+		if ($currentExtent !== null) {
+			$currentLayerEpsg = array();
+			$currentLayerEpsg["epsg"] = $currentExtent->epsg;
+			$currentLayerEpsg["minx"] = $currentExtent->min->x;
+			$currentLayerEpsg["miny"] = $currentExtent->min->y; 
+			$currentLayerEpsg["maxx"] = $currentExtent->max->x;
+			$currentLayerEpsg["maxy"] = $currentExtent->max->y;
+			array_push($newLayer->layer_epsg, $currentLayerEpsg);
+		}
+
+
+		//
+		// set layer style
+		//
+		for ($i = 0; $i < count($currentLayer["format"]); $i++) {
+			$layerStyleIndex = count($newLayer->gui_layer_style) - 1;
+			$newLayer->layer_style[$layerStyleIndex] = array();
+			$currentStyle = $newLayer->layer_style[$layerStyleIndex];
+			$currentStyle["name"] = $currentLayer["style"][$i]["name"];
+			$currentStyle["title"] = $currentLayer["style"][$i]["title"];
+			$currentStyle["legendurl"] = $currentLayer["style"][$i]["legendurl"];
+			$currentStyle["legendurl_format"] = $currentLayer["style"][$i]["legendurl_type"];
+		}
+	}
+	
+	  function createJsObjFromWMS_($parent=0){
+	  	$str = "";
 	  	if(!$this->wms_title || $this->wms_title == ""){
-			echo "alert('Error: no valid capabilities-document !!');";
+			$str .= "alert('Error: no valid capabilities-document !!');";
 			die; exit;
 		}
 			if($parent){
-				echo "parent.";
+				$str .=  "parent.";
 			}
 			// wms_title and abstract have previously been urlencoded
 			// this solution may not yet be the ultimate one
@@ -709,19 +891,19 @@
 					"'" . $this->gui_wms_opacity ."'," .
 					"'" . $this->gui_wms_sldurl ."" .
 					"');";
-			echo $add_wms_string;
+			$str .=  $add_wms_string;
 			
 		for($i=0;$i<count($this->data_format);$i++){
 			if($parent){
-				echo "parent.";
+				$str .=  "parent.";
 			}		
-			echo "wms_add_data_type_format('". $this->data_type[$i] ."','". $this->data_format[$i] ."');\n";		
+			$str .= "wms_add_data_type_format('". $this->data_type[$i] ."','". $this->data_format[$i] ."');\n";		
 		}
 		for($i=0; $i<count($this->objLayer); $i++){
 			if($parent){
-				echo "parent.";
+				$str .= "parent.";
 			}
-			print ("wms_add_layer('". 
+			$str .=  "wms_add_layer('". 
 				$this->objLayer[$i]->layer_parent ."','". 
 				$this->objLayer[$i]->layer_uid ."','". 
 				addslashes($this->objLayer[$i]->layer_name) . "','". 
@@ -741,37 +923,43 @@
 				$this->objLayer[$i]->gui_layer_querylayer ."','". 
 				$this->objLayer[$i]->gui_layer_minscale ."','". 
 				$this->objLayer[$i]->gui_layer_maxscale ."','".
-				$this->objLayer[$i]->gui_layer_wfs_featuretype ."');\n");
+				$this->objLayer[$i]->gui_layer_wfs_featuretype ."');\n";
 				
 			for($j=0; $j<count($this->objLayer[$i]->layer_epsg);$j++){
 				if($i==0){
 					if($parent){
-					echo "parent.";
+						$str .= "parent.";
 					}
-					print("wms_addSRS('". 
+					$str .= "wms_addSRS('". 
 						$this->objLayer[$i]->layer_epsg[$j]["epsg"] ."','". 
 						$this->objLayer[$i]->layer_epsg[$j]["minx"] ."','". 
 						$this->objLayer[$i]->layer_epsg[$j]["miny"] ."','". 
 						$this->objLayer[$i]->layer_epsg[$j]["maxx"] ."','". 
-						$this->objLayer[$i]->layer_epsg[$j]["maxy"] ."');\n");
+						$this->objLayer[$i]->layer_epsg[$j]["maxy"] ."');\n";
 				}
 				if($parent){
-				echo "parent.";
+					$str .=  "parent.";
 				}
-				print("layer_addEpsg('". 
+				$str .= "layer_addEpsg('". 
 					$this->objLayer[$i]->layer_epsg[$j]["epsg"] ."','". 
 					$this->objLayer[$i]->layer_epsg[$j]["minx"] ."','". 
 					$this->objLayer[$i]->layer_epsg[$j]["miny"] ."','". 
 					$this->objLayer[$i]->layer_epsg[$j]["maxx"] ."','". 
-					$this->objLayer[$i]->layer_epsg[$j]["maxy"] ."');\n");
+					$this->objLayer[$i]->layer_epsg[$j]["maxy"] ."');\n";
 			}
 			for($j=0; $j<count($this->objLayer[$i]->layer_style);$j++){
 				if($parent){
-				echo "parent.";
+				$str .= "parent.";
 				}
-				print("wms_addLayerStyle('".$this->objLayer[$i]->layer_style[$j]["name"]."', '".$this->objLayer[$i]->layer_style[$j]["title"]."', ".$j.",".$i.",'".$this->objLayer[$i]->layer_style[$j]["legendurl"]."', '".$this->objLayer[$i]->layer_style[$j]["legendformat"]."');\n");
+				$str .= "wms_addLayerStyle('".$this->objLayer[$i]->layer_style[$j]["name"].
+					"', '".$this->objLayer[$i]->layer_style[$j]["title"].
+					"', ".$j.
+					",".$i.
+					",'".$this->objLayer[$i]->layer_style[$j]["legendurl"].
+					"', '".$this->objLayer[$i]->layer_style[$j]["legendformat"]."');\n";
 			}
 		}
+		return $str;
 	  }
 	  
 	  function createJsLayerObjFromWMS($parent=0, $layer_name){
@@ -1734,10 +1922,45 @@
 		if($this->wms_getcapabilities == '' || $this->wms_getmap == '' ){
 			echo "<br>Missing parameters: <br>";
 			$this->displayWMS();
-			echo "<br> Data not commited<br>";
+			print_r($this);
+			echo "<br> Data not committed<br>";
 			die();
 		}
 	}
+	
+	/**
+	 * Selects all WMS of the current user from the database.
+	 * Then it creates the corresponding WMS object and returns
+	 * these objects as an array.
+	 * 
+	 * @return wms[]
+	 * @param $appId String
+	 */
+	public static function selectMyWmsByApplication ($appId) {
+		// check if user is permitted to access the application
+		$currentUser = new User($_SESSION["mb_user_id"]);
+		$appArray = $currentUser->getApplicationsByPermission(false);
+		if (!in_array($appId, $appArray)) {
+			$e = new mb_warning("class_wms.php: selectMyWmsByApplication(): User '" . $currentUser . "' is not allowed to acces application '" . $appId . "'.");
+			return array();
+		}
+		
+		// get WMS of this application
+		$sql = "SELECT fkey_wms_id FROM gui_wms WHERE " . 
+				"fkey_gui_id = $1 ORDER BY gui_wms_position";
+		$v = array($appId);
+		$t = array('s');
+		$res = db_prep_query($sql,$v,$t);
+		
+		// instantiate PHP objects and store in array
+		$wmsArray = array();
+		while ($row = db_fetch_array($res)) {
+			$currentWms = new wms();
+			$currentWms->createObjFromDB($appId, $row["fkey_wms_id"]);
+			array_push($wmsArray, $currentWms);
+		}
+		return $wmsArray;
+	}
 }
 class layer extends wms {	
 	var $layer_id;
@@ -1768,5 +1991,11 @@
 		$this->layer_parent = $parent;	
 		//var_dump($this);	
 	}
+
+	public function __toString () {
+		$e = new mb_exception("TITLE: " . $this->layer_title);
+		return $this->layer_title;
+	}
+	
 }
 ?>
\ No newline at end of file



More information about the Mapbender_commits mailing list