[Mapbender-commits] r4878 - in trunk/mapbender/http: classes php

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Thu Oct 29 13:13:05 EDT 2009


Author: kmq
Date: 2009-10-29 13:13:04 -0400 (Thu, 29 Oct 2009)
New Revision: 4878

Modified:
   trunk/mapbender/http/classes/class_wmc.php
   trunk/mapbender/http/php/mod_loadwmc_server.php
Log:
automatically update links to WMSs in WMCs from database when they're loaded

Modified: trunk/mapbender/http/classes/class_wmc.php
===================================================================
--- trunk/mapbender/http/classes/class_wmc.php	2009-10-29 09:20:12 UTC (rev 4877)
+++ trunk/mapbender/http/classes/class_wmc.php	2009-10-29 17:13:04 UTC (rev 4878)
@@ -92,6 +92,7 @@
 	// set in constructor
 	var $wmc_id;
 	var $userId;
+    var $timestamp;
 
 	// set during parsing
 	var $wmc_version;
@@ -123,6 +124,7 @@
 	public function __construct () {
 		$this->userId = Mapbender::session()->get("mb_user_id");
 		$this->wmc_id = time();
+		$this->timestamp = time();
 	} 
 	
 	// ---------------------------------------------------------------------------
@@ -151,6 +153,20 @@
 			return false;
 		}
 		$this->createObjFromWMC_xml($doc);
+        // FIXME:too hackisch to be proper
+        $sql = "SELECT wmc_timestamp FROM mb_user_wmc WHERE wmc_id = $1";
+        $v = array($wmcId);
+        $t = array("i");
+        
+        // FIXME: might as well fetch title too, as long as we ŕe here  
+        $res = db_prep_query($sql,$v,$t);
+        if(db_error) { return false; } //FIXME: no really need erromessage when this fails
+        if($row = db_fetch_row($res))
+        {
+          $this->timestamp = $row[0];
+        
+        }
+        
 		return true;
 	}
 
@@ -296,6 +312,19 @@
         db_commit();
 		return $result;
 	}
+    
+
+    /*
+    * overwrites an exact version of a wmc in the database
+    */
+    public function update_existing($xml,$id)
+    {
+      $sql = "UPDATE mb_user_wmc SET wmc = $1 WHERE wmc_id = $2";
+      $v = array($xml,$id);
+      $t = array("s","i");
+      $res = db_prep_query($sql,$v,$t);
+      if(db_error()) { $e = new mb_exception("There was an error saving an updated WMC"); }
+    }
 	
 	/**
 	 * deletes a {@link http://www.mapbender.org/index.php/WMC WMC} 

Modified: trunk/mapbender/http/php/mod_loadwmc_server.php
===================================================================
--- trunk/mapbender/http/php/mod_loadwmc_server.php	2009-10-29 09:20:12 UTC (rev 4877)
+++ trunk/mapbender/http/php/mod_loadwmc_server.php	2009-10-29 17:13:04 UTC (rev 4878)
@@ -2,6 +2,7 @@
 require_once(dirname(__FILE__) . "/../php/mb_validateSession.php");
 require_once(dirname(__FILE__) . "/../classes/class_user.php");
 require_once(dirname(__FILE__) . "/../classes/class_wmc.php");
+require_once(dirname(__FILE__) . "/../classes/class_wmcToXml.php");
 require_once(dirname(__FILE__) . "/../classes/class_json.php");
 require_once(dirname(__FILE__) . "/../classes/class_administration.php");
 
@@ -109,8 +110,67 @@
 	// loads a WMC (returns array of JS code)
 	case 'loadWmc':
 		$wmcId = $queryObj->parameters->id;
-		$wmc->createFromDb($wmcId);
-		$jsArray = $wmc->toJavaScript();
+        $wmc->createFromDb($wmcId);
+        
+        $query_mbWMSId = "/wmc:ViewContext/wmc:LayerList/wmc:Layer/wmc:Extension/mapbender:wms_id";
+            
+        try{
+          $WMCDoc = DOMDocument::loadXML($wmc->toXml());
+        } catch (Exception $E) {
+          echo "WMC XML is broken";
+        }   
+
+        $xpath = new DOMXPath($WMCDoc);
+        $xpath->registerNamespace("wmc","http://www.opengis.net/context");
+        $xpath->registerNamespace("mapbender","http://www.mapbender.org/context");
+        $xpath->registerNamespace("xlink","http://www.w3.org/1999/xlink");
+
+        $WMSIdList = $xpath->query($query_mbWMSId);
+        foreach($WMSIdList as $WMSId)
+        {
+          $id =  $WMSId->nodeValue;
+          $sql = "SELECT wms_timestamp,wms_getmap,wms_getlegendurl FROM wms WHERE wms_id = $1";
+          $v = array($id);
+          $t = array("t");
+            
+          $res = db_prep_query($sql,$v,$t);
+          if(db_error()){true; } //FIMXE: PROPER ERROR MESSAGE
+          
+          if($row = db_fetch_row($res))
+          {
+            $wms_timestamp = $row[0];
+            if($wmc->timestamp < $wms_timestamp)
+            {
+              // wmc is fresh, life is good
+            }else{
+
+              $MapResources = $xpath->query("../../wmc:Server/wmc:OnlineResource",$WMSId);
+              foreach($MapResources as $MapResource)
+              {
+                $MapResource->setAttribute("xlink:href",$row[1]);
+              }
+
+              $LegendResources = $xpath->query("../../wmc:StyleList/wmc:Style/wmc:LegendURL/wmc:OnlineResource",$WMSId);
+              foreach($LegendResources as $LegendResource)
+              {
+                $base = $row[2];
+                $origurl = explode('&', $LegendResource->getAttribute("xlink:href"),2);
+                $url = $base . $origurl[1]; 
+                $LegendResource->setAttribute("xlink:href",$url);
+              }
+            
+
+            }
+
+
+          }
+      
+        }
+        $updatedWMC = $WMCDoc->saveXML();
+        $wmc->update_exisiting($updatedWMC,$wmsId);
+        $wmc->createFromXml($updatedWMC);
+		
+        $jsArray = $wmc->toJavaScript();
 		if ($jsArray) {
 			$resultObj["javascript"] = $jsArray;
 		}



More information about the Mapbender_commits mailing list