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

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Thu Sep 2 04:49:18 EDT 2010


Author: christoph
Date: 2010-09-02 08:49:18 +0000 (Thu, 02 Sep 2010)
New Revision: 6872

Added:
   trunk/mapbender/http/classes/class_elementVar.php
Modified:
   trunk/mapbender/http/classes/class_element.php
   trunk/mapbender/http/classes/class_gui.php
   trunk/mapbender/http/php/mod_exportGUI.php
Log:
refactoring of SQL export

Modified: trunk/mapbender/http/classes/class_element.php
===================================================================
--- trunk/mapbender/http/classes/class_element.php	2010-09-02 08:44:28 UTC (rev 6871)
+++ trunk/mapbender/http/classes/class_element.php	2010-09-02 08:49:18 UTC (rev 6872)
@@ -1,23 +1,12 @@
 <?php
-# $Id: class_bbox.php 1965 2008-01-15 08:24:29Z christoph $
-# http://www.mapbender.org/index.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.
+# License:
+# Copyright (c) 2009, Open Source Geospatial Foundation
+# This program is dual licensed under the GNU General Public License 
+# and Simplified BSD license.  
+# http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
 
-require_once(dirname(__FILE__)."/../../core/globalSettings.php");
+require_once dirname(__FILE__) . "/../../core/globalSettings.php";
+require_once dirname(__FILE__) . "/../classes/class_elementVar.php";
 
 define("ELEMENT_PATTERN", "/sessionID/");
 
@@ -46,6 +35,7 @@
 	var $requires;
 	var $helpUrl;
 	var $isBodyAndUsesSplashScreen = false;
+	var $elementVars = array();
 	
 	public function __contruct() {
 		
@@ -85,6 +75,16 @@
 			$this->target = $row["e_target"];
 			$this->requires = $row["e_requires"];
 			$this->helpUrl = $row["e_url"];
+
+			$sql = "SELECT var_name FROM gui_element_vars WHERE fkey_gui_id = $1 AND fkey_e_id = $2;";
+			$v = array($applicationId, $id);
+			$t = array("s", "s");
+			$res = db_prep_query($sql, $v, $t);
+
+			while ($row = db_fetch_assoc($res)) {
+				$name = $row["var_name"];
+				$this->elementVars[]= new ElementVar($applicationId, $id, $name);
+			}
 			return true;
 		}
 		return false;		
@@ -94,6 +94,40 @@
 		return $this->toHtml();
 	}
 	
+	public function toSql () {
+		$insert = "INSERT INTO gui_element (fkey_gui_id, e_id, e_pos, e_public, e_comment, e_title, e_element,";
+		$insert .= "e_src, e_attributes, e_left, e_top, e_width, e_height, e_z_index, e_more_styles,";
+		$insert .= " e_content, e_closetag, e_js_file, e_mb_mod, e_target, e_requires,e_url) VALUES (";
+		$insert.="'".$this->guiId."',";
+		$insert.="'".$this->id."',";
+		$insert.="".$this->pos.",";
+		$insert.="".$this->isPublic.",";
+		$insert.="'".db_escape_string($this->comment)."',";
+		$insert.="'".db_escape_string($this->title)."',";
+		$insert.="'".$this->element."',";
+		$insert.="'".$this->src."',";
+		$insert.="'".db_escape_string($this->attributes)."',";
+		$insert.="".$this->left.",";
+		$insert.="".$this->top.",";
+		$insert.="".$this->width.",";
+		$insert.="".$this->height.",";
+		$insert.="".$this->zIndex.",";		
+		$insert.="'".$this->moreStyles."',";
+		$insert.="'".db_escape_string($this->content)."',";
+		$insert.="'".$this->closeTag."',";
+		$insert.="'".$this->jsFile."',";		
+		$insert.="'".$this->mbMod."',";
+		$insert.="'".$this->target."',";
+		$insert.="'".$this->requires."',";
+		$insert.="'".$this->helpUrl."'";
+		$insert.= ");\n";
+		
+		for ($i = 0; $i < count($this->elementVars); $i++) {
+			$insert .= $this->elementVars[$i]->toSql();
+		}
+		return preg_replace("/,,/", ",NULL ,", $insert);		
+	}
+	
 	public function getJavaScriptModules () {
 		$jsArray = array();
 		if ($this->mbMod != "") {

Added: trunk/mapbender/http/classes/class_elementVar.php
===================================================================
--- trunk/mapbender/http/classes/class_elementVar.php	                        (rev 0)
+++ trunk/mapbender/http/classes/class_elementVar.php	2010-09-02 08:49:18 UTC (rev 6872)
@@ -0,0 +1,119 @@
+<?php
+# License:
+# Copyright (c) 2009, Open Source Geospatial Foundation
+# This program is dual licensed under the GNU General Public License 
+# and Simplified BSD license.  
+# http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
+
+require_once(dirname(__FILE__)."/../../core/globalSettings.php");
+require_once(dirname(__FILE__)."/../classes/class_RPCEndpoint.php");
+
+/**
+ * A Mapbender user as described in the table mb_user.
+ */
+class ElementVar implements RPCObject{
+	/**
+	 * @var Integer The User ID
+	 */
+	var $applicationId;
+	var $elementId;
+	var $name;  
+	var $value;
+	var $context;
+	var $type;
+	
+	/**
+	 * Constructor
+	 * @param $application String 	name of the application
+	 * @param $element String 	name of the element
+	 */
+	public function __construct () {
+		if (func_num_args() === 3) {
+			try {
+				$this->applicationId = func_get_arg(0);
+				$this->elementId = func_get_arg(1);
+				$this->name = func_get_arg(2);
+				$this->load();
+			}
+			catch (Exception $e) {
+				throw new Exception ("Could not initialize element var.");
+			}
+		}
+	}	
+
+	
+	/**
+	 * @return String the name of this element var
+	 */
+	public function __toString () {
+		return (string) $this->name;	
+	}
+
+
+    /*
+    * @return Assoc Array containing the fields to send to the user
+    */
+    public function getFields() {
+        $result = array(
+        );
+		return $result;
+	}
+
+	public function create() {
+	}
+
+
+	/*
+	*	@param	$changes JSON  keys and their values of what to change in the object
+	*/
+	public function change($changes) {
+	}
+
+	public function commit() {
+	}
+
+	public function remove() {
+	}
+	
+	public function toSql () {
+		$insert .= "INSERT INTO gui_element_vars(fkey_gui_id, fkey_e_id, var_name, var_value, context, var_type";
+		$insert .= ") VALUES (";
+		$insert .= "'" . $this->applicationId . "',";
+		$insert .= "'" . $this->elementId . "',";
+		$insert .= "'" . $this->name . "',";
+		$insert .= "'" . db_escape_string($this->value) . "',";
+		$insert .= "'" . db_escape_string($this->context) . "',";
+		$insert .= "'" . $this->type . "'";
+		$insert.=");\n";
+		return $insert;
+	}
+
+	public function load() {
+		$sql = "SELECT * from gui_element_vars WHERE fkey_gui_id = $1 AND fkey_e_id = $2 AND var_name = $3; ";
+		$v = array($this->applicationId, $this->elementId, $this->name);
+		$t = array("s", "s", "s");
+		$res = db_prep_query($sql, $v, $t);
+
+		if ($row = db_fetch_array($res)) {
+			$this->value = $row["var_value"];
+			$this->context = $row["context"];
+			$this->type = $row["var_type"];
+		}
+		else {
+			 throw new Exception("no such element var");
+		}
+	}
+
+	public static function getList($filter) {
+      return array();
+    }
+
+    public static function byName($name) {
+      return null;
+    }
+	
+	public function isValid () {
+		return true;
+	}
+}
+?>

Modified: trunk/mapbender/http/classes/class_gui.php
===================================================================
--- trunk/mapbender/http/classes/class_gui.php	2010-09-02 08:44:28 UTC (rev 6871)
+++ trunk/mapbender/http/classes/class_gui.php	2010-09-02 08:49:18 UTC (rev 6872)
@@ -67,6 +67,30 @@
 		return $result;
 	}
 
+	public function getElementByName ($id) {
+		for ($i = 0; $i < count($this->elementArray); $i++) {
+			$e = $this->elementArray[$i];
+			if ($e->id === $id) {
+				return $e;
+			}
+		}
+		throw new Exception ("Element " . $id . " does not exist in application " . $this->name);
+	}
+
+	public function toSql () {
+		$insert =  "INSERT INTO gui (gui_id, gui_name, gui_description, gui_public) VALUES ";
+		$insert .= "('" . $this->id . "','" . $this->name . "','" . 
+			$this->description . "'," . $this->public . ");\n";
+	
+		//gui_element
+		foreach ($this->elementArray as $element) {
+			$insert .= $element->toSql();
+		}
+		$insert = preg_replace("/,,/",",NULL,",$insert);
+		$insert = preg_replace("/, ,/", ",NULL,",$insert);	
+		return $insert;	
+	}
+
 	public function  create() {
 		if($this->name == ""){ $e = new Exception("Can't create user  without name");}
 	    

Modified: trunk/mapbender/http/php/mod_exportGUI.php
===================================================================
--- trunk/mapbender/http/php/mod_exportGUI.php	2010-09-02 08:44:28 UTC (rev 6871)
+++ trunk/mapbender/http/php/mod_exportGUI.php	2010-09-02 08:49:18 UTC (rev 6872)
@@ -56,105 +56,25 @@
 
 <?php
 
-require_once(dirname(__FILE__)."/../classes/class_administration.php");
+require_once dirname(__FILE__)."/../classes/class_administration.php";
+require_once dirname(__FILE__)."/../classes/class_gui.php";
+
 $admin = new administration();
 $permguis = $admin->getGuisByPermission(Mapbender::session()->get("mb_user_id"),true);
 
  ###export
 
 if($guiList){
-	$insert = "";
-	//gui
-	$sql_gui = "SELECT * FROM gui WHERE gui_name = $1 ";
-	$v = array($guiList);
-	$t = array('s');
-	$res_gui = db_prep_query($sql_gui,$v,$t);
-	
-	$i = 0;
-	while ($row = db_fetch_row($res_gui)){
-		$insert =  "INSERT INTO gui (gui_id, gui_name, gui_description, gui_public) VALUES ";
-		$insert.= "('".db_result($res_gui, 0, 0)."','".db_result($res_gui, 0, 1)."','".db_result($res_gui, 0, 2)."',".db_result($res_gui, 0, 3).");\n";
+	$gui = gui::byName($guiList);
+	try {
+		$insert = $gui->toSql();
 	}
-
-	//gui_element
-	$sql_gel = "SELECT * from gui_element WHERE fkey_gui_id = $1 ORDER BY e_id";
-	$v = array($guiList);
-	$t = array('s');
-	$res_gel = db_prep_query($sql_gel,$v,$t);
-	$cnt_gel = 0;
-
-	while ($row = db_fetch_array($res_gel)){
-		$insert.="INSERT INTO gui_element (fkey_gui_id, e_id, e_pos, e_public, e_comment, e_title, e_element,";
-		$insert.="e_src, e_attributes, e_left, e_top, e_width, e_height, e_z_index, e_more_styles,";
-		$insert.=" e_content, e_closetag, e_js_file, e_mb_mod, e_target, e_requires,e_url) VALUES (";
-		
-		$insert.="'".$row["fkey_gui_id"]."',";
-		$insert.="'".$row["e_id"]."',";
-		$insert.="".$row["e_pos"].",";
-		$insert.="".$row["e_public"].",";
-		$insert.="'".db_escape_string($row["e_comment"])."',";
-		$insert.="'".db_escape_string($row["e_title"])."',";
-		$insert.="'".$row["e_element"]."',";
-		$insert.="'".$row["e_src"]."',";
-		$insert.="'".db_escape_string($row["e_attributes"])."',";
-		$insert.="".$row["e_left"].",";
-		$insert.="".$row["e_top"].",";
-		$insert.="".$row["e_width"].",";
-		$insert.="".$row["e_height"].",";
-		$insert.="".$row["e_z_index"].",";		
-		$insert.="'".$row["e_more_styles"]."',";
-		$insert.="'".db_escape_string($row["e_content"])."',";
-		$insert.="'".$row["e_closetag"]."',";
-		$insert.="'".$row["e_js_file"]."',";		
-		$insert.="'".$row["e_mb_mod"]."',";
-		$insert.="'".$row["e_target"]."',";
-		$insert.="'".$row["e_requires"]."',";
-		$insert.="'".$row["e_url"]."'";
-		$insert.= ");\n";
-		$insert = preg_replace("/,,/", ",NULL ,", $insert);
+	catch (Exception $e) {
+		$insert = $e->message;
 	}
 	
-	
-	$sql_gelvars = "SELECT * from gui_element_vars WHERE fkey_gui_id = $1 ORDER BY fkey_e_id, var_name";
-	$v = array($guiList);
-	$t = array('s');
-	$res_gelvars = db_prep_query($sql_gelvars,$v,$t);
-	$cnt_gelvars = 0;
-
-	while ($row = db_fetch_row($res_gelvars)){
-		
-		$insert.="INSERT INTO gui_element_vars(";
-		$i=0;
-		while($i < db_numfields($res_gelvars)){
-			if($i > 0){
-				$insert.=", ";
-			}
-			$insert.=db_fieldname($res_gelvars, $i);
-			$i++;
-		}
-		$insert.=") VALUES (";
-		$i = 0;
-		while($i < db_numfields($res_gelvars)){
-			if($i > 0){
-				$insert.=", ";
-			}
-			$temp = db_escape_string($row[$i]);
-			$temp = preg_replace("/>/","&gt;",$temp);
-			$temp = preg_replace("/</","&lt;",$temp);
-			$insert.="'" . $temp . "'";
-			$i++;
-		}
-		$insert.=");\n";
-
-		$cnt_gelvars++;
-
-	}
-	
-	$insert = preg_replace("/,,/",",NULL,",$insert);
-	$insert = preg_replace("/, ,/", ",NULL,",$insert);
-	
 	echo "<textarea rows=40 cols=80>";
-	echo $insert; 
+	echo htmlentities($insert, ENT_QUOTES, CHARSET); 
 	echo "</textarea>";
 }
 



More information about the Mapbender_commits mailing list